diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-11 05:13:15 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-11 05:13:15 +0000 |
commit | da54af6fa3cbdc88a5c07451697e071710a500ad (patch) | |
tree | 7a425b53d8439c8eecc7f9f85df14e6b73514e6a /java/sca | |
parent | d4bbe6fa031a26a4b3d86f455174a6cc0ed1cdd3 (diff) |
Improve the XML schema validation to enable XSDs in other modules
Fix an issue to write to XMLStreamWriter from SUN JDK 1.6
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@802988 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
27 files changed, 313 insertions, 460 deletions
diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java index 4034ba3612..dbce9d2d8d 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java @@ -211,7 +211,7 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { * @throws XMLStreamException */ protected void writeStart(XMLStreamWriter writer, String name, XAttr... attrs) throws XMLStreamException { - writeStart(writer, SCA11_NS, name, attrs); + super.writeStart(writer, SCA11_NS, name, attrs); } /** @@ -220,10 +220,7 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { * @throws XMLStreamException */ protected void writeStartDocument(XMLStreamWriter writer, String name, XAttr... attrs) throws XMLStreamException { - writer.writeStartDocument(); - writer.setDefaultNamespace(SCA11_NS); - writeStart(writer, SCA11_NS, name, attrs); - writer.writeDefaultNamespace(SCA11_NS); + super.writeStartDocument(writer, SCA11_NS, name, attrs); } /** diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java index a4f0da5ae9..15754b32e4 100644 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java @@ -25,7 +25,6 @@ import static org.junit.Assert.assertNotNull; import java.io.ByteArrayOutputStream; import java.io.StringReader; -import javax.xml.namespace.QName; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamReader; @@ -46,11 +45,8 @@ import org.junit.Test; */ public class ReadWriteAnyAttributeTestCase { - private static final QName EXTENDED_ATTRIBUTE = new QName("http://test", "customAttribute"); - private static final String XML = "<?xml version='1.0' encoding='UTF-8'?>"+ "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" " + - "xmlns:ns1=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" " + "targetNamespace=\"http://calc\" " + "name=\"Calculator\">"+ "<component name=\"AddServiceComponent\" xmlns:test=\"http://test\" test:customAttribute=\"customValue\">"+ diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyElementTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyElementTestCase.java index 256c706035..0b04c43ad5 100644 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyElementTestCase.java +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyElementTestCase.java @@ -43,7 +43,7 @@ import org.junit.Test; public class ReadWriteAnyElementTestCase { private static final String XML_RECURSIVE_EXTENDED_ELEMENT = "<?xml version='1.0' encoding='UTF-8'?>" + - "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" xmlns:ns1=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://temp\" name=\"RecursiveExtendedElement\">" + + "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://temp\" name=\"RecursiveExtendedElement\">" + "<unknownElement>" + "<subUnknownElement1 attribute=\"anyAttribute\" />" + "<subUnknownElement2 />" + @@ -52,7 +52,7 @@ public class ReadWriteAnyElementTestCase { private static final String XML_UNKNOWN_IMPL = "<?xml version='1.0' encoding='UTF-8'?>" + - "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" xmlns:ns1=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://temp\" name=\"aaaa\" autowire=\"false\">" + + "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://temp\" name=\"aaaa\" autowire=\"false\">" + "<component name=\"unknownImpl\">" + "<implementation.unknown class=\"raymond\" />" + "<service name=\"service\">" + @@ -63,7 +63,7 @@ public class ReadWriteAnyElementTestCase { private static final String XML_UNKNOWN_IMPL_WITH_INVALID_ATTRIBUTE = "<?xml version='1.0' encoding='UTF-8'?>" + - "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" xmlns:ns1=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://temp\" name=\"aaaa\" autowire=\"false\">" + + "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://temp\" name=\"aaaa\" autowire=\"false\">" + "<component name=\"unknownImpl\">" + "<implementation.unknown class=\"raymond\" />" + "<service name=\"service\" requires=\"\">" + diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteLocalCompositeTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteLocalCompositeTestCase.java index bd21c0630e..e86da1c8b4 100644 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteLocalCompositeTestCase.java +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteLocalCompositeTestCase.java @@ -52,7 +52,7 @@ public class ReadWriteLocalCompositeTestCase { private ExtensibleStAXArtifactProcessor staxProcessor; private static final String LOCAL_COMPOSITE_XML = "<?xml version='1.0' encoding='UTF-8'?>"+ - "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" xmlns:ns1=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://localcalc\" name=\"LocalCalculator\" local=\"true\">"+ + "<composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://localcalc\" name=\"LocalCalculator\" local=\"true\">"+ "</composite>"; @Before diff --git a/java/sca/modules/assembly-xsd/src/main/java/org/apache/tuscany/sca/assembly/xsd/Constants.java b/java/sca/modules/assembly-xsd/src/main/java/org/apache/tuscany/sca/assembly/xsd/Constants.java index b265eb4c07..7498834a2c 100644 --- a/java/sca/modules/assembly-xsd/src/main/java/org/apache/tuscany/sca/assembly/xsd/Constants.java +++ b/java/sca/modules/assembly-xsd/src/main/java/org/apache/tuscany/sca/assembly/xsd/Constants.java @@ -19,6 +19,10 @@ package org.apache.tuscany.sca.assembly.xsd; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + import javax.xml.namespace.QName; /** @@ -98,4 +102,40 @@ public interface Constants { String ZERO_N = "0..n"; String ONE_ONE = "1..1"; String ONE_N = "1..n"; + + /** + * Cache for public XSDs and DTDs + */ + Map<String, URL> CACHED_XSDS = XSDCache.cache(); + + static class XSDCache { + static Map<String, URL> cache() { + Map<String, URL> cachedXSDs = new HashMap<String, URL>(); + cachedXSDs.put(Constants.SCA11_NS, Constants.class.getResource("/sca-1.1-cd04.xsd")); + cachedXSDs + .put("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", + Constants.class + .getResource("/org/apache/tuscany/sca/assembly/xsd/oasis-200401-wss-wssecurity-secext-1.0.xsd")); + cachedXSDs + .put("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", + Constants.class + .getResource("/org/apache/tuscany/sca/assembly/xsd/oasis-200401-wss-wssecurity-utility-1.0.xsd")); + cachedXSDs.put("http://www.w3.org/2005/08/addressing", Constants.class + .getResource("/org/apache/tuscany/sca/assembly/xsd/ws-addr.xsd")); + cachedXSDs.put("http://www.w3.org/ns/ws-policy", Constants.class + .getResource("/org/apache/tuscany/sca/assembly/xsd/ws-policy.xsd")); + cachedXSDs.put("http://www.w3.org/ns/wsdl-instance", Constants.class + .getResource("/org/apache/tuscany/sca/assembly/xsd/wsdli.xsd")); + cachedXSDs.put("http://www.w3.org/XML/1998/namespace", Constants.class + .getResource("/org/apache/tuscany/sca/assembly/xsd/xml.xsd")); + cachedXSDs.put("http://www.w3.org/2000/09/xmldsig#", Constants.class + .getResource("/org/apache/tuscany/sca/assembly/xsd/xmldsig-core-schema.xsd")); + + cachedXSDs.put("-//W3C//DTD XMLSCHEMA 200102//EN", Constants.class + .getResource("/org/apache/tuscany/sca/assembly/xsd/XMLSchema.dtd")); + cachedXSDs.put("datatypes", Constants.class + .getResource("/org/apache/tuscany/sca/assembly/xsd/datatypes.dtd")); + return cachedXSDs; + } + }; } diff --git a/java/sca/modules/binding-sca-runtime/src/test/java/org/apace/tuscany/sca/binding/sca/xml/WriteTestCase.java b/java/sca/modules/binding-sca-runtime/src/test/java/org/apace/tuscany/sca/binding/sca/xml/WriteTestCase.java index 6073094249..e40fc34fbc 100644 --- a/java/sca/modules/binding-sca-runtime/src/test/java/org/apace/tuscany/sca/binding/sca/xml/WriteTestCase.java +++ b/java/sca/modules/binding-sca-runtime/src/test/java/org/apace/tuscany/sca/binding/sca/xml/WriteTestCase.java @@ -66,7 +66,7 @@ public class WriteTestCase { assertNotNull(componentType); ByteArrayOutputStream bos = new ByteArrayOutputStream(); staxProcessor.write(componentType, outputFactory.createXMLStreamWriter(bos)); - assertEquals("<?xml version='1.0' encoding='UTF-8'?><componentType xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" xmlns:ns1=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\"><service name=\"CalculatorService\"><binding.sca /><interface.java interface=\"calculator.CalculatorService\" /></service><reference name=\"addService\"><binding.sca /><interface.java interface=\"calculator.AddService\" /></reference></componentType>", + assertEquals("<?xml version='1.0' encoding='UTF-8'?><componentType xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" ><service name=\"CalculatorService\"><binding.sca /><interface.java interface=\"calculator.CalculatorService\" /></service><reference name=\"addService\"><binding.sca /><interface.java interface=\"calculator.AddService\" /></reference></componentType>", bos.toString()); } @@ -78,7 +78,7 @@ public class WriteTestCase { assertNotNull(composite); ByteArrayOutputStream bos = new ByteArrayOutputStream(); staxProcessor.write(composite, outputFactory.createXMLStreamWriter(bos)); - assertEquals("<?xml version='1.0' encoding='UTF-8'?><composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" xmlns:ns1=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://calc\" name=\"Calculator\"><service name=\"CalculatorService\" promote=\"CalculatorServiceComponent\"><binding.sca /><interface.java interface=\"calculator.CalculatorService\" /></service><component name=\"CalculatorServiceComponent\"><implementation.java class=\"calculator.CalculatorServiceImpl\" /><reference name=\"addService\" target=\"AddServiceComponent\"><binding.sca /></reference><reference name=\"subtractService\" target=\"SubtractServiceComponent\" /><reference name=\"multiplyService\" target=\"MultiplyServiceComponent\" /><reference name=\"divideService\" target=\"DivideServiceComponent\" /></component><component name=\"AddServiceComponent\"><implementation.java class=\"calculator.AddServiceImpl\" /><service name=\"AddService\"><binding.sca /><interface.java interface=\"calculator.AddService\" /></service></component><component name=\"SubtractServiceComponent\"><implementation.java class=\"calculator.SubtractServiceImpl\" /></component><component name=\"MultiplyServiceComponent\"><implementation.java class=\"calculator.MultiplyServiceImpl\" /></component><component name=\"DivideServiceComponent\"><implementation.java class=\"calculator.DivideServiceImpl\" /></component></composite>", + assertEquals("<?xml version='1.0' encoding='UTF-8'?><composite xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\" targetNamespace=\"http://calc\" name=\"Calculator\"><service name=\"CalculatorService\" promote=\"CalculatorServiceComponent\"><binding.sca /><interface.java interface=\"calculator.CalculatorService\" /></service><component name=\"CalculatorServiceComponent\"><implementation.java class=\"calculator.CalculatorServiceImpl\" /><reference name=\"addService\" target=\"AddServiceComponent\"><binding.sca /></reference><reference name=\"subtractService\" target=\"SubtractServiceComponent\" /><reference name=\"multiplyService\" target=\"MultiplyServiceComponent\" /><reference name=\"divideService\" target=\"DivideServiceComponent\" /></component><component name=\"AddServiceComponent\"><implementation.java class=\"calculator.AddServiceImpl\" /><service name=\"AddService\"><binding.sca /><interface.java interface=\"calculator.AddService\" /></service></component><component name=\"SubtractServiceComponent\"><implementation.java class=\"calculator.SubtractServiceImpl\" /></component><component name=\"MultiplyServiceComponent\"><implementation.java class=\"calculator.MultiplyServiceImpl\" /></component><component name=\"DivideServiceComponent\"><implementation.java class=\"calculator.DivideServiceImpl\" /></component></composite>", bos.toString()); } diff --git a/java/sca/modules/binding-ws-axis2/META-INF/MANIFEST.MF b/java/sca/modules/binding-ws-axis2/META-INF/MANIFEST.MF index 039176856c..ae68eec237 100644 --- a/java/sca/modules/binding-ws-axis2/META-INF/MANIFEST.MF +++ b/java/sca/modules/binding-ws-axis2/META-INF/MANIFEST.MF @@ -76,13 +76,7 @@ Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt Bundle-Description: Apache Tuscany SCA Axis2-based WS Binding Extensio
n
Eclipse-RegisterBuddy: org.apache.axis2.kernel
-Import-Package: javax.xml.stream,
- javax.xml.transform,
- javax.xml.transform.dom,
- javax.xml.parsers,
- org.w3c.dom,
- javax.activation;resolution:=optional,
- org.xml.sax,
+Import-Package: javax.activation;resolution:=optional,
javax.security.auth.callback;resolution:=optional,
javax.servlet,
javax.servlet.http,
@@ -93,6 +87,10 @@ Import-Package: javax.xml.stream, javax.wsdl.factory;resolution:=optional,
javax.wsdl.xml;resolution:=optional,
javax.xml.namespace,
+ javax.xml.parsers,
+ javax.xml.stream,
+ javax.xml.transform,
+ javax.xml.transform.dom,
org.apache.axiom.om,
org.apache.axiom.om.impl.builder,
org.apache.axiom.om.util,
@@ -124,12 +122,13 @@ Import-Package: javax.xml.stream, org.apache.tuscany.sca.binding.ws.axis2.policy.authentication.token;version="2.0.0",
org.apache.tuscany.sca.binding.ws.axis2.policy.configuration;version="2.0.0",
org.apache.tuscany.sca.binding.ws.axis2.policy.header;version="2.0.0",
+ org.apache.tuscany.sca.common.xml;version="2.0.0",
org.apache.tuscany.sca.contribution.processor;version="2.0.0",
org.apache.tuscany.sca.core;version="2.0.0",
- org.apache.tuscany.sca.extensibility;version="2.0.0",
org.apache.tuscany.sca.core.assembly;version="2.0.0",
org.apache.tuscany.sca.databinding;version="2.0.0",
org.apache.tuscany.sca.definitions;version="2.0.0",
+ org.apache.tuscany.sca.extensibility;version="2.0.0",
org.apache.tuscany.sca.host.http;version="2.0.0",
org.apache.tuscany.sca.interfacedef;version="2.0.0",
org.apache.tuscany.sca.interfacedef.java;version="2.0.0",
@@ -150,7 +149,9 @@ Import-Package: javax.xml.stream, org.apache.ws.security,
org.apache.ws.security.handler,
org.oasisopen.sca;version="2.0.0",
- org.oasisopen.sca.annotation;version="2.0.0";resolution:=optional
+ org.oasisopen.sca.annotation;version="2.0.0";resolution:=optional,
+ org.w3c.dom,
+ org.xml.sax
Bundle-SymbolicName: org.apache.tuscany.sca.binding.ws.axis2
Bundle-DocURL: http://www.apache.org/
Bundle-RequiredExecutionEnvironment: J2SE-1.5,
diff --git a/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/Axis2ServiceClient.java b/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/Axis2ServiceClient.java index 6a44c875e3..be4e902eaa 100644 --- a/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/Axis2ServiceClient.java +++ b/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/Axis2ServiceClient.java @@ -70,13 +70,13 @@ import org.apache.commons.httpclient.params.HttpConnectionManagerParams; import org.apache.tuscany.sca.assembly.AbstractContract; import org.apache.tuscany.sca.binding.ws.WebServiceBinding; import org.apache.tuscany.sca.binding.ws.axis2.policy.configuration.Axis2ConfigParamPolicy; +import org.apache.tuscany.sca.common.xml.XMLDocumentHelper; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.MessageFactory; import org.apache.tuscany.sca.policy.PolicySet; import org.apache.tuscany.sca.policy.PolicySubject; import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.xsd.xml.XMLDocumentHelper; import org.apache.ws.commons.schema.resolver.URIResolver; public class Axis2ServiceClient { @@ -205,7 +205,7 @@ public class Axis2ServiceClient { baseUri = definition.getDocumentBaseURI(); } URL url = new URL(new URL(baseUri), schemaLocation); - return XMLDocumentHelper.getInputSource(url); + return XMLDocumentHelper.getInputSource(url); } catch (IOException e) { return null; } diff --git a/java/sca/modules/common-java/META-INF/MANIFEST.MF b/java/sca/modules/common-java/META-INF/MANIFEST.MF index ad66c6adb3..08d71266b7 100644 --- a/java/sca/modules/common-java/META-INF/MANIFEST.MF +++ b/java/sca/modules/common-java/META-INF/MANIFEST.MF @@ -14,6 +14,7 @@ Import-Package: org.apache.tuscany.sca.core;version="2.0.0", org.oasisopen.sca.annotation;version="2.0.0"
Bundle-SymbolicName: org.apache.tuscany.sca.common.java
Bundle-DocURL: http://www.apache.org/
-Export-Package: org.apache.tuscany.sca.common.java.reflection;version="2.0.0"
+Export-Package: org.apache.tuscany.sca.common.java.io,
+ org.apache.tuscany.sca.common.java.reflection;version="2.0.0"
diff --git a/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/io/IOHelper.java b/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/io/IOHelper.java new file mode 100644 index 0000000000..94aa7220e1 --- /dev/null +++ b/java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/io/IOHelper.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.common.java.io; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.JarURLConnection; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; + +/** + * + */ +public class IOHelper { + public static InputStream openStream(URL url) throws IOException { + URLConnection connection = url.openConnection(); + if (connection instanceof JarURLConnection) { + // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041014 + connection.setUseCaches(false); + } + InputStream is = connection.getInputStream(); + return is; + } + + /** + * Escape the space in URL string + * @param uri + * @return + */ + public static URI createURI(String uri) { + if (uri == null) { + return null; + } + if (uri.indexOf('%') != -1) { + // Avoid double-escaping + return URI.create(uri); + } + int index = uri.indexOf(':'); + String scheme = null; + String ssp = uri; + if (index != -1) { + scheme = uri.substring(0, index); + ssp = uri.substring(index + 1); + } + try { + return new URI(scheme, ssp, null); + } catch (URISyntaxException e) { + throw new IllegalArgumentException(e); + } + } + + public static URI toURI(URL url) { + if (url == null) { + return null; + } + return createURI(url.toString()); + } + + /** + * Returns the File object representing the given URL. + * + * @param url + * @return + */ + public static File toFile(URL url) { + if (url == null || !url.getProtocol().equals("file")) { + return null; + } else { + String filename = url.getFile().replace('/', File.separatorChar); + int pos = 0; + while ((pos = filename.indexOf('%', pos)) >= 0) { + if (pos + 2 < filename.length()) { + String hexStr = filename.substring(pos + 1, pos + 3); + char ch = (char)Integer.parseInt(hexStr, 16); + filename = filename.substring(0, pos) + ch + filename.substring(pos + 3); + } + } + return new File(filename); + } + } + + /** + * Returns the location of the classpath entry, JAR, WAR etc. containing the given class. + * + * @param clazz + * @return + */ + public static URL codeLocation(Class<?> clazz) { + URL url = clazz.getProtectionDomain().getCodeSource().getLocation(); + if (url == null) { + url = clazz.getResource("/" + clazz.getName().replace('.', '/') + ".class"); + } + return url; + } + +} diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/XMLDocumentHelper.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/XMLDocumentHelper.java index d4dba04c01..33a97f4342 100644 --- a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/XMLDocumentHelper.java +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/XMLDocumentHelper.java @@ -140,10 +140,10 @@ public class XMLDocumentHelper { } public static InputSource getInputSource(URL url, InputStream is) throws IOException { - is = new BufferedInputStream(is); - String encoding = getEncoding(is); + // is = new BufferedInputStream(is); + // String encoding = getEncoding(is); InputSource inputSource = new InputSource(is); - inputSource.setEncoding(encoding); + // inputSource.setEncoding(encoding); // [rfeng] Make sure we set the system id as it will be used as the base URI for nested import/include inputSource.setSystemId(url.toString()); return inputSource; diff --git a/java/sca/modules/contribution/META-INF/MANIFEST.MF b/java/sca/modules/contribution/META-INF/MANIFEST.MF index 6d2c1e8380..f2f8b45d37 100644 --- a/java/sca/modules/contribution/META-INF/MANIFEST.MF +++ b/java/sca/modules/contribution/META-INF/MANIFEST.MF @@ -38,6 +38,8 @@ Import-Package: javax.xml.namespace, org.apache.tuscany.sca.assembly;version="2.0.0",
org.apache.tuscany.sca.assembly.impl;version="2.0.0",
org.apache.tuscany.sca.assembly.xsd;version="2.0.0";resolution:=optional,
+ org.apache.tuscany.sca.common.xml;version="2.0.0",
+ org.apache.tuscany.sca.common.xml.stax;version="2.0.0",
org.apache.tuscany.sca.contribution;version="2.0.0",
org.apache.tuscany.sca.contribution.java;version="2.0.0",
org.apache.tuscany.sca.contribution.namespace;version="2.0.0",
diff --git a/java/sca/modules/contribution/pom.xml b/java/sca/modules/contribution/pom.xml index 5443e2aab1..c020aa467c 100644 --- a/java/sca/modules/contribution/pom.xml +++ b/java/sca/modules/contribution/pom.xml @@ -42,24 +42,11 @@ </dependency> <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-stax-api_1.0_spec</artifactId> - <version>1.0.1</version> - </dependency> - - <dependency> - <groupId>org.codehaus.woodstox</groupId> - <artifactId>wstx-asl</artifactId> - <version>3.2.4</version> - <scope>runtime</scope> - <exclusions> - <exclusion> - <groupId>stax</groupId> - <artifactId>stax-api</artifactId> - </exclusion> - </exclusions> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-common-xml</artifactId> + <version>2.0-SNAPSHOT</version> </dependency> - + </dependencies> </project> diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java index b649ed6804..5f7095f80a 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java @@ -23,6 +23,7 @@ import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; @@ -189,7 +190,7 @@ public abstract class BaseStAXArtifactProcessor { * @param uri * @throws XMLStreamException */ - private String writeElementPrefix(XMLStreamWriter writer, String uri) throws XMLStreamException { + private String setPrefix(XMLStreamWriter writer, String uri) throws XMLStreamException { if (uri == null) { return null; } @@ -220,11 +221,15 @@ public abstract class BaseStAXArtifactProcessor { * @throws XMLStreamException */ protected void writeStart(XMLStreamWriter writer, String uri, String name, XAttr... attrs) throws XMLStreamException { - String prefix = writeElementPrefix(writer, uri); +// String prefix = setPrefix(writer, uri); writer.writeStartElement(uri, name); - if (prefix != null){ - writer.writeNamespace(prefix,uri); - } + + // [rfeng] When the XMLStreamWriter is in the repairing namespace mode, we should not try to write namespace + // as it will create duplicate namespace declarations + +// if (prefix != null){ +// writer.writeNamespace(prefix,uri); +// } writeAttributePrefixes(writer, attrs); writeAttributes(writer, attrs); } @@ -257,7 +262,7 @@ public abstract class BaseStAXArtifactProcessor { writer.writeStartDocument(); writer.setDefaultNamespace(uri); writeStart(writer, uri, name, attrs); - writer.writeDefaultNamespace(uri); + // writer.writeDefaultNamespace(uri); } /** @@ -535,10 +540,10 @@ public abstract class BaseStAXArtifactProcessor { // Write a QName str = writeQNameValue(writer, (QName)value); - } else if (value instanceof List) { + } else if (value instanceof Collection) { // Write a list of values - List<?> values = (List<?>)value; + Collection<?> values = (Collection<?>)value; if (values.isEmpty()) { return; } @@ -599,10 +604,10 @@ public abstract class BaseStAXArtifactProcessor { // Write prefix for a single QName value writeQNamePrefix(writer, (QName)value); - } else if (value instanceof List) { + } else if (value instanceof Collection) { // Write prefixes for a list of values - for (Object v: (List<?>)value) { + for (Object v: (Collection<?>)value) { if (v instanceof QName) { // Write prefix for a QName value writeQNamePrefix(writer, (QName)v); diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java index 8aac0ce3f6..f1ef36d0f9 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java @@ -22,16 +22,21 @@ package org.apache.tuscany.sca.contribution.processor; import java.io.IOException; import java.io.InputStream; import java.io.Reader; +import java.io.StringReader; import java.net.URL; -import java.net.URLConnection; import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import javax.xml.XMLConstants; +import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.EventFilter; @@ -44,10 +49,13 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.util.XMLEventAllocator; import javax.xml.transform.Source; -import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.sax.SAXSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; +import org.apache.tuscany.sca.assembly.xsd.Constants; +import org.apache.tuscany.sca.common.xml.XMLDocumentHelper; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.core.UtilityExtensionPoint; @@ -59,6 +67,7 @@ import org.w3c.dom.DOMImplementation; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSInput; import org.w3c.dom.ls.LSResourceResolver; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** @@ -77,6 +86,7 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory private boolean initialized; private boolean hasSchemas; private Schema aggregatedSchema; + private StAXHelper helper; public DefaultValidatingXMLInputFactory(ExtensionPointRegistry registry) { FactoryExtensionPoint factoryExtensionPoint = registry.getExtensionPoint(FactoryExtensionPoint.class); @@ -85,6 +95,7 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory this.schemas = registry.getExtensionPoint(ValidationSchemaExtensionPoint.class); this.monitor = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(MonitorFactory.class).createMonitor(); + this.helper = StAXHelper.getInstance(registry); } /** @@ -131,12 +142,50 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory monitor.problem(problem); } } + + public static final QName XSD = new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "schema"); + + private Collection<? extends Source> aggregate(URL... urls) throws IOException, XMLStreamException { + if (urls.length == 1) { + return Collections.singletonList(new SAXSource(XMLDocumentHelper.getInputSource(urls[0]))); + } + Map<String, Collection<URL>> map = new HashMap<String, Collection<URL>>(); + + for (URL url : urls) { + String tns = helper.readAttribute(url, XSD, "targetNamespace"); + Collection<URL> collection = map.get(tns); + if (collection == null) { + collection = new HashSet<URL>(); + map.put(tns, collection); + } + collection.add(url); + } + List<Source> sources = new ArrayList<Source>(); + for (Map.Entry<String, Collection<URL>> e : map.entrySet()) { + if (e.getValue().size() == 1) { + sources.add(new SAXSource(XMLDocumentHelper.getInputSource(e.getValue().iterator().next()))); + } else { + StringBuffer xsd = new StringBuffer("<schema xmlns=\"http://www.w3.org/2001/XMLSchema\""); + if (e.getKey() != null) { + xsd.append(" targetNamespace=\"").append(e.getKey()).append("\""); + } + xsd.append(">"); + for (URL url : e.getValue()) { + xsd.append("<include schemaLocation=\"").append(url).append("\"/>"); + } + xsd.append("</schema>"); + SAXSource source = new SAXSource(new InputSource(new StringReader(xsd.toString()))); + sources.add(source); + } + } + return sources; + } /** * Initialize the registered schemas and create an aggregated schema for * validation. */ - private void initializeSchemas() { + private synchronized void initializeSchemas() { if (initialized) { return; } @@ -151,41 +200,15 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory } else { hasSchemas = true; } - final Source[] sources = new Source[n]; - for (int i =0; i < n; i++) { - final String uri = uris.get(i); - // Allow privileged access to open URL stream. Requires FilePermission in security policy. - final URL url = new URL( uri ); - InputStream urlStream; - try { - urlStream = AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>() { - public InputStream run() throws IOException { - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); - return connection.getInputStream(); - } - }); - } catch (PrivilegedActionException e) { - error("PrivilegedActionException", url, (IOException)e.getException()); - throw (IOException)e.getException(); - } - sources[i] = new StreamSource(urlStream, uri); + + URL[] urls = new URL[uris.size()]; + for (int i = 0; i < urls.length; i++) { + urls[i] = new URL(uris.get(i)); } + final Collection<? extends Source> sources = aggregate(urls); // Create an aggregated validation schemas from all the XSDs final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - - /* - // Set the feature to avoid DTD processing - try { - schemaFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); - schemaFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); - schemaFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); - } catch (SAXException e) { - // Ignore - } - */ - DOMImplementation impl = null; try { impl = documentBuilderFactory.newDocumentBuilder().getDOMImplementation(); @@ -201,7 +224,7 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory try { aggregatedSchema = AccessController.doPrivileged(new PrivilegedExceptionAction<Schema>() { public Schema run() throws SAXException { - return schemaFactory.newSchema(sources); + return schemaFactory.newSchema(sources.toArray(new Source[sources.size()])); } }); } catch (PrivilegedActionException e) { @@ -371,36 +394,6 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory inputFactory.setXMLResolver(arg0); } - /** - * Cache for public XSDs and DTDs - */ - private static Map<String, URL> cachedXSDs = new HashMap<String, URL>(); - static { - cachedXSDs - .put("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", - DefaultValidatingXMLInputFactory.class - .getResource("/org/apache/tuscany/sca/assembly/xsd/oasis-200401-wss-wssecurity-secext-1.0.xsd")); - cachedXSDs - .put("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", - DefaultValidatingXMLInputFactory.class - .getResource("/org/apache/tuscany/sca/assembly/xsd/oasis-200401-wss-wssecurity-utility-1.0.xsd")); - cachedXSDs.put("http://www.w3.org/2005/08/addressing", DefaultValidatingXMLInputFactory.class - .getResource("/org/apache/tuscany/sca/assembly/xsd/ws-addr.xsd")); - cachedXSDs.put("http://www.w3.org/ns/ws-policy", DefaultValidatingXMLInputFactory.class - .getResource("/org/apache/tuscany/sca/assembly/xsd/ws-policy.xsd")); - cachedXSDs.put("http://www.w3.org/ns/wsdl-instance", DefaultValidatingXMLInputFactory.class - .getResource("/org/apache/tuscany/sca/assembly/xsd/wsdli.xsd")); - cachedXSDs.put("http://www.w3.org/XML/1998/namespace", DefaultValidatingXMLInputFactory.class - .getResource("/org/apache/tuscany/sca/assembly/xsd/xml.xsd")); - cachedXSDs.put("http://www.w3.org/2000/09/xmldsig#", DefaultValidatingXMLInputFactory.class - .getResource("/org/apache/tuscany/sca/assembly/xsd/xmldsig-core-schema.xsd")); - - cachedXSDs.put("-//W3C//DTD XMLSCHEMA 200102//EN", DefaultValidatingXMLInputFactory.class - .getResource("/org/apache/tuscany/sca/assembly/xsd/XMLSchema.dtd")); - cachedXSDs.put("datatypes", DefaultValidatingXMLInputFactory.class - .getResource("/org/apache/tuscany/sca/assembly/xsd/datatypes.dtd")); - }; - public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { String key = null; if("http://www.w3.org/2001/XMLSchema".equals(type)) { @@ -408,10 +401,12 @@ public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory } else if("http://www.w3.org/TR/REC-xml".equals(type)) { key = publicId; } - URL url = cachedXSDs.get(key); - if (url != null) { + URL url = Constants.CACHED_XSDS.get(key); + if (url != null && !Constants.SCA11_NS.equals(namespaceURI)) { systemId = url.toString(); - } + } else if (url != null && systemId == null) { + systemId = url.toString(); + } LSInput input = ls.createLSInput(); input.setBaseURI(baseURI); diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java index c21c4c6a44..15cc7b043b 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java @@ -72,7 +72,7 @@ public class ExtensibleStAXArtifactProcessor implements StAXArtifactProcessor<Ob this.inputFactory = inputFactory; this.outputFactory = outputFactory; if (this.outputFactory != null) { - this.outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE); + this.outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); } this.monitor = monitor; } diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java index d8b66562a2..a875689e1a 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXAttributeProcessor.java @@ -75,7 +75,7 @@ public class ExtensibleStAXAttributeProcessor implements StAXAttributeProcessor< this.inputFactory = inputFactory; this.outputFactory = outputFactory; if (this.outputFactory != null) { - this.outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE); + this.outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); } this.monitor = monitor; } diff --git a/java/sca/modules/interface-wsdl/META-INF/MANIFEST.MF b/java/sca/modules/interface-wsdl/META-INF/MANIFEST.MF index 06fd255ade..15fab8fc0b 100644 --- a/java/sca/modules/interface-wsdl/META-INF/MANIFEST.MF +++ b/java/sca/modules/interface-wsdl/META-INF/MANIFEST.MF @@ -24,6 +24,8 @@ Import-Package: javax.wsdl, javax.xml.stream,
org.apache.tuscany.sca.assembly;version="2.0.0",
org.apache.tuscany.sca.assembly.builder;version="2.0.0";resolution:=optional,
+ org.apache.tuscany.sca.common.xml;version="2.0.0",
+ org.apache.tuscany.sca.common.xml.stax;version="2.0.0",
org.apache.tuscany.sca.contribution;version="2.0.0";resolution:=optional,
org.apache.tuscany.sca.contribution.namespace;version="2.0.0",
org.apache.tuscany.sca.contribution.processor;version="2.0.0";resolution:=optional,
diff --git a/java/sca/modules/interface-wsdl/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java b/java/sca/modules/interface-wsdl/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java index 11e8472166..10c909a548 100644 --- a/java/sca/modules/interface-wsdl/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java +++ b/java/sca/modules/interface-wsdl/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java @@ -20,7 +20,6 @@ package org.apache.tuscany.sca.interfacedef.wsdl.xml; import java.io.IOException; -import java.io.InputStream; import java.net.URI; import java.net.URL; import java.util.List; @@ -29,14 +28,16 @@ import javax.wsdl.Definition; import javax.wsdl.Import; import javax.xml.namespace.QName; import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper.Attribute; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; @@ -58,16 +59,18 @@ public class WSDLDocumentProcessor implements URLArtifactProcessor<WSDLDefinitio public static final QName XSD = new QName("http://www.w3.org/2001/XMLSchema", "schema"); private XMLInputFactory inputFactory; - + private StAXHelper helper; private WSDLFactory factory; private XSDFactory xsdFactory; private Monitor monitor; - public WSDLDocumentProcessor(FactoryExtensionPoint modelFactories, Monitor monitor) { + public WSDLDocumentProcessor(ExtensionPointRegistry registry, StAXArtifactProcessor processor, Monitor monitor) { + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); this.factory = modelFactories.getFactory(WSDLFactory.class); this.xsdFactory = modelFactories.getFactory(XSDFactory.class); this.inputFactory = modelFactories.getFactory(XMLInputFactory.class); this.monitor = monitor; + this.helper = StAXHelper.getInstance(registry); } /** @@ -191,43 +194,28 @@ public class WSDLDocumentProcessor implements URLArtifactProcessor<WSDLDefinitio wsdlDefinition.setUnresolved(true); wsdlDefinition.setLocation(doc.toURI()); - InputStream is = doc.openStream(); - try { - XMLStreamReader reader = inputFactory.createXMLStreamReader(is); - int eventType = reader.getEventType(); - int index = 0; - while (true) { - if (eventType == XMLStreamConstants.START_ELEMENT) { - if (WSDL11.equals(reader.getName())) { - String tns = reader.getAttributeValue(null, "targetNamespace"); - wsdlDefinition.setNamespace(tns); - // The definition is marked as resolved but not loaded - wsdlDefinition.setUnresolved(false); - wsdlDefinition.setDefinition(null); - } - if (XSD.equals(reader.getName())) { - String tns = reader.getAttributeValue(null, "targetNamespace"); - XSDefinition xsd = xsdFactory.createXSDefinition(); - xsd.setUnresolved(true); - xsd.setNamespace(tns); - xsd.setLocation(URI.create(doc.toURI() + "#" + index)); - index++; - // The definition is marked as resolved but not loaded - xsd.setUnresolved(false); - xsd.setSchema(null); - wsdlDefinition.getXmlSchemas().add(xsd); - } - } - if (reader.hasNext()) { - eventType = reader.next(); - } else { - break; - } - } - return wsdlDefinition; - } finally { - is.close(); + Attribute attr1 = new Attribute(WSDL11, "targetNamespace"); + Attribute attr2 = new Attribute(XSD, "targetNamespace"); + Attribute[] attrs = helper.readAttributes(doc, attr1, attr2); + + wsdlDefinition.setNamespace(attr1.getValues().get(0)); + // The definition is marked as resolved but not loaded + wsdlDefinition.setUnresolved(false); + wsdlDefinition.setDefinition(null); + + int index = 0; + for (String tns : attr2.getValues()) { + XSDefinition xsd = xsdFactory.createXSDefinition(); + xsd.setUnresolved(true); + xsd.setNamespace(tns); + xsd.setLocation(URI.create(doc.toURI() + "#" + index)); + index++; + // The definition is marked as resolved but not loaded + xsd.setUnresolved(false); + xsd.setSchema(null); + wsdlDefinition.getXmlSchemas().add(xsd); } + return wsdlDefinition; } } diff --git a/java/sca/modules/interface-wsdl/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java b/java/sca/modules/interface-wsdl/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java index 2e14c4885c..6f6a5a2302 100644 --- a/java/sca/modules/interface-wsdl/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java +++ b/java/sca/modules/interface-wsdl/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java @@ -25,10 +25,10 @@ import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Collections; import javax.wsdl.Definition; import javax.wsdl.Operation; @@ -48,6 +48,7 @@ import javax.wsdl.xml.WSDLLocator; import javax.wsdl.xml.WSDLReader; import javax.xml.namespace.QName; +import org.apache.tuscany.sca.common.xml.XMLDocumentHelper; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; @@ -62,7 +63,6 @@ import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; import org.apache.tuscany.sca.xsd.XSDFactory; import org.apache.tuscany.sca.xsd.XSDefinition; -import org.apache.tuscany.sca.xsd.xml.XMLDocumentHelper; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -211,7 +211,7 @@ public class WSDLModelResolver implements ModelResolver { return null; } latestImportURI = url.toString(); - return XMLDocumentHelper.getInputSource(url); + return XMLDocumentHelper.getInputSource(url); } catch (Exception e) { throw new ContributionRuntimeException(e); } diff --git a/java/sca/modules/interface-wsdl/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelperTestCase.java b/java/sca/modules/interface-wsdl/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelperTestCase.java deleted file mode 100644 index 4558bafdb8..0000000000 --- a/java/sca/modules/interface-wsdl/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelperTestCase.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.interfacedef.wsdl.xml; - -import java.net.URL; - -import javax.xml.stream.XMLInputFactory; - -import org.apache.tuscany.sca.xsd.xml.XMLDocumentHelper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -/** - * @version $Rev$ $Date$ - */ -public class XMLDocumentHelperTestCase { - private URL wsdl; - private URL xsd; - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - wsdl = getClass().getResource("/wsdl/helloworld-interface.wsdl"); - } - - @Test - public void testReadTNS() throws Exception { - String tns = XMLDocumentHelper.readTargetNamespace(wsdl, XMLDocumentHelper.WSDL11, true, "targetNamespace", XMLInputFactory.newInstance()); - Assert.assertEquals("http://helloworld", tns); - } - -} diff --git a/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessorTestCase.java b/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessorTestCase.java index 6b5bce2d13..ff0144db99 100644 --- a/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessorTestCase.java +++ b/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessorTestCase.java @@ -67,6 +67,7 @@ public class NodeConfigurationProcessorTestCase { NodeConfiguration config = (NodeConfiguration) processor.read(reader); StringWriter sw = new StringWriter(); XMLOutputFactory xmlOutputFactory = factories.getFactory(XMLOutputFactory.class); + xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(sw); processor.write(config, writer); writer.flush(); diff --git a/java/sca/modules/xsd/META-INF/MANIFEST.MF b/java/sca/modules/xsd/META-INF/MANIFEST.MF index f88a2e4ad3..ab10d33283 100644 --- a/java/sca/modules/xsd/META-INF/MANIFEST.MF +++ b/java/sca/modules/xsd/META-INF/MANIFEST.MF @@ -29,6 +29,8 @@ Bundle-Description: Apache Tuscany SCA XSD Model Import-Package: javax.xml.namespace,
javax.xml.stream,
org.apache.tuscany.sca.assembly;version="2.0.0",
+ org.apache.tuscany.sca.common.xml;version="2.0.0",
+ org.apache.tuscany.sca.common.xml.stax;version="2.0.0",
org.apache.tuscany.sca.contribution;version="2.0.0",
org.apache.tuscany.sca.contribution.namespace;version="2.0.0",
org.apache.tuscany.sca.contribution.processor;version="2.0.0",
diff --git a/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XMLDocumentHelper.java b/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XMLDocumentHelper.java deleted file mode 100644 index a057bbe155..0000000000 --- a/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XMLDocumentHelper.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.xsd.xml; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.net.URL; -import java.net.URLConnection; - -import javax.xml.namespace.QName; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamConstants; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; - -import org.xml.sax.InputSource; - -/** - * @version $Rev$ $Date$ - */ -public class XMLDocumentHelper { - public static final QName WSDL11 = new QName("http://schemas.xmlsoap.org/wsdl/", "definitions"); - public static final QName XSD = new QName("http://www.w3.org/2001/XMLSchema", "schema"); - public static final QName WSDL20 = new QName("http://www.w3.org/ns/wsdl", "description"); - - protected static final int BUFFER_SIZE = 256; - - /** - * Detect the XML encoding of the document - * - * @param is The input stream - * @return The encoding - * @throws IOException - */ - public static String getEncoding(InputStream is) throws IOException { - if (!is.markSupported()) - is = new BufferedInputStream(is); - - byte[] buffer = readBuffer(is); - return getXMLEncoding(buffer); - } - - /** - * Searches the array of bytes to determine the XML encoding. - */ - protected static String getXMLEncoding(byte[] bytes) { - String javaEncoding = null; - - if (bytes.length >= 4) { - if (((bytes[0] == -2) && (bytes[1] == -1)) || ((bytes[0] == 0) && (bytes[1] == 60))) - javaEncoding = "UnicodeBig"; - else if (((bytes[0] == -1) && (bytes[1] == -2)) || ((bytes[0] == 60) && (bytes[1] == 0))) - javaEncoding = "UnicodeLittle"; - else if ((bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) - javaEncoding = "UTF8"; - } - - String header = null; - - try { - if (javaEncoding != null) - header = new String(bytes, 0, bytes.length, javaEncoding); - else - header = new String(bytes, 0, bytes.length); - } catch (UnsupportedEncodingException e) { - return null; - } - - if (!header.startsWith("<?xml")) - return "UTF-8"; - - int endOfXMLPI = header.indexOf("?>"); - int encodingIndex = header.indexOf("encoding", 6); - - if ((encodingIndex == -1) || (encodingIndex > endOfXMLPI)) - return "UTF-8"; - - int firstQuoteIndex = header.indexOf("\"", encodingIndex); - int lastQuoteIndex; - - if ((firstQuoteIndex == -1) || (firstQuoteIndex > endOfXMLPI)) { - firstQuoteIndex = header.indexOf("'", encodingIndex); - lastQuoteIndex = header.indexOf("'", firstQuoteIndex + 1); - } else - lastQuoteIndex = header.indexOf("\"", firstQuoteIndex + 1); - - return header.substring(firstQuoteIndex + 1, lastQuoteIndex); - } - - protected static byte[] readBuffer(InputStream is) throws IOException { - if (is.available() == 0) { - return new byte[0]; - } - - byte[] buffer = new byte[BUFFER_SIZE]; - is.mark(BUFFER_SIZE); - int bytesRead = is.read(buffer, 0, BUFFER_SIZE); - int totalBytesRead = bytesRead; - - while (bytesRead != -1 && (totalBytesRead < BUFFER_SIZE)) { - bytesRead = is.read(buffer, totalBytesRead, BUFFER_SIZE - totalBytesRead); - - if (bytesRead != -1) - totalBytesRead += bytesRead; - } - - if (totalBytesRead < BUFFER_SIZE) { - byte[] smallerBuffer = new byte[totalBytesRead]; - System.arraycopy(buffer, 0, smallerBuffer, 0, totalBytesRead); - smallerBuffer = buffer; - } - - is.reset(); - return buffer; - } - - public static InputSource getInputSource(URL url) throws IOException { - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); - InputStream is = connection.getInputStream(); - return getInputSource(url, is); - } - - public static InputSource getInputSource(URL url, InputStream is) throws IOException { - is = new BufferedInputStream(is); - String encoding = getEncoding(is); - InputSource inputSource = new InputSource(is); - inputSource.setEncoding(encoding); - // [rfeng] Make sure we set the system id as it will be used as the base URI for nested import/include - inputSource.setSystemId(url.toString()); - return inputSource; - } - - public static String readTargetNamespace(URL doc, QName element, boolean rootOnly, String attribute, XMLInputFactory inputFactory) - throws IOException, XMLStreamException { - if (attribute == null) { - attribute = "targetNamespace"; - } - URLConnection connection = doc.openConnection(); - connection.setUseCaches(false); - InputStream is = connection.getInputStream(); - try { - XMLStreamReader reader = inputFactory.createXMLStreamReader(is); - int eventType = reader.getEventType(); - while (true) { - if (eventType == XMLStreamConstants.START_ELEMENT) { - if (element.equals(reader.getName())) { - return reader.getAttributeValue(null, attribute); - } else if (rootOnly) { - return null; - } - } - if (reader.hasNext()) { - eventType = reader.next(); - } else { - break; - } - } - return null; - } finally { - is.close(); - } - } - -} diff --git a/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDDocumentProcessor.java b/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDDocumentProcessor.java index 1d84b6c0a0..c7e07fbe60 100644 --- a/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDDocumentProcessor.java +++ b/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDDocumentProcessor.java @@ -25,10 +25,13 @@ import java.net.URL; import javax.xml.namespace.QName; import javax.xml.stream.XMLInputFactory; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.Problem; @@ -42,15 +45,17 @@ import org.apache.tuscany.sca.xsd.XSDefinition; * @version $Rev$ $Date$ */ public class XSDDocumentProcessor implements URLArtifactProcessor<XSDefinition> { - + private StAXHelper helper; private XSDFactory factory; private XMLInputFactory inputFactory; private Monitor monitor; - public XSDDocumentProcessor(FactoryExtensionPoint modelFactories, Monitor monitor) { + public XSDDocumentProcessor(ExtensionPointRegistry registry, StAXArtifactProcessor processor, Monitor monitor) { + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); this.factory = modelFactories.getFactory(XSDFactory.class); this.inputFactory = modelFactories.getFactory(XMLInputFactory.class); this.monitor = monitor; + this.helper = StAXHelper.getInstance(registry); } /** @@ -93,7 +98,7 @@ public class XSDDocumentProcessor implements URLArtifactProcessor<XSDefinition> protected XSDefinition indexRead(URL doc) throws Exception { XSDefinition xsd = factory.createXSDefinition(); xsd.setUnresolved(true); - xsd.setNamespace(XMLDocumentHelper.readTargetNamespace(doc, XSD, true, "targetNamespace", inputFactory)); + xsd.setNamespace(helper.readAttribute(doc, XSD, "targetNamespace")); xsd.setLocation(doc.toURI()); xsd.setUnresolved(false); return xsd; diff --git a/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java b/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java index f9f91202ba..8a99ffb922 100644 --- a/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java +++ b/java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java @@ -29,6 +29,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.tuscany.sca.common.xml.XMLDocumentHelper; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.DefaultImport; diff --git a/java/sca/modules/xsd/src/test/java/org/apache/tuscany/sca/xsd/xml/XMLDocumentHelperTestCase.java b/java/sca/modules/xsd/src/test/java/org/apache/tuscany/sca/xsd/xml/XMLDocumentHelperTestCase.java deleted file mode 100644 index 56831e23d3..0000000000 --- a/java/sca/modules/xsd/src/test/java/org/apache/tuscany/sca/xsd/xml/XMLDocumentHelperTestCase.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.xsd.xml; - -import java.net.URL; - -import javax.xml.stream.XMLInputFactory; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -/** - * @version $Rev$ $Date$ - */ -public class XMLDocumentHelperTestCase { - private URL xsd; - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - xsd = getClass().getResource("/xsd/greeting.xsd"); - } - - @Test - public void testReadTNS() throws Exception { - String tns = XMLDocumentHelper.readTargetNamespace(xsd, XMLDocumentHelper.XSD, true, null, XMLInputFactory.newInstance()); - Assert.assertEquals("http://greeting", tns); - } - -} |