001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.test.xml.schema.core.lib.dom.parser;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.Collection;
047: import java.util.Iterator;
048: import java.util.TreeMap;
049: import javax.xml.parsers.DocumentBuilder;
050: import javax.xml.parsers.DocumentBuilderFactory;
051: import javax.xml.parsers.ParserConfigurationException;
052: import org.w3c.dom.Document;
053: import org.w3c.dom.NamedNodeMap;
054: import org.w3c.dom.Node;
055: import org.w3c.dom.NodeList;
056: import org.xml.sax.SAXException;
057: import org.netbeans.test.xml.schema.core.lib.types.ComponentCategories;
058:
059: /**
060: *
061: * @author ca@netbeans.org
062: */
063: public class SchemaDOMBuilder {
064:
065: public static final int SCHEMA_LEVEL = 1;
066: public static final int GLOBAL_COMPONENTS_LEVEL = 2;
067:
068: Node m_schema = null;
069: TreeMap<String, Node> m_globalAttributes = new TreeMap<String, Node>();
070: TreeMap<String, Node> m_globalAttributeGroups = new TreeMap<String, Node>();
071: TreeMap<String, Node> m_globalComplexTypes = new TreeMap<String, Node>();
072: TreeMap<String, Node> m_globalElements = new TreeMap<String, Node>();
073: TreeMap<String, Node> m_globalGroups = new TreeMap<String, Node>();
074: TreeMap<String, Node> m_referencedSchemas = new TreeMap<String, Node>();
075: TreeMap<String, Node> m_globalSimpleTypes = new TreeMap<String, Node>();
076:
077: public int m_lineNmb;
078: private String m_strFileName;
079:
080: private Document m_doc = null;
081:
082: /**
083: * Creates a new instance of SchemaDOMBuilder
084: */
085: public SchemaDOMBuilder() {
086: }
087:
088: /**
089: * @param args the command line arguments
090: */
091:
092: public void setFileName(String fileName) {
093: m_strFileName = fileName;
094: }
095:
096: public void setInitialLineNumber(int lineNmb) {
097: m_lineNmb = lineNmb;
098: }
099:
100: public NodeIterator getNodeIterator(ComponentCategories category) {
101: NodeIterator iterator = null;
102:
103: switch (category) {
104: case ATTRIBUTES:
105: iterator = new NodeIterator(m_globalAttributes);
106: break;
107: case ATTRIBUTE_GROUPS:
108: iterator = new NodeIterator(m_globalAttributeGroups);
109: break;
110: case COMPLEX_TYPES:
111: iterator = new NodeIterator(m_globalComplexTypes);
112: break;
113: case ELEMENTS:
114: iterator = new NodeIterator(m_globalElements);
115: break;
116: case GROUPS:
117: iterator = new NodeIterator(m_globalGroups);
118: break;
119: case REFERENCED_SCHEMAS:
120: iterator = new NodeIterator(m_referencedSchemas);
121: break;
122: case SIMPLE_TYPES:
123: iterator = new NodeIterator(m_globalSimpleTypes);
124: break;
125: }
126: return iterator;
127: }
128:
129: public void build() {
130: File file = new File(m_strFileName);
131:
132: try {
133: m_doc = getDocumentBuilder().parse(file);
134: } catch (SAXException e) {
135: System.out.println("SAXException: " + e.getMessage());
136: return;
137: } catch (IOException e) {
138: System.out.println("IOException: " + e.getMessage());
139: return;
140: }
141:
142: categorizeNodes(m_doc, 0);
143:
144: adaptDocToColumnView(m_doc);
145: }
146:
147: private void adaptDocToColumnView(Node node) {
148:
149: if (node.getNodeType() == Node.ELEMENT_NODE) {
150: String strComponentName = removePrefix(node.getNodeName());
151:
152: if (strComponentName.equals("simpleType")) {
153: NodeList childList = node.getChildNodes();
154:
155: for (int i = 0; i < childList.getLength(); i++) {
156: Node child = childList.item(i);
157: String childName = removePrefix(child.getNodeName());
158: if (childName.equals("restriction")) {
159: NodeList childList1 = child.getChildNodes();
160: for (int j = 0; j < childList1.getLength(); j++) {
161: Node child1 = childList1.item(j);
162: String childName1 = removePrefix(child1
163: .getNodeName());
164: if (childName1.equals("enumeration")) {
165: node.appendChild(child1);
166: j--;
167: } else {
168: child.removeChild(child1);
169: j--;
170: }
171: }
172: node.removeChild(child);
173: i--;
174: } else if (childName.equals("list")) {
175: node.removeChild(child);
176: i--;
177: }
178: }
179: } else if (strComponentName.equals("complexType")) {
180: NodeList childList = node.getChildNodes();
181:
182: for (int i = 0; i < childList.getLength(); i++) {
183: Node child = childList.item(i);
184: String childName = removePrefix(child.getNodeName());
185: if (childName.equals("simpleContent")
186: || childName.equals("complexContent")) {
187: NodeList childList1 = child.getChildNodes();
188: for (int j = 0; j < childList1.getLength(); j++) {
189: Node child1 = childList1.item(j);
190: String childName1 = removePrefix(child1
191: .getNodeName());
192: if (childName1.equals("restriction")
193: || childName1.equals("extension")) {
194: NodeList childList2 = child1
195: .getChildNodes();
196: for (int k = 0; k < childList2
197: .getLength(); k++) {
198: Node child2 = childList2.item(k);
199: node.appendChild(child2);
200: k--;
201: }
202: child.removeChild(child1);
203: j--;
204: }
205: node.removeChild(child);
206: i--;
207: }
208: }
209: }
210: }
211: }
212:
213: NodeList childList = node.getChildNodes();
214:
215: for (int i = 0; i < childList.getLength(); i++) {
216: adaptDocToColumnView(childList.item(i));
217: }
218:
219: }
220:
221: public static void main(String[] args) {
222:
223: SchemaDOMBuilder builder = new SchemaDOMBuilder();
224:
225: builder.setFileName(args[0]);
226:
227: builder.build();
228:
229: builder.printNodes(builder.m_doc, 0);
230: }
231:
232: private void printGlobalComponents(TreeMap<String, Node> map) {
233: Collection c = map.values();
234: Iterator iterator = c.iterator();
235: while (iterator.hasNext()) {
236: Node node = (Node) iterator.next();
237: ExtraNodeInfo sn = ExtraNodeInfo.getExtraNodeInfo(node);
238: printNodeInfo(node);
239: System.out.println("\n line " + sn.getLineNmb());
240: System.out.println(" component name "
241: + sn.getComponentName());
242: }
243: }
244:
245: private static DocumentBuilder getDocumentBuilder() {
246:
247: DocumentBuilderFactory factory = DocumentBuilderFactory
248: .newInstance();
249:
250: DocumentBuilder builder = null;
251:
252: try {
253: builder = factory.newDocumentBuilder();
254: } catch (ParserConfigurationException e) {
255: }
256:
257: return builder;
258: }
259:
260: private void categorizeNodes(Node node, int level) {
261:
262: while (true) {
263: String strValue = node.getNodeValue();
264: if (strValue != null) {
265: char[] value = strValue.toCharArray();
266:
267: for (int i = 0; i < value.length; i++) {
268: if (value[i] == '\n') {
269: m_lineNmb++;
270: }
271: }
272: }
273:
274: switch (node.getNodeType()) {
275: case Node.TEXT_NODE:
276: strValue = strValue.replace("\r\n", "").trim();
277: if (strValue.length() == 0) {
278: Node parent = node.getParentNode();
279: Node nextNode = node.getNextSibling();
280: parent.removeChild(node);
281: node = nextNode;
282: if (node != null)
283: continue;
284: return;
285: }
286: case Node.COMMENT_NODE:
287: Node parent1 = node.getParentNode();
288: Node nextNode1 = node.getNextSibling();
289: parent1.removeChild(node);
290: node = nextNode1;
291: if (node != null)
292: continue;
293: return;
294: }
295: break;
296: }
297:
298: String strComponentName = removePrefix(node.getNodeName());
299: ExtraNodeInfo schemaNode = new ExtraNodeInfo(m_lineNmb,
300: strComponentName, node,
301: level == GLOBAL_COMPONENTS_LEVEL);
302: node.setUserData("", schemaNode, null);
303:
304: switch (level) {
305: case GLOBAL_COMPONENTS_LEVEL:
306: NamedNodeMap map = node.getAttributes();
307: if (map != null) {
308: Node nameAttrNode = map.getNamedItem("name");
309: if (strComponentName.equals("attribute")) {
310: String strName = nameAttrNode.getNodeValue();
311: m_globalAttributes.put(strName, node);
312: } else if (strComponentName.equals("attributeGroup")) {
313: String strName = nameAttrNode.getNodeValue();
314: m_globalAttributeGroups.put(strName, node);
315: } else if (strComponentName.equals("complexType")) {
316: String strName = nameAttrNode.getNodeValue();
317: m_globalComplexTypes.put(strName, node);
318: } else if (strComponentName.equals("element")) {
319: String strName = nameAttrNode.getNodeValue();
320: m_globalElements.put(strName, node);
321: } else if (strComponentName.equals("group")) {
322: String strName = nameAttrNode.getNodeValue();
323: m_globalGroups.put(strName, node);
324: } else if (strComponentName.equals("include")
325: || strComponentName.equals("import")
326: || strComponentName.equals("redefine")) {
327: } else if (strComponentName.equals("simpleType")) {
328: String strName = nameAttrNode.getNodeValue();
329: m_globalSimpleTypes.put(strName, node);
330: }
331: }
332: break;
333: case SCHEMA_LEVEL:
334: m_schema = node;
335: break;
336: }
337:
338: NodeList childList = node.getChildNodes();
339:
340: for (int i = 0; i < childList.getLength(); i++) {
341: categorizeNodes(childList.item(i), level + 1);
342: }
343: }
344:
345: private void printNodes(Node node, int level) {
346:
347: System.out.println();
348: System.out.print("*");
349:
350: for (int j = 0; j < level; j++) {
351: System.out.print("|");
352: }
353:
354: printNodeInfo(node);
355:
356: for (Node child = node.getFirstChild(); child != null; child = child
357: .getNextSibling()) {
358: printNodes(child, level + 1);
359: }
360: }
361:
362: private void printNodeInfo(Node node) {
363: System.out.print("Component [" + node.getNodeName()
364: + "], Node type [" + node.getNodeType()
365: + "], Node value [" + node.getNodeValue() + "]");
366: NamedNodeMap attrMap = node.getAttributes();
367:
368: if (attrMap != null && attrMap.getLength() > 0) {
369: System.out.print(", Attrs: ");
370: for (int i = 0; i < attrMap.getLength(); i++) {
371: System.out.print(attrMap.item(i).getNodeName() + "=\""
372: + attrMap.item(i).getNodeValue() + "\" ");
373: }
374: }
375: }
376:
377: private String removePrefix(String qualifiedName) {
378: return qualifiedName
379: .substring(qualifiedName.lastIndexOf(":") + 1);
380: }
381: }
|