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.7KB

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