KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > DocumentTypeSchemaWsdlParser


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.retriever;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.xml.namespace.NamespaceContext JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.xpath.XPath JavaDoc;
30 import javax.xml.xpath.XPathConstants JavaDoc;
31 import javax.xml.xpath.XPathFactory JavaDoc;
32 import org.netbeans.modules.xml.retriever.catalog.Utilities.DocumentTypesEnum;
33 import org.netbeans.modules.xml.retriever.catalog.Utilities.HashNamespaceResolver;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38
39 /**
40  *
41  * @author girix
42  */

43 public class DocumentTypeSchemaWsdlParser implements DocumentTypeParser{
44     
45     /** Creates a new instance of DocumentTypeSchemaParser */
46     public DocumentTypeSchemaWsdlParser() {
47     }
48     
49     public boolean accept(String JavaDoc mimeType) {
50         if(mimeType != null && mimeType.equalsIgnoreCase(DocumentTypesEnum.schema.toString())) //noi18n
51
return true;
52         if(mimeType != null && mimeType.equalsIgnoreCase(DocumentTypesEnum.wsdl.toString())) //noi18n
53
return true;
54         return false;
55     }
56     
57     
58     public List JavaDoc<String JavaDoc> getAllLocationOfReferencedEntities(FileObject fob) throws Exception JavaDoc{
59         return getAllLocationOfReferencedEntities(FileUtil.toFile(fob));
60     }
61     
62     
63     private static XPath JavaDoc xpath = null;
64     
65     private void initXpath() throws Exception JavaDoc{
66         if(xpath == null){
67             xpath = XPathFactory.newInstance().newXPath();
68             xpath.setNamespaceContext(getNamespaceContext());
69             xpath.compile(IConstants.XPATH_SCHEMA_IMPORT_LOCATION);
70             xpath.compile(IConstants.XPATH_SCHEMA_INCLUDE_LOCATION);
71             xpath.compile(IConstants.XPATH_SCHEMA_REDEFINE_LOCATION);
72             xpath.compile(IConstants.XPATH_WSDL_IMPORT_LOCATION);
73             xpath.compile(IConstants.XPATH_WSDL_TAG);
74             xpath.compile(IConstants.XPATH_SCHEMA_TAG);
75             
76         }
77     }
78     
79     private Map JavaDoc<String JavaDoc, String JavaDoc> namespaces = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
80     private Map JavaDoc<String JavaDoc, String JavaDoc> prefixes = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
81     
82     private NamespaceContext JavaDoc getNamespaceContext() {
83         namespaces.put("xsd","http://www.w3.org/2001/XMLSchema"); //NOI18N
84
prefixes.put("http://www.w3.org/2001/XMLSchema", "xsd"); //NOI18N
85
namespaces.put("wsdl", "http://schemas.xmlsoap.org/wsdl/"); //NOI18N
86
prefixes.put("http://schemas.xmlsoap.org/wsdl/", "wsdl"); //NOI18N
87
return new HashNamespaceResolver(namespaces, prefixes);
88     }
89     
90     private Node JavaDoc getDOMTree(File JavaDoc parsedFile) throws Exception JavaDoc{
91         DocumentBuilderFactory JavaDoc dbfact = DocumentBuilderFactory.newInstance();
92         dbfact.setNamespaceAware(true);
93         FileObject parsedFileObject = FileUtil.toFileObject(FileUtil.normalizeFile(parsedFile));
94         Node JavaDoc node = dbfact.newDocumentBuilder().parse(parsedFileObject.getInputStream());
95         return node;
96     }
97     
98     
99     public List JavaDoc<String JavaDoc> getAllLocationOfReferencedEntities(File JavaDoc parsedFile) throws Exception JavaDoc{
100         List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
101         initXpath();
102         Node JavaDoc documentNode = getDOMTree(parsedFile);
103         
104         //scan for schema imports'
105
String JavaDoc locationExpression = IConstants.XPATH_SCHEMA_IMPORT_LOCATION; //noi18n
106
NodeList JavaDoc nodes = (NodeList JavaDoc) xpath.evaluate(locationExpression, documentNode, XPathConstants.NODESET);
107         if((nodes != null) && (nodes.getLength() > 0)){
108             for(int i=0; i<nodes.getLength();i++){
109                 Node JavaDoc node = nodes.item(i);
110                 result.add(node.getNodeValue());
111             }
112         }
113         
114         //scan for schema includes'
115
locationExpression = IConstants.XPATH_SCHEMA_INCLUDE_LOCATION; //noi18n
116
nodes = (NodeList JavaDoc) xpath.evaluate(locationExpression, documentNode, XPathConstants.NODESET);
117         if((nodes != null) && (nodes.getLength() > 0)){
118             for(int i=0; i<nodes.getLength();i++){
119                 result.add(nodes.item(i).getNodeValue());
120             }
121         }
122         
123         //scan for schema redefines'
124
locationExpression = IConstants.XPATH_SCHEMA_REDEFINE_LOCATION; //noi18n
125
nodes = (NodeList JavaDoc) xpath.evaluate(locationExpression, documentNode, XPathConstants.NODESET);
126         if((nodes != null) && (nodes.getLength() > 0)){
127             for(int i=0; i<nodes.getLength();i++){
128                 result.add(nodes.item(i).getNodeValue());
129             }
130         }
131         
132         //scan for wsdl imports'
133
locationExpression = IConstants.XPATH_WSDL_IMPORT_LOCATION;
134         nodes = (NodeList JavaDoc) xpath.evaluate(locationExpression, documentNode, XPathConstants.NODESET);
135         if((nodes != null) && (nodes.getLength() > 0)){
136             for(int i=0; i<nodes.getLength();i++){
137                 result.add(nodes.item(i).getNodeValue());
138             }
139         }
140         
141         return result;
142     }
143     
144     
145     public String JavaDoc getFileExtensionByParsing(File JavaDoc parsedFile) throws Exception JavaDoc{
146         String JavaDoc result = null;
147         initXpath();
148         Node JavaDoc documentNode = getDOMTree(parsedFile);
149         
150         //scan for schema tag
151
String JavaDoc locationExpression = IConstants.XPATH_SCHEMA_TAG;
152         NodeList JavaDoc nodes = (NodeList JavaDoc) xpath.evaluate(locationExpression, documentNode, XPathConstants.NODESET);
153         if((nodes != null) && (nodes.getLength() > 0)){
154             return "xsd"; //noi18n
155
}
156         
157         //scan for wsdl tag
158
locationExpression = IConstants.XPATH_WSDL_TAG;
159         nodes = (NodeList JavaDoc) xpath.evaluate(locationExpression, documentNode, XPathConstants.NODESET);
160         if((nodes != null) && (nodes.getLength() > 0)){
161             return "wsdl"; //noi18n
162
}
163         return "xml"; //noi18n
164
}
165     
166     
167     
168     
169     public static void main(String JavaDoc[] args) throws Exception JavaDoc{
170         DocumentTypeSchemaWsdlParser dtsp = new DocumentTypeSchemaWsdlParser();
171         /*List<String> result = dtsp.getAllLocationOfReferencedEntities(new File(args[0]));
172         for(String str: result){
173             System.out.println(str);
174         }*/

175         System.out.println(dtsp.getFileExtensionByParsing(new File JavaDoc("D:\\temp\\xml\\maindoc\\UBL-Order-1.0.xsd")));
176         System.out.println(dtsp.getFileExtensionByParsing(new File JavaDoc("C:\\Documents and Settings\\girix\\JavaApplication5\\src\\javaapplication5\\newuntitled.wsdl")));
177         System.out.println(dtsp.getFileExtensionByParsing(new File JavaDoc("C:\\Documents and Settings\\girix\\Desktop\\ToDo.txt")));
178         
179     }
180 }
181
Popular Tags