diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-07 07:37:15 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-07 07:37:15 +0000 |
commit | 9fa61b1574a693c2f100a23578d8159d1af54df2 (patch) | |
tree | 97756693fa9601d095fe68237c6614c629dcd274 /java/sca/modules | |
parent | 8ec974b5e3303e80dd96d752214790dc1ec21708 (diff) |
Replace the DOMHelper from common-xml module
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@801902 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules')
22 files changed, 164 insertions, 300 deletions
diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java index 4f6af5e791..3c3a2f2097 100644 --- a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/dom/DOMHelper.java @@ -26,10 +26,12 @@ import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamResult; @@ -97,6 +99,17 @@ public class DOMHelper { InputSource is = new InputSource(new StringReader(xmlString)); return builder.parse(is); } + + public Document load(Source source) { + Transformer transformer = newTransformer(); + DOMResult result = new DOMResult(newDocument()); + try { + transformer.transform(source, result); + } catch (TransformerException e) { + throw new IllegalArgumentException(e); + } + return (Document)result.getNode(); + } public NodeContentHandler createContentHandler(Node root) { if (root == null) { diff --git a/java/sca/modules/databinding-jaxb-axiom/META-INF/MANIFEST.MF b/java/sca/modules/databinding-jaxb-axiom/META-INF/MANIFEST.MF index a8b196f77c..a59109acef 100644 --- a/java/sca/modules/databinding-jaxb-axiom/META-INF/MANIFEST.MF +++ b/java/sca/modules/databinding-jaxb-axiom/META-INF/MANIFEST.MF @@ -25,6 +25,7 @@ Import-Package: javax.xml.bind, org.apache.axiom.om.impl,
org.apache.axiom.om.impl.builder,
org.apache.axiom.om.util,
+ org.apache.tuscany.sca.core;version="2.0.0",
org.apache.tuscany.sca.databinding;version="2.0.0",
org.apache.tuscany.sca.databinding.impl;version="2.0.0",
org.apache.tuscany.sca.databinding.jaxb;version="2.0.0",
diff --git a/java/sca/modules/databinding-jaxb-axiom/src/test/java/org/apache/tuscany/databinding/jaxb/axiom/JAXB2OMTestCase.java b/java/sca/modules/databinding-jaxb-axiom/src/test/java/org/apache/tuscany/databinding/jaxb/axiom/JAXB2OMTestCase.java index 543a76594f..9f79eeed04 100644 --- a/java/sca/modules/databinding-jaxb-axiom/src/test/java/org/apache/tuscany/databinding/jaxb/axiom/JAXB2OMTestCase.java +++ b/java/sca/modules/databinding-jaxb-axiom/src/test/java/org/apache/tuscany/databinding/jaxb/axiom/JAXB2OMTestCase.java @@ -26,6 +26,7 @@ import javax.xml.stream.XMLStreamReader; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.builder.StAXOMBuilder; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.impl.TransformationContextImpl; import org.apache.tuscany.sca.databinding.jaxb.JAXB2Node; @@ -69,7 +70,7 @@ public class JAXB2OMTestCase { System.out.println(sw.toString()); start = System.currentTimeMillis(); - Node node = new JAXB2Node().transform(po, tContext); + Node node = new JAXB2Node(new DefaultExtensionPointRegistry()).transform(po, tContext); XMLStreamReader reader = new Node2XMLStreamReader().transform(node, null); om = new StAXOMBuilder(reader).getDocumentElement(); sw = new StringWriter(); diff --git a/java/sca/modules/databinding-jaxb/META-INF/MANIFEST.MF b/java/sca/modules/databinding-jaxb/META-INF/MANIFEST.MF index 274579ba03..c4ca84e5fd 100644 --- a/java/sca/modules/databinding-jaxb/META-INF/MANIFEST.MF +++ b/java/sca/modules/databinding-jaxb/META-INF/MANIFEST.MF @@ -32,7 +32,9 @@ Import-Package: javax.activation, javax.xml.transform.dom,
javax.xml.transform.stream,
org.apache.tuscany.sca.assembly;version="2.0.0",
+ org.apache.tuscany.sca.common.xml.dom;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.databinding;version="2.0.0",
org.apache.tuscany.sca.databinding.impl;version="2.0.0",
org.apache.tuscany.sca.databinding.jaxb;version="2.0.0",
diff --git a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2Node.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2Node.java index 3e262c7d12..1ff8f79059 100644 --- a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2Node.java +++ b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2Node.java @@ -21,11 +21,13 @@ package org.apache.tuscany.sca.databinding.jaxb; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.TransformationException; import org.apache.tuscany.sca.databinding.impl.BaseTransformer; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; +import org.apache.tuscany.sca.databinding.xml.DOMDataBinding; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -34,7 +36,13 @@ import org.w3c.dom.Node; * @version $Rev$ $Date$ */ public class JAXB2Node extends BaseTransformer<Object, Node> implements PullTransformer<Object, Node> { - + private DOMHelper helper; + + public JAXB2Node(ExtensionPointRegistry registry) { + super(); + helper = DOMHelper.getInstance(registry); + } + public Node transform(Object source, TransformationContext tContext) { // if (source == null) { // return null; @@ -44,10 +52,10 @@ public class JAXB2Node extends BaseTransformer<Object, Node> implements PullTran Marshaller marshaller = context.createMarshaller(); // FIXME: The default Marshaller doesn't support // marshaller.getNode() - Document document = DOMHelper.newDocument(); + Document document = helper.newDocument(); Object jaxbElement = JAXBContextHelper.createJAXBElement(context, tContext.getSourceDataType(), source); marshaller.marshal(jaxbElement, document); - return DOMHelper.adjustElementName(tContext, document.getDocumentElement()); + return DOMDataBinding.adjustElementName(tContext, document.getDocumentElement()); } catch (Exception e) { throw new TransformationException(e); } diff --git a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java index 38efd288cb..14e5148050 100644 --- a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java +++ b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java @@ -26,10 +26,11 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.WrapperHandler; import org.apache.tuscany.sca.databinding.XMLTypeHelper; import org.apache.tuscany.sca.databinding.impl.BaseDataBinding; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; import org.apache.tuscany.sca.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; @@ -49,11 +50,13 @@ public class JAXBDataBinding extends BaseDataBinding { private JAXBWrapperHandler wrapperHandler; private JAXBTypeHelper xmlTypeHelper; + private DOMHelper domHelper; - public JAXBDataBinding() { + public JAXBDataBinding(ExtensionPointRegistry registry) { super(NAME, JAXBElement.class); this.wrapperHandler = new JAXBWrapperHandler(); this.xmlTypeHelper = new JAXBTypeHelper(); + this.domHelper = DOMHelper.getInstance(registry); } @Override @@ -105,7 +108,7 @@ public class JAXBDataBinding extends BaseDataBinding { } JAXBContext context = JAXBContextHelper.createJAXBContext(dataType); arg = JAXBContextHelper.createJAXBElement(context, dataType, arg); - Document doc = DOMHelper.newDocument(); + Document doc = domHelper.newDocument(); context.createMarshaller().marshal(arg, doc); Object value = context.createUnmarshaller().unmarshal(doc, dataType.getPhysical()); if (isElement && value instanceof JAXBElement) { diff --git a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBindingTestCase.java b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBindingTestCase.java index 97038734f5..e612af396b 100644 --- a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBindingTestCase.java +++ b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBindingTestCase.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertTrue; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; @@ -46,7 +47,7 @@ public class JAXBDataBindingTestCase { @Before public void setUp() throws Exception { - binding = new JAXBDataBinding(); + binding = new JAXBDataBinding(new DefaultExtensionPointRegistry()); } /** diff --git a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTestCase.java b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTestCase.java index 61e3fcaaab..f1329ab571 100644 --- a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTestCase.java +++ b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTestCase.java @@ -22,6 +22,8 @@ import java.io.StringReader; import javax.xml.namespace.QName; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.impl.TransformationContextImpl; import org.apache.tuscany.sca.interfacedef.DataType; @@ -68,6 +70,8 @@ public class JAXBTestCase { + " </item>" + " </items>" + "</ipo:purchaseOrder>"; + + private static ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); @Test public void testTransform() throws Exception { @@ -85,7 +89,7 @@ public class JAXBTestCase { TransformationContext tContext1 = new TransformationContextImpl(); tContext1.setSourceDataType(sourceDataType); - JAXB2Node t1 = new JAXB2Node(); + JAXB2Node t1 = new JAXB2Node(registry); Node node = t1.transform(object1, tContext1); Assert.assertNotNull(node); @@ -114,7 +118,7 @@ public class JAXBTestCase { TransformationContext tContext1 = new TransformationContextImpl(); tContext1.setSourceDataType(sourceDataType); - JAXB2Node t1 = new JAXB2Node(); + JAXB2Node t1 = new JAXB2Node(registry); Node node = t1.transform(object1, tContext1); Assert.assertNotNull(node); @@ -134,7 +138,7 @@ public class JAXBTestCase { tContext1.setSourceDataType(sourceDataType); - JAXB2Node t1 = new JAXB2Node(); + JAXB2Node t1 = new JAXB2Node(registry); PurchaseOrderType po = new ObjectFactory().createPurchaseOrderType(); Node node = t1.transform(po, tContext1); diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/DOMHelper.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/DOMHelper.java deleted file mode 100644 index 64b3d22c10..0000000000 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/DOMHelper.java +++ /dev/null @@ -1,163 +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.databinding.impl; - -import javax.xml.namespace.QName; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.apache.tuscany.sca.databinding.TransformationContext; -import org.apache.tuscany.sca.interfacedef.DataType; -import org.apache.tuscany.sca.interfacedef.util.XMLType; -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; - -/** - * Helper for DOM - * - * @version $Rev$ $Date$ - */ -public final class DOMHelper { - private static DocumentBuilderFactory FACTORY; - - private DOMHelper() { - } - - public static Document newDocument() throws ParserConfigurationException { - return newDocumentBuilder().newDocument(); - } - - public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { - init(); - return FACTORY.newDocumentBuilder(); - } - - /** - * - */ - private static synchronized void init() { - if (FACTORY == null) { - FACTORY = DocumentBuilderFactory.newInstance(); - FACTORY.setNamespaceAware(true); - } - } - - public static QName getQName(Node node) { - String ns = node.getNamespaceURI(); - if (ns == null) { - ns = ""; - } - // node.getLocalName() will return null if it is created using DOM Level - // 1 method - // such as createElement() - return new QName(ns, node.getNodeName()); - } - - public static Element createElement(Document document, QName name) { - String prefix = name.getPrefix(); - String qname = - (prefix != null && prefix.length() > 0) ? prefix + ":" + name.getLocalPart() : name.getLocalPart(); - return document.createElementNS(name.getNamespaceURI(), qname); - } - - /** - * Wrap an element as a DOM document - * @param node - * @return - */ - public static Document promote(Node node) { - if (node instanceof Document) { - return (Document)node; - } - Element element = (Element)node; - Document doc = element.getOwnerDocument(); - if (doc.getDocumentElement() == element) { - return doc; - } - doc = (Document)element.getOwnerDocument().cloneNode(false); - Element schema = (Element)doc.importNode(element, true); - doc.appendChild(schema); - Node parent = element.getParentNode(); - while (parent instanceof Element) { - Element root = (Element)parent; - NamedNodeMap nodeMap = root.getAttributes(); - for (int i = 0; i < nodeMap.getLength(); i++) { - Attr attr = (Attr)nodeMap.item(i); - String name = attr.getName(); - if ("xmlns".equals(name) || name.startsWith("xmlns:")) { - if (schema.getAttributeNode(name) == null) { - schema.setAttributeNodeNS((Attr)doc.importNode(attr, true)); - } - } - } - parent = parent.getParentNode(); - } - return doc; - } - - /** - * @param context - * @param element - */ - public static Element adjustElementName(TransformationContext context, Element element) { - if (context != null) { - DataType dataType = context.getTargetDataType(); - Object logical = dataType == null ? null : dataType.getLogical(); - if (!(logical instanceof XMLType)) { - return element; - } - XMLType xmlType = (XMLType)logical; - QName name = new QName(element.getNamespaceURI(), element.getLocalName()); - if (xmlType.isElement() && !xmlType.getElementName().equals(name)) { - QName newName = xmlType.getElementName(); - String prefix = newName.getPrefix(); - String qname = newName.getLocalPart(); - if (prefix != null && !prefix.equals("")) { - qname = prefix + ":" + qname; - } - Document doc = element.getOwnerDocument(); - Element newElement = doc.createElementNS(newName.getNamespaceURI(), qname); - // Copy the attributes to the new element - NamedNodeMap attrs = element.getAttributes(); - for (int i = 0; i < attrs.getLength(); i++) { - Attr attr = (Attr)doc.importNode(attrs.item(i), true); - newElement.getAttributes().setNamedItem(attr); - } - - // Move all the children - while (element.hasChildNodes()) { - newElement.appendChild(element.getFirstChild()); - } - - // Replace the old node with the new node - if (element.getParentNode() != null) { - element.getParentNode().replaceChild(newElement, element); - } - - return newElement; - } - } - return element; - } - -} diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/javabeans/JavaBean2DOMNodeTransformer.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/javabeans/JavaBean2DOMNodeTransformer.java index b29fdf4c55..f582499b70 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/javabeans/JavaBean2DOMNodeTransformer.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/javabeans/JavaBean2DOMNodeTransformer.java @@ -19,9 +19,9 @@ package org.apache.tuscany.sca.databinding.javabeans; import javax.xml.namespace.QName; -import javax.xml.parsers.ParserConfigurationException; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -34,15 +34,11 @@ import org.w3c.dom.Node; public class JavaBean2DOMNodeTransformer extends JavaBean2XMLTransformer<Node> { public static final String COLON = ":"; - private Document factory; + private DOMHelper helper; - public JavaBean2DOMNodeTransformer() { + public JavaBean2DOMNodeTransformer(ExtensionPointRegistry registry) { super(); - try { - factory = DOMHelper.newDocument(); - } catch (ParserConfigurationException e) { - throw new Java2XMLMapperException(e); - } + helper = DOMHelper.getInstance(registry); } @Override @@ -55,16 +51,17 @@ public class JavaBean2DOMNodeTransformer extends JavaBean2XMLTransformer<Node> { String qualifedName = (qName.getPrefix() == null || qName.getPrefix().length() <= 0) ? qName.getLocalPart() : qName.getPrefix() + COLON + qName.getLocalPart(); - return factory.createElementNS(qName.getNamespaceURI(), qualifedName); + return helper.newDocument().createElementNS(qName.getNamespaceURI(), qualifedName); } @Override public void appendText(Node parentElement, String textData) throws Java2XMLMapperException { + Document document = helper.newDocument(); Node textNode; if (textData != null) { - textNode = factory.createTextNode(textData); + textNode = document.createTextNode(textData); } else { - Attr nil = factory.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:nil"); + Attr nil = document.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:nil"); nil.setValue("true"); textNode = nil; } diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMDataBinding.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMDataBinding.java index e029604722..ed0b5c9923 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMDataBinding.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMDataBinding.java @@ -21,11 +21,18 @@ package org.apache.tuscany.sca.databinding.xml; import javax.xml.namespace.QName; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.WrapperHandler; import org.apache.tuscany.sca.databinding.impl.BaseDataBinding; import org.apache.tuscany.sca.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.util.XMLType; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; /** @@ -38,14 +45,17 @@ public class DOMDataBinding extends BaseDataBinding { public static final String ROOT_NAMESPACE = "http://tuscany.apache.org/xmlns/sca/databinding/dom/1.0"; public static final QName ROOT_ELEMENT = new QName(ROOT_NAMESPACE, "root"); + + private DOMHelper domHelper; - public DOMDataBinding() { + public DOMDataBinding(ExtensionPointRegistry registry) { super(NAME, Node.class); + this.domHelper = DOMHelper.getInstance(registry); } @Override public WrapperHandler getWrapperHandler() { - return new DOMWrapperHandler(); + return new DOMWrapperHandler(domHelper); } @Override @@ -68,4 +78,49 @@ public class DOMDataBinding extends BaseDataBinding { } return false; } + + /** + * @param context + * @param element + */ + public static Element adjustElementName(TransformationContext context, Element element) { + if (context != null) { + DataType dataType = context.getTargetDataType(); + Object logical = dataType == null ? null : dataType.getLogical(); + if (!(logical instanceof XMLType)) { + return element; + } + XMLType xmlType = (XMLType)logical; + QName name = new QName(element.getNamespaceURI(), element.getLocalName()); + if (xmlType.isElement() && !xmlType.getElementName().equals(name)) { + QName newName = xmlType.getElementName(); + String prefix = newName.getPrefix(); + String qname = newName.getLocalPart(); + if (prefix != null && !prefix.equals("")) { + qname = prefix + ":" + qname; + } + Document doc = element.getOwnerDocument(); + Element newElement = doc.createElementNS(newName.getNamespaceURI(), qname); + // Copy the attributes to the new element + NamedNodeMap attrs = element.getAttributes(); + for (int i = 0; i < attrs.getLength(); i++) { + Attr attr = (Attr)doc.importNode(attrs.item(i), true); + newElement.getAttributes().setNamedItem(attr); + } + + // Move all the children + while (element.hasChildNodes()) { + newElement.appendChild(element.getFirstChild()); + } + + // Replace the old node with the new node + if (element.getParentNode() != null) { + element.getParentNode().replaceChild(newElement, element); + } + + return newElement; + } + } + return element; + } } diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMWrapperHandler.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMWrapperHandler.java index 9e1ebb3522..4ce5dc6c69 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMWrapperHandler.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMWrapperHandler.java @@ -25,11 +25,9 @@ import java.util.List; import java.util.Set; import javax.xml.namespace.QName; -import javax.xml.parsers.ParserConfigurationException; -import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; import org.apache.tuscany.sca.databinding.WrapperHandler; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; import org.apache.tuscany.sca.interfacedef.DataType; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; @@ -41,22 +39,21 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DOMWrapperHandler implements WrapperHandler<Node> { - - public DOMWrapperHandler() { + private DOMHelper domHelper; + + public DOMWrapperHandler(DOMHelper domHelper) { super(); + this.domHelper = domHelper; } public Node create(Operation operation, boolean input) { - try { - WrapperInfo wrapperInfo = operation.getWrapper(); - ElementInfo element = input ? wrapperInfo.getInputWrapperElement() : wrapperInfo.getOutputWrapperElement(); - // Class<?> wrapperClass = input ? wrapperInfo.getInputWrapperClass() : wrapperInfo.getOutputWrapperClass(); - Document document = DOMHelper.newDocument(); - QName name = element.getQName(); - return DOMHelper.createElement(document, name); - } catch (ParserConfigurationException e) { - throw new TransformationException(e); - } + + WrapperInfo wrapperInfo = operation.getWrapper(); + ElementInfo element = input ? wrapperInfo.getInputWrapperElement() : wrapperInfo.getOutputWrapperElement(); + // Class<?> wrapperClass = input ? wrapperInfo.getInputWrapperClass() : wrapperInfo.getOutputWrapperClass(); + Document document = domHelper.newDocument(); + QName name = element.getQName(); + return DOMHelper.createElement(document, name); } public void setChildren(Node wrapper, diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SimpleJavaType2Node.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SimpleJavaType2Node.java index d2625fba11..0bb13bb3ea 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SimpleJavaType2Node.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/SimpleJavaType2Node.java @@ -19,12 +19,11 @@ package org.apache.tuscany.sca.databinding.xml; import javax.xml.namespace.QName; -import javax.xml.parsers.ParserConfigurationException; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.TransformationContext; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; import org.apache.tuscany.sca.databinding.impl.Java2SimpleTypeTransformer; -import org.apache.tuscany.sca.databinding.javabeans.Java2XMLMapperException; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -35,23 +34,19 @@ import org.w3c.dom.Node; * @version $Rev$ $Date$ */ public class SimpleJavaType2Node extends Java2SimpleTypeTransformer<Node> { - - private Document factory; - - public SimpleJavaType2Node() { + private DOMHelper helper; + + public SimpleJavaType2Node(ExtensionPointRegistry registry) { super(); - try { - factory = DOMHelper.newDocument(); - } catch (ParserConfigurationException e) { - throw new Java2XMLMapperException(e); - } + helper = DOMHelper.getInstance(registry); } - + @Override protected Node createElement(QName element, String text, TransformationContext context) { if (element == null) { element = DOMDataBinding.ROOT_ELEMENT; } + Document factory = helper.newDocument(); Node root = DOMHelper.createElement(factory, element); if (text != null) { root.appendChild(factory.createTextNode(text)); diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2NodeTransformer.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2NodeTransformer.java index ee9aa1bb76..54b281acb5 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2NodeTransformer.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/Source2NodeTransformer.java @@ -19,14 +19,13 @@ package org.apache.tuscany.sca.databinding.xml; import javax.xml.transform.Source; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMResult; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.TransformationException; import org.apache.tuscany.sca.databinding.impl.BaseTransformer; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -37,15 +36,17 @@ import org.w3c.dom.Node; */ public class Source2NodeTransformer extends BaseTransformer<Source, Node> implements PullTransformer<Source, Node> { - private static final TransformerFactory FACTORY = TransformerFactory.newInstance(); - + private DOMHelper helper; + + public Source2NodeTransformer(ExtensionPointRegistry registry) { + super(); + helper = DOMHelper.getInstance(registry); + } + public Node transform(Source source, TransformationContext context) { try { - javax.xml.transform.Transformer transformer = FACTORY.newTransformer(); - DOMResult result = new DOMResult(); - transformer.transform(source, result); - Document doc = (Document) result.getNode(); - return DOMHelper.adjustElementName(context, doc.getDocumentElement()); + Document doc = helper.load(source); + return DOMDataBinding.adjustElementName(context, doc.getDocumentElement()); } catch (Exception e) { throw new TransformationException(e); } diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/String2Node.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/String2Node.java index 062e10cc45..548342094e 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/String2Node.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/String2Node.java @@ -18,25 +18,25 @@ */ package org.apache.tuscany.sca.databinding.xml; -import java.io.StringReader; - -import javax.xml.parsers.DocumentBuilder; - +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.TransformationException; import org.apache.tuscany.sca.databinding.impl.BaseTransformer; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; import org.w3c.dom.Node; -import org.xml.sax.InputSource; public class String2Node extends BaseTransformer<String, Node> implements PullTransformer<String, Node> { - + private DOMHelper helper; + + public String2Node(ExtensionPointRegistry registry) { + super(); + helper = DOMHelper.getInstance(registry); + } + public Node transform(String source, TransformationContext context) { try { - DocumentBuilder builder = DOMHelper.newDocumentBuilder(); - InputSource inputSource = new InputSource(new StringReader(source)); - return builder.parse(inputSource); + return helper.load(source); } catch (Exception e) { throw new TransformationException(e); } diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2Node.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2Node.java index a180e21ab8..2ebfcf06e9 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2Node.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/XMLStreamReader2Node.java @@ -26,7 +26,6 @@ import org.apache.tuscany.sca.databinding.PullTransformer; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.TransformationException; import org.apache.tuscany.sca.databinding.impl.BaseTransformer; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.ContentHandler; @@ -55,7 +54,7 @@ public class XMLStreamReader2Node extends BaseTransformer<XMLStreamReader, Node> source.close(); if (node instanceof Document) { Document doc = (Document)node; - return DOMHelper.adjustElementName(context, doc.getDocumentElement()); + return DOMDataBinding.adjustElementName(context, doc.getDocumentElement()); } else { return node; } diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/extension/DOMHelperTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/extension/DOMHelperTestCase.java deleted file mode 100644 index 5e57f64b5f..0000000000 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/extension/DOMHelperTestCase.java +++ /dev/null @@ -1,53 +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.databinding.extension; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import javax.xml.namespace.QName; -import javax.xml.parsers.DocumentBuilder; - -import org.apache.tuscany.sca.databinding.impl.DOMHelper; -import org.junit.Test; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -/** - * - * @version $Rev$ $Date$ - */ -public class DOMHelperTestCase { - private static final QName FOO_NAME = new QName("http://foo", "foo"); - - @Test - public void testDOM() throws Exception { - DocumentBuilder builder = DOMHelper.newDocumentBuilder(); - assertNotNull(builder); - Document document = DOMHelper.newDocument(); - assertNotNull(document); - Element element = DOMHelper.createElement(document, FOO_NAME); - document.appendChild(element); - QName name = DOMHelper.getQName(element); - assertEquals(FOO_NAME, name); - - } - -} diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DOM2StAXTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DOM2StAXTestCase.java index 4da91bd783..27d111374a 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DOM2StAXTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DOM2StAXTestCase.java @@ -78,7 +78,7 @@ public class DOM2StAXTestCase { @Test public void testTransformation() throws Exception { - String2Node t1 = new String2Node(); + String2Node t1 = new String2Node(registry); Node node = t1.transform(IPO_XML, null); Node2XMLStreamReader t2 = new Node2XMLStreamReader(); XMLStreamReader reader = t2.transform(node, null); @@ -90,7 +90,7 @@ public class DOM2StAXTestCase { @Test public void testTransformation2() throws Exception { - String2Node t1 = new String2Node(); + String2Node t1 = new String2Node(registry); Node node = t1.transform(CRAZY_XML, null); Node2XMLStreamReader t2 = new Node2XMLStreamReader(); XMLStreamReader reader = t2.transform(node, null); @@ -103,7 +103,7 @@ public class DOM2StAXTestCase { @Test public void testTransformation3() throws Exception { - String2Node t1 = new String2Node(); + String2Node t1 = new String2Node(registry); Node node = t1.transform(IPO_XML, null); DOMXmlNodeImpl element = new DOMXmlNodeImpl(node); XmlTreeStreamReaderImpl reader = new XmlTreeStreamReaderImpl(element); diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DataPipeTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DataPipeTestCase.java index a120b313ff..682fbd4243 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DataPipeTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/DataPipeTestCase.java @@ -25,9 +25,10 @@ import java.io.OutputStream; import java.io.Reader; import java.io.Writer; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.databinding.DataPipe; import org.apache.tuscany.sca.databinding.DataPipeTransformer; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; import org.apache.tuscany.sca.databinding.impl.PipedTransformer; import org.junit.Assert; import org.junit.Test; @@ -79,7 +80,7 @@ public class DataPipeTestCase { Writer2ReaderDataPipe pipe = new Writer2ReaderDataPipe(); PipedTransformer<Node, Writer, Reader> transformer = new PipedTransformer<Node, Writer, Reader>(node2Writer, pipe); - Document document = DOMHelper.newDocument(); + Document document = DOMHelper.getInstance(new DefaultExtensionPointRegistry()).newDocument(); Element element = document.createElementNS("http://ns1", "root"); document.appendChild(element); Reader reader = transformer.transform(document, null); diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/Node2StringTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/Node2StringTestCase.java index 1ca420a18a..516b57c851 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/Node2StringTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/Node2StringTestCase.java @@ -18,7 +18,8 @@ */ package org.apache.tuscany.sca.databinding.xml; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -31,7 +32,7 @@ public class Node2StringTestCase { @Test public void testTransformation() throws Exception { - Document document = DOMHelper.newDocument(); + Document document = DOMHelper.getInstance(new DefaultExtensionPointRegistry()).newDocument(); Element element = document.createElementNS("http://ns1", "test"); document.appendChild(element); diff --git a/java/sca/modules/implementation-java-runtime/META-INF/MANIFEST.MF b/java/sca/modules/implementation-java-runtime/META-INF/MANIFEST.MF index 3f6275a2bb..ab4d626d6e 100644 --- a/java/sca/modules/implementation-java-runtime/META-INF/MANIFEST.MF +++ b/java/sca/modules/implementation-java-runtime/META-INF/MANIFEST.MF @@ -28,6 +28,7 @@ Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt Bundle-Description: Apache Tuscany SCA Java Implementation Model
Import-Package: javax.xml.namespace,
org.apache.tuscany.sca.assembly;version="2.0.0",
+ org.apache.tuscany.sca.common.xml.dom;version="2.0.0",
org.apache.tuscany.sca.context;version="2.0.0",
org.apache.tuscany.sca.core;version="2.0.0",
org.apache.tuscany.sca.core.context;version="2.0.0",
diff --git a/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java b/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java index d99ce25686..84ccc20be9 100644 --- a/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java +++ b/java/sca/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java @@ -25,6 +25,7 @@ import java.util.StringTokenizer; import org.apache.tuscany.sca.assembly.ComponentProperty; import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; import org.apache.tuscany.sca.context.PropertyValueFactory; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.UtilityExtensionPoint; @@ -32,7 +33,6 @@ import org.apache.tuscany.sca.core.factory.ObjectCreationException; import org.apache.tuscany.sca.core.factory.ObjectFactory; import org.apache.tuscany.sca.databinding.Mediator; import org.apache.tuscany.sca.databinding.SimpleTypeMapper; -import org.apache.tuscany.sca.databinding.impl.DOMHelper; import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; import org.apache.tuscany.sca.databinding.xml.DOMDataBinding; import org.apache.tuscany.sca.implementation.java.JavaElementImpl; |