1 17 package org.apache.servicemix.common.tools.wsdl; 18 19 import java.net.URI ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.xml.parsers.DocumentBuilder ; 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 import org.xml.sax.InputSource ; 36 37 42 public class SchemaCollection { 43 44 private static Log log = LogFactory.getLog(SchemaCollection.class); 45 46 private Map schemas; 47 private URI baseUri; 48 49 public SchemaCollection() { 50 this(null); 51 } 52 53 public SchemaCollection(URI baseUri) { 54 if (log.isDebugEnabled()) { 55 log.debug("Initializing schema collection with baseUri: " + baseUri); 56 } 57 this.baseUri = baseUri; 58 this.schemas = new HashMap (); 59 } 60 61 public Schema getSchema(String namespaceURI) { 62 return (Schema) schemas.get(namespaceURI); 63 } 64 65 public void read(Element elem, URI sourceUri) throws Exception { 66 Schema schema = new Schema(); 67 schema.setSourceUri(sourceUri); 68 schema.setRoot(elem); 69 schema.setNamespace(elem.getAttribute("targetNamespace")); 70 schemas.put(schema.getNamespace(), schema); 71 handleImports(schema); 72 } 73 74 public void read(String location, URI baseUri) throws Exception { 75 if (log.isDebugEnabled()) { 76 log.debug("Reading schema at '" + location + "' with baseUri '" + baseUri + "'"); 77 } 78 if (baseUri == null) { 79 baseUri = this.baseUri; 80 } 81 URI loc; 82 if (baseUri != null) { 83 loc = resolve(baseUri, location); 84 if (!loc.isAbsolute()) { 85 throw new IllegalArgumentException ("Unable to resolve '" + loc.toString() + "' relative to '" + baseUri + "'"); 86 } 87 } else { 88 loc = new URI (location); 89 if (!loc.isAbsolute()) { 90 throw new IllegalArgumentException ("Location '" + loc.toString() + "' is not absolute and no baseUri specified"); 91 } 92 } 93 InputSource inputSource = new InputSource (); 94 inputSource.setByteStream(loc.toURL().openStream()); 95 inputSource.setSystemId(loc.toString()); 96 read(inputSource); 97 } 98 99 public void read(InputSource inputSource) throws Exception { 100 DocumentBuilderFactory docFac = DocumentBuilderFactory.newInstance(); 101 docFac.setNamespaceAware(true); 102 DocumentBuilder builder = docFac.newDocumentBuilder(); 103 Document doc = builder.parse(inputSource); 104 read(doc.getDocumentElement(), 105 inputSource.getSystemId() != null ? new URI (inputSource.getSystemId()) : null); 106 } 107 108 protected void handleImports(Schema schema) throws Exception { 109 NodeList children = schema.getRoot().getChildNodes(); 110 List imports = new ArrayList (); 111 for (int i = 0; i < children.getLength(); i++) { 112 Node child = children.item(i); 113 if (child instanceof Element ) { 114 Element ce = (Element ) child; 115 if ("http://www.w3.org/2001/XMLSchema".equals(ce.getNamespaceURI()) && 116 "import".equals(ce.getLocalName())) { 117 imports.add(ce); 118 } 119 } 120 } 121 for (Iterator iter = imports.iterator(); iter.hasNext();) { 122 Element ce = (Element ) iter.next(); 123 String namespace = ce.getAttribute("namespace"); 124 if (schemas.get(namespace) == null) { 125 String location = ce.getAttribute("schemaLocation"); 126 if (location != null && !"".equals(location)) { 127 read(location, schema.getSourceUri()); 128 } 129 } 130 schema.addImport(namespace); 131 schema.getRoot().removeChild(ce); 132 } 133 } 134 135 protected static URI resolve(URI base, String location) { 136 if ("jar".equals(base.getScheme())) { 137 String str = base.toString(); 138 String [] parts = str.split("!"); 139 parts[1] = URI.create(parts[1]).resolve(location).toString(); 140 return URI.create(parts[0] + "!" + parts[1]); 141 } 142 return base.resolve(location); 143 } 144 145 } 146 | Popular Tags |