2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.model
.common
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStream
;
15 import java
.io
.InputStreamReader
;
16 import java
.io
.Reader
;
19 import javax
.wsdl
.xml
.WSDLLocator
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.xml
.sax
.InputSource
;
25 import com
.ibm
.wsdl
.util
.StringUtils
;
28 * WSDLLocator based almost totally upon WSIFWSDLLocatorImpl by Owen Burroughs
29 * Created primarily because locating the required wsdl documents in the classpath
30 * seems like a safer thing than using a file path and relying on users remembering to
31 * copy the correct wsdl files into the correct places.
34 * @author Owen Burroughs
36 public class LSIDWSDLLocator
implements WSDLLocator
{
37 private static Log log
= LogFactory
.getLog(LSIDWSDLLocator
.class);
39 private Reader baseReader
= null;
40 private Reader importReader
= null;
41 private String contextURI
= null;
42 private String wsdlLocation
= null;
43 private String documentBase
= null;
44 private String importBase
= null;
45 private ClassLoader loader
= null;
47 public LSIDWSDLLocator(String ctxt
, String wsdlURI
, ClassLoader cl
) {
49 wsdlLocation
= wsdlURI
;
53 public LSIDWSDLLocator(String docBase
, Reader reader
, ClassLoader cl
) {
54 documentBase
= docBase
;
60 * @see javax.wsdl.xml.WSDLLocator#getBaseReader()
62 public Reader
getBaseReader() {
63 if (baseReader
== null) {
66 URL contextURL
= (contextURI
!= null) ? StringUtils
.getURL(
67 null, contextURI
) : null;
69 InputStream in
= null;
71 if (contextURL
!= null)
72 url
= new URL(contextURL
, wsdlLocation
);
74 if (wsdlLocation
.indexOf(":") == -1)
75 url
= new URL("file", null, wsdlLocation
);
77 url
= new URL(wsdlLocation
);
79 String wsdlRelativeLocation
= url
.getPath();
80 if (wsdlRelativeLocation
.startsWith("/"))
81 wsdlRelativeLocation
= wsdlRelativeLocation
84 .getResourceAsStream(wsdlRelativeLocation
);
85 baseReader
= new InputStreamReader(in
);
86 } catch (Exception exc
) {
89 if (baseReader
== null) {
90 url
= StringUtils
.getURL(contextURL
, wsdlLocation
);
91 baseReader
= new InputStreamReader(StringUtils
92 .getContentAsInputStream(url
));
95 documentBase
= url
.toString();
96 } catch (Exception e
) {
97 documentBase
= wsdlLocation
;
105 * @see javax.wsdl.xml.WSDLLocator#getBaseURI()
107 public String
getBaseURI() {
112 * used to read imports as a document is parsed
114 * @see javax.wsdl.xml.WSDLLocator#getImportReader(String, String)
116 public Reader
getImportReader(String base
, String relativeLocation
) {
117 // Reset importReader if finding import within import
119 boolean triedSU
= false;
121 // If a ClassLoader was used to load the base
123 // are we need to use it to find the import.
125 if (loader
!= null) {
126 if (relativeLocation
.startsWith("/")
127 || relativeLocation
.startsWith("\\")) {
128 // Relative location has been specified from a root dir.
130 // using a ClassLoader, root dirs don't mean anything.
131 relativeLocation
= relativeLocation
.substring(1,relativeLocation
.length());
132 InputStream in
= loader
.getResourceAsStream(relativeLocation
);
133 importReader
= new InputStreamReader(in
);
134 } else if (relativeLocation
.indexOf("://") != -1) {
135 // This is a fully specified URL of some kind so don't
137 // ClassLoader to find the import.
139 url
= StringUtils
.getURL(null, relativeLocation
);
140 importReader
= new InputStreamReader(StringUtils
141 .getContentAsInputStream(url
));
143 // Import location has been specified relative to the
145 // and so we can to try to form the complete path to it.
147 int i
= base
.lastIndexOf("/");
149 i
= base
.lastIndexOf("\\");
152 String path
= base
.substring(0, i
+ 1);
153 String resolvedPath
= path
+ relativeLocation
;
154 if (relativeLocation
.startsWith("..")) {
155 resolvedPath
= resolvePath(path
,
158 if (resolvedPath
== null) {
159 throw new Exception("Invalid Path");
162 // Make sure that resolved path starts with
164 if (resolvedPath
.startsWith("file:")) {
165 url
= new URL(null, resolvedPath
);
167 url
= new URL(null, "file:" + resolvedPath
);
170 url
= new URL(null, "file:" + base
+ File
.separator
+ relativeLocation
);
172 InputStream in
= loader
.getResourceAsStream(url
.getPath());
173 importReader
= new InputStreamReader(in
);
175 url
= new URL(null, "file:" + relativeLocation
);
176 InputStream in
= loader
.getResourceAsStream(url
.getPath());
177 importReader
= new InputStreamReader(in
);
182 URL contextURL
= (base
!= null) ? StringUtils
.getURL(null,
184 url
= StringUtils
.getURL(contextURL
, relativeLocation
);
185 importReader
= new InputStreamReader(StringUtils
186 .getContentAsInputStream(url
));
188 importBase
= (url
== null) ? relativeLocation
: url
.toString();
189 } catch (Exception e
) {
190 log
.error(e
.toString());
191 log
.error(e
.getMessage());
192 // If we have not tried using a non-ClassLoader route, try it
197 URL contextURL
= (base
!= null) ? StringUtils
.getURL(
199 URL url
= StringUtils
.getURL(contextURL
,
201 importReader
= new InputStreamReader(StringUtils
202 .getContentAsInputStream(url
));
203 importBase
= (url
== null) ? relativeLocation
: url
205 } catch (Exception e2
) {
206 log
.error(e2
.toString());
207 log
.error("Cannot find " + importBase
+ " so setting importBase to unknownImportURI");
208 // we can't find the import so set a temporary value for
209 // the import URI. This is
210 // necessary to avoid a NullPointerException in
212 importBase
= "unknownImportURI";
215 log
.error("Cannot find " + importBase
+ " so setting importBase to unknownImportURI");
216 // we can't find the import so set a temporary value for the
217 // import URI. This is
218 // necessary to avoid a NullPointerException in
220 importBase
= "unknownImportURI";
228 * Resolve a path when the relative location begins with ..
230 private String
resolvePath(String ba
, String rel
) {
231 StringBuffer sb
= new StringBuffer(rel
);
233 while (sb
.length() > 0) {
234 if (sb
.length() > 3 && sb
.charAt(0) == '.'
235 && sb
.charAt(1) == '.'
236 && (sb
.charAt(2) == '/' || sb
.charAt(2) == '\\')) {
243 StringBuffer sb2
= new StringBuffer(ba
);
244 int j
= sb2
.length() - 1;
246 for (int k
= j
; k
>= 0; k
--) {
247 if (k
!= j
&& (sb2
.charAt(k
) == '/' || sb2
.charAt(k
) == '\\')) {
258 return sb2
.toString() + sb
.toString();
262 * @see javax.wsdl.xml.WSDLLocator#getLatestImportURI()
264 public String
getLatestImportURI() {
269 * @see javax.wsdl.xml.WSDLLocator#getBaseInputSource()
271 public InputSource
getBaseInputSource() {
272 return new InputSource(getBaseReader());
276 * @see javax.wsdl.xml.WSDLLocator#getImportInputSource(String, String)
278 public InputSource
getImportInputSource(String arg0
, String arg1
) {
279 return new InputSource(getImportReader(arg0
, arg1
));
282 public void close() {
283 if (baseReader
!= null)
286 } catch (IOException e
) {
287 // TODO Auto-generated catch block
290 if (importReader
!= null)
292 importReader
.close();
293 } catch (IOException e
) {
294 // TODO Auto-generated catch block