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 18 /* $Id: PlanRenderer.java 985571 2010-08-14 19:28:26Z jeremias $ */ 19 20 package org.apache.fop.plan; 21 22 23 import java.util.Calendar; 24 import java.util.Date; 25 import java.util.HashMap; 26 import java.util.Locale; 27 import java.util.StringTokenizer; 28 29 import org.w3c.dom.Document; 30 import org.w3c.dom.Element; 31 import org.w3c.dom.Node; 32 import org.w3c.dom.NodeList; 33 34 import org.apache.batik.dom.svg.SVGDOMImplementation; 35 36 public class PlanRenderer { 37 38 private static final String SVG_NAMESPACE = SVGDOMImplementation.SVG_NAMESPACE_URI; 39 40 private String fontFamily = "sansserif"; 41 private float fontSize = 12; 42 private String type = ""; 43 private String lang = ""; 44 private String country = ""; 45 private String variant = ""; 46 private float width; 47 private float height; 48 private float topEdge; 49 private float rightEdge; 50 private HashMap hints = new HashMap(); 51 52 private String[] colours; 53 private String[] darkcolours; 54 setFontInfo(String fam, float si)55 public void setFontInfo(String fam, float si) { 56 fontFamily = fam; 57 fontSize = si; 58 } 59 getWidth()60 public float getWidth() { 61 return width; 62 } 63 getHeight()64 public float getHeight() { 65 return height; 66 } 67 toFloat(String str)68 protected float toFloat(String str) { 69 return Float.parseFloat(str); 70 } 71 createSVGDocument(Document planDoc)72 public Document createSVGDocument(Document planDoc) { 73 74 Element svgRoot = planDoc.getDocumentElement(); 75 76 width = toFloat(svgRoot.getAttribute("width")); 77 height = toFloat(svgRoot.getAttribute("height")); 78 type = svgRoot.getAttribute("type"); 79 lang = svgRoot.getAttribute("lang"); 80 country = svgRoot.getAttribute("country"); 81 variant = svgRoot.getAttribute("variant"); 82 String style = svgRoot.getAttribute("style"); 83 parseStyle(style); 84 85 Locale locale = new Locale(lang, country == null ? "" : country, 86 variant == null ? "" : variant); 87 88 String start = svgRoot.getAttribute("start"); 89 String end = svgRoot.getAttribute("end"); 90 Date sd = getDate(start, locale); 91 Date ed = getDate(end, locale); 92 93 String title = ""; 94 EventList data = null; 95 NodeList childs = svgRoot.getChildNodes(); 96 for (int i = 0; i < childs.getLength(); i++) { 97 Node obj = childs.item(i); 98 String nname = obj.getNodeName(); 99 if (nname.equals("title")) { 100 title = ((Element) obj).getFirstChild().getNodeValue(); 101 } else if (nname.equals("events")) { 102 data = getEvents((Element) obj, locale); 103 } 104 } 105 106 SimplePlanDrawer planDrawer = new SimplePlanDrawer(); 107 planDrawer.setStartDate(sd); 108 planDrawer.setEndDate(ed); 109 hints.put(PlanHints.FONT_FAMILY, fontFamily); 110 hints.put(PlanHints.FONT_SIZE, new Float(fontSize)); 111 hints.put(PlanHints.LOCALE, locale); 112 Document doc 113 = planDrawer.createDocument(data, width, height, hints); 114 return doc; 115 } 116 parseStyle(String style)117 protected void parseStyle(String style) { 118 hints.put(PlanHints.PLAN_BORDER, new Boolean(true)); 119 hints.put(PlanHints.FONT_FAMILY, fontFamily); 120 hints.put(PlanHints.FONT_SIZE, new Float(fontSize)); 121 hints.put(PlanHints.LABEL_FONT_SIZE, new Float(fontSize)); 122 hints.put(PlanHints.LABEL_FONT, fontFamily); 123 hints.put(PlanHints.LABEL_TYPE, "textOnly"); 124 125 StringTokenizer st = new StringTokenizer(style, ";"); 126 while (st.hasMoreTokens()) { 127 String pair = st.nextToken().trim(); 128 int index = pair.indexOf(":"); 129 String name = pair.substring(0, index).trim(); 130 String val = pair.substring(index + 1).trim(); 131 if (name.equals(PlanHints.PLAN_BORDER)) { 132 hints.put(name, Boolean.valueOf(val)); 133 } else if (name.equals(PlanHints.FONT_SIZE)) { 134 hints.put(name, Float.valueOf(val)); 135 } else if (name.equals(PlanHints.LABEL_FONT_SIZE)) { 136 hints.put(name, Float.valueOf(val)); 137 } else { 138 hints.put(name, val); 139 } 140 } 141 } 142 getActionInfo(Element ele, Locale locale)143 public ActionInfo getActionInfo(Element ele, Locale locale) { 144 String t = ele.getAttribute("type"); 145 146 NodeList childs = ele.getChildNodes(); 147 ActionInfo data = new ActionInfo(); 148 if (t.equals("milestone")) { 149 data.setType(ActionInfo.MILESTONE); 150 } else if (t.equals("task")) { 151 data.setType(ActionInfo.TASK); 152 } else if (t.equals("grouping")) { 153 data.setType(ActionInfo.GROUPING); 154 } else { 155 throw new IllegalArgumentException("Unknown action type: " + t); 156 } 157 158 for (int i = 0; i < childs.getLength(); i++) { 159 Node obj = childs.item(i); 160 String nname = obj.getNodeName(); 161 if (nname.equals("label")) { 162 String dat = ((Element) obj).getFirstChild().getNodeValue(); 163 data.setLabel(dat); 164 } else if (nname.equals("owner")) { 165 String dat = ((Element) obj).getFirstChild().getNodeValue(); 166 data.setOwner(dat); 167 } else if (nname.equals("startdate")) { 168 Date dat = getDate((Element) obj, locale); 169 data.setStartDate(dat); 170 } else if (nname.equals("enddate")) { 171 Date dat = getDate((Element) obj, locale); 172 data.setEndDate(dat); 173 } 174 } 175 return data; 176 } 177 getEvents(Element ele, Locale locale)178 public EventList getEvents(Element ele, Locale locale) { 179 EventList data = new EventList(); 180 NodeList childs = ele.getChildNodes(); 181 for (int i = 0; i < childs.getLength(); i++) { 182 Node obj = childs.item(i); 183 if (obj.getNodeName().equals("group")) { 184 GroupInfo dat = getGroupInfo((Element) obj, locale); 185 data.addGroupInfo(dat); 186 } 187 } 188 return data; 189 } 190 getGroupInfo(Element ele, Locale locale)191 public GroupInfo getGroupInfo(Element ele, Locale locale) { 192 NodeList childs = ele.getChildNodes(); 193 GroupInfo data = new GroupInfo(ele.getAttribute("name")); 194 for (int i = 0; i < childs.getLength(); i++) { 195 Node obj = childs.item(i); 196 if (obj.getNodeName().equals("action")) { 197 ActionInfo dat = getActionInfo((Element) obj, locale); 198 data.addActionInfo(dat); 199 } 200 } 201 return data; 202 } 203 getDate(Element ele, Locale locale)204 public Date getDate(Element ele, Locale locale) { 205 String label = ele.getFirstChild().getNodeValue(); 206 return getDate(label, locale); 207 } 208 getDate(String label, Locale locale)209 public Date getDate(String label, Locale locale) { 210 Calendar cal = Calendar.getInstance(locale); 211 212 String str; 213 str = label.substring(0, 4); 214 int intVal = Integer.valueOf(str).intValue(); 215 cal.set(Calendar.YEAR, intVal); 216 217 str = label.substring(4, 6); 218 intVal = Integer.valueOf(str).intValue(); 219 cal.set(Calendar.MONTH, intVal - 1); 220 221 str = label.substring(6, 8); 222 intVal = Integer.valueOf(str).intValue(); 223 cal.set(Calendar.DATE, intVal); 224 return cal.getTime(); 225 } 226 } 227 228