diff options
Diffstat (limited to 'java/sca')
42 files changed, 403 insertions, 1175 deletions
diff --git a/java/sca/modules/assembly-xml/META-INF/MANIFEST.MF b/java/sca/modules/assembly-xml/META-INF/MANIFEST.MF index b34277f708..d08fa53dd6 100644 --- a/java/sca/modules/assembly-xml/META-INF/MANIFEST.MF +++ b/java/sca/modules/assembly-xml/META-INF/MANIFEST.MF @@ -18,22 +18,31 @@ Bnd-LastModified: 1225397124328 Bundle-ManifestVersion: 2
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-Description: Apache Tuscany SCA XML Assembly Model
-Import-Package: javax.xml.namespace,javax.xml.parsers,javax.xml.stream
- ,javax.xml.transform,javax.xml.transform.dom,javax.xml.xpath,org.apac
- he.tuscany.sca.assembly;version="2.0.0",org.apache.tuscany.sca.assembly
- .xml;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.tus
- cany.sca.contribution.namespace;version="2.0.0",org.apache.tuscany.sca.
- contribution.processor;version="2.0.0",org.apache.tuscany.sca.contribut
- ion.resolver;version="2.0.0",org.apache.tuscany.sca.core;version="2.0.0",
- org.apache.tuscany.sca.definitions;version="2.0.0",org.apache.tuscany.s
- ca.interfacedef;version="2.0.0",org.apache.tuscany.sca.interfacedef.imp
- l;version="2.0.0",org.apache.tuscany.sca.monitor;version="2.0.0",org.apac
- he.tuscany.sca.policy;version="2.0.0",org.apache.tuscany.sca.policy.uti
- l;version="2.0.0",org.w3c.dom,org.xml.sa
- x;resolution:=optional,javax.xml
- .validation;resolution:=optional,org.apache.tuscany.sca.assembly.buil
- der;version="2.0.0";resolution:=optional
+Import-Package: javax.xml.namespace,
+ javax.xml.parsers,
+ javax.xml.stream,
+ javax.xml.transform,
+ javax.xml.transform.dom,
+ javax.xml.validation;resolution:=optional,
+ javax.xml.xpath,
+ 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.assembly.xml;version="2.0.0",
+ org.apache.tuscany.sca.common.java.io;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",
+ org.apache.tuscany.sca.contribution.processor;version="2.0.0",
+ org.apache.tuscany.sca.contribution.resolver;version="2.0.0",
+ org.apache.tuscany.sca.core;version="2.0.0",
+ org.apache.tuscany.sca.definitions;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef;version="2.0.0",
+ org.apache.tuscany.sca.interfacedef.impl;version="2.0.0",
+ org.apache.tuscany.sca.monitor;version="2.0.0",
+ org.apache.tuscany.sca.policy;version="2.0.0",
+ org.apache.tuscany.sca.policy.util;version="2.0.0",
+ org.w3c.dom,
+ org.xml.sax;resolution:=optional
Bundle-SymbolicName: org.apache.tuscany.sca.assembly.xml
Bundle-DocURL: http://www.apache.org/
Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6
diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java index 969115cf4c..672476a834 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java @@ -23,13 +23,13 @@ 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.assembly.ComponentType; +import org.apache.tuscany.sca.common.java.io.IOHelper; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; @@ -64,9 +64,7 @@ public class ComponentTypeDocumentProcessor extends BaseAssemblyProcessor implem 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/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java index ec059ace81..bb3e88e8fb 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java @@ -20,13 +20,10 @@ package org.apache.tuscany.sca.assembly.xml; import java.io.ByteArrayInputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; -import java.net.URLConnection; import java.util.Collection; import javax.xml.parsers.DocumentBuilderFactory; @@ -35,6 +32,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.common.java.io.IOHelper; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; @@ -84,25 +82,14 @@ public class CompositeDocumentProcessor extends BaseAssemblyProcessor implements throw new ContributionReadException("Request to read composite with uri or url NULL"); } // end if InputStream scdlStream = null; + try { - URLConnection connection; - if (url.getProtocol().equals("file")) { - File scdlFile = new File(url.toURI().getPath()); - connection = scdlFile.toURL().openConnection(); - } else { - connection = url.openConnection(); - } - connection.setUseCaches(false); - scdlStream = connection.getInputStream(); + scdlStream = IOHelper.openStream(url);; } catch (IOException e) { ContributionReadException ce = new ContributionReadException("Exception reading " + uri, e); error("ContributionReadException", url, ce); throw ce; - } catch (URISyntaxException e) { - ContributionReadException ce = new ContributionReadException("Exception reading " + uri, e); - error("ContributionReadException", url, ce); - throw ce; - } + } return read(uri, scdlStream); } diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java index 401fa7123e..135fad43e7 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java @@ -23,13 +23,13 @@ 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.assembly.ConstrainingType; +import org.apache.tuscany.sca.common.java.io.IOHelper; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; @@ -64,33 +64,13 @@ public class ConstrainingTypeDocumentProcessor extends BaseAssemblyProcessor imp 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(); // Read the constrainingType model ConstrainingType constrainingType = (ConstrainingType)extensionProcessor.read(reader); - - // For debugging purposes, write it back to XML -// if (constrainingType != null) { -// try { -// ByteArrayOutputStream bos = new ByteArrayOutputStream(); -// XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); -// outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); -// extensionProcessor.write(constrainingType, outputFactory.createXMLStreamWriter(bos)); -// Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(bos.toByteArray())); -// OutputFormat format = new OutputFormat(); -// format.setIndenting(true); -// format.setIndent(2); -// XMLSerializer serializer = new XMLSerializer(System.out, format); -// serializer.serialize(document); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } - + return constrainingType; } catch (XMLStreamException e) { diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicySubjectProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicySubjectProcessor.java index d69f30aceb..386fb24bf9 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicySubjectProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicySubjectProcessor.java @@ -146,15 +146,6 @@ public class PolicySubjectProcessor extends BaseStAXArtifactProcessor { } /** - * Write policies - * @param attachPoint - * @return - */ - public void writePolicyPrefixes(Object attachPoint, XMLStreamWriter writer) throws XMLStreamException { - writePolicyPrefixes(attachPoint, (Operation)null, writer); - } - - /** * Write policies associated with an operation * @param attachPoint * @param operation @@ -178,16 +169,6 @@ public class PolicySubjectProcessor extends BaseStAXArtifactProcessor { } /** - * Write policies - * @param attachPoint - * @return - */ - public void writePolicyPrefixes(Object attachPoint, Operation operation, XMLStreamWriter writer) throws XMLStreamException { - XAttr attr = writePolicies(attachPoint, operation); - attr.writePrefix(writer); - } - - /** * Write policy intents associated with an operation. * @param attachPoint * @param operation diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/SCABindingProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/SCABindingProcessor.java index 88d8abdf2e..29608ac534 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/SCABindingProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/SCABindingProcessor.java @@ -111,7 +111,6 @@ public class SCABindingProcessor implements StAXArtifactProcessor<SCABinding> { public void write(SCABinding scaBinding, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { // Write <binding.sca> - policyProcessor.writePolicyPrefixes(scaBinding, writer); writer.writeStartElement(SCA11_NS, BINDING_SCA); policyProcessor.writePolicyAttributes(scaBinding, writer); diff --git a/java/sca/modules/binding-rmi-runtime/META-INF/MANIFEST.MF b/java/sca/modules/binding-rmi-runtime/META-INF/MANIFEST.MF index 4696b5cb84..21b3d0f6e1 100644 --- a/java/sca/modules/binding-rmi-runtime/META-INF/MANIFEST.MF +++ b/java/sca/modules/binding-rmi-runtime/META-INF/MANIFEST.MF @@ -10,11 +10,11 @@ Bnd-LastModified: 1225397324375 Bundle-ManifestVersion: 2
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-Description: Apache Tuscany SCA RMI Binding Extension
-Import-Package: org.objectweb.asm,
- net.sf.cglib.core,
+Import-Package: net.sf.cglib.core,
net.sf.cglib.proxy,
org.apache.tuscany.sca.assembly;version="2.0.0",
org.apache.tuscany.sca.binding.rmi;version="2.0.0",
+ org.apache.tuscany.sca.common.java.classloader;version="2.0.0",
org.apache.tuscany.sca.core;version="2.0.0",
org.apache.tuscany.sca.host.rmi;version="2.0.0",
org.apache.tuscany.sca.interfacedef;version="2.0.0",
@@ -25,7 +25,8 @@ Import-Package: org.objectweb.asm, org.apache.tuscany.sca.provider;version="2.0.0",
org.apache.tuscany.sca.runtime;version="2.0.0",
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.objectweb.asm
Bundle-SymbolicName: org.apache.tuscany.sca.binding.rmi.runtime
Bundle-DocURL: http://www.apache.org/
Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 diff --git a/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIServiceBindingProvider.java b/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIServiceBindingProvider.java index 506c2385fb..c572b4e19b 100644 --- a/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIServiceBindingProvider.java +++ b/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIServiceBindingProvider.java @@ -34,6 +34,7 @@ import net.sf.cglib.proxy.MethodProxy; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.binding.rmi.RMIBinding; +import org.apache.tuscany.sca.common.java.classloader.ClassLoaderDelegate; import org.apache.tuscany.sca.host.rmi.RMIHost; import org.apache.tuscany.sca.host.rmi.RMIHostException; import org.apache.tuscany.sca.interfacedef.Interface; @@ -140,33 +141,11 @@ public class RMIServiceBindingProvider implements ServiceBindingProvider { /* * In OSGi, the classloader for the interface cannot access the classes for the CGLIB */ - enhancer.setClassLoader(new MixedClassLoader(targetJavaInterface.getClassLoader(), getClass().getClassLoader())); + enhancer.setClassLoader(new ClassLoaderDelegate(targetJavaInterface.getClassLoader(), getClass().getClassLoader())); enhancer.setInterfaces(new Class[] {targetJavaInterface}); return (Remote)enhancer.create(); } - private static class MixedClassLoader extends ClassLoader { - private ClassLoader runtime; - - public MixedClassLoader(ClassLoader parent, ClassLoader runtime) { - super(parent); - this.runtime = runtime; - } - - @Override - protected Class<?> findClass(String name) throws ClassNotFoundException { - try { - return super.findClass(name); - } catch (ClassNotFoundException e) { - if (runtime != null && runtime != getParent()) { - return runtime.loadClass(name); - } else { - throw e; - } - } - } - } - private Object invokeTarget(Operation op, Object[] args) throws InvocationTargetException { return wire.invoke(op, args); } diff --git a/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/WebServiceBindingProcessor.java b/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/WebServiceBindingProcessor.java index 3e9b07c4be..ecc72d18f4 100644 --- a/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/WebServiceBindingProcessor.java +++ b/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/WebServiceBindingProcessor.java @@ -232,7 +232,6 @@ public class WebServiceBindingProcessor implements StAXArtifactProcessor<WebServ XMLStreamException { // Write a <binding.ws> - policyProcessor.writePolicyPrefixes(wsBinding, writer); writer.writeStartElement(SCA11_NS, BINDING_WS); policyProcessor.writePolicyAttributes(wsBinding, writer); diff --git a/java/sca/modules/common-java/META-INF/MANIFEST.MF b/java/sca/modules/common-java/META-INF/MANIFEST.MF index 08d71266b7..9b86dddeae 100644 --- a/java/sca/modules/common-java/META-INF/MANIFEST.MF +++ b/java/sca/modules/common-java/META-INF/MANIFEST.MF @@ -8,13 +8,19 @@ Bnd-LastModified: 1225397108125 Bundle-ManifestVersion: 2
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-Description: Apache Tuscany SCA Common Java
-Import-Package: org.apache.tuscany.sca.core;version="2.0.0",
+Import-Package: org.apache.tuscany.sca.common.java.classloader;version="2.0.0",
+ org.apache.tuscany.sca.common.java.collection;version="2.0.0",
+ org.apache.tuscany.sca.common.java.io;version="2.0.0",
+ org.apache.tuscany.sca.common.java.reflection;version="2.0.0",
+ org.apache.tuscany.sca.core;version="2.0.0",
org.apache.tuscany.sca.extensibility;version="2.0.0",
org.oasisopen.sca;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.io,
+Export-Package: org.apache.tuscany.sca.common.java.classloader;version="2.0.0",
+ org.apache.tuscany.sca.common.java.collection;version="2.0.0",
+ org.apache.tuscany.sca.common.java.io;version="2.0.0",
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 index 94aa7220e1..f43ff4538b 100644 --- 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 @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.common.java.io; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.JarURLConnection; @@ -29,10 +30,16 @@ import java.net.URL; import java.net.URLConnection; /** - * + * Helper class for I/O operations */ public class IOHelper { + public static InputStream openStream(URL url) throws IOException { + // Handle file:<relative path> which strictly speaking is not a valid file URL + File file = toFile(url); + if (file != null) { + return new FileInputStream(file); + } URLConnection connection = url.openConnection(); if (connection instanceof JarURLConnection) { // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041014 @@ -42,6 +49,16 @@ public class IOHelper { return is; } + public static void close(InputStream is) { + if (is != null) { + try { + is.close(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + } + /** * Escape the space in URL string * @param uri diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/StAXHelper.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/StAXHelper.java index 8ec45e9f9f..09d9d3062d 100644 --- a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/StAXHelper.java +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/StAXHelper.java @@ -70,16 +70,16 @@ public class StAXHelper { factories.getFactory(XMLInputFactory.class); inputFactory = factories.getFactory(XMLInputFactory.class); outputFactory = factories.getFactory(XMLOutputFactory.class); + outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); domHelper = utilities.getUtility(DOMHelper.class); } - + public static StAXHelper getInstance(ExtensionPointRegistry registry) { UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); return utilities.getUtility(StAXHelper.class); } - /** * @param inputFactory * @param outputFactory @@ -89,6 +89,9 @@ public class StAXHelper { super(); this.inputFactory = inputFactory; this.outputFactory = outputFactory; + if (outputFactory != null) { + this.outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); + } this.domHelper = domHelper; } @@ -117,7 +120,7 @@ public class StAXHelper { StringReader reader = new StringReader(string); return createXMLStreamReader(reader); } - + private static InputStream openStream(URL url) throws IOException { URLConnection connection = url.openConnection(); if (connection instanceof JarURLConnection) { @@ -127,7 +130,7 @@ public class StAXHelper { InputStream is = connection.getInputStream(); return is; } - + public XMLStreamReader createXMLStreamReader(URL url) throws XMLStreamException { try { return createXMLStreamReader(openStream(url)); @@ -143,10 +146,8 @@ public class StAXHelper { } public void save(XMLStreamReader reader, OutputStream outputStream) throws XMLStreamException { - XMLStreamSerializer serializer = new XMLStreamSerializer(); XMLStreamWriter streamWriter = createXMLStreamWriter(outputStream); - serializer.serialize(reader, streamWriter); - streamWriter.flush(); + save(reader, streamWriter); } public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream) throws XMLStreamException { @@ -154,10 +155,8 @@ public class StAXHelper { } public void save(XMLStreamReader reader, Writer writer) throws XMLStreamException { - XMLStreamSerializer serializer = new XMLStreamSerializer(); XMLStreamWriter streamWriter = createXMLStreamWriter(writer); - serializer.serialize(reader, streamWriter); - streamWriter.flush(); + save(reader, streamWriter); } public XMLStreamWriter createXMLStreamWriter(Writer writer) throws XMLStreamException { @@ -192,7 +191,7 @@ public class StAXHelper { } public void save(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { - XMLStreamSerializer serializer = new XMLStreamSerializer(); + XMLStreamSerializer serializer = new XMLStreamSerializer(isReparingNamespaces()); serializer.serialize(reader, writer); writer.flush(); } @@ -202,7 +201,6 @@ public class StAXHelper { new StAX2SAXAdapter(false).parse(reader, contentHandler); } - /** * @param url * @param element @@ -212,8 +210,7 @@ public class StAXHelper { * @throws IOException * @throws XMLStreamException */ - public String readAttribute(URL url, QName element, String attribute) throws IOException, - XMLStreamException { + public String readAttribute(URL url, QName element, String attribute) throws IOException, XMLStreamException { if (attribute == null) { attribute = "targetNamespace"; } @@ -225,8 +222,7 @@ public class StAXHelper { } } - public List<String> readAttributes(URL url, QName element, String attribute) throws IOException, - XMLStreamException { + public List<String> readAttributes(URL url, QName element, String attribute) throws IOException, XMLStreamException { if (attribute == null) { attribute = "targetNamespace"; } @@ -238,7 +234,7 @@ public class StAXHelper { reader.close(); } } - + /** * Returns the boolean value of an attribute. * @param reader @@ -356,7 +352,6 @@ public class StAXHelper { } } - private Attribute[] readAttributes(XMLStreamReader reader, AttributeFilter filter) throws XMLStreamException { XMLStreamReader newReader = inputFactory.createFilteredReader(reader, filter); while (filter.proceed() && newReader.hasNext()) { @@ -377,7 +372,7 @@ public class StAXHelper { public Attribute[] readAttributes(XMLStreamReader reader, Attribute... attributes) throws XMLStreamException { return readAttributes(reader, new AttributeFilter(false, attributes)); } - + private String readAttributeFromRoot(XMLStreamReader reader, Attribute filter) throws XMLStreamException { Attribute[] attrs = readAttributes(reader, new AttributeFilter(true, filter)); List<String> values = attrs[0].getValues(); @@ -387,13 +382,13 @@ public class StAXHelper { return values.get(0); } } - + public String readAttributeFromRoot(XMLStreamReader reader, QName element, String attributeName) throws XMLStreamException { Attribute filter = new Attribute(element, attributeName); return readAttributeFromRoot(reader, filter); } - + public static class Attribute { private QName element; private String name; @@ -412,32 +407,32 @@ public class StAXHelper { public List<String> getValues() { return values; } - + } - + private static class AttributeFilter implements StreamFilter { private boolean proceed = true; private Attribute[] attributes; private boolean rootOnly; - + /** * @param rootOnly */ - public AttributeFilter(boolean rootOnly, Attribute...attributes) { + public AttributeFilter(boolean rootOnly, Attribute... attributes) { super(); this.rootOnly = rootOnly; this.attributes = attributes; } public boolean accept(XMLStreamReader reader) { - if(attributes==null || attributes.length==0) { + if (attributes == null || attributes.length == 0) { proceed = false; return true; } - if(reader.getEventType() == XMLStreamConstants.START_ELEMENT) { + if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) { QName name = reader.getName(); - for(Attribute attr: attributes) { - if(attr.element.equals(name)) { + for (Attribute attr : attributes) { + if (attr.element.equals(name)) { attr.values.add(reader.getAttributeValue(null, attr.name)); } } @@ -447,11 +442,57 @@ public class StAXHelper { } return true; } - + public boolean proceed() { return proceed; } - + + } + + public XMLInputFactory getInputFactory() { + return inputFactory; + } + + private boolean isReparingNamespaces() { + if (outputFactory == null) { + return Boolean.TRUE; + } + return outputFactory.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES) == Boolean.TRUE; + } + + public XMLOutputFactory getOutputFactory() { + return outputFactory; + } + + public String writeAttribute(XMLStreamWriter writer, QName name, String value) throws XMLStreamException { + return writeAttribute(writer, name.getPrefix(), name.getLocalPart(), name.getNamespaceURI(), value); + } + + public String writeAttribute(XMLStreamWriter writer, + String prefix, + String localName, + String namespaceURI, + String value) throws XMLStreamException { + if (value == null) { + return null; + } + XMLStreamSerializer serializer = new XMLStreamSerializer(isReparingNamespaces()); + return serializer.writeAttribute(writer, prefix, localName, namespaceURI, value); + } + + public void writeStartElement(XMLStreamWriter writer, QName name) throws XMLStreamException { + writeStartElement(writer, name.getPrefix(), name.getLocalPart(), name.getNamespaceURI()); + } + + public void writeStartElement(XMLStreamWriter writer, String prefix, String localName, String namespaceURI) + throws XMLStreamException { + XMLStreamSerializer serializer = new XMLStreamSerializer(isReparingNamespaces()); + serializer.writeStartElement(writer, prefix, localName, namespaceURI); + } + + public String writeNamespace(XMLStreamWriter writer, String prefix, String namespaceURI) throws XMLStreamException { + XMLStreamSerializer serializer = new XMLStreamSerializer(isReparingNamespaces()); + return serializer.writeNamespace(writer, prefix, namespaceURI); } } diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/impl/XMLStreamSerializer.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/impl/XMLStreamSerializer.java index 64ac0a40e5..5ef5a03d9f 100644 --- a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/impl/XMLStreamSerializer.java +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/impl/XMLStreamSerializer.java @@ -19,7 +19,11 @@ package org.apache.tuscany.sca.common.xml.stax.impl; +import static javax.xml.XMLConstants.DEFAULT_NS_PREFIX; +import static javax.xml.XMLConstants.NULL_NS_URI; + 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; @@ -46,6 +50,23 @@ public class XMLStreamSerializer implements XMLStreamConstants { private int depth; /** + * A flag to tell if the writer has javax.xml.stream.isRepairingNamespaces set to true + */ + private boolean isRepairingNamespaces = true; + + /** + * @param isRepairingNamespaces + */ + public XMLStreamSerializer(boolean isRepairingNamespaces) { + super(); + this.isRepairingNamespaces = isRepairingNamespaces; + } + + public XMLStreamSerializer() { + this(true); + } + + /** * Generates a unique namespace prefix that is not in the scope of the NamespaceContext * * @param nsCtxt @@ -81,62 +102,89 @@ public class XMLStreamSerializer implements XMLStreamConstants { int count = reader.getAttributeCount(); String prefix; String namespaceName; - String writerPrefix; + String localName; + String value; 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 = ""; - } + localName = reader.getAttributeLocalName(i); + value = reader.getAttributeValue(i); - 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)); - } + writeAttribute(writer, prefix, localName, namespaceName, value); + + } + } + + public void writeAttribute(XMLStreamWriter writer, QName name, String value) throws XMLStreamException { + writeAttribute(writer, name.getPrefix(), name.getLocalPart(), name.getNamespaceURI(), value); + } + + public String writeAttribute(XMLStreamWriter writer, + String prefix, + String localName, + String namespaceURI, + String value) throws XMLStreamException { + String writerPrefix; + /* + * 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 (namespaceURI == null) { + namespaceURI = NULL_NS_URI; + } + + if (prefix == null) { + prefix = DEFAULT_NS_PREFIX; + } + + if (isRepairingNamespaces) { + writer.writeAttribute(prefix, namespaceURI, localName, value); + return writer.getPrefix(namespaceURI); + } + + writerPrefix = writer.getPrefix(namespaceURI); + + if (!NULL_NS_URI.equals(namespaceURI)) { + if (writerPrefix != null && isDefaultNSPrefix(prefix)) { + // prefix has already being declared but this particular attrib has a + // no prefix attached. So use the prefix provided by the writer + + writer.writeAttribute(writerPrefix, namespaceURI, localName, value); + return writerPrefix; + + } else if (!isDefaultNSPrefix(prefix) && !prefix.equals(writerPrefix)) { + // writer prefix is available but different from the current + // prefix of the attrib. We should be declaring the new prefix + // as a namespace declaration + + writer.writeNamespace(prefix, namespaceURI); + writer.writeAttribute(prefix, namespaceURI, localName, value); + return prefix; + + } else if (isDefaultNSPrefix(prefix)) { + // 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 + + prefix = generateUniquePrefix(writer.getNamespaceContext()); + writer.writeNamespace(prefix, namespaceURI); + writer.writeAttribute(prefix, namespaceURI, localName, value); + return prefix; } else { - // empty namespace is equal to no namespace! - writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i)); + writer.writeAttribute(prefix, namespaceURI, localName, value); + return prefix; } - + } else { + // empty namespace is equal to no namespace! + writer.writeAttribute(localName, value); + return prefix; } } + private boolean isDefaultNSPrefix(String prefix) { + return (prefix == null || prefix.equals(DEFAULT_NS_PREFIX)); + } + /** * Method serializeCData. * @@ -165,38 +213,13 @@ public class XMLStreamSerializer implements XMLStreamConstants { * @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()); - } + writeStartElement(writer, reader.getName()); // 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); } @@ -205,6 +228,45 @@ public class XMLStreamSerializer implements XMLStreamConstants { } + public void writeStartElement(XMLStreamWriter writer, QName name) throws XMLStreamException { + writeStartElement(writer, name.getPrefix(), name.getLocalPart(), name.getNamespaceURI()); + } + + public void writeStartElement(XMLStreamWriter writer, String prefix, String localName, String namespaceURI) + throws XMLStreamException { + + if (namespaceURI == null) { + namespaceURI = NULL_NS_URI; + } + if (prefix == null) { + prefix = DEFAULT_NS_PREFIX; + } + + if (isRepairingNamespaces) { + writer.writeStartElement(prefix, localName, namespaceURI); + return; + } + + String writerPrefix = writer.getPrefix(namespaceURI); + if (writerPrefix != null) { + // Namespace is bound + writer.writeStartElement(writerPrefix, localName, namespaceURI); + } else { + // Namespace is not bound + if (NULL_NS_URI.equals(namespaceURI)) { + writer.writeStartElement(localName); + String defaultNS = writer.getNamespaceContext().getNamespaceURI(DEFAULT_NS_PREFIX); + if (defaultNS != null && !NULL_NS_URI.equals(defaultNS)) { + writer.writeNamespace(prefix, namespaceURI); + } + } else { + writer.writeStartElement(prefix, localName, namespaceURI); + // writeNamespace() will call setPrefix() + writer.writeNamespace(prefix, namespaceURI); + } + } + } + /** * Method serializeEndElement. * @@ -224,10 +286,22 @@ public class XMLStreamSerializer implements XMLStreamConstants { * @throws XMLStreamException */ private void serializeNamespace(String prefix, String uri, XMLStreamWriter writer) throws XMLStreamException { + writeNamespace(writer, prefix, uri); + } + + public String writeNamespace(XMLStreamWriter writer, String prefix, String uri) throws XMLStreamException { + if (uri == null) { + uri = NULL_NS_URI; + } String prefix1 = writer.getPrefix(uri); if (prefix1 == null) { + if (prefix == null) { + prefix = DEFAULT_NS_PREFIX; + } writer.writeNamespace(prefix, uri); - // writer.setPrefix(prefix, uri); + return prefix; + } else { + return prefix1; } } diff --git a/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/StAXHelperTestCase.java b/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/StAXHelperTestCase.java index e853513e5a..77f060b081 100644 --- a/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/StAXHelperTestCase.java +++ b/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/StAXHelperTestCase.java @@ -25,6 +25,7 @@ import java.net.URL; import java.util.List; import javax.xml.namespace.QName; +import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamReader; import org.apache.tuscany.sca.common.xml.stax.StAXHelper.Attribute; @@ -41,8 +42,8 @@ import org.w3c.dom.Node; */ public class StAXHelperTestCase { private static final String XML = - "<a:foo xmlns:a='http://a' name='foo'><bar name='bar'>" + "<doo a:name='doo' xmlns:a='http://doo'/>" - + "</bar></a:foo>"; + "<a:foo xmlns:a='http://foo' name='foo' xmlns='http://foo1'><bar name='bar'>" + "<doo a:name='doo' xmlns:a='http://doo'/>" + + "</bar><bar1 xmlns='http://bar1' name='bar1'/><bar2 xmlns='' name='bar2'/></a:foo>"; public static final QName WSDL11 = new QName("http://schemas.xmlsoap.org/wsdl/", "definitions"); public static final QName WSDL20 = new QName("http://www.w3.org/ns/wsdl", "description"); public static final QName XSD = new QName("http://www.w3.org/2001/XMLSchema", "schema"); @@ -62,7 +63,18 @@ public class StAXHelperTestCase { xml = helper.saveAsString(reader); XMLAssert.assertXMLEqual(XML, xml); } - + + @Test + public void testNoRepairingNamespaces() throws Exception { + StAXHelper helper = new StAXHelper(new DefaultExtensionPointRegistry()); + helper.getOutputFactory().setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE); + XMLStreamReader reader = helper.createXMLStreamReader(XML); + String xml = helper.saveAsString(reader); + XMLAssert.assertXMLEqual(XML, xml); + reader = helper.createXMLStreamReader(xml); + assertNotNull(reader); + } + @Test public void testIndex() throws Exception { StAXHelper helper = new StAXHelper(new DefaultExtensionPointRegistry()); diff --git a/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/reader/XmlTreeStreamReaderTestCase.java b/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/reader/XmlTreeStreamReaderTestCase.java index 2c2f1f0583..264c1a0d61 100644 --- a/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/reader/XmlTreeStreamReaderTestCase.java +++ b/java/sca/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/reader/XmlTreeStreamReaderTestCase.java @@ -35,9 +35,6 @@ import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; import org.apache.tuscany.sca.common.xml.stax.impl.XMLStreamSerializer; -import org.apache.tuscany.sca.common.xml.stax.reader.XmlNode; -import org.apache.tuscany.sca.common.xml.stax.reader.XmlNodeIterator; -import org.apache.tuscany.sca.common.xml.stax.reader.XmlTreeStreamReaderImpl; import org.custommonkey.xmlunit.XMLAssert; import org.junit.Assert; import org.junit.Before; @@ -144,7 +141,9 @@ public class XmlTreeStreamReaderTestCase { XmlTreeStreamReaderImpl reader = new XmlTreeStreamReaderImpl(root); XMLStreamSerializer serializer = new XMLStreamSerializer(); StringWriter sw = new StringWriter(); - XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(sw); + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); + XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sw); serializer.serialize(reader, writer); String xml = sw.toString(); XMLAssert.assertXMLEqual(XML_RESULT, xml); 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()); } diff --git a/java/sca/modules/definitions-xml/META-INF/MANIFEST.MF b/java/sca/modules/definitions-xml/META-INF/MANIFEST.MF index 682aacaf39..b9e4e04b91 100644 --- a/java/sca/modules/definitions-xml/META-INF/MANIFEST.MF +++ b/java/sca/modules/definitions-xml/META-INF/MANIFEST.MF @@ -12,6 +12,7 @@ Bundle-Description: Apache Tuscany SCA Definitions XML Model Import-Package: javax.xml.namespace,
javax.xml.stream,
org.apache.tuscany.sca.assembly;version="2.0.0",
+ org.apache.tuscany.sca.common.java.io;version="2.0.0",
org.apache.tuscany.sca.contribution.processor;version="2.0.0",
org.apache.tuscany.sca.contribution.resolver;version="2.0.0",
org.apache.tuscany.sca.core;version="2.0.0",
diff --git a/java/sca/modules/definitions-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsDocumentProcessor.java b/java/sca/modules/definitions-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsDocumentProcessor.java index 7213783478..3691212960 100644 --- a/java/sca/modules/definitions-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsDocumentProcessor.java +++ b/java/sca/modules/definitions-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsDocumentProcessor.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URL; -import java.net.URLConnection; import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; @@ -33,6 +32,7 @@ import javax.xml.stream.XMLStreamConstants; 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.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; @@ -103,9 +103,7 @@ public class DefinitionsDocumentProcessor implements URLArtifactProcessor<Defini try { urlStream = AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>() { public InputStream run() throws IOException { - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); - return connection.getInputStream(); + return IOHelper.openStream(url); } }); } catch (PrivilegedActionException e) { diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java index 4f74094a1f..173a37cd77 100644 --- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java +++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/xml/JavaImplementationProcessor.java @@ -153,7 +153,6 @@ public class JavaImplementationProcessor implements StAXArtifactProcessor<JavaIm XMLStreamException { // Write an <implementation.java> - policyProcessor.writePolicyPrefixes(javaImplementation, writer); writer.writeStartElement(SCA11_NS, IMPLEMENTATION_JAVA); policyProcessor.writePolicyAttributes(javaImplementation, writer); diff --git a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java index 7d863c99de..c45f8ccaa7 100644 --- a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java +++ b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/xml/WriteTestCase.java @@ -19,8 +19,8 @@ package org.apache.tuscany.sca.implementation.java.xml; -import java.io.ByteArrayOutputStream; import java.io.InputStream; +import java.io.StringWriter; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; @@ -50,6 +50,7 @@ public class WriteTestCase { DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); inputFactory = XMLInputFactory.newInstance(); outputFactory = XMLOutputFactory.newInstance(); + outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory, null); } @@ -59,8 +60,9 @@ public class WriteTestCase { InputStream is = getClass().getResourceAsStream("Calculator.composite"); Composite composite = (Composite)staxProcessor.read(inputFactory.createXMLStreamReader(is)); Assert.assertNotNull(composite); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - staxProcessor.write(composite, outputFactory.createXMLStreamWriter(bos)); + StringWriter sw = new StringWriter(); + staxProcessor.write(composite, outputFactory.createXMLStreamWriter(sw)); + System.out.println(sw.toString()); } } diff --git a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java index ed67479eed..38883ec062 100644 --- a/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java +++ b/java/sca/modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/xml/SpringImplementationProcessor.java @@ -172,7 +172,6 @@ public class SpringImplementationProcessor implements StAXArtifactProcessor<Spri public void write(SpringImplementation springImplementation, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { // Write <implementation.spring> - policyProcessor.writePolicyPrefixes(springImplementation, writer); writer.writeStartElement(Constants.SCA11_NS, IMPLEMENTATION_SPRING); policyProcessor.writePolicyAttributes(springImplementation, writer); diff --git a/java/sca/modules/interface-wsdl/META-INF/MANIFEST.MF b/java/sca/modules/interface-wsdl/META-INF/MANIFEST.MF index 15fab8fc0b..05f351494f 100644 --- a/java/sca/modules/interface-wsdl/META-INF/MANIFEST.MF +++ b/java/sca/modules/interface-wsdl/META-INF/MANIFEST.MF @@ -24,6 +24,7 @@ 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.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";resolution:=optional,
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 ad05f2b626..e925e815fa 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 @@ -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.java.io.IOHelper; import org.apache.tuscany.sca.common.xml.XMLDocumentHelper; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; @@ -386,7 +387,7 @@ public class WSDLModelResolver implements ModelResolver { try { URL artifactURL = wsdlDef.getLocation().toURL(); // Read a WSDL document - InputStream is = artifactURL.openStream(); + InputStream is = IOHelper.openStream(artifactURL); WSDLReader reader = wsdl4jFactory.newWSDLReader(); reader.setFeature("javax.wsdl.verbose", false); reader.setFeature("javax.wsdl.importDocuments", true); diff --git a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java b/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java deleted file mode 100644 index 3df50597b6..0000000000 --- a/java/sca/modules/node-impl-osgi/src/main/java/org/apache/tuscany/sca/node/osgi/impl/NodeUtil.java +++ /dev/null @@ -1,87 +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.node.osgi.impl; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; - -import org.apache.tuscany.sca.contribution.Contribution; -import org.apache.tuscany.sca.contribution.ContributionFactory; - -/** - * NodeUtil - * - * @version $Rev$ $Date$ - */ -public class NodeUtil { - private NodeUtil() { - } - - static Contribution contribution(ContributionFactory contributionFactory, org.apache.tuscany.sca.node.Contribution c) { - Contribution contribution = contributionFactory.createContribution(); - contribution.setURI(c.getURI()); - contribution.setLocation(c.getLocation()); - contribution.setUnresolved(true); - return contribution; - } - - /** - * Open a URL connection without cache - * @param url - * @return - * @throws IOException - */ - static InputStream openStream(URL url) throws IOException { - InputStream is = null; - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); - is = connection.getInputStream(); - return is; - } - - /** - * Escape the space in URL string - * @param uri - * @return - */ - static URI createURI(String uri) { - 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); - } - } - -} diff --git a/java/sca/modules/node-impl/META-INF/MANIFEST.MF b/java/sca/modules/node-impl/META-INF/MANIFEST.MF index 181aa5a40d..bb4dc4f5e5 100644 --- a/java/sca/modules/node-impl/META-INF/MANIFEST.MF +++ b/java/sca/modules/node-impl/META-INF/MANIFEST.MF @@ -23,6 +23,8 @@ Import-Package: javax.management, org.apache.tuscany.sca.assembly;version="2.0.0",
org.apache.tuscany.sca.assembly.builder;version="2.0.0",
org.apache.tuscany.sca.assembly.xml;version="2.0.0",
+ org.apache.tuscany.sca.common.java.io;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.processor;version="2.0.0",
org.apache.tuscany.sca.contribution.resolver;version="2.0.0",
diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessor.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessor.java index 31f21ee09f..e91c5961fc 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessor.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessor.java @@ -25,19 +25,19 @@ import java.io.StringWriter; import java.util.StringTokenizer; import javax.xml.namespace.QName; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; 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; import org.apache.tuscany.sca.node.configuration.BindingConfiguration; @@ -65,25 +65,15 @@ public class NodeConfigurationProcessor extends BaseStAXArtifactProcessor implem private StAXArtifactProcessor processor; private NodeConfigurationFactory nodeConfigurationFactory; - private XMLInputFactory xmlInputFactory; - private XMLOutputFactory xmlOutputFactory; + private StAXHelper helper; - public NodeConfigurationProcessor(FactoryExtensionPoint modelFactories, + public NodeConfigurationProcessor(ExtensionPointRegistry registry, StAXArtifactProcessor processor, Monitor monitor) { + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); this.nodeConfigurationFactory = modelFactories.getFactory(NodeConfigurationFactory.class); this.processor = processor; - this.xmlInputFactory = modelFactories.getFactory(XMLInputFactory.class); - this.xmlOutputFactory = modelFactories.getFactory(XMLOutputFactory.class); - } - - public NodeConfigurationProcessor(NodeConfigurationFactory nodeConfigurationFactory, - XMLInputFactory xmlInputFactory, - XMLOutputFactory xmlOutputFactory) { - super(); - this.nodeConfigurationFactory = nodeConfigurationFactory; - this.xmlInputFactory = xmlInputFactory; - this.xmlOutputFactory = xmlOutputFactory; + this.helper = StAXHelper.getInstance(registry); } public QName getArtifactType() { @@ -153,9 +143,8 @@ public class NodeConfigurationProcessor extends BaseStAXArtifactProcessor implem } */ StringWriter sw = new StringWriter(); - XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(sw); - new XMLStreamSerializer().serialize(reader, writer); - writer.flush(); + XMLStreamWriter writer = helper.createXMLStreamWriter(sw); + helper.save(reader, writer); composite.setContent(sw.toString()); } break; @@ -207,9 +196,9 @@ public class NodeConfigurationProcessor extends BaseStAXArtifactProcessor implem new XAttr("location", dc.getLocation()), new XAttr("contribution", dc.getContributionURI())); if (dc.getContent() != null) { - XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(new StringReader(dc.getContent())); + XMLStreamReader reader = helper.createXMLStreamReader(new StringReader(dc.getContent())); reader.nextTag(); // Move to the first element - new XMLStreamSerializer().serialize(reader, writer); + helper.save(reader, writer); reader.close(); } writeEnd(writer); diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/XMLStreamSerializer.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/XMLStreamSerializer.java deleted file mode 100644 index 1ddf25953d..0000000000 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/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.node.configuration.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/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java index 9f2303a93f..8c4ebabe79 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeFactoryImpl.java @@ -20,8 +20,8 @@ package org.apache.tuscany.sca.node.impl; import static java.lang.System.currentTimeMillis; -import static org.apache.tuscany.sca.node.impl.NodeUtil.createURI; -import static org.apache.tuscany.sca.node.impl.NodeUtil.openStream; +import static org.apache.tuscany.sca.common.java.io.IOHelper.createURI; +import static org.apache.tuscany.sca.common.java.io.IOHelper.openStream; import java.io.File; import java.io.IOException; @@ -48,7 +48,6 @@ import java.util.logging.Logger; 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; @@ -58,6 +57,7 @@ import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; import org.apache.tuscany.sca.assembly.builder.CompositeBuilderExtensionPoint; import org.apache.tuscany.sca.assembly.builder.CompositeBuilderTmp; import org.apache.tuscany.sca.assembly.builder.EndpointReferenceBuilder; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; @@ -100,7 +100,6 @@ import org.apache.tuscany.sca.node.configuration.BindingConfiguration; import org.apache.tuscany.sca.node.configuration.ContributionConfiguration; import org.apache.tuscany.sca.node.configuration.DeploymentComposite; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; -import org.apache.tuscany.sca.node.configuration.xml.NodeConfigurationProcessor; import org.apache.tuscany.sca.work.WorkScheduler; import org.oasisopen.sca.ServiceRuntimeException; @@ -171,12 +170,14 @@ public class NodeFactoryImpl extends NodeFactory { @Override public NodeConfiguration loadConfiguration(InputStream xml) { try { - XMLInputFactory inputFactory = getFactory(XMLInputFactory.class); - XMLOutputFactory outputFactory = getFactory(XMLOutputFactory.class); - XMLStreamReader reader = inputFactory.createXMLStreamReader(xml); - NodeConfigurationProcessor processor = new NodeConfigurationProcessor(this, inputFactory, outputFactory); + init(); + StAXHelper helper = StAXHelper.getInstance(extensionPoints); + XMLStreamReader reader = helper.createXMLStreamReader(xml); + StAXArtifactProcessorExtensionPoint processors = + extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + StAXArtifactProcessor processor = processors.getProcessor(NodeConfiguration.class); reader.nextTag(); - NodeConfiguration config = processor.read(reader); + NodeConfiguration config = (NodeConfiguration)processor.read(reader); xml.close(); return config; } catch (Throwable e) { diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java index ca9706bc22..92bd54a3d8 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java @@ -33,6 +33,7 @@ import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.CompositeService; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.common.java.io.IOHelper; import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.UtilityExtensionPoint; @@ -116,7 +117,7 @@ public class NodeImpl implements Node, Client { // Start the composite compositeActivator.start(compositeActivator.getDomainComposite()); - NodeFinder.addNode(NodeUtil.createURI(configuration.getDomainURI()), this); + NodeFinder.addNode(IOHelper.createURI(configuration.getDomainURI()), this); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); try { diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeUtil.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeUtil.java index 3f65d49740..4da41442f5 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeUtil.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeUtil.java @@ -19,13 +19,6 @@ package org.apache.tuscany.sca.node.impl; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; - import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.ContributionFactory; @@ -47,45 +40,4 @@ public class NodeUtil { return contribution; } - /** - * Open a URL connection without cache - * @param url - * @return - * @throws IOException - */ - static InputStream openStream(URL url) throws IOException { - InputStream is = null; - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); - is = connection.getInputStream(); - return is; - } - - /** - * Escape the space in URL string - * @param uri - * @return - */ - 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); - } - } - } diff --git a/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeUtilTestCase.java b/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeUtilTestCase.java index 725bce5e09..5a47465702 100644 --- a/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeUtilTestCase.java +++ b/java/sca/modules/node-impl/src/test/java/org/apache/tuscany/sca/node/impl/NodeUtilTestCase.java @@ -19,6 +19,7 @@ package org.apache.tuscany.sca.node.impl; +import org.apache.tuscany.sca.common.java.io.IOHelper; import org.junit.Assert; import org.junit.Test; @@ -29,10 +30,10 @@ import org.junit.Test; public class NodeUtilTestCase { @Test public void testCreateURI() { - Assert.assertEquals("/a/b", NodeUtil.createURI("/a/b").toString()); - Assert.assertEquals("/a%20b", NodeUtil.createURI("/a b").toString()); - Assert.assertEquals("file:/a/b", NodeUtil.createURI("file:/a/b").toString()); - Assert.assertEquals("file:/a%20b", NodeUtil.createURI("file:/a b").toString()); - Assert.assertEquals("file:/a%20b", NodeUtil.createURI("file:/a%20b").toString()); + Assert.assertEquals("/a/b", IOHelper.createURI("/a/b").toString()); + Assert.assertEquals("/a%20b", IOHelper.createURI("/a b").toString()); + Assert.assertEquals("file:/a/b", IOHelper.createURI("file:/a/b").toString()); + Assert.assertEquals("file:/a%20b", IOHelper.createURI("file:/a b").toString()); + Assert.assertEquals("file:/a%20b", IOHelper.createURI("file:/a%20b").toString()); } } diff --git a/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite b/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite index 073b636793..4d2dbe721c 100644 --- a/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite +++ b/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/Calculator.composite @@ -23,7 +23,7 @@ name="Calculator"> <component name="CalculatorServiceComponent" requires="sca:confidentiality"> - <implementation.java class="calculator.CalculatorServiceImpl"/> + <implementation.java class="calculator.CalculatorServiceImpl" xmlns:test="http://test" requiers="test:TestIntentOne"/> <reference name="addService" target="AddServiceComponent" /> <reference name="subtractService" target="SubtractServiceComponent" /> <reference name="multiplyService" target="MultiplyServiceComponent" /> diff --git a/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/test_definitions.xml b/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/test_definitions.xml index 6d41a484d7..17880254a1 100644 --- a/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/test_definitions.xml +++ b/java/sca/modules/policy-xml/src/test/resources/org/apache/tuscany/sca/policy/xml/test_definitions.xml @@ -64,7 +64,7 @@ <policySet name="SecureMessagingPolicies" provides="test:confidentiality" appliesTo="binding.ws" xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> - <intentMap provides="test:confidentiality" default="transport"> + <intentMap provides="test:confidentiality"> <qualifier name="transport"> <wsp:PolicyAttachment> <!-- policy expression and policy subject for "transport" alternative --> @@ -81,7 +81,7 @@ <policySet name="SecurityPolicy" provides="test:confidentiality" appliesTo="binding.ws" xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> - <intentMap provides="test:confidentiality" default="message"> + <intentMap provides="test:confidentiality"> <qualifier name="message"> <wsp:PolicyAttachment> <!-- policy attachment for body encryption --> @@ -111,7 +111,7 @@ <policySet name="ConfidentialityPolicies" provides="test:confidentiality" appliesTo="binding.ws" xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> - <intentMap provides="test:confidentiality" default="transport"> + <intentMap provides="test:confidentiality"> <qualifier name="transport"> <wsp:PolicyAttachment> <!-- policy expression and policy subject for "transport" diff --git a/java/sca/modules/pom.xml b/java/sca/modules/pom.xml index a318dc764d..218971f8f5 100644 --- a/java/sca/modules/pom.xml +++ b/java/sca/modules/pom.xml @@ -44,6 +44,7 @@ <module>binding-ws-axis2-policy</module> <module>binding-ws-wsdlgen</module> <module>binding-ws-xml</module> + <module>common-java</module> <module>common-xml</module> <module>contribution</module> <module>core</module> |