KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > common > tools > wsdl > SchemaCollection


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.common.tools.wsdl;
18
19 import java.net.URI JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36
37 /**
38  * Collection of schemas.
39  *
40  * @author gnodet
41  */

42 public class SchemaCollection {
43
44     private static Log log = LogFactory.getLog(SchemaCollection.class);
45     
46     private Map JavaDoc schemas;
47     private URI JavaDoc baseUri;
48     
49     public SchemaCollection() {
50         this(null);
51     }
52     
53     public SchemaCollection(URI JavaDoc baseUri) {
54         if (log.isDebugEnabled()) {
55             log.debug("Initializing schema collection with baseUri: " + baseUri);
56         }
57         this.baseUri = baseUri;
58         this.schemas = new HashMap JavaDoc();
59     }
60     
61     public Schema getSchema(String JavaDoc namespaceURI) {
62         return (Schema) schemas.get(namespaceURI);
63     }
64     
65     public void read(Element JavaDoc elem, URI JavaDoc sourceUri) throws Exception JavaDoc {
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 JavaDoc location, URI JavaDoc baseUri) throws Exception JavaDoc {
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 JavaDoc loc;
82         if (baseUri != null) {
83             loc = resolve(baseUri, location);
84             if (!loc.isAbsolute()) {
85                 throw new IllegalArgumentException JavaDoc("Unable to resolve '" + loc.toString() + "' relative to '" + baseUri + "'");
86             }
87         } else {
88             loc = new URI JavaDoc(location);
89             if (!loc.isAbsolute()) {
90                 throw new IllegalArgumentException JavaDoc("Location '" + loc.toString() + "' is not absolute and no baseUri specified");
91             }
92         }
93         InputSource JavaDoc inputSource = new InputSource JavaDoc();
94         inputSource.setByteStream(loc.toURL().openStream());
95         inputSource.setSystemId(loc.toString());
96         read(inputSource);
97     }
98     
99     public void read(InputSource JavaDoc inputSource) throws Exception JavaDoc {
100         DocumentBuilderFactory JavaDoc docFac = DocumentBuilderFactory.newInstance();
101         docFac.setNamespaceAware(true);
102         DocumentBuilder JavaDoc builder = docFac.newDocumentBuilder();
103         Document JavaDoc doc = builder.parse(inputSource);
104         read(doc.getDocumentElement(),
105              inputSource.getSystemId() != null ? new URI JavaDoc(inputSource.getSystemId()) : null);
106     }
107     
108     protected void handleImports(Schema schema) throws Exception JavaDoc {
109         NodeList JavaDoc children = schema.getRoot().getChildNodes();
110         List JavaDoc imports = new ArrayList JavaDoc();
111         for (int i = 0; i < children.getLength(); i++) {
112             Node JavaDoc child = children.item(i);
113             if (child instanceof Element JavaDoc) {
114                 Element JavaDoc ce = (Element JavaDoc) 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 JavaDoc iter = imports.iterator(); iter.hasNext();) {
122             Element JavaDoc ce = (Element JavaDoc) iter.next();
123             String JavaDoc namespace = ce.getAttribute("namespace");
124             if (schemas.get(namespace) == null) {
125                 String JavaDoc 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 JavaDoc resolve(URI JavaDoc base, String JavaDoc location) {
136         if ("jar".equals(base.getScheme())) {
137             String JavaDoc str = base.toString();
138             String JavaDoc[] 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