diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-12 23:43:27 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-12 23:43:27 +0000 |
commit | c122a7379b57d45d2c994728171d20609842f7c5 (patch) | |
tree | 9ea10773724f65216b0ebe8d155db4dabaca08f9 /java/sca/modules/contribution | |
parent | aaa85d1773cf5740e22913a9b5027f0bfebbf1de (diff) |
Fix issues around XMLStreamWriter based on the isRepairingNamespaces property of XMLOutputFactory
More refactoring to use common-xml and common-java
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@803742 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
9 files changed, 53 insertions, 481 deletions
diff --git a/java/sca/modules/contribution/META-INF/MANIFEST.MF b/java/sca/modules/contribution/META-INF/MANIFEST.MF index f2f8b45d37..939a445d13 100644 --- a/java/sca/modules/contribution/META-INF/MANIFEST.MF +++ b/java/sca/modules/contribution/META-INF/MANIFEST.MF @@ -38,6 +38,7 @@ 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.java.io;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",
diff --git a/java/sca/modules/contribution/pom.xml b/java/sca/modules/contribution/pom.xml index c020aa467c..617b4559f8 100644 --- a/java/sca/modules/contribution/pom.xml +++ b/java/sca/modules/contribution/pom.xml @@ -47,6 +47,11 @@ <version>2.0-SNAPSHOT</version> </dependency> + <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-common-java</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 5f7095f80a..a11a69867b 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 @@ -22,16 +22,11 @@ package org.apache.tuscany.sca.contribution.processor; 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; -import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; -import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; @@ -39,6 +34,7 @@ import javax.xml.stream.XMLStreamWriter; import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.Extensible; import org.apache.tuscany.sca.assembly.Extension; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; /** @@ -47,7 +43,10 @@ import org.apache.tuscany.sca.assembly.Extension; * @version $Rev$ $Date$ */ public abstract class BaseStAXArtifactProcessor { - + /** + * The StAXHelper without states + */ + private static final StAXHelper helper = new StAXHelper(null, null, null); /** * Returns a QName from a string. * @param reader @@ -55,18 +54,7 @@ public abstract class BaseStAXArtifactProcessor { * @return */ protected QName getQNameValue(XMLStreamReader reader, String value) { - if (value != null) { - int index = value.indexOf(':'); - String prefix = index == -1 ? "" : value.substring(0, index); - String localName = index == -1 ? value : value.substring(index + 1); - String ns = reader.getNamespaceContext().getNamespaceURI(prefix); - if (ns == null) { - ns = ""; - } - return new QName(ns, localName, prefix); - } else { - return null; - } + return StAXHelper.getValueAsQName(reader, value); } /** @@ -76,11 +64,12 @@ public abstract class BaseStAXArtifactProcessor { * @return */ protected boolean getBoolean(XMLStreamReader reader, String name) { - String value = reader.getAttributeValue(null, name); - if (value == null) { + Boolean attr = StAXHelper.getAttributeAsBoolean(reader, name); + if (attr == null) { return false; + } else { + return attr.booleanValue(); } - return Boolean.valueOf(value); } /** @@ -90,8 +79,7 @@ public abstract class BaseStAXArtifactProcessor { * @return */ protected QName getQName(XMLStreamReader reader, String name) { - String qname = reader.getAttributeValue(null, name); - return getQNameValue(reader, qname); + return StAXHelper.getAttributeAsQName(reader, name); } /** @@ -101,16 +89,7 @@ public abstract class BaseStAXArtifactProcessor { * @return */ protected List<QName> getQNames(XMLStreamReader reader, String name) { - String value = reader.getAttributeValue(null, name); - if (value != null) { - List<QName> qnames = new ArrayList<QName>(); - for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { - qnames.add(getQNameValue(reader, tokens.nextToken())); - } - return qnames; - } else { - return Collections.emptyList(); - } + return StAXHelper.getAttributeAsQNames(reader, name); } /** @@ -120,7 +99,7 @@ public abstract class BaseStAXArtifactProcessor { * @return */ protected String getString(XMLStreamReader reader, String name) { - return reader.getAttributeValue(null, name); + return StAXHelper.getAttributeAsString(reader, name); } /** @@ -130,7 +109,7 @@ public abstract class BaseStAXArtifactProcessor { * @return */ protected boolean isSet(XMLStreamReader reader, String name) { - return reader.getAttributeValue(null, name) != null; + return StAXHelper.isAttributePresent(reader, name); } /** @@ -140,8 +119,7 @@ public abstract class BaseStAXArtifactProcessor { * returned. */ protected QName getXSIType(XMLStreamReader reader) { - String qname = reader.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); - return getQNameValue(reader, qname); + return StAXHelper.getXSIType(reader); } /** @@ -170,18 +148,7 @@ public abstract class BaseStAXArtifactProcessor { * @throws XMLStreamException if there was a problem reading the stream */ protected void skipToEndElement(XMLStreamReader reader) throws XMLStreamException { - int depth = 0; - while (reader.hasNext()) { - int event = reader.next(); - if (event == XMLStreamConstants.START_ELEMENT) { - depth++; - } else if (event == XMLStreamConstants.END_ELEMENT) { - if (depth == 0) { - return; - } - depth--; - } - } + StAXHelper.skipToEndElement(reader); } /** @@ -220,17 +187,9 @@ public abstract class BaseStAXArtifactProcessor { * @param attrs * @throws XMLStreamException */ - protected void writeStart(XMLStreamWriter writer, String uri, String name, XAttr... attrs) throws XMLStreamException { -// String prefix = setPrefix(writer, uri); - writer.writeStartElement(uri, name); - - // [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); + protected void writeStart(XMLStreamWriter writer, String uri, String name, XAttr... attrs) + throws XMLStreamException { + helper.writeStartElement(writer, "", name, uri); writeAttributes(writer, attrs); } @@ -299,19 +258,6 @@ public abstract class BaseStAXArtifactProcessor { } /** - * Write attribute prefixes to the current element. - * @param writer - * @param attrs - * @throws XMLStreamException - */ - protected void writeAttributePrefixes(XMLStreamWriter writer, XAttr... attrs) throws XMLStreamException { - for (XAttr attr : attrs) { - if (attr != null) - attr.writePrefix(writer); - } - } - - /** * * @param reader * @param elementName @@ -457,6 +403,10 @@ public abstract class BaseStAXArtifactProcessor { public XAttr(String name, QName value) { this(null, name, value); } + + public String toString() { + return uri == null ? name + "=\"" + value + "\"" : "{" + uri + "}" + name + "=\"" + value + "\""; + } /** * Writes a string from a QName and registers a prefix for its namespace. @@ -466,66 +416,14 @@ public abstract class BaseStAXArtifactProcessor { */ private String writeQNameValue(XMLStreamWriter writer, QName qname) throws XMLStreamException { if (qname != null) { - String prefix = qname.getPrefix(); - String uri = qname.getNamespaceURI(); - prefix = writer.getPrefix(uri); - if (prefix != null) { - - // Use the prefix already bound to the given URI - if (prefix.length() > 0) { - return prefix + ":" + qname.getLocalPart(); - } else { - - // Empty prefix, just return the local part of the given qname - return qname.getLocalPart(); - } - + String prefix = helper.writeNamespace(writer, qname.getPrefix(), qname.getNamespaceURI()); + if ("".equals(prefix)) { + return qname.getLocalPart(); } else { - - // Find an available prefix and bind it to the given URI - NamespaceContext nsc = writer.getNamespaceContext(); - for (int i=1; ; i++) { - prefix = "ns" + i; - if (nsc.getNamespaceURI(prefix) == null) { - break; - } - } - writer.setPrefix(prefix, uri); - writer.writeNamespace(prefix, uri); return prefix + ":" + qname.getLocalPart(); } - } else { - return null; - } - } - - /** - * Registers a prefix for the namespace of a QName. - * @param reader - * @param value - * @return - */ - private void writeQNamePrefix(XMLStreamWriter writer, QName qname) throws XMLStreamException { - if (qname != null) { - String prefix = qname.getPrefix(); - String uri = qname.getNamespaceURI(); - prefix = writer.getPrefix(uri); - if (prefix != null) { - return; - } else { - - // Find an available prefix and bind it to the given URI - NamespaceContext nsc = writer.getNamespaceContext(); - for (int i=1; ; i++) { - prefix = "ns" + i; - if (nsc.getNamespaceURI(prefix) == null) { - break; - } - } - writer.setPrefix(prefix, uri); - writer.writeNamespace(prefix, uri); - } } + return null; } /** @@ -585,39 +483,7 @@ public abstract class BaseStAXArtifactProcessor { return; } - // Write the attribute - if (uri != null && !uri.equals(SCA11_NS)) { - writer.writeAttribute(uri, name, str); - } else { - writer.writeAttribute(name,str); - } - } - - /** - * Registers a prefix for the namespace of a QName or list of QNames - * @param writer - * @throws XMLStreamException - */ - public void writePrefix(XMLStreamWriter writer) throws XMLStreamException { - if (value instanceof QName) { - - // Write prefix for a single QName value - writeQNamePrefix(writer, (QName)value); - - } else if (value instanceof Collection) { - - // Write prefixes for a list of values - for (Object v: (Collection<?>)value) { - if (v instanceof QName) { - // Write prefix for a QName value - writeQNamePrefix(writer, (QName)v); - - } else if (v instanceof XAttr) { - // Write prefix for an XAttr value - ((XAttr)v).writePrefix(writer); - } - } - } + helper.writeAttribute(writer, "", name, uri, str); } } diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java index eb1946c4b8..2bb6b59e57 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java @@ -24,10 +24,10 @@ import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; -import java.net.URLConnection; import java.util.List; import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.common.java.io.IOHelper; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; import org.apache.tuscany.sca.contribution.ContributionMetadata; @@ -128,9 +128,7 @@ public class ContributionInfoProcessor implements URLArtifactProcessor<Contribut URL url = scanner.getArtifactURL(contribution, path); try { // Check if the file actually exists before trying to read it - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); - InputStream is = connection.getInputStream(); + InputStream is = IOHelper.openStream(url); is.close(); } catch (IOException e) { continue; diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java index 02618c489b..931a0bce4a 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/AnyElementProcessor.java @@ -19,22 +19,21 @@ package org.apache.tuscany.sca.contribution.processor.xml; import java.io.StringReader; -import java.io.StringWriter; import javax.xml.namespace.QName; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.Extension; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; import org.apache.tuscany.sca.contribution.Constants; 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.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.monitor.Monitor; @@ -42,17 +41,16 @@ public class AnyElementProcessor implements StAXArtifactProcessor<Extension> { private static final QName ANY_ELEMENT = new QName(Constants.XMLSCHEMA_NS, "any"); private AssemblyFactory assemblyFactory; - private XMLInputFactory xmlInputFactory; - private XMLOutputFactory xmlOutputFactory; + private StAXHelper helper; @SuppressWarnings("unused") private Monitor monitor; - public AnyElementProcessor(FactoryExtensionPoint modelFactories, Monitor monitor) { + public AnyElementProcessor(ExtensionPointRegistry extensionPoints, StAXArtifactProcessor<Object> extensionProcessor, Monitor monitor) { + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); - xmlInputFactory = modelFactories.getFactory(XMLInputFactory.class); - xmlOutputFactory = modelFactories.getFactory(XMLOutputFactory.class); + this.helper = StAXHelper.getInstance(extensionPoints); this.monitor = monitor; } @@ -74,15 +72,10 @@ public class AnyElementProcessor implements StAXArtifactProcessor<Extension> { */ public Extension read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { QName name = reader.getName(); - XMLStreamSerializer serializer = new XMLStreamSerializer(); - StringWriter sw = new StringWriter(); - XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(sw); - serializer.serialize(reader, writer); - writer.flush(); - + String xml = helper.saveAsString(reader); Extension ext = assemblyFactory.createExtension(); ext.setQName(name); - ext.setValue(sw.toString()); + ext.setValue(xml); return ext; } @@ -99,11 +92,10 @@ public class AnyElementProcessor implements StAXArtifactProcessor<Extension> { return; } String xml = (String) value; - XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(new StringReader(xml)); + XMLStreamReader reader = helper.createXMLStreamReader(new StringReader(xml)); // Position the reader to the root element reader.nextTag(); - XMLStreamSerializer serializer = new XMLStreamSerializer(); - serializer.serialize(reader, writer); + helper.save(reader, writer); } public void resolve(Extension model, ModelResolver resolver) throws ContributionResolveException { diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLStreamSerializer.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLStreamSerializer.java deleted file mode 100644 index 7c8ba4296e..0000000000 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/xml/XMLStreamSerializer.java +++ /dev/null @@ -1,287 +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.contribution.processor.xml; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.stream.XMLStreamConstants; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.XMLStreamWriter; - -/** - * The XMLStreamSerializer pulls events from the XMLStreamReader and dumps into the XMLStreamWriter - * - * @version $Rev$ $Date$ - */ -public class XMLStreamSerializer implements XMLStreamConstants { - public static final String NAMESPACE_PREFIX = "ns"; - private static int namespaceSuffix; - - /* - * The behavior of the Serializer is such that it returns when it encounters the starting element for the second - * time. The depth variable tracks the depth of the Serializer and tells it when to return. Note that it is assumed - * that this Serialization starts on an Element. - */ - - /** - * Field depth - */ - private int depth; - - /** - * Generates a unique namespace prefix that is not in the scope of the NamespaceContext - * - * @param nsCtxt - * @return string - */ - private String generateUniquePrefix(NamespaceContext nsCtxt) { - String prefix = NAMESPACE_PREFIX + namespaceSuffix++; - // null should be returned if the prefix is not bound! - while (nsCtxt.getNamespaceURI(prefix) != null) { - prefix = NAMESPACE_PREFIX + namespaceSuffix++; - } - - return prefix; - } - - /** - * Method serialize. - * - * @param node - * @param writer - * @throws XMLStreamException - */ - public void serialize(XMLStreamReader node, XMLStreamWriter writer) throws XMLStreamException { - serializeNode(node, writer); - } - - /** - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeAttributes(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - int count = reader.getAttributeCount(); - String prefix; - String namespaceName; - String writerPrefix; - for (int i = 0; i < count; i++) { - prefix = reader.getAttributePrefix(i); - namespaceName = reader.getAttributeNamespace(i); - /* - * Due to parser implementations returning null as the namespace URI (for the empty namespace) we need to - * make sure that we deal with a namespace name that is not null. The best way to work around this issue is - * to set the namespace URI to "" if it is null - */ - if (namespaceName == null) { - namespaceName = ""; - } - - writerPrefix = writer.getPrefix(namespaceName); - - if (!"".equals(namespaceName)) { - // prefix has already being declared but this particular - // attrib has a - // no prefix attached. So use the prefix provided by the - // writer - if (writerPrefix != null && (prefix == null || prefix.equals(""))) { - writer.writeAttribute(writerPrefix, namespaceName, reader.getAttributeLocalName(i), reader - .getAttributeValue(i)); - - // writer prefix is available but different from the - // current - // prefix of the attrib. We should be declaring the new - // prefix - // as a namespace declaration - } else if (prefix != null && !"".equals(prefix) && !prefix.equals(writerPrefix)) { - writer.writeNamespace(prefix, namespaceName); - writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader - .getAttributeValue(i)); - - // prefix is null (or empty), but the namespace name is - // valid! it has not - // being written previously also. So we need to generate - // a prefix - // here - } else if (prefix == null || prefix.equals("")) { - prefix = generateUniquePrefix(writer.getNamespaceContext()); - writer.writeNamespace(prefix, namespaceName); - writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader - .getAttributeValue(i)); - } else { - writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader - .getAttributeValue(i)); - } - } else { - // empty namespace is equal to no namespace! - writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i)); - } - - } - } - - /** - * Method serializeCData. - * - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeCData(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - writer.writeCData(reader.getText()); - } - - /** - * Method serializeComment. - * - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeComment(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - writer.writeComment(reader.getText()); - } - - /** - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeElement(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - String prefix = reader.getPrefix(); - String nameSpaceName = reader.getNamespaceURI(); - if (nameSpaceName != null) { - String writerPrefix = writer.getPrefix(nameSpaceName); - if (writerPrefix != null) { - writer.writeStartElement(nameSpaceName, reader.getLocalName()); - } else { - if (prefix != null) { - writer.writeStartElement(prefix, reader.getLocalName(), nameSpaceName); - writer.writeNamespace(prefix, nameSpaceName); - // writer.setPrefix(prefix, nameSpaceName); - } else { - // [rfeng] We need to set default NS 1st before calling writeStateElement - writer.setDefaultNamespace(nameSpaceName); - writer.writeStartElement(nameSpaceName, reader.getLocalName()); - writer.writeDefaultNamespace(nameSpaceName); - } - } - } else { - writer.writeStartElement(reader.getLocalName()); - } - - // add the namespaces - int count = reader.getNamespaceCount(); - String namespacePrefix; - for (int i = 0; i < count; i++) { - namespacePrefix = reader.getNamespacePrefix(i); - // [rfeng] The following is commented out to allow to default ns - // if (namespacePrefix != null && namespacePrefix.length() == 0) { - // continue; - // } - - serializeNamespace(namespacePrefix, reader.getNamespaceURI(i), writer); - } - - // add attributes - serializeAttributes(reader, writer); - - } - - /** - * Method serializeEndElement. - * - * @param writer - * @throws XMLStreamException - */ - protected void serializeEndElement(XMLStreamWriter writer) throws XMLStreamException { - writer.writeEndElement(); - } - - /** - * Method serializeNamespace. - * - * @param prefix - * @param uri - * @param writer - * @throws XMLStreamException - */ - private void serializeNamespace(String prefix, String uri, XMLStreamWriter writer) throws XMLStreamException { - String prefix1 = writer.getPrefix(uri); - if (prefix1 == null) { - writer.writeNamespace(prefix, uri); - // writer.setPrefix(prefix, uri); - } - } - - /** - * Method serializeNode. - * - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeNode(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - while (true) { - int event = reader.getEventType(); - if (event == START_ELEMENT) { - serializeElement(reader, writer); - depth++; - } else if (event == ATTRIBUTE) { - serializeAttributes(reader, writer); - } else if (event == CHARACTERS) { - serializeText(reader, writer); - } else if (event == COMMENT) { - serializeComment(reader, writer); - } else if (event == CDATA) { - serializeCData(reader, writer); - } else if (event == END_ELEMENT) { - serializeEndElement(writer); - depth--; - } else if (event == START_DOCUMENT) { - depth++; // if a start document is found then increment - writer.writeStartDocument(); - // the depth - } else if (event == END_DOCUMENT) { - if (depth != 0) { - depth--; // for the end document - reduce the depth - } - writer.writeEndDocument(); - } - if (depth == 0) { - break; - } - if (reader.hasNext()) { - reader.next(); - } else { - break; - } - } - } - - /** - * @param reader - * @param writer - * @throws XMLStreamException - */ - protected void serializeText(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - writer.writeCharacters(reader.getText()); - } -} diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java index f1fb6f2a70..2c5b849a20 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java @@ -22,7 +22,6 @@ package org.apache.tuscany.sca.contribution.scanner.impl; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -30,6 +29,7 @@ import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; +import org.apache.tuscany.sca.common.java.io.IOHelper; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.PackageType; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; @@ -68,9 +68,7 @@ public class JarContributionScanner implements ContributionScanner { // Assume the URL references a JAR file try { URL url = new URL(contribution.getLocation()); - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); - JarInputStream jar = new JarInputStream(connection.getInputStream()); + JarInputStream jar = new JarInputStream(IOHelper.openStream(url)); try { Set<String> names = new HashSet<String>(); while (true) { diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataDocumentProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataDocumentProcessor.java index d5fa606997..0ab817e1bc 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataDocumentProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataDocumentProcessor.java @@ -22,12 +22,12 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URL; -import java.net.URLConnection; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.common.java.io.IOHelper; import org.apache.tuscany.sca.contribution.ContributionMetadata; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; @@ -93,9 +93,7 @@ public class ContributionMetadataDocumentProcessor implements URLArtifactProcess try { // Create a stream reader - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); - urlStream = connection.getInputStream(); + urlStream = IOHelper.openStream(url); XMLStreamReader reader = inputFactory.createXMLStreamReader(url.toString(), urlStream); reader.nextTag(); diff --git a/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java b/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java index d1c18beee5..b72c476267 100644 --- a/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java +++ b/java/sca/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java @@ -57,7 +57,7 @@ public class ContributionMetadataProcessorTestCase { "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<contribution xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200903\"" + " xmlns:ns=\"http://ns\" ns:foo=\"extended\">" + "<deployable composite=\"ns:Composite1\"/>" - + "<deployable composite=\"ns:Composite2\"/>" + + "<deployable xmlns:ns2=\"http://ns2\" composite=\"ns2:Composite2\"/>" + "<ns:bar x=\"1\"/>" + "</contribution>"; @@ -78,6 +78,7 @@ public class ContributionMetadataProcessorTestCase { inputFactory = XMLInputFactory.newInstance(); outputFactory = XMLOutputFactory.newInstance(); + outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); // Create a monitor UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); @@ -142,7 +143,7 @@ public class ContributionMetadataProcessorTestCase { assertEquals(2, contribution.getDeployables().size()); deployable = new QName("http://ns", "Composite1"); assertEquals(deployable, contribution.getDeployables().get(0).getName()); - deployable = new QName("http://ns", "Composite2"); + deployable = new QName("http://ns2", "Composite2"); assertEquals(deployable, contribution.getDeployables().get(1).getName()); } |