summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml
diff options
context:
space:
mode:
Diffstat (limited to 'tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml')
-rw-r--r--tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationATOMProcessor.java160
-rw-r--r--tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessor.java240
-rw-r--r--tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/XMLStreamSerializer.java287
3 files changed, 687 insertions, 0 deletions
diff --git a/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationATOMProcessor.java b/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationATOMProcessor.java
new file mode 100644
index 0000000000..d3d0c5d077
--- /dev/null
+++ b/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationATOMProcessor.java
@@ -0,0 +1,160 @@
+/*
+ * 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 static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
+import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
+
+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;
+
+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.FactoryExtensionPoint;
+import org.apache.tuscany.sca.monitor.Monitor;
+import org.apache.tuscany.sca.node.configuration.ContributionConfiguration;
+import org.apache.tuscany.sca.node.configuration.NodeConfiguration;
+import org.apache.tuscany.sca.node.configuration.NodeConfigurationFactory;
+
+/**
+ * Implements a StAX artifact processor for configured node implementations.
+ *
+ * @version $Rev: 704156 $ $Date: 2008-10-13 09:31:59 -0700 (Mon, 13 Oct 2008) $
+ */
+public class NodeConfigurationATOMProcessor extends BaseStAXArtifactProcessor implements
+ StAXArtifactProcessor<NodeConfiguration> {
+
+ private static final String ATOM_NS = "http://www.w3.org/2005/Atom";
+ private static final QName FEED_QNAME = new QName(ATOM_NS, "feed");
+ private static final QName ENTRY_QNAME = new QName(ATOM_NS, "entry");
+ private static final QName ID_QNAME = new QName(ATOM_NS, "id");
+ private static final QName LINK_QNAME = new QName(ATOM_NS, "link");
+ private static final QName CONTENT_QNAME = new QName(ATOM_NS, "content");
+ private static final String HREF = "href";
+
+ private NodeConfigurationFactory factory;
+
+ public NodeConfigurationATOMProcessor(FactoryExtensionPoint modelFactories, Monitor monitor) {
+ this.factory = modelFactories.getFactory(NodeConfigurationFactory.class);
+ }
+
+ public QName getArtifactType() {
+ return null;
+ }
+
+ public Class<NodeConfiguration> getModelType() {
+ // Returns the type of model processed by this processor
+ return NodeConfiguration.class;
+ }
+
+ public NodeConfiguration read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
+
+ NodeConfiguration config = factory.createNodeConfiguration();
+
+ // Read a feed containing links to the composite and the contributions assigned to
+ // the node
+ ContributionConfiguration contribution = null;
+ boolean id = false;
+ QName name = null;
+
+ while (reader.hasNext()) {
+ int event = reader.getEventType();
+ switch (event) {
+
+ case START_ELEMENT:
+ name = reader.getName();
+
+ if (ENTRY_QNAME.equals(name)) {
+
+ // Read an <entry>
+ contribution = factory.createContributionConfiguration();
+ } else if (ID_QNAME.equals(name)) {
+
+ // Read an <id>
+ id = true;
+
+ } else if (LINK_QNAME.equals(name)) {
+
+ // Read a <link>
+ String href = getString(reader, HREF);
+
+ if (contribution != null) {
+ contribution.setLocation(href);
+ }
+ } else if (CONTENT_QNAME.equals(name)) {
+ // Read a <content>
+ } else if (FEED_QNAME.equals(name)) {
+ // Read a <feed>
+ }
+ break;
+
+ case XMLStreamConstants.CHARACTERS:
+
+ // Read characters inside an <id> element
+ if (id) {
+ if (contribution != null) {
+ contribution.setURI(reader.getText());
+ }
+ }
+ break;
+
+ case END_ELEMENT:
+ name = reader.getName();
+
+ // Clear current state when reading reaching end element
+ if (ENTRY_QNAME.equals(name)) {
+ if (contribution != null) {
+ config.getContributions().add(contribution);
+ }
+
+ contribution = null;
+
+ } else if (ID_QNAME.equals(name)) {
+ id = false;
+
+ } else if (FEED_QNAME.equals(name)) {
+
+ // We've reached the end of the feed
+ return config;
+ }
+ break;
+ }
+
+ // Read the next element
+ if (reader.hasNext()) {
+ reader.next();
+ }
+ }
+ return config;
+ }
+
+ public void resolve(NodeConfiguration implementation, ModelResolver resolver) throws ContributionResolveException {
+ }
+
+ public void write(NodeConfiguration implementation, XMLStreamWriter writer) throws ContributionWriteException,
+ XMLStreamException {
+ }
+}
diff --git a/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessor.java b/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessor.java
new file mode 100644
index 0000000000..fb16cf461d
--- /dev/null
+++ b/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/NodeConfigurationProcessor.java
@@ -0,0 +1,240 @@
+/*
+ * 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 static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
+
+import java.io.StringReader;
+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.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.FactoryExtensionPoint;
+import org.apache.tuscany.sca.monitor.Monitor;
+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.NodeConfigurationFactory;
+
+/**
+ * Implements a StAX artifact processor for node implementations.
+ *
+ * @version $Rev: 750323 $ $Date: 2009-03-04 21:52:01 -0800 (Wed, 04 Mar 2009) $
+ */
+public class NodeConfigurationProcessor extends BaseStAXArtifactProcessor implements
+ StAXArtifactProcessor<NodeConfiguration> {
+ private static final String SCA11_TUSCANY_NS = "http://tuscany.apache.org/xmlns/sca/1.1";
+ private static final QName NODE = new QName(SCA11_TUSCANY_NS, "node");
+ private static final QName CONTRIBUTION = new QName(SCA11_TUSCANY_NS, "contribution");
+ private static final QName BINDING = new QName(SCA11_TUSCANY_NS, "binding");
+ private static final QName BASE_URI = new QName(SCA11_TUSCANY_NS, "baseURI");
+ private static final QName DEPLOYMENT_COMPOSITE = new QName(SCA11_TUSCANY_NS, "deploymentComposite");
+
+ private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200903";
+ private static final QName COMPOSITE = new QName(SCA11_NS, "composite");
+
+ private StAXArtifactProcessor processor;
+ private NodeConfigurationFactory nodeConfigurationFactory;
+ private XMLInputFactory xmlInputFactory;
+ private XMLOutputFactory xmlOutputFactory;
+
+ public NodeConfigurationProcessor(FactoryExtensionPoint modelFactories,
+ StAXArtifactProcessor processor,
+ Monitor monitor) {
+ 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;
+ }
+
+ public QName getArtifactType() {
+ // Returns the QName of the XML element processed by this processor
+ return NODE;
+ }
+
+ public Class<NodeConfiguration> getModelType() {
+ // Returns the type of model processed by this processor
+ return NodeConfiguration.class;
+ }
+
+ public NodeConfiguration read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
+
+ NodeConfiguration node = null;
+ ContributionConfiguration contribution = null;
+ DeploymentComposite composite = null;
+ BindingConfiguration binding = null;
+
+ // Skip to end element
+ while (true) {
+ int event = reader.getEventType();
+ switch (event) {
+ case XMLStreamConstants.START_ELEMENT:
+ QName name = reader.getName();
+ if (NODE.equals(name)) {
+ node = nodeConfigurationFactory.createNodeConfiguration();
+ node.setURI(reader.getAttributeValue(null, "uri"));
+ node.setDomainURI(reader.getAttributeValue(null, "domain"));
+ } else if (CONTRIBUTION.equals(name)) {
+ contribution = nodeConfigurationFactory.createContributionConfiguration();
+ contribution.setURI(reader.getAttributeValue(null, "uri"));
+ contribution.setLocation(reader.getAttributeValue(null, "location"));
+ node.getContributions().add(contribution);
+ } else if (BINDING.equals(name)) {
+ binding = nodeConfigurationFactory.createBindingConfiguration();
+ binding.setBindingType(getQName(reader, "name"));
+ String baseURIs = reader.getAttributeValue(null, "baseURIs");
+ if (baseURIs != null) {
+ StringTokenizer tokenizer = new StringTokenizer(baseURIs);
+ while (tokenizer.hasMoreTokens()) {
+ binding.getBaseURIs().add(tokenizer.nextToken());
+ }
+ }
+ node.getBindings().add(binding);
+ } else if (DEPLOYMENT_COMPOSITE.equals(name)) {
+ composite = nodeConfigurationFactory.createDeploymentComposite();
+ composite.setLocation(reader.getAttributeValue(null, "location"));
+ if (contribution != null) {
+ contribution.getDeploymentComposites().add(composite);
+ }
+ } else if(BASE_URI.equals(name)) {
+ // We also support <baseURI> element
+ String baseURI = reader.getElementText();
+ if (baseURI != null && binding != null) {
+ baseURI = baseURI.trim();
+ binding.addBaseURI(baseURI);
+ }
+ // getElementText() moves the event to END_ELEMENT
+ continue;
+ } else if (COMPOSITE.equals(name)) {
+ /*
+ Object model = processor.read(reader);
+ if (model instanceof Composite) {
+ // FIXME: We need to capture the text here
+ // composite.setComposite((Composite)model);
+ }
+ */
+ StringWriter sw = new StringWriter();
+ XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(sw);
+ new XMLStreamSerializer().serialize(reader, writer);
+ writer.flush();
+ composite.setContent(sw.toString());
+ }
+ break;
+
+ case END_ELEMENT:
+ name = reader.getName();
+ if (NODE.equals(name)) {
+ return node;
+ } else if (CONTRIBUTION.equals(name)) {
+ contribution = null;
+ } else if (DEPLOYMENT_COMPOSITE.equals(name)) {
+ composite = null;
+ } else if (BINDING.equals(name)) {
+ binding = null;
+ }
+ }
+ if (reader.hasNext()) {
+ reader.next();
+ } else {
+ break;
+ }
+ }
+
+ return node;
+ }
+
+ public void resolve(NodeConfiguration node, ModelResolver resolver) throws ContributionResolveException {
+ }
+
+ public void write(NodeConfiguration node, XMLStreamWriter writer) throws ContributionWriteException,
+ XMLStreamException {
+
+ writeStart(writer,
+ NODE.getNamespaceURI(),
+ NODE.getLocalPart(),
+ new XAttr("uri", node.getURI()),
+ new XAttr("domain", node.getDomainURI()));
+
+ for (ContributionConfiguration c : node.getContributions()) {
+ writeStart(writer,
+ CONTRIBUTION.getNamespaceURI(),
+ CONTRIBUTION.getLocalPart(),
+ new XAttr("uri", c.getURI()),
+ new XAttr("location", c.getLocation()));
+ for (DeploymentComposite dc : c.getDeploymentComposites()) {
+ writeStart(writer,
+ DEPLOYMENT_COMPOSITE.getNamespaceURI(),
+ DEPLOYMENT_COMPOSITE.getLocalPart(),
+ new XAttr("location", dc.getLocation()),
+ new XAttr("contribution", dc.getContributionURI()));
+ if (dc.getContent() != null) {
+ XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(new StringReader(dc.getContent()));
+ reader.nextTag(); // Move to the first element
+ new XMLStreamSerializer().serialize(reader, writer);
+ reader.close();
+ }
+ writeEnd(writer);
+ }
+ writeEnd(writer);
+ }
+
+ for (BindingConfiguration b : node.getBindings()) {
+ StringBuffer uris = new StringBuffer();
+ for (String uri : b.getBaseURIs()) {
+ uris.append(uri).append(' ');
+ }
+ if (uris.length() > 0) {
+ uris.deleteCharAt(uris.length() - 1); // Remove the trailing space
+ } else {
+ uris = null;
+ }
+ writeStart(writer,
+ BINDING.getNamespaceURI(),
+ BINDING.getLocalPart(),
+ new XAttr("name", b.getBindingType()),
+ new XAttr("baseURIs", uris.toString()));
+ writeEnd(writer);
+ }
+
+ writeEnd(writer);
+ }
+}
diff --git a/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/XMLStreamSerializer.java b/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/XMLStreamSerializer.java
new file mode 100644
index 0000000000..38d8215cb2
--- /dev/null
+++ b/tags/java/sca/2.0-M3-RC5/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/configuration/xml/XMLStreamSerializer.java
@@ -0,0 +1,287 @@
+/*
+ * 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: 656146 $ $Date: 2008-05-14 01:22:08 -0700 (Wed, 14 May 2008) $
+ */
+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());
+ }
+}