You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PlanRenderer.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.plan;
  18. import java.util.Calendar;
  19. import java.util.Date;
  20. import java.util.HashMap;
  21. import java.util.Locale;
  22. import java.util.StringTokenizer;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.Element;
  25. import org.w3c.dom.Node;
  26. import org.w3c.dom.NodeList;
  27. import org.apache.batik.dom.svg.SVGDOMImplementation;
  28. public class PlanRenderer {
  29. private static final String SVG_NAMESPACE = SVGDOMImplementation.SVG_NAMESPACE_URI;
  30. private String fontFamily = "sansserif";
  31. private float fontSize = 12;
  32. private String type = "";
  33. private String lang = "";
  34. private String country = "";
  35. private String variant = "";
  36. private float width;
  37. private float height;
  38. private float topEdge;
  39. private float rightEdge;
  40. private HashMap hints = new HashMap();
  41. private String[] colours;
  42. private String[] darkcolours;
  43. public void setFontInfo(String fam, float si) {
  44. fontFamily = fam;
  45. fontSize = si;
  46. }
  47. public float getWidth() {
  48. return width;
  49. }
  50. public float getHeight() {
  51. return height;
  52. }
  53. protected float toFloat(String str) {
  54. return Float.parseFloat(str);
  55. }
  56. public Document createSVGDocument(Document planDoc) {
  57. Element svgRoot = planDoc.getDocumentElement();
  58. width = toFloat(svgRoot.getAttribute("width"));
  59. height = toFloat(svgRoot.getAttribute("height"));
  60. type = svgRoot.getAttribute("type");
  61. lang = svgRoot.getAttribute("lang");
  62. country = svgRoot.getAttribute("country");
  63. variant = svgRoot.getAttribute("variant");
  64. String style = svgRoot.getAttribute("style");
  65. parseStyle(style);
  66. Locale locale = new Locale(lang, country == null ? "" : country,
  67. variant == null ? "" : variant);
  68. String start = svgRoot.getAttribute("start");
  69. String end = svgRoot.getAttribute("end");
  70. Date sd = getDate(start, locale);
  71. Date ed = getDate(end, locale);
  72. String title = "";
  73. EventList data = null;
  74. NodeList childs = svgRoot.getChildNodes();
  75. for (int i = 0; i < childs.getLength(); i++) {
  76. Node obj = childs.item(i);
  77. String nname = obj.getNodeName();
  78. if (nname.equals("title")) {
  79. title = ((Element) obj).getFirstChild().getNodeValue();
  80. } else if (nname.equals("events")) {
  81. data = getEvents((Element) obj, locale);
  82. }
  83. }
  84. SimplePlanDrawer planDrawer = new SimplePlanDrawer();
  85. planDrawer.setStartDate(sd);
  86. planDrawer.setEndDate(ed);
  87. hints.put(PlanHints.FONT_FAMILY, fontFamily);
  88. hints.put(PlanHints.FONT_SIZE, new Float(fontSize));
  89. hints.put(PlanHints.LOCALE, locale);
  90. Document doc =
  91. planDrawer.createDocument(data, width, height, hints);
  92. return doc;
  93. }
  94. protected void parseStyle(String style) {
  95. hints.put(PlanHints.PLAN_BORDER, new Boolean(true));
  96. hints.put(PlanHints.FONT_FAMILY, fontFamily);
  97. hints.put(PlanHints.FONT_SIZE, new Float(fontSize));
  98. hints.put(PlanHints.LABEL_FONT_SIZE, new Float(fontSize));
  99. hints.put(PlanHints.LABEL_FONT, fontFamily);
  100. hints.put(PlanHints.LABEL_TYPE, "textOnly");
  101. StringTokenizer st = new StringTokenizer(style, ";");
  102. while (st.hasMoreTokens()) {
  103. String pair = st.nextToken().trim();
  104. int index = pair.indexOf(":");
  105. String name = pair.substring(0, index).trim();
  106. String val = pair.substring(index + 1).trim();
  107. if (name.equals(PlanHints.PLAN_BORDER)) {
  108. hints.put(name, Boolean.valueOf(val));
  109. } else if (name.equals(PlanHints.FONT_SIZE)) {
  110. hints.put(name, Float.valueOf(val));
  111. } else if (name.equals(PlanHints.LABEL_FONT_SIZE)) {
  112. hints.put(name, Float.valueOf(val));
  113. } else {
  114. hints.put(name, val);
  115. }
  116. }
  117. }
  118. public ActionInfo getActionInfo(Element ele, Locale locale) {
  119. String t = ele.getAttribute("type");
  120. NodeList childs = ele.getChildNodes();
  121. ActionInfo data = new ActionInfo();
  122. if (t.equals("milestone")) {
  123. data.setType(ActionInfo.MILESTONE);
  124. } else if (t.equals("task")) {
  125. data.setType(ActionInfo.TASK);
  126. } else if (t.equals("grouping")) {
  127. data.setType(ActionInfo.GROUPING);
  128. } else {
  129. }
  130. for (int i = 0; i < childs.getLength(); i++) {
  131. Node obj = childs.item(i);
  132. String nname = obj.getNodeName();
  133. if (nname.equals("label")) {
  134. String dat = ((Element) obj).getFirstChild().getNodeValue();
  135. data.setLabel(dat);
  136. } else if (nname.equals("owner")) {
  137. String dat = ((Element) obj).getFirstChild().getNodeValue();
  138. data.setOwner(dat);
  139. } else if (nname.equals("startdate")) {
  140. Date dat = getDate((Element) obj, locale);
  141. data.setStartDate(dat);
  142. } else if (nname.equals("enddate")) {
  143. Date dat = getDate((Element) obj, locale);
  144. data.setEndDate(dat);
  145. }
  146. }
  147. return data;
  148. }
  149. public EventList getEvents(Element ele, Locale locale) {
  150. EventList data = new EventList();
  151. NodeList childs = ele.getChildNodes();
  152. for (int i = 0; i < childs.getLength(); i++) {
  153. Node obj = childs.item(i);
  154. if (obj.getNodeName().equals("group")) {
  155. GroupInfo dat = getGroupInfo((Element) obj, locale);
  156. data.addGroupInfo(dat);
  157. }
  158. }
  159. return data;
  160. }
  161. public GroupInfo getGroupInfo(Element ele, Locale locale) {
  162. NodeList childs = ele.getChildNodes();
  163. GroupInfo data = new GroupInfo(ele.getAttribute("name"));
  164. for (int i = 0; i < childs.getLength(); i++) {
  165. Node obj = childs.item(i);
  166. if (obj.getNodeName().equals("action")) {
  167. ActionInfo dat = getActionInfo((Element) obj, locale);
  168. data.addActionInfo(dat);
  169. }
  170. }
  171. return data;
  172. }
  173. public Date getDate(Element ele, Locale locale) {
  174. String label = ele.getFirstChild().getNodeValue();
  175. return getDate(label, locale);
  176. }
  177. public Date getDate(String label, Locale locale) {
  178. Calendar cal = Calendar.getInstance(locale);
  179. String str;
  180. str = label.substring(0, 4);
  181. int intVal = Integer.valueOf(str).intValue();
  182. cal.set(Calendar.YEAR, intVal);
  183. str = label.substring(4, 6);
  184. intVal = Integer.valueOf(str).intValue();
  185. cal.set(Calendar.MONTH, intVal - 1);
  186. str = label.substring(6, 8);
  187. intVal = Integer.valueOf(str).intValue();
  188. cal.set(Calendar.DATE, intVal);
  189. return cal.getTime();
  190. }
  191. }