From da54af6fa3cbdc88a5c07451697e071710a500ad Mon Sep 17 00:00:00 2001 From: rfeng Date: Tue, 11 Aug 2009 05:13:15 +0000 Subject: 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 --- .../sca/assembly/xml/BaseAssemblyProcessor.java | 7 +- .../xml/ReadWriteAnyAttributeTestCase.java | 4 - .../assembly/xml/ReadWriteAnyElementTestCase.java | 6 +- .../xml/ReadWriteLocalCompositeTestCase.java | 2 +- .../apache/tuscany/sca/assembly/xsd/Constants.java | 40 +++++ .../tuscany/sca/binding/sca/xml/WriteTestCase.java | 4 +- .../modules/binding-ws-axis2/META-INF/MANIFEST.MF | 19 ++- .../sca/binding/ws/axis2/Axis2ServiceClient.java | 4 +- java/sca/modules/common-java/META-INF/MANIFEST.MF | 3 +- .../tuscany/sca/common/java/io/IOHelper.java | 116 +++++++++++++ .../tuscany/sca/common/xml/XMLDocumentHelper.java | 6 +- java/sca/modules/contribution/META-INF/MANIFEST.MF | 2 + java/sca/modules/contribution/pom.xml | 21 +-- .../processor/BaseStAXArtifactProcessor.java | 25 +-- .../DefaultValidatingXMLInputFactory.java | 131 +++++++-------- .../processor/ExtensibleStAXArtifactProcessor.java | 2 +- .../ExtensibleStAXAttributeProcessor.java | 2 +- .../modules/interface-wsdl/META-INF/MANIFEST.MF | 2 + .../wsdl/xml/WSDLDocumentProcessor.java | 70 ++++---- .../interfacedef/wsdl/xml/WSDLModelResolver.java | 6 +- .../wsdl/xml/XMLDocumentHelperTestCase.java | 52 ------ .../xml/NodeConfigurationProcessorTestCase.java | 1 + java/sca/modules/xsd/META-INF/MANIFEST.MF | 2 + .../tuscany/sca/xsd/xml/XMLDocumentHelper.java | 184 --------------------- .../tuscany/sca/xsd/xml/XSDDocumentProcessor.java | 11 +- .../tuscany/sca/xsd/xml/XSDModelResolver.java | 1 + .../sca/xsd/xml/XMLDocumentHelperTestCase.java | 50 ------ 27 files changed, 313 insertions(+), 460 deletions(-) create mode 100644 java/sca/modules/common-java/src/main/java/org/apache/tuscany/sca/common/java/io/IOHelper.java delete mode 100644 java/sca/modules/interface-wsdl/src/test/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XMLDocumentHelperTestCase.java delete mode 100644 java/sca/modules/xsd/src/main/java/org/apache/tuscany/sca/xsd/xml/XMLDocumentHelper.java delete mode 100644 java/sca/modules/xsd/src/test/java/org/apache/tuscany/sca/xsd/xml/XMLDocumentHelperTestCase.java (limited to 'java') 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 = ""+ ""+ ""+ 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 = "" + - "" + + "" + "" + "" + "" + @@ -52,7 +52,7 @@ public class ReadWriteAnyElementTestCase { private static final String XML_UNKNOWN_IMPL = "" + - "" + + "" + "" + "" + "" + @@ -63,7 +63,7 @@ public class ReadWriteAnyElementTestCase { private static final String XML_UNKNOWN_IMPL_WITH_INVALID_ATTRIBUTE = "" + - "" + + "" + "" + "" + "" + 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 = ""+ - ""+ + ""+ ""; @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 CACHED_XSDS = XSDCache.cache(); + + static class XSDCache { + static Map cache() { + Map cachedXSDs = new HashMap(); + 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("", + assertEquals("", bos.toString()); } @@ -78,7 +78,7 @@ public class WriteTestCase { assertNotNull(composite); ByteArrayOutputStream bos = new ByteArrayOutputStream(); staxProcessor.write(composite, outputFactory.createXMLStreamWriter(bos)); - assertEquals("", + assertEquals("", 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 @@ - org.apache.geronimo.specs - geronimo-stax-api_1.0_spec - 1.0.1 - - - - org.codehaus.woodstox - wstx-asl - 3.2.4 - runtime - - - stax - stax-api - - + org.apache.tuscany.sca + tuscany-common-xml + 2.0-SNAPSHOT - + 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 aggregate(URL... urls) throws IOException, XMLStreamException { + if (urls.length == 1) { + return Collections.singletonList(new SAXSource(XMLDocumentHelper.getInputSource(urls[0]))); + } + Map> map = new HashMap>(); + + for (URL url : urls) { + String tns = helper.readAttribute(url, XSD, "targetNamespace"); + Collection collection = map.get(tns); + if (collection == null) { + collection = new HashSet(); + map.put(tns, collection); + } + collection.add(url); + } + List sources = new ArrayList(); + for (Map.Entry> e : map.entrySet()) { + if (e.getValue().size() == 1) { + sources.add(new SAXSource(XMLDocumentHelper.getInputSource(e.getValue().iterator().next()))); + } else { + StringBuffer xsd = new StringBuffer(""); + for (URL url : e.getValue()) { + xsd.append(""); + } + xsd.append(""); + 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() { - 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 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() { 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 cachedXSDs = new HashMap(); - 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= 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(""); - 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 { - + 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 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); - } - -} -- cgit v1.2.3