KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > xml > schema > core > lib > dom > parser > SchemaDOMBuilder


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.xml.schema.core.lib.dom.parser;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.TreeMap JavaDoc;
27 import javax.xml.parsers.DocumentBuilder JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.ParserConfigurationException JavaDoc;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.NamedNodeMap JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.netbeans.test.xml.schema.core.lib.types.ComponentCategories;
36
37
38 /**
39  *
40  * @author ca@netbeans.org
41  */

42 public class SchemaDOMBuilder {
43     
44     public static final int SCHEMA_LEVEL = 1;
45     public static final int GLOBAL_COMPONENTS_LEVEL = 2;
46     
47     Node JavaDoc m_schema = null;
48     TreeMap JavaDoc<String JavaDoc, Node JavaDoc> m_globalAttributes = new TreeMap JavaDoc<String JavaDoc, Node JavaDoc>();
49     TreeMap JavaDoc<String JavaDoc, Node JavaDoc> m_globalAttributeGroups = new TreeMap JavaDoc<String JavaDoc, Node JavaDoc>();
50     TreeMap JavaDoc<String JavaDoc, Node JavaDoc> m_globalComplexTypes = new TreeMap JavaDoc<String JavaDoc, Node JavaDoc>();
51     TreeMap JavaDoc<String JavaDoc, Node JavaDoc> m_globalElements = new TreeMap JavaDoc<String JavaDoc, Node JavaDoc>();
52     TreeMap JavaDoc<String JavaDoc, Node JavaDoc> m_globalGroups = new TreeMap JavaDoc<String JavaDoc, Node JavaDoc>();
53     TreeMap JavaDoc<String JavaDoc, Node JavaDoc> m_referencedSchemas = new TreeMap JavaDoc<String JavaDoc, Node JavaDoc>();
54     TreeMap JavaDoc<String JavaDoc, Node JavaDoc> m_globalSimpleTypes = new TreeMap JavaDoc<String JavaDoc, Node JavaDoc>();
55     
56     public int m_lineNmb;
57     private String JavaDoc m_strFileName;
58     
59     private Document JavaDoc m_doc = null;
60     
61     /**
62      * Creates a new instance of SchemaDOMBuilder
63      */

64     public SchemaDOMBuilder() {
65     }
66     
67     /**
68      * @param args the command line arguments
69      */

70     
71     public void setFileName(String JavaDoc fileName) {
72         m_strFileName = fileName;
73     }
74     
75     public void setInitialLineNumber(int lineNmb) {
76         m_lineNmb = lineNmb;
77     }
78     
79     public NodeIterator getNodeIterator(ComponentCategories category) {
80         NodeIterator iterator = null;
81         
82         switch (category) {
83             case ATTRIBUTES:
84                 iterator = new NodeIterator(m_globalAttributes);
85                 break;
86             case ATTRIBUTE_GROUPS:
87                 iterator = new NodeIterator(m_globalAttributeGroups);
88                 break;
89             case COMPLEX_TYPES:
90                 iterator = new NodeIterator(m_globalComplexTypes);
91                 break;
92             case ELEMENTS:
93                 iterator = new NodeIterator(m_globalElements);
94                 break;
95             case GROUPS:
96                 iterator = new NodeIterator(m_globalGroups);
97                 break;
98             case REFERENCED_SCHEMAS:
99                 iterator = new NodeIterator(m_referencedSchemas);
100                 break;
101             case SIMPLE_TYPES:
102                 iterator = new NodeIterator(m_globalSimpleTypes);
103                 break;
104         }
105         return iterator;
106     }
107     
108     public void build() {
109         File JavaDoc file = new File JavaDoc(m_strFileName);
110         
111         try {
112             m_doc = getDocumentBuilder().parse(file);
113         } catch (SAXException JavaDoc e) {
114             System.out.println("SAXException: " + e.getMessage());
115             return;
116         } catch (IOException JavaDoc e) {
117             System.out.println("IOException: " + e.getMessage());
118             return;
119         }
120         
121         categorizeNodes(m_doc, 0);
122         
123         adaptDocToColumnView(m_doc);
124     }
125     
126     
127     private void adaptDocToColumnView(Node JavaDoc node) {
128         
129         if (node.getNodeType() == Node.ELEMENT_NODE) {
130             String JavaDoc strComponentName = removePrefix(node.getNodeName());
131             
132             if (strComponentName.equals("simpleType")) {
133                 NodeList JavaDoc childList = node.getChildNodes();
134                 
135                 for (int i = 0; i < childList.getLength(); i++) {
136                     Node JavaDoc child = childList.item(i);
137                     String JavaDoc childName = removePrefix(child.getNodeName());
138                     if (childName.equals("restriction")) {
139                         NodeList JavaDoc childList1 = child.getChildNodes();
140                         for (int j = 0; j < childList1.getLength(); j++) {
141                             Node JavaDoc child1 = childList1.item(j);
142                             String JavaDoc childName1 = removePrefix(child1.getNodeName());
143                             if (childName1.equals("enumeration")) {
144                                 node.appendChild(child1);
145                                 j--;
146                             } else {
147                                 child.removeChild(child1);
148                                 j--;
149                             }
150                         }
151                         node.removeChild(child);
152                         i--;
153                     } else if (childName.equals("list")) {
154                         node.removeChild(child);
155                         i--;
156                     }
157                 }
158             } else if (strComponentName.equals("complexType")) {
159                 NodeList JavaDoc childList = node.getChildNodes();
160                 
161                 for (int i = 0; i < childList.getLength(); i++) {
162                     Node JavaDoc child = childList.item(i);
163                     String JavaDoc childName = removePrefix(child.getNodeName());
164                     if (childName.equals("simpleContent") || childName.equals("complexContent")) {
165                         NodeList JavaDoc childList1 = child.getChildNodes();
166                         for (int j = 0; j < childList1.getLength(); j++) {
167                             Node JavaDoc child1 = childList1.item(j);
168                             String JavaDoc childName1 = removePrefix(child1.getNodeName());
169                             if (childName1.equals("restriction") || childName1.equals("extension")) {
170                                 NodeList JavaDoc childList2 = child1.getChildNodes();
171                                 for (int k = 0; k < childList2.getLength(); k++) {
172                                     Node JavaDoc child2 = childList2.item(k);
173                                     node.appendChild(child2);
174                                     k--;
175                                 }
176                                 child.removeChild(child1);
177                                 j--;
178                             }
179                             node.removeChild(child);
180                             i--;
181                         }
182                     }
183                 }
184             }
185         }
186         
187         NodeList JavaDoc childList = node.getChildNodes();
188         
189         for (int i = 0; i < childList.getLength(); i++) {
190             adaptDocToColumnView(childList.item(i));
191         }
192         
193     }
194     
195     public static void main(String JavaDoc[] args) {
196         
197         SchemaDOMBuilder builder = new SchemaDOMBuilder();
198         
199         builder.setFileName(args[0]);
200         
201         builder.build();
202         
203         builder.printNodes(builder.m_doc, 0);
204     }
205     
206     private void printGlobalComponents(TreeMap JavaDoc<String JavaDoc, Node JavaDoc> map) {
207         Collection JavaDoc c = map.values();
208         Iterator JavaDoc iterator = c.iterator();
209         while(iterator.hasNext()) {
210             Node JavaDoc node = (Node JavaDoc) iterator.next();
211             ExtraNodeInfo sn = ExtraNodeInfo.getExtraNodeInfo(node);
212             printNodeInfo(node);
213             System.out.println("\n line " + sn.getLineNmb());
214             System.out.println(" component name " + sn.getComponentName());
215         }
216     }
217     
218     private static DocumentBuilder JavaDoc getDocumentBuilder() {
219         
220         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
221         
222         DocumentBuilder JavaDoc builder = null;
223         
224         try {
225             builder = factory.newDocumentBuilder();
226         } catch (ParserConfigurationException JavaDoc e) {}
227         
228         return builder;
229     }
230     
231     private void categorizeNodes(Node JavaDoc node, int level) {
232         
233         while (true) {
234             String JavaDoc strValue = node.getNodeValue();
235             if (strValue != null) {
236                 char[] value = strValue.toCharArray();
237                 
238                 for (int i = 0; i < value.length; i++) {
239                     if (value[i] == '\n') {
240                         m_lineNmb++;
241                     }
242                 }
243             }
244             
245             switch (node.getNodeType()) {
246                 case Node.TEXT_NODE:
247                     strValue = strValue.replace("\r\n", "").trim();
248                     if (strValue.length() == 0) {
249                         Node JavaDoc parent = node.getParentNode();
250                         Node JavaDoc nextNode = node.getNextSibling();
251                         parent.removeChild(node);
252                         node = nextNode;
253                         if (node != null) continue;
254                         return;
255                     }
256                 case Node.COMMENT_NODE:
257                     Node JavaDoc parent1 = node.getParentNode();
258                     Node JavaDoc nextNode1 = node.getNextSibling();
259                     parent1.removeChild(node);
260                     node = nextNode1;
261                     if (node != null) continue;
262                     return;
263             }
264             break;
265         }
266         
267         String JavaDoc strComponentName = removePrefix(node.getNodeName());
268         ExtraNodeInfo schemaNode = new ExtraNodeInfo(m_lineNmb, strComponentName, node, level == GLOBAL_COMPONENTS_LEVEL);
269         node.setUserData("", schemaNode, null);
270         
271         switch (level) {
272             case GLOBAL_COMPONENTS_LEVEL:
273                 NamedNodeMap JavaDoc map = node.getAttributes();
274                 if (map != null) {
275                     Node JavaDoc nameAttrNode = map.getNamedItem("name");
276                     if (strComponentName.equals("attribute")) {
277                         String JavaDoc strName = nameAttrNode.getNodeValue();
278                         m_globalAttributes.put(strName, node);
279                     } else if (strComponentName.equals("attributeGroup")) {
280                         String JavaDoc strName = nameAttrNode.getNodeValue();
281                         m_globalAttributeGroups.put(strName, node);
282                     } else if (strComponentName.equals("complexType")) {
283                         String JavaDoc strName = nameAttrNode.getNodeValue();
284                         m_globalComplexTypes.put(strName, node);
285                     } else if (strComponentName.equals("element")) {
286                         String JavaDoc strName = nameAttrNode.getNodeValue();
287                         m_globalElements.put(strName, node);
288                     } else if (strComponentName.equals("group")) {
289                         String JavaDoc strName = nameAttrNode.getNodeValue();
290                         m_globalGroups.put(strName, node);
291                     } else if (strComponentName.equals("include") || strComponentName.equals("import") || strComponentName.equals("redefine")) {
292                     } else if (strComponentName.equals("simpleType")) {
293                         String JavaDoc strName = nameAttrNode.getNodeValue();
294                         m_globalSimpleTypes.put(strName, node);
295                     }
296                 }
297                 break;
298             case SCHEMA_LEVEL:
299                 m_schema = node;
300                 break;
301         }
302         
303         NodeList JavaDoc childList = node.getChildNodes();
304         
305         for (int i = 0; i < childList.getLength(); i++) {
306             categorizeNodes(childList.item(i), level+1);
307         }
308     }
309     
310     private void printNodes(Node JavaDoc node, int level) {
311         
312         System.out.println();
313         System.out.print("*");
314         
315         for (int j = 0; j < level; j++) {
316             System.out.print("|");
317         }
318         
319         printNodeInfo(node);
320         
321         for (Node JavaDoc child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
322             printNodes(child, level+1);
323         }
324     }
325     
326     private void printNodeInfo(Node JavaDoc node) {
327         System.out.print("Component [" + node.getNodeName() + "], Node type [" + node.getNodeType() + "], Node value [" + node.getNodeValue() + "]");
328         NamedNodeMap JavaDoc attrMap = node.getAttributes();
329         
330         if (attrMap != null && attrMap.getLength() > 0) {
331             System.out.print(", Attrs: ");
332             for (int i = 0; i < attrMap.getLength(); i++) {
333                 System.out.print(attrMap.item(i).getNodeName() + "=\"" + attrMap.item(i).getNodeValue() + "\" ");
334             }
335         }
336     }
337     
338     private String JavaDoc removePrefix(String JavaDoc qualifiedName) {
339         return qualifiedName.substring(qualifiedName.lastIndexOf(":")+1);
340     }
341 }
342
Popular Tags