KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > axis > builder > SchemaInfoBuilder


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.geronimo.axis.builder;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Stack JavaDoc;
31 import java.util.jar.JarFile JavaDoc;
32 import java.util.zip.ZipEntry JavaDoc;
33 import javax.wsdl.Definition;
34 import javax.wsdl.Import;
35 import javax.wsdl.Port;
36 import javax.wsdl.Service;
37 import javax.wsdl.Types;
38 import javax.wsdl.WSDLException;
39 import javax.wsdl.extensions.ExtensibilityElement;
40 import javax.wsdl.extensions.UnknownExtensibilityElement;
41 import javax.wsdl.extensions.ExtensionRegistry;
42 import javax.wsdl.extensions.schema.Schema;
43 import javax.wsdl.extensions.soap.SOAPAddress;
44 import javax.wsdl.factory.WSDLFactory;
45 import javax.wsdl.xml.WSDLLocator;
46 import javax.wsdl.xml.WSDLReader;
47 import javax.xml.namespace.QName JavaDoc;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51 import org.apache.geronimo.common.DeploymentException;
52 import org.apache.geronimo.schema.SchemaConversionUtils;
53 import org.apache.xmlbeans.SchemaField;
54 import org.apache.xmlbeans.SchemaGlobalElement;
55 import org.apache.xmlbeans.SchemaParticle;
56 import org.apache.xmlbeans.SchemaType;
57 import org.apache.xmlbeans.SchemaTypeSystem;
58 import org.apache.xmlbeans.XmlBeans;
59 import org.apache.xmlbeans.XmlCursor;
60 import org.apache.xmlbeans.XmlError;
61 import org.apache.xmlbeans.XmlException;
62 import org.apache.xmlbeans.XmlObject;
63 import org.apache.xmlbeans.XmlOptions;
64 import org.w3.x2001.xmlSchema.SchemaDocument;
65 import org.w3c.dom.Element JavaDoc;
66 import org.xml.sax.EntityResolver JavaDoc;
67 import org.xml.sax.InputSource JavaDoc;
68 import org.xml.sax.SAXException JavaDoc;
69 import com.ibm.wsdl.extensions.PopulatedExtensionRegistry;
70 import com.ibm.wsdl.extensions.schema.SchemaConstants;
71
72 /**
73  * @version $Rev: $ $Date: $
74  */

75 public class SchemaInfoBuilder {
76     private static final Log log = LogFactory.getLog(SchemaInfoBuilder.class);
77     private static final SchemaTypeSystem basicTypeSystem;
78     private static final String JavaDoc[] errorNames = {"Error", "Warning", "Info"};
79
80     static {
81         URL JavaDoc url = WSDescriptorParser.class.getClassLoader().getResource("soap_encoding_1_1.xsd");
82         if (url == null) {
83             throw new RuntimeException JavaDoc("Could not locate soap encoding schema");
84         }
85         Collection JavaDoc errors = new ArrayList JavaDoc();
86         XmlOptions xmlOptions = new XmlOptions();
87         xmlOptions.setErrorListener(errors);
88         try {
89             XmlObject xmlObject = SchemaConversionUtils.parse(url);
90             basicTypeSystem = XmlBeans.compileXsd(new XmlObject[]{xmlObject}, XmlBeans.getBuiltinTypeSystem(), xmlOptions);
91             if (errors.size() > 0) {
92                 throw new RuntimeException JavaDoc("Could not compile schema type system: errors: " + errors);
93             }
94         } catch (XmlException e) {
95             throw new RuntimeException JavaDoc("Could not compile schema type system", e);
96         } catch (IOException JavaDoc e) {
97             throw new RuntimeException JavaDoc("Could not compile schema type system", e);
98         }
99     }
100
101     private final JarFile JavaDoc moduleFile;
102     private final Definition definition;
103     private final Stack JavaDoc uris = new Stack JavaDoc();
104     private final Map JavaDoc wsdlMap = new HashMap JavaDoc();
105     private final Map JavaDoc schemaTypeKeyToSchemaTypeMap;
106     private final Map JavaDoc complexTypeMap;
107     private final Map JavaDoc elementMap;
108     private final Map JavaDoc simpleTypeMap;
109     private final Map JavaDoc portMap;
110
111
112     public SchemaInfoBuilder(JarFile JavaDoc moduleFile, URI JavaDoc wsdlUri) throws DeploymentException {
113         this(moduleFile, wsdlUri, null, null);
114     }
115
116     public SchemaInfoBuilder(JarFile JavaDoc moduleFile, Definition definition) throws DeploymentException {
117         this(moduleFile, null, definition, null);
118     }
119
120     SchemaInfoBuilder(JarFile JavaDoc moduleFile, URI JavaDoc uri, SchemaTypeSystem schemaTypeSystem) throws DeploymentException {
121         this(moduleFile, uri, null, schemaTypeSystem);
122     }
123
124     SchemaInfoBuilder(JarFile JavaDoc moduleFile, URI JavaDoc uri, Definition definition, SchemaTypeSystem schemaTypeSystem) throws DeploymentException {
125         this.moduleFile = moduleFile;
126         if (uri != null) {
127             uris.push(uri);
128             if (definition == null && schemaTypeSystem == null) {
129                 definition = readWsdl(moduleFile, uri);
130             }
131         } else if (definition != null) {
132             try {
133                 uri = new URI JavaDoc(definition.getDocumentBaseURI());
134                 uris.push(uri);
135             } catch (URISyntaxException JavaDoc e) {
136                 throw new DeploymentException("Could not locate definition", e);
137             }
138         } else {
139             throw new DeploymentException("You must supply uri or definition");
140         }
141         if (schemaTypeSystem == null) {
142             schemaTypeSystem = compileSchemaTypeSystem(definition);
143         }
144         this.definition = definition;
145         schemaTypeKeyToSchemaTypeMap = buildSchemaTypeKeyToSchemaTypeMap(schemaTypeSystem);
146         complexTypeMap = buildComplexTypeMap();
147         simpleTypeMap = buildSimpleTypeMap();
148         elementMap = buildElementMap();
149         portMap = buildPortMap();
150     }
151
152     public Map JavaDoc getSchemaTypeKeyToSchemaTypeMap() {
153         return schemaTypeKeyToSchemaTypeMap;
154     }
155
156     public Definition getDefinition() {
157         return definition;
158     }
159
160     public Map JavaDoc getWsdlMap() {
161         return wsdlMap;
162     }
163
164     /**
165      * Find all the complex types in the previously constructed schema analysis.
166      * Put them in a map from complex type QName to schema fragment.
167      *
168      * @return
169      */

170     public Map JavaDoc getComplexTypesInWsdl() {
171         return complexTypeMap;
172     }
173
174     private Map JavaDoc buildComplexTypeMap() {
175         Map JavaDoc complexTypeMap = new HashMap JavaDoc();
176         for (Iterator JavaDoc iterator = schemaTypeKeyToSchemaTypeMap.entrySet().iterator(); iterator.hasNext();) {
177             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
178             SchemaTypeKey key = (SchemaTypeKey) entry.getKey();
179             if (!key.isSimpleType() && !key.isAnonymous()) {
180                 QName JavaDoc qName = key.getqName();
181                 SchemaType schemaType = (SchemaType) entry.getValue();
182                 complexTypeMap.put(qName, schemaType);
183             }
184         }
185         return complexTypeMap;
186     }
187
188     public Map JavaDoc getElementToTypeMap() {
189         return elementMap;
190     }
191
192     private Map JavaDoc buildElementMap() {
193         Map JavaDoc elementToTypeMap = new HashMap JavaDoc();
194         for (Iterator JavaDoc iterator = schemaTypeKeyToSchemaTypeMap.entrySet().iterator(); iterator.hasNext();) {
195             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
196             SchemaTypeKey key = (SchemaTypeKey) entry.getKey();
197             if (key.isElement()) {
198                 QName JavaDoc elementQName = key.getqName();
199                 SchemaType schemaType = (SchemaType) entry.getValue();
200                 QName JavaDoc typeQName = schemaType.getName();
201                 elementToTypeMap.put(elementQName, typeQName);
202             }
203         }
204         return elementToTypeMap;
205     }
206
207     /**
208      * Gets a map of all the javax.wsdl.Port instance in the WSDL definition keyed by the port's QName
209      * <p/>
210      * WSDL 1.1 spec: 2.6 "The name attribute provides a unique name among all ports defined within in the enclosing WSDL document."
211      *
212      * @return
213      */

214
215     public Map JavaDoc getPortMap() {
216         return portMap;
217     }
218
219     private Map JavaDoc buildPortMap() {
220         HashMap JavaDoc ports = new HashMap JavaDoc();
221         if (definition != null) {
222             Collection JavaDoc services = definition.getServices().values();
223             for (Iterator JavaDoc iterator = services.iterator(); iterator.hasNext();) {
224                 Service service = (Service) iterator.next();
225                 ports.putAll(service.getPorts());
226             }
227         }
228         return ports;
229     }
230
231     public Map JavaDoc getSimpleTypeMap() {
232         return simpleTypeMap;
233     }
234
235     private Map JavaDoc buildSimpleTypeMap() {
236         Map JavaDoc simpleTypeMap = new HashMap JavaDoc();
237         for (Iterator JavaDoc iterator = schemaTypeKeyToSchemaTypeMap.entrySet().iterator(); iterator.hasNext();) {
238             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
239             SchemaTypeKey key = (SchemaTypeKey) entry.getKey();
240             if (key.isSimpleType() && !key.isAnonymous()) {
241                 QName JavaDoc qName = key.getqName();
242                 SchemaType schemaType = (SchemaType) entry.getValue();
243                 simpleTypeMap.put(qName, schemaType);
244             }
245         }
246         return simpleTypeMap;
247     }
248
249     public SchemaTypeSystem compileSchemaTypeSystem(Definition definition) throws DeploymentException {
250         List JavaDoc schemaList = new ArrayList JavaDoc();
251         addImportsFromDefinition(definition, schemaList);
252 // System.out.println("Schemas: " + schemaList);
253
Collection JavaDoc errors = new ArrayList JavaDoc();
254         XmlOptions xmlOptions = new XmlOptions();
255         xmlOptions.setErrorListener(errors);
256         xmlOptions.setEntityResolver(new JarEntityResolver());
257         XmlObject[] schemas = (XmlObject[]) schemaList.toArray(new XmlObject[schemaList.size()]);
258         try {
259             SchemaTypeSystem schemaTypeSystem = XmlBeans.compileXsd(schemas, basicTypeSystem, xmlOptions);
260             if (errors.size() > 0) {
261                 boolean wasError = false;
262                 for (Iterator JavaDoc iterator = errors.iterator(); iterator.hasNext();) {
263                     XmlError xmlError = (XmlError) iterator.next();
264                     log.info("Severity: " + errorNames[xmlError.getSeverity()] + ", message: " + xmlError);
265                     if (xmlError.getSeverity() == XmlError.SEVERITY_ERROR) {
266                         wasError = true;
267                     }
268                 }
269                 if (wasError) {
270                     throw new DeploymentException("Could not compile schema type system, see log for errors");
271                 }
272             }
273             return schemaTypeSystem;
274         } catch (XmlException e) {
275             throw new DeploymentException("Could not compile schema type system", e);
276         }
277     }
278
279     private void addImportsFromDefinition(Definition definition, List JavaDoc schemaList) throws DeploymentException {
280         Map JavaDoc namespaceMap = definition.getNamespaces();
281         Types types = definition.getTypes();
282         if (types != null) {
283             List JavaDoc schemas = types.getExtensibilityElements();
284             for (Iterator JavaDoc iterator = schemas.iterator(); iterator.hasNext();) {
285                 Object JavaDoc o = iterator.next();
286                 if (o instanceof Schema) {
287                     Schema unknownExtensibilityElement = (Schema) o;
288                     QName JavaDoc elementType = unknownExtensibilityElement.getElementType();
289                     if (new QName JavaDoc("http://www.w3.org/2001/XMLSchema", "schema").equals(elementType)) {
290                         Element element = unknownExtensibilityElement.getElement();
291                         addSchemaElement(element, namespaceMap, schemaList);
292                     }
293                 } else if (o instanceof UnknownExtensibilityElement) {
294                     //This is allegedly obsolete as of axis-wsdl4j-1.2-RC3.jar which includes the Schema extension above.
295
//The change notes imply that imported schemas should end up in Schema elements. They don't, so this is still needed.
296
UnknownExtensibilityElement unknownExtensibilityElement = (UnknownExtensibilityElement) o;
297                     Element element = unknownExtensibilityElement.getElement();
298                     String JavaDoc elementNamespace = element.getNamespaceURI();
299                     String JavaDoc elementLocalName = element.getNodeName();
300                     if ("http://www.w3.org/2001/XMLSchema".equals(elementNamespace) && "schema".equals(elementLocalName)) {
301                         addSchemaElement(element, namespaceMap, schemaList);
302                     }
303                 }
304             }
305         }
306         Map JavaDoc imports = definition.getImports();
307         if (imports != null) {
308             for (Iterator JavaDoc iterator = imports.entrySet().iterator(); iterator.hasNext();) {
309                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
310                 String JavaDoc namespaceURI = (String JavaDoc) entry.getKey();
311                 List JavaDoc importList = (List JavaDoc) entry.getValue();
312                 for (Iterator JavaDoc iterator1 = importList.iterator(); iterator1.hasNext();) {
313                     Import anImport = (Import) iterator1.next();
314                     //according to the 1.1 jwsdl mr shcema imports are supposed to show up here,
315
//but according to the 1.0 spec there is supposed to be no Definition.
316
Definition definition1 = anImport.getDefinition();
317                     if (definition1 != null) {
318                         try {
319                             URI JavaDoc uri = new URI JavaDoc(definition1.getDocumentBaseURI());
320                             uris.push(uri);
321                         } catch (URISyntaxException JavaDoc e) {
322                             throw new DeploymentException("Could not locate definition", e);
323                         }
324                         try {
325                             addImportsFromDefinition(definition1, schemaList);
326                         } finally {
327                             uris.pop();
328                         }
329                     } else {
330                         log.warn("Missing definition in import for namespace " + namespaceURI);
331                     }
332                 }
333             }
334         }
335     }
336
337     private void addSchemaElement(Element element, Map JavaDoc namespaceMap, List JavaDoc schemaList) throws DeploymentException {
338         try {
339             XmlObject xmlObject = parseWithNamespaces(element, namespaceMap);
340             schemaList.add(xmlObject);
341         } catch (XmlException e) {
342             throw new DeploymentException("Could not parse schema element", e);
343         }
344     }
345
346     static XmlObject parseWithNamespaces(Element element, Map JavaDoc namespaceMap) throws XmlException {
347         XmlObject xmlObject = SchemaConversionUtils.parse(element);
348         XmlCursor cursor = xmlObject.newCursor();
349         try {
350             cursor.toFirstContentToken();
351             for (Iterator JavaDoc namespaces = namespaceMap.entrySet().iterator(); namespaces.hasNext();) {
352                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) namespaces.next();
353                 cursor.insertNamespace((String JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue());
354             }
355         } finally {
356             cursor.dispose();
357         }
358         return xmlObject;
359     }
360
361     /**
362      * builds a map of SchemaTypeKey containing jaxrpc-style fake QName and context info to xmlbeans SchemaType object.
363      *
364      * @param schemaTypeSystem
365      * @return
366      */

367     private Map JavaDoc buildSchemaTypeKeyToSchemaTypeMap(SchemaTypeSystem schemaTypeSystem) {
368         Map JavaDoc qnameMap = new HashMap JavaDoc();
369         SchemaType[] globalTypes = schemaTypeSystem.globalTypes();
370         for (int i = 0; i < globalTypes.length; i++) {
371             SchemaType globalType = globalTypes[i];
372             QName JavaDoc typeQName = globalType.getName();
373             addSchemaType(typeQName, globalType, false, qnameMap);
374         }
375         SchemaGlobalElement[] globalElements = schemaTypeSystem.globalElements();
376         for (int i = 0; i < globalElements.length; i++) {
377             SchemaGlobalElement globalElement = globalElements[i];
378             addElement(globalElement, null, qnameMap);
379         }
380         return qnameMap;
381     }
382
383     private void addElement(SchemaField element, SchemaTypeKey key, Map JavaDoc qnameMap) {
384         //TODO is this null if element is a ref?
385
QName JavaDoc elementName = element.getName();
386         String JavaDoc elementNamespace = elementName.getNamespaceURI();
387         //"" namespace means local element with elementFormDefault="unqualified"
388
if (elementNamespace == null || elementNamespace.equals("")) {
389             elementNamespace = key.getqName().getNamespaceURI();
390         }
391         String JavaDoc elementQNameLocalName;
392         SchemaTypeKey elementKey = null;
393         if (key == null) {
394             //top level. rule 2.a,
395
elementQNameLocalName = elementName.getLocalPart();
396             elementKey = new SchemaTypeKey(elementName, true, false, false, elementName);
397         } else {
398             //not top level. rule 2.b, key will be for enclosing Type.
399
QName JavaDoc enclosingTypeQName = key.getqName();
400             String JavaDoc enclosingTypeLocalName = enclosingTypeQName.getLocalPart();
401             elementQNameLocalName = enclosingTypeLocalName + ">" + elementName.getLocalPart();
402             QName JavaDoc subElementName = new QName JavaDoc(elementNamespace, elementQNameLocalName);
403             elementKey = new SchemaTypeKey(subElementName, true, false, true, elementName);
404         }
405         SchemaType schemaType = element.getType();
406         qnameMap.put(elementKey, schemaType);
407 // new Exception("Adding: " + elementKey.getqName().getLocalPart()).printStackTrace();
408
//check if it's an array. maxOccurs is null if unbounded
409
//element should always be a SchemaParticle... this is a workaround for XMLBEANS-137
410
if (element instanceof SchemaParticle) {
411             addArrayForms((SchemaParticle) element, elementKey.getqName(), qnameMap, schemaType);
412         } else {
413             log.warn("element is not a schemaParticle! " + element);
414         }
415         //now, name for type. Rule 1.b, type inside an element
416
String JavaDoc typeQNameLocalPart = ">" + elementQNameLocalName;
417         QName JavaDoc typeQName = new QName JavaDoc(elementNamespace, typeQNameLocalPart);
418         boolean isAnonymous = true;
419         addSchemaType(typeQName, schemaType, isAnonymous, qnameMap);
420     }
421
422     private void addSchemaType(QName JavaDoc typeQName, SchemaType schemaType, boolean anonymous, Map JavaDoc qnameMap) {
423         SchemaTypeKey typeKey = new SchemaTypeKey(typeQName, false, schemaType.isSimpleType(), anonymous, null);
424         qnameMap.put(typeKey, schemaType);
425 // new Exception("Adding: " + typeKey.getqName().getLocalPart()).printStackTrace();
426
//TODO xmlbeans recommends using summary info from getElementProperties and getAttributeProperties instead of traversing the content model by hand.
427
SchemaParticle schemaParticle = schemaType.getContentModel();
428         if (schemaParticle != null) {
429             addSchemaParticle(schemaParticle, typeKey, qnameMap);
430         }
431     }
432
433
434     private void addSchemaParticle(SchemaParticle schemaParticle, SchemaTypeKey key, Map JavaDoc qnameMap) {
435         if (schemaParticle.getParticleType() == SchemaParticle.ELEMENT) {
436             SchemaType elementType = schemaParticle.getType();
437             SchemaField element = elementType.getContainerField();
438             //element will be null if the type is defined elsewhere, such as a built in type.
439
if (element != null) {
440                 addElement(element, key, qnameMap);
441             } else {
442                 QName JavaDoc keyQName = key.getqName();
443                 //TODO I can't distinguish between 3.a and 3.b, so generate names both ways.
444
//3.b
445
String JavaDoc localPart = schemaParticle.getName().getLocalPart();
446                 QName JavaDoc elementName = new QName JavaDoc(keyQName.getNamespaceURI(), localPart);
447                 addArrayForms(schemaParticle, elementName, qnameMap, elementType);
448                 //3.a
449
localPart = keyQName.getLocalPart() + ">" + schemaParticle.getName().getLocalPart();
450                 elementName = new QName JavaDoc(keyQName.getNamespaceURI(), localPart);
451                 addArrayForms(schemaParticle, elementName, qnameMap, elementType);
452             }
453         } else {
454             try {
455                 SchemaParticle[] children = schemaParticle.getParticleChildren();
456                 for (int i = 0; i < children.length; i++) {
457                     SchemaParticle child = children[i];
458                     addSchemaParticle(child, key, qnameMap);
459                 }
460             } catch (NullPointerException JavaDoc e) {
461                 //ignore xmlbeans bug
462
}
463         }
464     }
465
466     private void addArrayForms(SchemaParticle schemaParticle, QName JavaDoc keyName, Map JavaDoc qnameMap, SchemaType elementType) {
467         //it may be a ref or a built in type. If it's an array (maxOccurs >1) form a type for it.
468
if (schemaParticle.getIntMaxOccurs() > 1) {
469             String JavaDoc maxOccurs = schemaParticle.getMaxOccurs() == null ? "unbounded" : "" + schemaParticle.getIntMaxOccurs();
470             int minOccurs = schemaParticle.getIntMinOccurs();
471             QName JavaDoc elementName = schemaParticle.getName();
472             String JavaDoc arrayQNameLocalName = keyName.getLocalPart() + "[" + minOccurs + "," + maxOccurs + "]";
473             String JavaDoc elementNamespace = elementName.getNamespaceURI();
474             if (elementNamespace == null || elementNamespace.equals("")) {
475                 elementNamespace = keyName.getNamespaceURI();
476             }
477             QName JavaDoc arrayName = new QName JavaDoc(elementNamespace, arrayQNameLocalName);
478             SchemaTypeKey arrayKey = new SchemaTypeKey(arrayName, false, false, true, elementName);
479             //TODO not clear we want the schemaType as the value
480
qnameMap.put(arrayKey, elementType);
481 // new Exception("Adding: " + arrayKey.getqName().getLocalPart()).printStackTrace();
482
if (minOccurs == 1) {
483                 arrayQNameLocalName = keyName.getLocalPart() + "[," + maxOccurs + "]";
484                 arrayName = new QName JavaDoc(elementNamespace, arrayQNameLocalName);
485                 arrayKey = new SchemaTypeKey(arrayName, false, false, true, elementName);
486                 //TODO not clear we want the schemaType as the value
487
qnameMap.put(arrayKey, elementType);
488             }
489         }
490     }
491
492
493     public Definition readWsdl(JarFile JavaDoc moduleFile, URI JavaDoc wsdlURI) throws DeploymentException {
494         Definition definition;
495         WSDLFactory wsdlFactory = null;
496         try {
497             wsdlFactory = WSDLFactory.newInstance();
498         } catch (WSDLException e) {
499             throw new DeploymentException("Could not create WSDLFactory", e);
500         }
501         WSDLReader wsdlReaderNoImport = wsdlFactory.newWSDLReader();
502         wsdlReaderNoImport.setFeature("javax.wsdl.importDocuments", false);
503         ExtensionRegistry extensionRegistry = new PopulatedExtensionRegistry();
504         extensionRegistry.mapExtensionTypes(Types.class, SchemaConstants.Q_ELEM_XSD_1999,
505             UnknownExtensibilityElement.class);
506         extensionRegistry.registerDeserializer(Types.class, SchemaConstants.Q_ELEM_XSD_1999,
507             extensionRegistry.getDefaultDeserializer());
508         extensionRegistry.registerSerializer(Types.class, SchemaConstants.Q_ELEM_XSD_1999,
509             extensionRegistry.getDefaultSerializer());
510
511         extensionRegistry.mapExtensionTypes(Types.class, SchemaConstants.Q_ELEM_XSD_2000,
512             UnknownExtensibilityElement.class);
513         extensionRegistry.registerDeserializer(Types.class, SchemaConstants.Q_ELEM_XSD_2000,
514                 extensionRegistry.getDefaultDeserializer());
515         extensionRegistry.registerSerializer(Types.class, SchemaConstants.Q_ELEM_XSD_2000,
516                 extensionRegistry.getDefaultSerializer());
517
518         extensionRegistry.mapExtensionTypes(Types.class, SchemaConstants.Q_ELEM_XSD_2001,
519             UnknownExtensibilityElement.class);
520         extensionRegistry.registerDeserializer(Types.class, SchemaConstants.Q_ELEM_XSD_2001,
521                 extensionRegistry.getDefaultDeserializer());
522         extensionRegistry.registerSerializer(Types.class, SchemaConstants.Q_ELEM_XSD_2001,
523                 extensionRegistry.getDefaultSerializer());
524         wsdlReaderNoImport.setExtensionRegistry(extensionRegistry);
525         JarWSDLLocator wsdlLocator = new JarWSDLLocator(wsdlURI, wsdlReaderNoImport);
526         WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
527         try {
528             definition = wsdlReader.readWSDL(wsdlLocator);
529         } catch (WSDLException e) {
530             throw new DeploymentException("Failed to read wsdl document", e);
531         }
532         return definition;
533     }
534
535     public static ExtensibilityElement getExtensibilityElement(Class JavaDoc clazz, List JavaDoc extensibilityElements) throws DeploymentException {
536         for (Iterator JavaDoc iterator = extensibilityElements.iterator(); iterator.hasNext();) {
537             ExtensibilityElement extensibilityElement = (ExtensibilityElement) iterator.next();
538             if (clazz.isAssignableFrom(extensibilityElement.getClass())) {
539                 return extensibilityElement;
540             }
541         }
542         throw new DeploymentException("No element of class " + clazz.getName() + " found");
543     }
544
545     public static void updatePortLocations(Service service, Map JavaDoc portLocations) throws DeploymentException {
546         for (Iterator JavaDoc iterator = portLocations.entrySet().iterator(); iterator.hasNext();) {
547             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
548             String JavaDoc portName = (String JavaDoc) entry.getKey();
549             String JavaDoc location = (String JavaDoc) entry.getValue();
550             Port port = service.getPort(portName);
551             if (port == null) {
552                 throw new DeploymentException("No port named " + portName + " found in service " + service.getQName());
553             }
554             SOAPAddress soapAddress = (SOAPAddress) getExtensibilityElement(SOAPAddress.class, port.getExtensibilityElements());
555             soapAddress.setLocationURI(location);
556         }
557     }
558
559     private class JarEntityResolver implements EntityResolver JavaDoc {
560
561         private final static String JavaDoc PROJECT_URL_PREFIX = "project://local/";
562         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
563             //seems like this must be a bug in xmlbeans...
564
if (systemId.indexOf(PROJECT_URL_PREFIX) > -1) {
565                 systemId = systemId.substring(PROJECT_URL_PREFIX.length());
566             }
567             URI JavaDoc location = ((URI JavaDoc) uris.peek()).resolve(systemId);
568             InputStream JavaDoc wsdlInputStream = null;
569             try {
570                 ZipEntry JavaDoc entry = moduleFile.getEntry(location.toString());
571                 wsdlInputStream = moduleFile.getInputStream(entry);
572                 XmlObject xmlObject = SchemaDocument.Factory.parse(wsdlInputStream);
573                 wsdlMap.put(location, xmlObject);
574                 wsdlInputStream.close();
575                 wsdlInputStream = moduleFile.getInputStream(entry);
576             } catch (XmlException e) {
577                 throw (IOException JavaDoc)new IOException JavaDoc("Could not parse schema document").initCause(e);
578             }
579             return new InputSource JavaDoc(wsdlInputStream);
580         }
581     }
582
583     class JarWSDLLocator implements WSDLLocator {
584
585         private final URI JavaDoc wsdlURI;
586         private final WSDLReader wsdlReader;
587         private URI JavaDoc latestImportURI;
588
589         public JarWSDLLocator(URI JavaDoc wsdlURI, WSDLReader wsdlReader) {
590             this.wsdlURI = wsdlURI;
591             this.wsdlReader = wsdlReader;
592         }
593
594         public InputSource JavaDoc getBaseInputSource() {
595             InputStream JavaDoc wsdlInputStream = null;
596             try {
597                 ZipEntry JavaDoc entry = moduleFile.getEntry(wsdlURI.toString());
598                 wsdlInputStream = moduleFile.getInputStream(entry);
599                 Definition definition = wsdlReader.readWSDL(wsdlURI.toString(), new InputSource JavaDoc(wsdlInputStream));
600                 wsdlMap.put(wsdlURI, definition);
601                 wsdlInputStream.close();
602                 wsdlInputStream = moduleFile.getInputStream(entry);
603             } catch (Exception JavaDoc e) {
604                 throw new RuntimeException JavaDoc("Could not open stream to wsdl file", e);
605             }
606             return new InputSource JavaDoc(wsdlInputStream);
607         }
608
609         public String JavaDoc getBaseURI() {
610             return wsdlURI.toString();
611         }
612
613         public InputSource JavaDoc getImportInputSource(String JavaDoc parentLocation, String JavaDoc relativeLocation) {
614             URI JavaDoc parentURI = URI.create(parentLocation);
615             latestImportURI = parentURI.resolve(relativeLocation);
616             InputStream JavaDoc importInputStream = null;
617             try {
618                 ZipEntry JavaDoc entry = moduleFile.getEntry(latestImportURI.toString());
619                 importInputStream = moduleFile.getInputStream(entry);
620                 try {
621                     Definition definition = wsdlReader.readWSDL(wsdlURI.toString(), new InputSource JavaDoc(importInputStream));
622                     wsdlMap.put(latestImportURI, definition);
623                     importInputStream.close();
624                 } catch (WSDLException e) {
625                     //probably was a schema rather than wsdl. If there are real problems they will show up later.
626
}
627                 importInputStream = moduleFile.getInputStream(entry);
628             } catch (Exception JavaDoc e) {
629                 throw new RuntimeException JavaDoc("Could not open stream to import file", e);
630             }
631             InputSource JavaDoc inputSource = new InputSource JavaDoc(importInputStream);
632             inputSource.setSystemId(getLatestImportURI());
633             return inputSource;
634         }
635
636         public String JavaDoc getLatestImportURI() {
637             return latestImportURI.toString();
638         }
639     }
640 }
641
Popular Tags