1 19 20 package org.netbeans.modules.xml.retriever; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 import javax.xml.namespace.NamespaceContext ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.xpath.XPath ; 30 import javax.xml.xpath.XPathConstants ; 31 import javax.xml.xpath.XPathFactory ; 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 ; 37 import org.w3c.dom.NodeList ; 38 39 43 public class DocumentTypeSchemaWsdlParser implements DocumentTypeParser{ 44 45 46 public DocumentTypeSchemaWsdlParser() { 47 } 48 49 public boolean accept(String mimeType) { 50 if(mimeType != null && mimeType.equalsIgnoreCase(DocumentTypesEnum.schema.toString())) return true; 52 if(mimeType != null && mimeType.equalsIgnoreCase(DocumentTypesEnum.wsdl.toString())) return true; 54 return false; 55 } 56 57 58 public List <String > getAllLocationOfReferencedEntities(FileObject fob) throws Exception { 59 return getAllLocationOfReferencedEntities(FileUtil.toFile(fob)); 60 } 61 62 63 private static XPath xpath = null; 64 65 private void initXpath() throws Exception { 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 <String , String > namespaces = new HashMap <String ,String >(); 80 private Map <String , String > prefixes = new HashMap <String ,String >(); 81 82 private NamespaceContext getNamespaceContext() { 83 namespaces.put("xsd","http://www.w3.org/2001/XMLSchema"); prefixes.put("http://www.w3.org/2001/XMLSchema", "xsd"); namespaces.put("wsdl", "http://schemas.xmlsoap.org/wsdl/"); prefixes.put("http://schemas.xmlsoap.org/wsdl/", "wsdl"); return new HashNamespaceResolver(namespaces, prefixes); 88 } 89 90 private Node getDOMTree(File parsedFile) throws Exception { 91 DocumentBuilderFactory dbfact = DocumentBuilderFactory.newInstance(); 92 dbfact.setNamespaceAware(true); 93 FileObject parsedFileObject = FileUtil.toFileObject(FileUtil.normalizeFile(parsedFile)); 94 Node node = dbfact.newDocumentBuilder().parse(parsedFileObject.getInputStream()); 95 return node; 96 } 97 98 99 public List <String > getAllLocationOfReferencedEntities(File parsedFile) throws Exception { 100 List <String > result = new ArrayList <String >(); 101 initXpath(); 102 Node documentNode = getDOMTree(parsedFile); 103 104 String locationExpression = IConstants.XPATH_SCHEMA_IMPORT_LOCATION; NodeList nodes = (NodeList ) xpath.evaluate(locationExpression, documentNode, XPathConstants.NODESET); 107 if((nodes != null) && (nodes.getLength() > 0)){ 108 for(int i=0; i<nodes.getLength();i++){ 109 Node node = nodes.item(i); 110 result.add(node.getNodeValue()); 111 } 112 } 113 114 locationExpression = IConstants.XPATH_SCHEMA_INCLUDE_LOCATION; nodes = (NodeList ) 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 locationExpression = IConstants.XPATH_SCHEMA_REDEFINE_LOCATION; nodes = (NodeList ) 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 locationExpression = IConstants.XPATH_WSDL_IMPORT_LOCATION; 134 nodes = (NodeList ) 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 getFileExtensionByParsing(File parsedFile) throws Exception { 146 String result = null; 147 initXpath(); 148 Node documentNode = getDOMTree(parsedFile); 149 150 String locationExpression = IConstants.XPATH_SCHEMA_TAG; 152 NodeList nodes = (NodeList ) xpath.evaluate(locationExpression, documentNode, XPathConstants.NODESET); 153 if((nodes != null) && (nodes.getLength() > 0)){ 154 return "xsd"; } 156 157 locationExpression = IConstants.XPATH_WSDL_TAG; 159 nodes = (NodeList ) xpath.evaluate(locationExpression, documentNode, XPathConstants.NODESET); 160 if((nodes != null) && (nodes.getLength() > 0)){ 161 return "wsdl"; } 163 return "xml"; } 165 166 167 168 169 public static void main(String [] args) throws Exception { 170 DocumentTypeSchemaWsdlParser dtsp = new DocumentTypeSchemaWsdlParser(); 171 175 System.out.println(dtsp.getFileExtensionByParsing(new File ("D:\\temp\\xml\\maindoc\\UBL-Order-1.0.xsd"))); 176 System.out.println(dtsp.getFileExtensionByParsing(new File ("C:\\Documents and Settings\\girix\\JavaApplication5\\src\\javaapplication5\\newuntitled.wsdl"))); 177 System.out.println(dtsp.getFileExtensionByParsing(new File ("C:\\Documents and Settings\\girix\\Desktop\\ToDo.txt"))); 178 179 } 180 } 181 | Popular Tags |