KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > plan > PlanRenderer


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 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.plan;
21
22
23 import java.util.Calendar JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33
34 import org.apache.batik.dom.svg.SVGDOMImplementation;
35
36 public class PlanRenderer {
37     
38     private static final String JavaDoc SVG_NAMESPACE = SVGDOMImplementation.SVG_NAMESPACE_URI;
39     
40     private String JavaDoc fontFamily = "sansserif";
41     private float fontSize = 12;
42     private String JavaDoc type = "";
43     private String JavaDoc lang = "";
44     private String JavaDoc country = "";
45     private String JavaDoc variant = "";
46     private float width;
47     private float height;
48     private float topEdge;
49     private float rightEdge;
50     private HashMap JavaDoc hints = new HashMap JavaDoc();
51
52     private String JavaDoc[] colours;
53     private String JavaDoc[] darkcolours;
54
55     public void setFontInfo(String JavaDoc 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 JavaDoc str) {
69         return Float.parseFloat(str);
70     }
71
72     public Document JavaDoc createSVGDocument(Document JavaDoc planDoc) {
73
74         Element JavaDoc 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 JavaDoc style = svgRoot.getAttribute("style");
83         parseStyle(style);
84
85         Locale JavaDoc locale = new Locale JavaDoc(lang, country == null ? "" : country,
86                                    variant == null ? "" : variant);
87
88         String JavaDoc start = svgRoot.getAttribute("start");
89         String JavaDoc end = svgRoot.getAttribute("end");
90         Date JavaDoc sd = getDate(start, locale);
91         Date JavaDoc ed = getDate(end, locale);
92
93         String JavaDoc title = "";
94         EventList data = null;
95         NodeList JavaDoc childs = svgRoot.getChildNodes();
96         for (int i = 0; i < childs.getLength(); i++) {
97             Node JavaDoc obj = childs.item(i);
98             String JavaDoc nname = obj.getNodeName();
99             if (nname.equals("title")) {
100                 title = ((Element JavaDoc) obj).getFirstChild().getNodeValue();
101             } else if (nname.equals("events")) {
102                 data = getEvents((Element JavaDoc) 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 JavaDoc(fontSize));
111         hints.put(PlanHints.LOCALE, locale);
112         Document JavaDoc doc =
113           planDrawer.createDocument(data, width, height, hints);
114         return doc;
115     }
116
117     protected void parseStyle(String JavaDoc style) {
118         hints.put(PlanHints.PLAN_BORDER, new Boolean JavaDoc(true));
119         hints.put(PlanHints.FONT_FAMILY, fontFamily);
120         hints.put(PlanHints.FONT_SIZE, new Float JavaDoc(fontSize));
121         hints.put(PlanHints.LABEL_FONT_SIZE, new Float JavaDoc(fontSize));
122         hints.put(PlanHints.LABEL_FONT, fontFamily);
123         hints.put(PlanHints.LABEL_TYPE, "textOnly");
124
125         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(style, ";");
126         while (st.hasMoreTokens()) {
127             String JavaDoc pair = st.nextToken().trim();
128             int index = pair.indexOf(":");
129             String JavaDoc name = pair.substring(0, index).trim();
130             String JavaDoc 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 JavaDoc ele, Locale JavaDoc locale) {
144         String JavaDoc t = ele.getAttribute("type");
145
146         NodeList JavaDoc 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 JavaDoc obj = childs.item(i);
159             String JavaDoc nname = obj.getNodeName();
160             if (nname.equals("label")) {
161                 String JavaDoc dat = ((Element JavaDoc) obj).getFirstChild().getNodeValue();
162                 data.setLabel(dat);
163             } else if (nname.equals("owner")) {
164                 String JavaDoc dat = ((Element JavaDoc) obj).getFirstChild().getNodeValue();
165                 data.setOwner(dat);
166             } else if (nname.equals("startdate")) {
167                 Date JavaDoc dat = getDate((Element JavaDoc) obj, locale);
168                 data.setStartDate(dat);
169             } else if (nname.equals("enddate")) {
170                 Date JavaDoc dat = getDate((Element JavaDoc) obj, locale);
171                 data.setEndDate(dat);
172             }
173         }
174         return data;
175     }
176
177     public EventList getEvents(Element JavaDoc ele, Locale JavaDoc locale) {
178         EventList data = new EventList();
179         NodeList JavaDoc childs = ele.getChildNodes();
180         for (int i = 0; i < childs.getLength(); i++) {
181             Node JavaDoc obj = childs.item(i);
182             if (obj.getNodeName().equals("group")) {
183                 GroupInfo dat = getGroupInfo((Element JavaDoc) obj, locale);
184                 data.addGroupInfo(dat);
185             }
186         }
187         return data;
188     }
189
190     public GroupInfo getGroupInfo(Element JavaDoc ele, Locale JavaDoc locale) {
191         NodeList JavaDoc childs = ele.getChildNodes();
192         GroupInfo data = new GroupInfo(ele.getAttribute("name"));
193         for (int i = 0; i < childs.getLength(); i++) {
194             Node JavaDoc obj = childs.item(i);
195             if (obj.getNodeName().equals("action")) {
196                 ActionInfo dat = getActionInfo((Element JavaDoc) obj, locale);
197                 data.addActionInfo(dat);
198             }
199         }
200         return data;
201     }
202
203     public Date JavaDoc getDate(Element JavaDoc ele, Locale JavaDoc locale) {
204         String JavaDoc label = ele.getFirstChild().getNodeValue();
205         return getDate(label, locale);
206     }
207
208     public Date JavaDoc getDate(String JavaDoc label, Locale JavaDoc locale) {
209         Calendar JavaDoc cal = Calendar.getInstance(locale);
210
211         String JavaDoc 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