1 17 18 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 55 public void setFontInfo(String fam, float si) { 56 fontFamily = fam; 57 fontSize = si; 58 } 59 60 public float getWidth() { 61 return width; 62 } 63 64 public float getHeight() { 65 return height; 66 } 67 68 protected float toFloat(String str) { 69 return Float.parseFloat(str); 70 } 71 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 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 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 } 156 157 for (int i = 0; i < childs.getLength(); i++) { 158 Node obj = childs.item(i); 159 String nname = obj.getNodeName(); 160 if (nname.equals("label")) { 161 String dat = ((Element ) obj).getFirstChild().getNodeValue(); 162 data.setLabel(dat); 163 } else if (nname.equals("owner")) { 164 String dat = ((Element ) obj).getFirstChild().getNodeValue(); 165 data.setOwner(dat); 166 } else if (nname.equals("startdate")) { 167 Date dat = getDate((Element ) obj, locale); 168 data.setStartDate(dat); 169 } else if (nname.equals("enddate")) { 170 Date dat = getDate((Element ) obj, locale); 171 data.setEndDate(dat); 172 } 173 } 174 return data; 175 } 176 177 public EventList getEvents(Element ele, Locale locale) { 178 EventList data = new EventList(); 179 NodeList childs = ele.getChildNodes(); 180 for (int i = 0; i < childs.getLength(); i++) { 181 Node obj = childs.item(i); 182 if (obj.getNodeName().equals("group")) { 183 GroupInfo dat = getGroupInfo((Element ) obj, locale); 184 data.addGroupInfo(dat); 185 } 186 } 187 return data; 188 } 189 190 public GroupInfo getGroupInfo(Element ele, Locale locale) { 191 NodeList childs = ele.getChildNodes(); 192 GroupInfo data = new GroupInfo(ele.getAttribute("name")); 193 for (int i = 0; i < childs.getLength(); i++) { 194 Node obj = childs.item(i); 195 if (obj.getNodeName().equals("action")) { 196 ActionInfo dat = getActionInfo((Element ) obj, locale); 197 data.addActionInfo(dat); 198 } 199 } 200 return data; 201 } 202 203 public Date getDate(Element ele, Locale locale) { 204 String label = ele.getFirstChild().getNodeValue(); 205 return getDate(label, locale); 206 } 207 208 public Date getDate(String label, Locale locale) { 209 Calendar cal = Calendar.getInstance(locale); 210 211 String str; 212 str = label.substring(0, 4); 213 int intVal = Integer.valueOf(str).intValue(); 214 cal.set(Calendar.YEAR, intVal); 215 216 str = label.substring(4, 6); 217 intVal = Integer.valueOf(str).intValue(); 218 cal.set(Calendar.MONTH, intVal - 1); 219 220 str = label.substring(6, 8); 221 intVal = Integer.valueOf(str).intValue(); 222 cal.set(Calendar.DATE, intVal); 223 return cal.getTime(); 224 } 225 } 226 227 | Popular Tags |