KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > skelet > XMLSkeletLoader


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.skelet;
19
20
21 import com.finalist.jag.util.Log;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.NodeList JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collection JavaDoc;
35
36 /**
37  * Class XMLSkeletLoader
38  *
39  *
40  * @author Wendel D. de Witte
41  * @version %I%, %G%
42  */

43 public class XMLSkeletLoader implements SkeletLoader {
44
45    /** Field parser */
46    private Document JavaDoc doc = null;
47
48    /** Field exception */
49    private Exception JavaDoc exception = null;
50
51    /** Field skeletObj */
52    private SkeletDataObj skeletObj = null;
53
54
55    /**
56     * Creates new ObjectModel
57     *
58     * @param config
59     */

60    public XMLSkeletLoader(File JavaDoc config) throws Exception JavaDoc {
61
62       Log.log("loading: " + config.getPath());
63
64       InputSource JavaDoc inputSource = new InputSource JavaDoc(
65          new InputStreamReader JavaDoc(new FileInputStream JavaDoc(config)));
66
67
68        DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
69        DocumentBuilder JavaDoc builder = null;
70        try {
71            builder = dbf.newDocumentBuilder();
72            doc = builder.parse(config);
73        } catch (Exception JavaDoc e) {
74            e.printStackTrace();
75        }
76    }
77
78
79
80    /**
81     * Method getTagLibrary
82     *
83     *
84     * @return
85     *
86     *
87     * @throws JagSkeletException
88     */

89    public SkeletDataObj getSkeletData() throws JagSkeletException {
90       if ((skeletObj == null) && (doc != null)) {
91          Element JavaDoc root = doc.getDocumentElement();
92          skeletObj = parseSkelet(root);
93          skeletObj.processReferences();
94       }
95
96       return skeletObj;
97    }
98
99
100    /**
101     * Method parseTagLibrary
102     *
103     *
104     * @param root
105     *
106     * @return
107     *
108     */

109    private SkeletDataObj parseSkelet(org.w3c.dom.Element JavaDoc root) {
110       SkeletDataObj obj = new SkeletDataObj(root.getNodeName());
111       Collection JavaDoc rootObj = (Collection JavaDoc) obj.getValue();
112       Node JavaDoc node = root.getFirstChild();
113       JagSkeletConfig config = getConfig(root);
114
115       while (node != null) {
116          if ((node.getNodeType() == node.ELEMENT_NODE)
117             && node.getNodeName().equals("module")) {
118             String JavaDoc name = getAttribute((Element JavaDoc) node, "name");
119             SkeletModule module = new SkeletModule(name);
120
121             module.setRefname(getAttribute((Element JavaDoc) node, "ref-name"));
122             module.setRefs(getAttributes((Element JavaDoc) node, "ref"));
123             module.setValue(getModuleData((Element JavaDoc) node));
124             rootObj.add(module);
125          }
126          node = node.getNextSibling();
127       }
128       obj.setConfig(config);
129       return obj;
130    }
131
132
133    /**
134     * Method getModuleData
135     *
136     *
137     * @param parent
138     *
139     * @return
140     *
141     */

142    private Collection JavaDoc getModuleData(org.w3c.dom.Element JavaDoc parent) {
143       Node JavaDoc node = parent.getFirstChild();
144       ArrayList JavaDoc dataModules = new ArrayList JavaDoc();
145
146       while (node != null) {
147          if ((node.getNodeType() == node.ELEMENT_NODE)
148             && node.getNodeName().equals("module-data")) {
149             String JavaDoc name = getAttribute((Element JavaDoc) node, "name");
150             if (((Element JavaDoc) node).getElementsByTagName("module-data").getLength() > 0) {
151                Collection JavaDoc value = getModuleData((Element JavaDoc) node);
152                dataModules.add(new ModuleData(name, value));
153             }
154             else if (node.hasChildNodes()) {
155                String JavaDoc value = node.getFirstChild().getNodeValue();
156                dataModules.add(new ModuleData(name, value));
157             }
158             else {
159                dataModules.add(new ModuleData(name, ""));
160             }
161          }
162          node = node.getNextSibling();
163       }
164       return dataModules;
165    }
166
167
168    private JagSkeletConfig getConfig(org.w3c.dom.Element JavaDoc root) {
169       JagSkeletConfig config = new JagSkeletConfig();
170       NodeList JavaDoc list = root.getElementsByTagName("config");
171
172       for (int i = 0; i < list.getLength(); i++) {
173          org.w3c.dom.Element JavaDoc tagnode = (org.w3c.dom.Element JavaDoc) list.item(i);
174          config.setAuthor(getAttribute(tagnode, "author"));
175          config.setVersion(getAttribute(tagnode, "version"));
176          config.setCompany(getAttribute(tagnode, "company"));
177       }
178
179       list = root.getElementsByTagName("templates");
180       for (int i = 0; i < list.getLength(); i++) {
181          org.w3c.dom.Element JavaDoc node = (org.w3c.dom.Element JavaDoc) list.item(i);
182          Node JavaDoc childNode = node.getFirstChild();
183          while (childNode != null) {
184             if (childNode.getNodeName().equals("template-root") &&
185                childNode.hasChildNodes()) {
186                config.addTemplateUrl(childNode.getFirstChild().getNodeValue());
187             }
188             childNode = childNode.getNextSibling();
189          }
190       }
191       return config;
192    }
193
194
195    /**
196     * Method getAttribute
197     *
198     *
199     * @param root
200     * @param label
201     *
202     * @return
203     *
204     */

205    private String JavaDoc getAttribute(org.w3c.dom.Element JavaDoc root, String JavaDoc label) {
206
207       String JavaDoc sAttribute = root.getAttribute(label);
208
209       if ((sAttribute == null) || (sAttribute.length() < 1)) {
210          NodeList JavaDoc list = root.getElementsByTagName(label);
211
212          if (list.getLength() > 0) {
213             org.w3c.dom.Element JavaDoc node = (org.w3c.dom.Element JavaDoc) list.item(0);
214
215             if (node.getFirstChild() != null) {
216                sAttribute = node.getFirstChild().getNodeValue();
217             }
218          }
219       }
220
221       return sAttribute;
222    }
223
224
225    /**
226     * Method getAttributes
227     *
228     *
229     * @param root
230     * @param label
231     *
232     * @return
233     *
234     */

235    private Collection JavaDoc getAttributes(org.w3c.dom.Element JavaDoc root, String JavaDoc label) {
236
237       ArrayList JavaDoc attrList = new ArrayList JavaDoc();
238       NodeList JavaDoc l2 = root.getElementsByTagName(label);
239
240       for (int i = 0; i < l2.getLength(); i++) {
241          org.w3c.dom.Element JavaDoc element = (org.w3c.dom.Element JavaDoc) l2.item(i);
242
243          if (element.getFirstChild() != null) {
244             attrList.add(element.getFirstChild().getNodeValue());
245          }
246       }
247
248       return attrList;
249    }
250
251
252    /**
253     * Method getException
254     *
255     *
256     * @return
257     *
258     */

259    public Exception JavaDoc getException() {
260       return exception;
261    }
262 }
Popular Tags