KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > metadata > wsdl > WSDL11DefinitionFactory


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.webservice.metadata.wsdl;
8
9 // $Id: WSDL11DefinitionFactory.java,v 1.1.2.2 2005/06/17 22:31:51 tdiesler Exp $
10

11 import org.jboss.logging.Logger;
12 import org.xml.sax.InputSource JavaDoc;
13
14 import javax.wsdl.Definition;
15 import javax.wsdl.WSDLException;
16 import javax.wsdl.factory.WSDLFactory;
17 import javax.wsdl.xml.WSDLLocator;
18 import javax.wsdl.xml.WSDLReader;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23
24 /**
25  * A factory that creates a WSDL-1.1 <code>Definition</code> from an URL.
26  *
27  * @author Thomas.Diesler@jboss.org
28  * @since 15-April-2004
29  */

30 public class WSDL11DefinitionFactory
31 {
32    // provide logging
33
private static final Logger log = Logger.getLogger(WSDL11DefinitionFactory.class);
34
35    // This feature is set by default in wsdl4j, it means the object structore contains the imported arguments
36
public static final String JavaDoc FEATURE_IMPORT_DOCUMENTS = "javax.wsdl.importDocuments";
37    // Set this feature for additional debugging output
38
public static final String JavaDoc FEATURE_VERBOSE = "javax.wsdl.verbose";
39
40    // The WSDLReader that is used by this factory
41
private WSDLReader wsdlReader;
42
43    // Hide constructor
44
private WSDL11DefinitionFactory() throws WSDLException
45    {
46       WSDLFactory wsdlFactory = WSDLFactory.newInstance();
47       wsdlReader = wsdlFactory.newWSDLReader();
48    }
49
50    /** Create a new instance of a wsdl factory */
51    public static WSDL11DefinitionFactory newInstance() throws WSDLException
52    {
53       return new WSDL11DefinitionFactory();
54    }
55
56    /** Set a feature on the underlying reader */
57    public void setFeature(String JavaDoc name, boolean value) throws IllegalArgumentException JavaDoc
58    {
59       wsdlReader.setFeature(name, value);
60    }
61
62    /**
63     * Read the wsdl document from the given URL
64     */

65    public Definition parse(URL JavaDoc wsdlLocation) throws WSDLException
66    {
67       if (wsdlLocation == null)
68          throw new IllegalArgumentException JavaDoc("URL cannot be null");
69
70       // wsdl4j-1.4 is quite noisy on system out in verbose mode
71
Definition wsdlDefinition = wsdlReader.readWSDL(new WSDLLocatorImpl(wsdlLocation));
72       return wsdlDefinition;
73    }
74
75    /* A WSDLLocator that can handle wsdl imports
76    */

77    public static class WSDLLocatorImpl implements WSDLLocator
78    {
79       private URL JavaDoc wsdlURL;
80       private String JavaDoc latestImportURI;
81
82       public WSDLLocatorImpl(URL JavaDoc wsdlFile)
83       {
84          if (wsdlFile == null)
85             throw new IllegalArgumentException JavaDoc("WSDL file argument cannot be null");
86
87          this.wsdlURL = wsdlFile;
88       }
89
90       public InputSource JavaDoc getBaseInputSource()
91       {
92          log.debug("getBaseInputSource [wsdlUrl=" + wsdlURL + "]");
93          try
94          {
95             InputStream JavaDoc is = wsdlURL.openStream();
96             if (is == null)
97                throw new IllegalArgumentException JavaDoc("Cannot obtain wsdl from [" + wsdlURL + "]");
98
99             return new InputSource JavaDoc(is);
100          }
101          catch (IOException JavaDoc e)
102          {
103             throw new RuntimeException JavaDoc("Cannot access wsdl from [" + wsdlURL + "], " + e.getMessage());
104          }
105       }
106
107       public String JavaDoc getBaseURI()
108       {
109          return wsdlURL.toExternalForm();
110       }
111
112       public InputSource JavaDoc getImportInputSource(String JavaDoc parent, String JavaDoc resource)
113       {
114          log.debug("getImportInputSource [parent=" + parent + ",resource=" + resource + "]");
115
116          URL JavaDoc parentURL = null;
117          try
118          {
119             parentURL = new URL JavaDoc(parent);
120          }
121          catch (MalformedURLException JavaDoc e)
122          {
123             log.error("Not a valid URL: " + parent);
124             return null;
125          }
126
127          String JavaDoc wsdlImport = null;
128          String JavaDoc external = parentURL.toExternalForm();
129
130          // An external URL
131
if (resource.startsWith("http://") || resource.startsWith("https://"))
132          {
133             wsdlImport = resource;
134          }
135
136          // Absolute path
137
else if (resource.startsWith("/"))
138          {
139             String JavaDoc beforePath = external.substring(0, external.indexOf(parentURL.getPath()));
140             wsdlImport = beforePath + resource;
141          }
142
143          // A relative path
144
else
145          {
146             String JavaDoc parentDir = external.substring(0, external.lastIndexOf("/"));
147
148             // remove references to current dir
149
while (resource.startsWith("./"))
150                resource = resource.substring(2);
151
152             // remove references to parentdir
153
while (resource.startsWith("../"))
154             {
155                parentDir = parentDir.substring(0, parentDir.lastIndexOf("/"));
156                resource = resource.substring(3);
157             }
158
159             wsdlImport = parentDir + "/" + resource;
160          }
161
162          try
163          {
164             log.debug("Resolved to: " + wsdlImport);
165             InputStream JavaDoc is = new URL JavaDoc(wsdlImport).openStream();
166             if (is == null)
167                throw new IllegalArgumentException JavaDoc("Cannot import wsdl from [" + wsdlImport + "]");
168
169             latestImportURI = wsdlImport;
170             return new InputSource JavaDoc(is);
171          }
172          catch (IOException JavaDoc e)
173          {
174             throw new RuntimeException JavaDoc("Cannot access imported wsdl [" + wsdlImport + "], " + e.getMessage());
175          }
176       }
177
178       public String JavaDoc getLatestImportURI()
179       {
180          return latestImportURI;
181       }
182    }
183 }
184
Popular Tags