Revision b5f21afa
Added by Andreas Müller 8 months ago
cdmlib-model/src/main/java/eu/etaxonomy/cdm/jaxb/FormattedTextAdapter.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2009 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
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. |
|
8 |
*/ |
|
9 |
|
|
10 |
package eu.etaxonomy.cdm.jaxb; |
|
11 |
|
|
12 |
import java.io.StringReader; |
|
13 |
import java.io.StringWriter; |
|
14 |
|
|
15 |
import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
16 |
import javax.xml.parsers.DocumentBuilder; |
|
17 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
18 |
import javax.xml.transform.OutputKeys; |
|
19 |
import javax.xml.transform.Result; |
|
20 |
import javax.xml.transform.Source; |
|
21 |
import javax.xml.transform.Transformer; |
|
22 |
import javax.xml.transform.TransformerFactory; |
|
23 |
import javax.xml.transform.dom.DOMSource; |
|
24 |
import javax.xml.transform.stream.StreamResult; |
|
25 |
|
|
26 |
import org.apache.commons.lang3.StringEscapeUtils; |
|
27 |
import org.apache.commons.logging.Log; |
|
28 |
import org.apache.commons.logging.LogFactory; |
|
29 |
import org.w3c.dom.DOMException; |
|
30 |
import org.w3c.dom.Document; |
|
31 |
import org.w3c.dom.DocumentFragment; |
|
32 |
import org.w3c.dom.NamedNodeMap; |
|
33 |
import org.w3c.dom.Node; |
|
34 |
import org.w3c.dom.NodeList; |
|
35 |
import org.xml.sax.InputSource; |
|
36 |
|
|
37 |
public class FormattedTextAdapter extends XmlAdapter<FormattedText,java.lang.String> { |
|
38 |
public static String[] NAMESPACE_PREFIXES = {"xmlns:common", |
|
39 |
"xmlns:agent", |
|
40 |
"xmlns:description", |
|
41 |
"xmlns:location", |
|
42 |
"xmlns:media", |
|
43 |
"xmlns:molecular", |
|
44 |
"xmlns:name", |
|
45 |
"xmlns:occurence", |
|
46 |
"xmlns:reference", |
|
47 |
"xmlns:taxon", |
|
48 |
"xmlns:view", |
|
49 |
"xmlns:xsi"}; |
|
50 |
|
|
51 |
@SuppressWarnings("unused") |
|
52 |
private static final Log logger = LogFactory.getLog(FormattedTextAdapter.class); |
|
53 |
|
|
54 |
@Override |
|
55 |
public FormattedText marshal(String string) throws Exception { |
|
56 |
if(string != null) { |
|
57 |
string = StringEscapeUtils.escapeXml(string); |
|
58 |
String documentString = "<?xml version=\"1.0\"?><text>" + string + "</text>"; |
|
59 |
//log.debug("Parsing " + documentString); |
|
60 |
FormattedText text = new FormattedText(); |
|
61 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
62 |
DocumentBuilder parser = factory.newDocumentBuilder(); |
|
63 |
Document document = parser.parse(new InputSource(new StringReader(documentString))); |
|
64 |
NodeList childNodes = document.getDocumentElement().getChildNodes(); |
|
65 |
for(int i = 0; i < childNodes.getLength(); i++) { |
|
66 |
Node node = childNodes.item(i); |
|
67 |
if(node instanceof org.w3c.dom.Text ) { |
|
68 |
org.w3c.dom.Text textNode = (org.w3c.dom.Text) node; |
|
69 |
|
|
70 |
text.getContent().add(textNode.getTextContent()); |
|
71 |
} else { |
|
72 |
text.getContent().add(node); |
|
73 |
} |
|
74 |
} |
|
75 |
return text; |
|
76 |
} |
|
77 |
return null; |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public String unmarshal(FormattedText text) throws Exception { |
|
82 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
83 |
Document document = factory.newDocumentBuilder().newDocument(); |
|
84 |
DocumentFragment documentFragment = document.createDocumentFragment(); |
|
85 |
|
|
86 |
for(Object object : text.getContent()) { |
|
87 |
if(object instanceof String) { |
|
88 |
String string = (String)object; |
|
89 |
documentFragment.appendChild(document.createTextNode(string)); |
|
90 |
} else { |
|
91 |
Node node = (Node)object; |
|
92 |
NamedNodeMap attributes = node.getAttributes(); |
|
93 |
for(String prefix : FormattedTextAdapter.NAMESPACE_PREFIXES) { |
|
94 |
try{ |
|
95 |
attributes.removeNamedItem(prefix); |
|
96 |
} catch(DOMException de){ |
|
97 |
if(de.code != DOMException.NOT_FOUND_ERR) { |
|
98 |
throw de; |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
} |
|
103 |
|
|
104 |
documentFragment.appendChild(document.importNode(node,true)); |
|
105 |
} |
|
106 |
|
|
107 |
} |
|
108 |
|
|
109 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
110 |
Transformer transformer = transformerFactory.newTransformer(); |
|
111 |
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION , "yes"); |
|
112 |
|
|
113 |
Source input = new DOMSource(documentFragment); |
|
114 |
StringWriter stringWriter = new StringWriter(); |
|
115 |
Result output = new StreamResult(stringWriter); |
|
116 |
transformer.transform(input, output); |
|
117 |
String result = stringWriter.toString(); |
|
118 |
result = StringEscapeUtils.unescapeXml(result); |
|
119 |
return result; |
|
120 |
} |
|
121 |
} |
|
1 |
/** |
|
2 |
* Copyright (C) 2009 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
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. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.jaxb; |
|
10 |
|
|
11 |
import java.io.StringReader; |
|
12 |
import java.io.StringWriter; |
|
13 |
|
|
14 |
import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
15 |
import javax.xml.parsers.DocumentBuilder; |
|
16 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
17 |
import javax.xml.transform.OutputKeys; |
|
18 |
import javax.xml.transform.Result; |
|
19 |
import javax.xml.transform.Source; |
|
20 |
import javax.xml.transform.Transformer; |
|
21 |
import javax.xml.transform.TransformerFactory; |
|
22 |
import javax.xml.transform.dom.DOMSource; |
|
23 |
import javax.xml.transform.stream.StreamResult; |
|
24 |
|
|
25 |
import org.apache.commons.text.StringEscapeUtils; |
|
26 |
import org.apache.logging.log4j.LogManager; |
|
27 |
import org.apache.logging.log4j.Logger; |
|
28 |
import org.w3c.dom.DOMException; |
|
29 |
import org.w3c.dom.Document; |
|
30 |
import org.w3c.dom.DocumentFragment; |
|
31 |
import org.w3c.dom.NamedNodeMap; |
|
32 |
import org.w3c.dom.Node; |
|
33 |
import org.w3c.dom.NodeList; |
|
34 |
import org.xml.sax.InputSource; |
|
35 |
|
|
36 |
public class FormattedTextAdapter extends XmlAdapter<FormattedText,java.lang.String> { |
|
37 |
public static String[] NAMESPACE_PREFIXES = {"xmlns:common", |
|
38 |
"xmlns:agent", |
|
39 |
"xmlns:description", |
|
40 |
"xmlns:location", |
|
41 |
"xmlns:media", |
|
42 |
"xmlns:molecular", |
|
43 |
"xmlns:name", |
|
44 |
"xmlns:occurence", |
|
45 |
"xmlns:reference", |
|
46 |
"xmlns:taxon", |
|
47 |
"xmlns:view", |
|
48 |
"xmlns:xsi"}; |
|
49 |
|
|
50 |
@SuppressWarnings("unused") |
|
51 |
private static final Logger logger = LogManager.getLogger(); |
|
52 |
|
|
53 |
@Override |
|
54 |
public FormattedText marshal(String string) throws Exception { |
|
55 |
if(string != null) { |
|
56 |
string = StringEscapeUtils .escapeXml11(string); |
|
57 |
String documentString = "<?xml version=\"1.0\"?><text>" + string + "</text>"; |
|
58 |
//log.debug("Parsing " + documentString); |
|
59 |
FormattedText text = new FormattedText(); |
|
60 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
61 |
DocumentBuilder parser = factory.newDocumentBuilder(); |
|
62 |
Document document = parser.parse(new InputSource(new StringReader(documentString))); |
|
63 |
NodeList childNodes = document.getDocumentElement().getChildNodes(); |
|
64 |
for(int i = 0; i < childNodes.getLength(); i++) { |
|
65 |
Node node = childNodes.item(i); |
|
66 |
if(node instanceof org.w3c.dom.Text ) { |
|
67 |
org.w3c.dom.Text textNode = (org.w3c.dom.Text) node; |
|
68 |
|
|
69 |
text.getContent().add(textNode.getTextContent()); |
|
70 |
} else { |
|
71 |
text.getContent().add(node); |
|
72 |
} |
|
73 |
} |
|
74 |
return text; |
|
75 |
} |
|
76 |
return null; |
|
77 |
} |
|
78 |
|
|
79 |
@Override |
|
80 |
public String unmarshal(FormattedText text) throws Exception { |
|
81 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
82 |
Document document = factory.newDocumentBuilder().newDocument(); |
|
83 |
DocumentFragment documentFragment = document.createDocumentFragment(); |
|
84 |
|
|
85 |
for(Object object : text.getContent()) { |
|
86 |
if(object instanceof String) { |
|
87 |
String string = (String)object; |
|
88 |
documentFragment.appendChild(document.createTextNode(string)); |
|
89 |
} else { |
|
90 |
Node node = (Node)object; |
|
91 |
NamedNodeMap attributes = node.getAttributes(); |
|
92 |
for(String prefix : FormattedTextAdapter.NAMESPACE_PREFIXES) { |
|
93 |
try{ |
|
94 |
attributes.removeNamedItem(prefix); |
|
95 |
} catch(DOMException de){ |
|
96 |
if(de.code != DOMException.NOT_FOUND_ERR) { |
|
97 |
throw de; |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
} |
|
102 |
|
|
103 |
documentFragment.appendChild(document.importNode(node,true)); |
|
104 |
} |
|
105 |
|
|
106 |
} |
|
107 |
|
|
108 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
109 |
Transformer transformer = transformerFactory.newTransformer(); |
|
110 |
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION , "yes"); |
|
111 |
|
|
112 |
Source input = new DOMSource(documentFragment); |
|
113 |
StringWriter stringWriter = new StringWriter(); |
|
114 |
Result output = new StreamResult(stringWriter); |
|
115 |
transformer.transform(input, output); |
|
116 |
String result = stringWriter.toString(); |
|
117 |
result = StringEscapeUtils.unescapeXml(result); |
|
118 |
return result; |
|
119 |
} |
|
120 |
} |
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/LSIDWSDLLocator.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2009 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
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. |
|
8 |
*/ |
|
9 |
|
|
10 |
package eu.etaxonomy.cdm.model.common; |
|
11 |
|
|
12 |
import java.io.File; |
|
13 |
import java.io.IOException; |
|
14 |
import java.io.InputStream; |
|
15 |
import java.io.InputStreamReader; |
|
16 |
import java.io.Reader; |
|
17 |
import java.net.URL; |
|
18 |
|
|
19 |
import javax.wsdl.xml.WSDLLocator; |
|
20 |
|
|
21 |
import org.apache.commons.logging.Log; |
|
22 |
import org.apache.commons.logging.LogFactory; |
|
23 |
import org.xml.sax.InputSource; |
|
24 |
|
|
25 |
import com.ibm.wsdl.util.StringUtils; |
|
26 |
|
|
27 |
/** |
|
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. |
|
32 |
* |
|
33 |
* @author ben.clark |
|
34 |
* @author Owen Burroughs |
|
35 |
*/ |
|
36 |
public class LSIDWSDLLocator implements WSDLLocator { |
|
37 |
private static Log log = LogFactory.getLog(LSIDWSDLLocator.class); |
|
38 |
|
|
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; |
|
46 |
|
|
47 |
public LSIDWSDLLocator(String ctxt, String wsdlURI, ClassLoader cl) { |
|
48 |
contextURI = ctxt; |
|
49 |
wsdlLocation = wsdlURI; |
|
50 |
loader = cl; |
|
51 |
} |
|
52 |
|
|
53 |
public LSIDWSDLLocator(String docBase, Reader reader, ClassLoader cl) { |
|
54 |
documentBase = docBase; |
|
55 |
baseReader = reader; |
|
56 |
loader = cl; |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* @see javax.wsdl.xml.WSDLLocator#getBaseReader() |
|
61 |
*/ |
|
62 |
public Reader getBaseReader() { |
|
63 |
if (baseReader == null) { |
|
64 |
try { |
|
65 |
URL url = null; |
|
66 |
URL contextURL = (contextURI != null) ? StringUtils.getURL( |
|
67 |
null, contextURI) : null; |
|
68 |
if (loader != null) { |
|
69 |
InputStream in = null; |
|
70 |
try { |
|
71 |
if (contextURL != null) { |
|
72 |
url = new URL(contextURL, wsdlLocation); |
|
73 |
} else { |
|
74 |
if (wsdlLocation.indexOf(":") == -1) { |
|
75 |
url = new URL("file", null, wsdlLocation); |
|
76 |
} else { |
|
77 |
url = new URL(wsdlLocation); |
|
78 |
} |
|
79 |
} |
|
80 |
String wsdlRelativeLocation = url.getPath(); |
|
81 |
if (wsdlRelativeLocation.startsWith("/")) { |
|
82 |
wsdlRelativeLocation = wsdlRelativeLocation |
|
83 |
.substring(1); |
|
84 |
} |
|
85 |
in = loader |
|
86 |
.getResourceAsStream(wsdlRelativeLocation); |
|
87 |
baseReader = new InputStreamReader(in); |
|
88 |
} catch (Exception exc) { |
|
89 |
} |
|
90 |
} |
|
91 |
if (baseReader == null) { |
|
92 |
url = StringUtils.getURL(contextURL, wsdlLocation); |
|
93 |
baseReader = new InputStreamReader(StringUtils |
|
94 |
.getContentAsInputStream(url)); |
|
95 |
} |
|
96 |
if (url != null) { |
|
97 |
documentBase = url.toString(); |
|
98 |
} |
|
99 |
} catch (Exception e) { |
|
100 |
documentBase = wsdlLocation; |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
return baseReader; |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* @see javax.wsdl.xml.WSDLLocator#getBaseURI() |
|
109 |
*/ |
|
110 |
@Override |
|
111 |
public String getBaseURI() { |
|
112 |
return documentBase; |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* used to read imports as a document is parsed |
|
117 |
* |
|
118 |
* @see javax.wsdl.xml.WSDLLocator#getImportReader(String, String) |
|
119 |
*/ |
|
120 |
public Reader getImportReader(String base, String relativeLocation) { |
|
121 |
// Reset importReader if finding import within import |
|
122 |
importReader = null; |
|
123 |
boolean triedSU = false; |
|
124 |
try { |
|
125 |
// If a ClassLoader was used to load the base |
|
126 |
// document, chances |
|
127 |
// are we need to use it to find the import. |
|
128 |
URL url = null; |
|
129 |
if (loader != null) { |
|
130 |
if (relativeLocation.startsWith("/") |
|
131 |
|| relativeLocation.startsWith("\\")) { |
|
132 |
// Relative location has been specified from a root dir. |
|
133 |
// However, |
|
134 |
// using a ClassLoader, root dirs don't mean anything. |
|
135 |
relativeLocation = relativeLocation.substring(1,relativeLocation.length()); |
|
136 |
InputStream in = loader.getResourceAsStream(relativeLocation); |
|
137 |
importReader = new InputStreamReader(in); |
|
138 |
} else if (relativeLocation.indexOf("://") != -1) { |
|
139 |
// This is a fully specified URL of some kind so don't |
|
140 |
// use the |
|
141 |
// ClassLoader to find the import. |
|
142 |
triedSU = true; |
|
143 |
url = StringUtils.getURL(null, relativeLocation); |
|
144 |
importReader = new InputStreamReader(StringUtils |
|
145 |
.getContentAsInputStream(url)); |
|
146 |
} else { |
|
147 |
// Import location has been specified relative to the |
|
148 |
// base document |
|
149 |
// and so we can to try to form the complete path to it. |
|
150 |
if (base != null) { |
|
151 |
int i = base.lastIndexOf("/"); |
|
152 |
if (i == -1) { |
|
153 |
i = base.lastIndexOf("\\"); |
|
154 |
} |
|
155 |
if (i != -1) { |
|
156 |
String path = base.substring(0, i + 1); |
|
157 |
String resolvedPath = path + relativeLocation; |
|
158 |
if (relativeLocation.startsWith("..")) { |
|
159 |
resolvedPath = resolvePath(path, |
|
160 |
relativeLocation); |
|
161 |
} |
|
162 |
if (resolvedPath == null) { |
|
163 |
throw new Exception("Invalid Path"); |
|
164 |
} |
|
165 |
|
|
166 |
// Make sure that resolved path starts with |
|
167 |
// file: |
|
168 |
if (resolvedPath.startsWith("file:")) { |
|
169 |
url = new URL(null, resolvedPath); |
|
170 |
} else { |
|
171 |
url = new URL(null, "file:" + resolvedPath); |
|
172 |
} |
|
173 |
} else { |
|
174 |
url = new URL(null, "file:" + base + File.separator + relativeLocation); |
|
175 |
} |
|
176 |
InputStream in = loader.getResourceAsStream(url.getPath()); |
|
177 |
importReader = new InputStreamReader(in); |
|
178 |
} else { |
|
179 |
url = new URL(null, "file:" + relativeLocation); |
|
180 |
InputStream in = loader.getResourceAsStream(url.getPath()); |
|
181 |
importReader = new InputStreamReader(in); |
|
182 |
} |
|
183 |
} |
|
184 |
} else { |
|
185 |
triedSU = true; |
|
186 |
URL contextURL = (base != null) ? StringUtils.getURL(null, |
|
187 |
base) : null; |
|
188 |
url = StringUtils.getURL(contextURL, relativeLocation); |
|
189 |
importReader = new InputStreamReader(StringUtils |
|
190 |
.getContentAsInputStream(url)); |
|
191 |
} |
|
192 |
importBase = (url == null) ? relativeLocation : url.toString(); |
|
193 |
} catch (Exception e) { |
|
194 |
log.error(e.toString()); |
|
195 |
log.error(e.getMessage()); |
|
196 |
// If we have not tried using a non-ClassLoader route, try it |
|
197 |
// now |
|
198 |
// as a last resort. |
|
199 |
if (!triedSU) { |
|
200 |
try { |
|
201 |
URL contextURL = (base != null) ? StringUtils.getURL( |
|
202 |
null, base) : null; |
|
203 |
URL url = StringUtils.getURL(contextURL, |
|
204 |
relativeLocation); |
|
205 |
importReader = new InputStreamReader(StringUtils |
|
206 |
.getContentAsInputStream(url)); |
|
207 |
importBase = (url == null) ? relativeLocation : url |
|
208 |
.toString(); |
|
209 |
} catch (Exception e2) { |
|
210 |
log.error(e2.toString()); |
|
211 |
log.error("Cannot find " + importBase + " so setting importBase to unknownImportURI"); |
|
212 |
// we can't find the import so set a temporary value for |
|
213 |
// the import URI. This is |
|
214 |
// necessary to avoid a NullPointerException in |
|
215 |
// WSDLReaderImpl |
|
216 |
importBase = "unknownImportURI"; |
|
217 |
} |
|
218 |
} else { |
|
219 |
log.error("Cannot find " + importBase + " so setting importBase to unknownImportURI"); |
|
220 |
// we can't find the import so set a temporary value for the |
|
221 |
// import URI. This is |
|
222 |
// necessary to avoid a NullPointerException in |
|
223 |
// WSDLReaderImpl |
|
224 |
importBase = "unknownImportURI"; |
|
225 |
} |
|
226 |
} |
|
227 |
|
|
228 |
return importReader; |
|
229 |
} |
|
230 |
|
|
231 |
/** |
|
232 |
* Resolve a path when the relative location begins with .. |
|
233 |
*/ |
|
234 |
private String resolvePath(String ba, String rel) { |
|
235 |
StringBuffer sb = new StringBuffer(rel); |
|
236 |
int dd = 0; |
|
237 |
while (sb.length() > 0) { |
|
238 |
if (sb.length() > 3 && sb.charAt(0) == '.' |
|
239 |
&& sb.charAt(1) == '.' |
|
240 |
&& (sb.charAt(2) == '/' || sb.charAt(2) == '\\')) { |
|
241 |
dd++; |
|
242 |
sb.delete(0, 3); |
|
243 |
} else { |
|
244 |
break; |
|
245 |
} |
|
246 |
} |
|
247 |
StringBuffer sb2 = new StringBuffer(ba); |
|
248 |
int j = sb2.length() - 1; |
|
249 |
int found = 0; |
|
250 |
for (int k = j; k >= 0; k--) { |
|
251 |
if (k != j && (sb2.charAt(k) == '/' || sb2.charAt(k) == '\\')) { |
|
252 |
found++; |
|
253 |
} |
|
254 |
if (found < dd) { |
|
255 |
sb2.deleteCharAt(k); |
|
256 |
} else { |
|
257 |
break; |
|
258 |
} |
|
259 |
} |
|
260 |
if (found + 1 < dd) { |
|
261 |
return null; |
|
262 |
} |
|
263 |
return sb2.toString() + sb.toString(); |
|
264 |
} |
|
265 |
|
|
266 |
/** |
|
267 |
* @see javax.wsdl.xml.WSDLLocator#getLatestImportURI() |
|
268 |
*/ |
|
269 |
@Override |
|
270 |
public String getLatestImportURI() { |
|
271 |
return importBase; |
|
272 |
} |
|
273 |
|
|
274 |
/** |
|
275 |
* @see javax.wsdl.xml.WSDLLocator#getBaseInputSource() |
|
276 |
*/ |
|
277 |
@Override |
|
278 |
public InputSource getBaseInputSource() { |
|
279 |
return new InputSource(getBaseReader()); |
|
280 |
} |
|
281 |
|
|
282 |
/** |
|
283 |
* @see javax.wsdl.xml.WSDLLocator#getImportInputSource(String, String) |
|
284 |
*/ |
|
285 |
@Override |
|
286 |
public InputSource getImportInputSource(String arg0, String arg1) { |
|
287 |
return new InputSource(getImportReader(arg0, arg1)); |
|
288 |
} |
|
289 |
|
|
290 |
@Override |
|
291 |
public void close() { |
|
292 |
if (baseReader != null) { |
|
293 |
try { |
|
294 |
baseReader.close(); |
|
295 |
} catch (IOException e) { |
|
296 |
// TODO Auto-generated catch block |
|
297 |
e.printStackTrace(); |
|
298 |
} |
|
299 |
} |
|
300 |
if (importReader != null) { |
|
301 |
try { |
|
302 |
importReader.close(); |
|
303 |
} catch (IOException e) { |
|
304 |
// TODO Auto-generated catch block |
|
305 |
e.printStackTrace(); |
|
306 |
} |
|
307 |
} |
|
308 |
} |
|
309 |
|
|
310 |
} |
|
1 |
/** |
|
2 |
* Copyright (C) 2009 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
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. |
|
8 |
*/ |
|
9 |
|
|
10 |
package eu.etaxonomy.cdm.model.common; |
|
11 |
|
|
12 |
import java.io.File; |
|
13 |
import java.io.IOException; |
|
14 |
import java.io.InputStream; |
|
15 |
import java.io.InputStreamReader; |
|
16 |
import java.io.Reader; |
|
17 |
import java.net.URL; |
|
18 |
|
|
19 |
import javax.wsdl.xml.WSDLLocator; |
|
20 |
|
|
21 |
import org.apache.logging.log4j.LogManager; |
|
22 |
import org.apache.logging.log4j.Logger; |
|
23 |
import org.xml.sax.InputSource; |
|
24 |
|
|
25 |
import com.ibm.wsdl.util.StringUtils; |
|
26 |
|
|
27 |
/** |
|
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. |
|
32 |
* |
|
33 |
* @author ben.clark |
|
34 |
* @author Owen Burroughs |
|
35 |
*/ |
|
36 |
public class LSIDWSDLLocator implements WSDLLocator { |
|
37 |
|
|
38 |
private static Logger logger = LogManager.getLogger(); |
|
39 |
|
|
40 |
private Reader baseReader = null; |
|
41 |
private Reader importReader = null; |
|
42 |
private String contextURI = null; |
|
43 |
private String wsdlLocation = null; |
|
44 |
private String documentBase = null; |
|
45 |
private String importBase = null; |
|
46 |
private ClassLoader loader = null; |
|
47 |
|
|
48 |
public LSIDWSDLLocator(String ctxt, String wsdlURI, ClassLoader cl) { |
|
49 |
contextURI = ctxt; |
|
50 |
wsdlLocation = wsdlURI; |
|
51 |
loader = cl; |
|
52 |
} |
|
53 |
|
|
54 |
public LSIDWSDLLocator(String docBase, Reader reader, ClassLoader cl) { |
|
55 |
documentBase = docBase; |
|
56 |
baseReader = reader; |
|
57 |
loader = cl; |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* @see javax.wsdl.xml.WSDLLocator#getBaseReader() |
|
62 |
*/ |
|
63 |
public Reader getBaseReader() { |
|
64 |
if (baseReader == null) { |
|
65 |
try { |
|
66 |
URL url = null; |
|
67 |
URL contextURL = (contextURI != null) ? StringUtils.getURL( |
|
68 |
null, contextURI) : null; |
|
69 |
if (loader != null) { |
|
70 |
InputStream in = null; |
|
71 |
try { |
|
72 |
if (contextURL != null) { |
|
73 |
url = new URL(contextURL, wsdlLocation); |
|
74 |
} else { |
|
75 |
if (wsdlLocation.indexOf(":") == -1) { |
|
76 |
url = new URL("file", null, wsdlLocation); |
|
77 |
} else { |
|
78 |
url = new URL(wsdlLocation); |
|
79 |
} |
|
80 |
} |
|
81 |
String wsdlRelativeLocation = url.getPath(); |
|
82 |
if (wsdlRelativeLocation.startsWith("/")) { |
|
83 |
wsdlRelativeLocation = wsdlRelativeLocation |
|
84 |
.substring(1); |
|
85 |
} |
|
86 |
in = loader |
|
87 |
.getResourceAsStream(wsdlRelativeLocation); |
|
88 |
baseReader = new InputStreamReader(in); |
|
89 |
} catch (Exception exc) { |
|
90 |
} |
|
91 |
} |
|
92 |
if (baseReader == null) { |
|
93 |
url = StringUtils.getURL(contextURL, wsdlLocation); |
|
94 |
baseReader = new InputStreamReader(StringUtils |
|
95 |
.getContentAsInputStream(url)); |
|
96 |
} |
|
97 |
if (url != null) { |
|
98 |
documentBase = url.toString(); |
|
99 |
} |
|
100 |
} catch (Exception e) { |
|
101 |
documentBase = wsdlLocation; |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
return baseReader; |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* @see javax.wsdl.xml.WSDLLocator#getBaseURI() |
|
110 |
*/ |
|
111 |
@Override |
|
112 |
public String getBaseURI() { |
|
113 |
return documentBase; |
|
114 |
} |
|
115 |
|
|
116 |
/** |
|
117 |
* used to read imports as a document is parsed |
|
118 |
* |
|
119 |
* @see javax.wsdl.xml.WSDLLocator#getImportReader(String, String) |
|
120 |
*/ |
|
121 |
public Reader getImportReader(String base, String relativeLocation) { |
|
122 |
// Reset importReader if finding import within import |
|
123 |
importReader = null; |
|
124 |
boolean triedSU = false; |
|
125 |
try { |
|
126 |
// If a ClassLoader was used to load the base |
|
127 |
// document, chances |
|
128 |
// are we need to use it to find the import. |
|
129 |
URL url = null; |
|
130 |
if (loader != null) { |
|
131 |
if (relativeLocation.startsWith("/") |
|
132 |
|| relativeLocation.startsWith("\\")) { |
|
133 |
// Relative location has been specified from a root dir. |
|
134 |
// However, |
|
135 |
// using a ClassLoader, root dirs don't mean anything. |
|
136 |
relativeLocation = relativeLocation.substring(1,relativeLocation.length()); |
|
137 |
InputStream in = loader.getResourceAsStream(relativeLocation); |
|
138 |
importReader = new InputStreamReader(in); |
|
139 |
} else if (relativeLocation.indexOf("://") != -1) { |
|
140 |
// This is a fully specified URL of some kind so don't |
|
141 |
// use the |
|
142 |
// ClassLoader to find the import. |
|
143 |
triedSU = true; |
|
144 |
url = StringUtils.getURL(null, relativeLocation); |
|
145 |
importReader = new InputStreamReader(StringUtils |
|
146 |
.getContentAsInputStream(url)); |
|
147 |
} else { |
|
148 |
// Import location has been specified relative to the |
|
149 |
// base document |
|
150 |
// and so we can to try to form the complete path to it. |
|
151 |
if (base != null) { |
|
152 |
int i = base.lastIndexOf("/"); |
|
153 |
if (i == -1) { |
|
154 |
i = base.lastIndexOf("\\"); |
|
155 |
} |
|
156 |
if (i != -1) { |
|
157 |
String path = base.substring(0, i + 1); |
|
158 |
String resolvedPath = path + relativeLocation; |
|
159 |
if (relativeLocation.startsWith("..")) { |
|
160 |
resolvedPath = resolvePath(path, |
|
161 |
relativeLocation); |
|
162 |
} |
|
163 |
if (resolvedPath == null) { |
|
164 |
throw new Exception("Invalid Path"); |
|
165 |
} |
|
166 |
|
|
167 |
// Make sure that resolved path starts with |
|
168 |
// file: |
|
169 |
if (resolvedPath.startsWith("file:")) { |
|
170 |
url = new URL(null, resolvedPath); |
|
171 |
} else { |
|
172 |
url = new URL(null, "file:" + resolvedPath); |
|
173 |
} |
|
174 |
} else { |
|
175 |
url = new URL(null, "file:" + base + File.separator + relativeLocation); |
|
176 |
} |
|
177 |
InputStream in = loader.getResourceAsStream(url.getPath()); |
|
178 |
importReader = new InputStreamReader(in); |
|
179 |
} else { |
|
180 |
url = new URL(null, "file:" + relativeLocation); |
|
181 |
InputStream in = loader.getResourceAsStream(url.getPath()); |
|
182 |
importReader = new InputStreamReader(in); |
|
183 |
} |
|
184 |
} |
|
185 |
} else { |
|
186 |
triedSU = true; |
|
187 |
URL contextURL = (base != null) ? StringUtils.getURL(null, |
|
188 |
base) : null; |
|
189 |
url = StringUtils.getURL(contextURL, relativeLocation); |
|
190 |
importReader = new InputStreamReader(StringUtils |
|
191 |
.getContentAsInputStream(url)); |
|
192 |
} |
|
193 |
importBase = (url == null) ? relativeLocation : url.toString(); |
|
194 |
} catch (Exception e) { |
|
195 |
logger.error(e.toString()); |
|
196 |
logger.error(e.getMessage()); |
|
197 |
// If we have not tried using a non-ClassLoader route, try it |
|
198 |
// now |
|
199 |
// as a last resort. |
|
200 |
if (!triedSU) { |
|
201 |
try { |
|
202 |
URL contextURL = (base != null) ? StringUtils.getURL( |
|
203 |
null, base) : null; |
|
204 |
URL url = StringUtils.getURL(contextURL, |
|
205 |
relativeLocation); |
|
206 |
importReader = new InputStreamReader(StringUtils |
|
207 |
.getContentAsInputStream(url)); |
|
208 |
importBase = (url == null) ? relativeLocation : url |
|
209 |
.toString(); |
|
210 |
} catch (Exception e2) { |
|
211 |
logger.error(e2.toString()); |
|
212 |
logger.error("Cannot find " + importBase + " so setting importBase to unknownImportURI"); |
|
213 |
// we can't find the import so set a temporary value for |
|
214 |
// the import URI. This is |
|
215 |
// necessary to avoid a NullPointerException in |
|
216 |
// WSDLReaderImpl |
|
217 |
importBase = "unknownImportURI"; |
|
218 |
} |
|
219 |
} else { |
|
220 |
logger.error("Cannot find " + importBase + " so setting importBase to unknownImportURI"); |
|
221 |
// we can't find the import so set a temporary value for the |
|
222 |
// import URI. This is |
|
223 |
// necessary to avoid a NullPointerException in |
|
224 |
// WSDLReaderImpl |
|
225 |
importBase = "unknownImportURI"; |
|
226 |
} |
|
227 |
} |
|
228 |
|
|
229 |
return importReader; |
|
230 |
} |
|
231 |
|
|
232 |
/** |
|
233 |
* Resolve a path when the relative location begins with .. |
|
234 |
*/ |
|
235 |
private String resolvePath(String ba, String rel) { |
|
236 |
StringBuffer sb = new StringBuffer(rel); |
|
237 |
int dd = 0; |
|
238 |
while (sb.length() > 0) { |
|
239 |
if (sb.length() > 3 && sb.charAt(0) == '.' |
|
240 |
&& sb.charAt(1) == '.' |
|
241 |
&& (sb.charAt(2) == '/' || sb.charAt(2) == '\\')) { |
|
242 |
dd++; |
|
243 |
sb.delete(0, 3); |
|
244 |
} else { |
|
245 |
break; |
|
246 |
} |
|
247 |
} |
|
248 |
StringBuffer sb2 = new StringBuffer(ba); |
|
249 |
int j = sb2.length() - 1; |
|
250 |
int found = 0; |
|
251 |
for (int k = j; k >= 0; k--) { |
|
252 |
if (k != j && (sb2.charAt(k) == '/' || sb2.charAt(k) == '\\')) { |
|
253 |
found++; |
|
254 |
} |
|
255 |
if (found < dd) { |
|
256 |
sb2.deleteCharAt(k); |
|
257 |
} else { |
|
258 |
break; |
|
259 |
} |
|
260 |
} |
|
261 |
if (found + 1 < dd) { |
|
262 |
return null; |
|
263 |
} |
|
264 |
return sb2.toString() + sb.toString(); |
|
265 |
} |
|
266 |
|
|
267 |
/** |
|
268 |
* @see javax.wsdl.xml.WSDLLocator#getLatestImportURI() |
|
269 |
*/ |
|
270 |
@Override |
|
271 |
public String getLatestImportURI() { |
|
272 |
return importBase; |
|
273 |
} |
|
274 |
|
|
275 |
/** |
|
276 |
* @see javax.wsdl.xml.WSDLLocator#getBaseInputSource() |
|
277 |
*/ |
|
278 |
@Override |
|
279 |
public InputSource getBaseInputSource() { |
|
280 |
return new InputSource(getBaseReader()); |
|
281 |
} |
|
282 |
|
|
283 |
/** |
|
284 |
* @see javax.wsdl.xml.WSDLLocator#getImportInputSource(String, String) |
|
285 |
*/ |
|
286 |
@Override |
|
287 |
public InputSource getImportInputSource(String arg0, String arg1) { |
|
288 |
return new InputSource(getImportReader(arg0, arg1)); |
|
289 |
} |
|
290 |
|
|
291 |
@Override |
|
292 |
public void close() { |
|
293 |
if (baseReader != null) { |
|
294 |
try { |
|
295 |
baseReader.close(); |
|
296 |
} catch (IOException e) { |
|
297 |
// TODO Auto-generated catch block |
|
298 |
e.printStackTrace(); |
|
299 |
} |
|
300 |
} |
|
301 |
if (importReader != null) { |
|
302 |
try { |
|
303 |
importReader.close(); |
|
304 |
} catch (IOException e) { |
|
305 |
// TODO Auto-generated catch block |
|
306 |
e.printStackTrace(); |
|
307 |
} |
|
308 |
} |
|
309 |
} |
|
310 |
|
|
311 |
} |
cdmlib-remote/pom.xml | ||
---|---|---|
89 | 89 |
<groupId>org.springframework</groupId> |
90 | 90 |
<artifactId>spring-mock</artifactId> |
91 | 91 |
</exclusion> |
92 |
<exclusion>
|
|
92 |
<exclusion> |
|
93 | 93 |
<groupId>log4j</groupId> |
94 | 94 |
<artifactId>log4j</artifactId> |
95 | 95 |
</exclusion> |
96 |
<exclusion> |
|
97 |
<groupId>commons-logging</groupId> |
|
98 |
<artifactId>commons-logging</artifactId> |
|
99 |
</exclusion> |
|
96 | 100 |
</exclusions> |
97 | 101 |
</dependency> |
98 | 102 |
<dependency> |
cdmlib-test/pom.xml | ||
---|---|---|
100 | 100 |
<groupId>org.unitils</groupId> |
101 | 101 |
<artifactId>unitils-core</artifactId> |
102 | 102 |
<scope>compile</scope> |
103 |
<exclusions> |
|
104 |
<exclusion> |
|
105 |
<!-- we use jcl-over-slf4j instead (see below) --> |
|
106 |
<groupId>commons-logging</groupId> |
|
107 |
<artifactId>commons-logging</artifactId> |
|
108 |
</exclusion> |
|
109 |
</exclusions> |
|
103 | 110 |
</dependency> |
111 |
<dependency> |
|
112 |
<!-- use as replacement for above commons-logging (https://www.slf4j.org/legacy.html) --> |
|
113 |
<groupId>org.slf4j</groupId> |
|
114 |
<artifactId>jcl-over-slf4j</artifactId> |
|
115 |
</dependency> |
|
116 |
|
|
104 | 117 |
<dependency> |
105 | 118 |
<groupId>org.unitils</groupId> |
106 | 119 |
<artifactId>unitils-database</artifactId> |
pom.xml | ||
---|---|---|
41 | 41 |
<doxia.version>1.11.1</doxia.version> <!-- checked 2021-12 --> |
42 | 42 |
<poi.version>5.2.2</poi.version> <!-- checked 2022-06 --> |
43 | 43 |
<jackson.version>2.13.3</jackson.version> <!-- checked 2022-05 --> |
44 |
<commons-logging.version>1.2</commons-logging.version> <!-- checked 2022-06 --> |
|
45 | 44 |
<!-- CAUTION when upgrading, 1.8x requires another log4j binding: https://logging.apache.org/log4j/2.x/log4j-slf4j-impl/index.html --> |
46 | 45 |
<slf4j.version>1.7.36</slf4j.version> <!-- checked 2022-06 --> |
47 | 46 |
<log4j.version>2.18.0</log4j.version> |
... | ... | |
940 | 939 |
<groupId>org.apache.httpcomponents</groupId> |
941 | 940 |
<artifactId>httpclient</artifactId> |
942 | 941 |
<version>${httpcomponents.version}</version> |
942 |
<exclusions> |
|
943 |
<exclusion> |
|
944 |
<!-- we use jcl-over-slf4j instead --> |
|
945 |
<groupId>commons-logging</groupId> |
|
946 |
<artifactId>commons-logging</artifactId> |
|
947 |
</exclusion> |
|
948 |
</exclusions> |
|
943 | 949 |
</dependency> |
944 | 950 |
<dependency> |
945 | 951 |
<groupId>org.apache.httpcomponents</groupId> |
... | ... | |
967 | 973 |
<artifactId>commons-codec</artifactId> |
968 | 974 |
<version>1.15</version> |
969 | 975 |
</dependency> |
970 |
<!-- only for version management, httpcore:4.2.4, commons-beanutils:1.8.3, |
|
971 |
httpclient:4.2.3, spring-modules-cache |
|
972 |
require 1.1 and batik-ext requires 1.0.4, so we update here to the latest |
|
973 |
version 1.1.2, poi and springframework require 1.1.3 --> |
|
974 |
<dependency> |
|
975 |
<groupId>commons-logging</groupId> |
|
976 |
<artifactId>commons-logging</artifactId> |
|
977 |
<version>${commons-logging.version}</version> |
|
978 |
</dependency> |
|
979 | 976 |
<!-- only for version management, hibernate-commons-annotations:4.0.1.Final |
980 | 977 |
requires 3.1.0.CR2 , so we update |
981 | 978 |
here to the latest version 3.4.1.Final --> |
... | ... | |
1027 | 1024 |
<version>2.4</version> |
1028 | 1025 |
<!-- classifier required as json-lib exists on maven central as json-lib-2.4-jdk15.jar and xxx-jdk13.jar, see #9887 --> |
1029 | 1026 |
<classifier>jdk15</classifier> |
1027 |
<exclusions> |
|
1028 |
<exclusion> |
|
1029 |
<!-- we use jcl-over-slf4j instead --> |
|
1030 |
<groupId>commons-logging</groupId> |
|
1031 |
<artifactId>commons-logging</artifactId> |
|
1032 |
</exclusion> |
|
1033 |
</exclusions> |
|
1030 | 1034 |
</dependency> |
1031 | 1035 |
<!-- media --> |
1032 | 1036 |
<dependency> |
... | ... | |
1421 | 1425 |
<groupId>org.springframework</groupId> |
1422 | 1426 |
<artifactId>spring-core</artifactId> |
1423 | 1427 |
<version>${spring.version}</version> |
1428 |
<exclusions> |
|
1429 |
<exclusion> |
|
1430 |
<groupId>commons-logging</groupId> |
|
1431 |
<artifactId>commons-logging</artifactId> |
|
1432 |
</exclusion> |
|
1433 |
</exclusions> |
|
1424 | 1434 |
</dependency> |
1425 | 1435 |
<dependency> |
1426 | 1436 |
<groupId>org.springframework</groupId> |
Also available in: Unified diff
ref #10072, ref #9359, ref #10095 replace commons-logging by jcl-over-slf4j and adapt some loggers to log4j