KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > taglib > TagLibraryLoader


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.taglib;
19
20 import com.finalist.jag.util.Log;
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.Element JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collection JavaDoc;
35
36 /**
37  * Class TagLibraryLoader
38  *
39  *
40  * @author Wendel D. de Witte
41  * @version %I%, %G%
42  */

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

57    public TagLibraryLoader(File JavaDoc config) {
58       Log.log("loading: " + config.getPath());
59       DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
60       DocumentBuilder JavaDoc builder = null;
61       try {
62           builder = dbf.newDocumentBuilder();
63           doc = builder.parse(config);
64       } catch (Exception JavaDoc e) {
65           e.printStackTrace();
66       }
67    }
68
69
70    /**
71     * Method getTagLibrary
72     *
73     *
74     * @return
75     *
76     */

77    public TagLibrary getTagLibrary() {
78
79       if (doc == null)
80          return null;
81       Element JavaDoc root = doc.getDocumentElement();
82
83       TagLibrary library = parseTagLibrary(root);
84       return library;
85    }
86
87
88    /**
89     * Method parseTagLibrary
90     *
91     *
92     * @param root
93     *
94     * @return
95     *
96     */

97    private TagLibrary parseTagLibrary(org.w3c.dom.Element JavaDoc root) {
98       TagLibrary tagLibrary = new TagLibrary();
99
100       tagLibrary.setShortName(getAttribute(root, "shortname"));
101       tagLibrary.setLibVersion(getAttribute(root, "tlibversion"));
102       tagLibrary.setJagVersion(getAttribute(root, "jagversion"));
103       tagLibrary.setInfo(getAttribute(root, "info"));
104       tagLibrary.setTagDefs(getTagDefs(root, "tag"));
105
106       return tagLibrary;
107    }
108
109
110    /**
111     * Method getTagDefs
112     *
113     *
114     * @param root
115     * @param label
116     *
117     * @return
118     *
119     */

120    private Collection JavaDoc getTagDefs(org.w3c.dom.Element JavaDoc root, String JavaDoc label) {
121       ArrayList JavaDoc defList = new ArrayList JavaDoc();
122       NodeList JavaDoc list = root.getElementsByTagName(label);
123
124       for (int i = 0; i < list.getLength(); i++) {
125          org.w3c.dom.Element JavaDoc tagnode = (org.w3c.dom.Element JavaDoc) list.item(i);
126          TagDef tagDef = new TagDef();
127
128          tagDef.setName(getAttribute(tagnode, "name"));
129          tagDef.setTagClass(getAttribute(tagnode, "tagclass"));
130          tagDef.setBodyContent(getAttribute(tagnode, "bodycontent"));
131          tagDef.setAttributeDefs(getAttributeDefs(tagnode, "attribute"));
132          defList.add(tagDef);
133       }
134
135       return defList;
136    }
137
138
139    /**
140     * Method getAttributeDefs
141     *
142     *
143     * @param root
144     * @param label
145     *
146     * @return
147     *
148     */

149    private Collection JavaDoc getAttributeDefs(org.w3c.dom.Element JavaDoc root, String JavaDoc label) {
150       ArrayList JavaDoc defList = new ArrayList JavaDoc();
151       NodeList JavaDoc list = root.getElementsByTagName(label);
152
153       for (int i = 0; i < list.getLength(); i++) {
154          org.w3c.dom.Element JavaDoc tagnode = (org.w3c.dom.Element JavaDoc) list.item(i);
155          AttributeDef attrDef = new AttributeDef();
156
157          attrDef.setName(getAttribute(tagnode, "name"));
158          attrDef.setRequired(getAttribute(tagnode, "required"));
159          defList.add(attrDef);
160       }
161
162       return defList;
163    }
164
165
166    /**
167     * Method getAttribute
168     *
169     *
170     * @param root
171     * @param label
172     *
173     * @return
174     *
175     */

176    private String JavaDoc getAttribute(org.w3c.dom.Element JavaDoc root, String JavaDoc label) {
177       String JavaDoc sAttribute = root.getAttribute(label);
178
179       if ((sAttribute == null) || (sAttribute.length() < 1)) {
180          NodeList JavaDoc list = root.getElementsByTagName(label);
181
182          if (list.getLength() > 0) {
183             org.w3c.dom.Element JavaDoc node = (org.w3c.dom.Element JavaDoc) list.item(0);
184             if (node.getFirstChild() != null)
185                sAttribute = node.getFirstChild().getNodeValue();
186          }
187       }
188       return sAttribute;
189    }
190
191
192    /**
193     * Method getException
194     *
195     *
196     * @return
197     *
198     */

199    public Exception JavaDoc getException() {
200       return exception;
201    }
202 }
Popular Tags