KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > util > ProjectDocumentHandler


1 package com.genimen.djeneric.tools.generator.util;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Enumeration JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import javax.xml.parsers.DocumentBuilder JavaDoc;
11 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
12 import javax.xml.transform.OutputKeys JavaDoc;
13 import javax.xml.transform.Transformer JavaDoc;
14 import javax.xml.transform.TransformerFactory JavaDoc;
15 import javax.xml.transform.dom.DOMSource JavaDoc;
16 import javax.xml.transform.stream.StreamResult JavaDoc;
17
18 import org.w3c.dom.Document JavaDoc;
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.NodeList JavaDoc;
21
22 import com.genimen.djeneric.util.DjLogger;
23
24 public class ProjectDocumentHandler
25 {
26   private Document JavaDoc _xmlDoc = null;
27
28   public final static String JavaDoc PROJECT_ELEMENT = "project";
29   public final static String JavaDoc PROJECT_OUTPUT_DIR = "dir";
30   public final static String JavaDoc PROJECT_OVERWRITE_ACTION = "overwriteaction";
31   public final static String JavaDoc PROJECT_OPENAFTERGENERATE = "openaftergenerate";
32
33   public final static String JavaDoc TEMPLATESET_ELEMENT = "templateset";
34   public final static String JavaDoc TEMPLATESET_NAME = "name";
35
36   public final static String JavaDoc TEMPLATESETTINGS_ELEMENT = "templatesettings";
37   public final static String JavaDoc TEMPLATESETTINGS_NAME = "name";
38   public final static String JavaDoc TEMPLATESETTINGS_ENABLED = "enabled";
39
40   public ProjectDocumentHandler() throws Exception JavaDoc
41   {
42   }
43
44   public ProjectDocumentHandler(String JavaDoc xmlString) throws Exception JavaDoc
45   {
46     ByteArrayInputStream JavaDoc sr = new ByteArrayInputStream JavaDoc(xmlString.getBytes("UTF8"));
47
48     DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance();
49     _xmlDoc = dfactory.newDocumentBuilder().parse(sr);
50   }
51
52   public String JavaDoc[] getTemplateSetIds()
53   {
54     ArrayList JavaDoc result = new ArrayList JavaDoc();
55
56     Element JavaDoc project = (Element JavaDoc) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0);
57
58     if (project != null)
59     {
60       NodeList JavaDoc templateSets = project.getElementsByTagName(TEMPLATESET_ELEMENT);
61       for (int t = 0; t < templateSets.getLength(); t++)
62       {
63         if (templateSets.item(t) instanceof Element JavaDoc)
64         {
65           Element JavaDoc templateSet = (Element JavaDoc) (templateSets.item(t));
66           result.add(templateSet.getAttribute(TEMPLATESET_NAME));
67         }
68       }
69     }
70     return (String JavaDoc[]) result.toArray(new String JavaDoc[0]);
71   }
72
73   public String JavaDoc[] getDisabledTemplates()
74   {
75     ArrayList JavaDoc result = new ArrayList JavaDoc();
76
77     Element JavaDoc project = (Element JavaDoc) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0);
78
79     if (project != null)
80     {
81       NodeList JavaDoc templateSets = project.getElementsByTagName(TEMPLATESETTINGS_ELEMENT);
82       for (int t = 0; t < templateSets.getLength(); t++)
83       {
84         if (templateSets.item(t) instanceof Element JavaDoc)
85         {
86           Element JavaDoc templateSet = (Element JavaDoc) (templateSets.item(t));
87           if (templateSet.getAttribute(TEMPLATESETTINGS_ENABLED).equals("false")) result.add(templateSet
88               .getAttribute(TEMPLATESETTINGS_NAME));
89         }
90       }
91     }
92     return (String JavaDoc[]) result.toArray(new String JavaDoc[0]);
93   }
94
95   public String JavaDoc getOutputPath()
96   {
97     Element JavaDoc project = (Element JavaDoc) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0);
98
99     if (project != null)
100     {
101       String JavaDoc result = project.getAttribute(PROJECT_OUTPUT_DIR);
102       if (result != null) return result;
103     }
104     return "";
105   }
106
107   public int getOverwriteAction()
108   {
109     Element JavaDoc project = (Element JavaDoc) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0);
110
111     if (project != null)
112     {
113       String JavaDoc result = project.getAttribute(PROJECT_OVERWRITE_ACTION);
114       try
115       {
116         if (result != null) return Integer.parseInt(result);
117       }
118       catch (Exception JavaDoc x)
119       {
120         DjLogger.log(x);
121       }
122     }
123     return ProjectProperties.OVERWRITE_ASK;
124   }
125
126   public boolean getOpenAfterGenerate()
127   {
128     Element JavaDoc project = (Element JavaDoc) _xmlDoc.getElementsByTagName(PROJECT_ELEMENT).item(0);
129
130     if (project != null)
131     {
132       return "true".equals(project.getAttribute(PROJECT_OPENAFTERGENERATE));
133     }
134     return false;
135   }
136
137   public String JavaDoc createXmlString(ProjectProperties projProps) throws Exception JavaDoc
138   {
139     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
140     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
141     Document JavaDoc doc = builder.newDocument();
142
143     Element JavaDoc root = doc.createElement(PROJECT_ELEMENT);
144     root.setAttribute(PROJECT_OUTPUT_DIR, projProps.getOutputPath());
145     root.setAttribute(PROJECT_OVERWRITE_ACTION, "" + projProps.getOverwriteAction());
146     root.setAttribute(PROJECT_OPENAFTERGENERATE, "" + projProps.isOpenAfterGenerate());
147
148     doc.appendChild(root);
149
150     Iterator JavaDoc it = projProps.getTemplateSetIterator();
151     while (it.hasNext())
152     {
153       Element JavaDoc setId = doc.createElement(TEMPLATESET_ELEMENT);
154       setId.setAttribute(TEMPLATESET_NAME, it.next().toString());
155
156       root.appendChild(setId);
157     }
158
159     Properties JavaDoc props = projProps.getTemplateProperties();
160     Enumeration JavaDoc enum1 = props.keys();
161     while (enum1.hasMoreElements())
162     {
163       String JavaDoc key = enum1.nextElement().toString();
164       if (key.endsWith(ProjectProperties.TEMPLATE_ENABLED_KEY))
165       {
166         boolean enabled = props.getProperty(key).equals("true");
167         if (!enabled)
168         {
169           int idx = key.indexOf(".");
170           String JavaDoc name = key.substring(0, idx);
171
172           Element JavaDoc disabled = doc.createElement(TEMPLATESETTINGS_ELEMENT);
173           disabled.setAttribute(TEMPLATESETTINGS_NAME, name);
174           disabled.setAttribute(TEMPLATESETTINGS_ENABLED, "false");
175           root.appendChild(disabled);
176         }
177       }
178     }
179     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
180
181     Transformer JavaDoc serializer = TransformerFactory.newInstance().newTransformer();
182     serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
183     serializer.setOutputProperty(OutputKeys.INDENT, "yes");
184     serializer.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(baos));
185
186     return baos.toString();
187   }
188
189   public void load(ProjectProperties projectProperties)
190   {
191     projectProperties.setOutputPath(getOutputPath());
192     String JavaDoc[] ids = getTemplateSetIds();
193     for (int i = 0; i < ids.length; i++)
194       projectProperties.addTemplate(ids[i]);
195     ids = getDisabledTemplates();
196
197     for (int i = 0; i < ids.length; i++)
198     {
199       projectProperties.enableTemplate(ids[i], false);
200     }
201     projectProperties.setOverwriteAction(getOverwriteAction());
202     projectProperties.setOpenAfterGenerate(getOpenAfterGenerate());
203     projectProperties.setModified(false);
204   }
205 }
Popular Tags