From 093fcdb0a7125ec9767592c16882e1fe3a993b3c Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 6 Mar 2009 21:35:43 +0000 Subject: Support the SCA property value based on the OASIS syntax git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@751079 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/assembly/xml/BaseAssemblyProcessor.java | 102 ++++++++++++++++++--- .../tuscany/sca/assembly/xml/WriteAllTestCase.java | 2 +- .../sca/assembly/xml/TestAllCalculator.composite | 16 ++-- .../assembly/xml/TestAllPolicyCalculator.composite | 9 +- 4 files changed, 106 insertions(+), 23 deletions(-) (limited to 'java/sca/modules/assembly-xml/src') diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java index c3c1ba7f42..03a087a5ec 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java @@ -21,6 +21,9 @@ package org.apache.tuscany.sca.assembly.xml; import static javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI; import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; +import static javax.xml.stream.XMLStreamConstants.CDATA; +import static javax.xml.stream.XMLStreamConstants.CHARACTERS; +import static javax.xml.stream.XMLStreamConstants.COMMENT; import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; import static org.apache.tuscany.sca.assembly.xml.Constants.ELEMENT; @@ -30,6 +33,7 @@ import static org.apache.tuscany.sca.assembly.xml.Constants.MUST_SUPPLY; import static org.apache.tuscany.sca.assembly.xml.Constants.NAME; import static org.apache.tuscany.sca.assembly.xml.Constants.ONE_N; import static org.apache.tuscany.sca.assembly.xml.Constants.ONE_ONE; +import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY; import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY_QNAME; import static org.apache.tuscany.sca.assembly.xml.Constants.SCA11_NS; import static org.apache.tuscany.sca.assembly.xml.Constants.TARGET; @@ -430,6 +434,7 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { return null; } + /* protected List readPropertyValue(XMLStreamReader reader) throws XMLStreamException, ContributionReadException { List values = new ArrayList(); @@ -455,7 +460,7 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { name = reader.getName(); if (PROPERTY_QNAME.equals(name)) { isTextForProperty = true; - continue; + break; } isTextForProperty = false; // Read @@ -489,8 +494,14 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { } break; } + if (reader.hasNext()) { + event = reader.next(); + } else { + return values; + } } } + */ /** * Read a property value into a DOM document. @@ -517,8 +528,13 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { throw ce; } - // root element has no namespace and local name "value" - Element root = document.createElementNS(null, "value"); + // Collect the property values as elements under the + Element root = document.createElementNS(SCA11_NS, "sca:" + PROPERTY); + String nameAttr = getString(reader, NAME); + if (nameAttr != null) { + root.setAttributeNS(SCA11_NS, "sca:" + NAME, nameAttr); + } + declareNamespace(root, "sca", SCA11_NS); if (type != null) { org.w3c.dom.Attr xsi = document.createAttributeNS(XMLNS_ATTRIBUTE_NS_URI, "xmlns:xsi"); xsi.setValue(W3C_XML_SCHEMA_INSTANCE_NS_URI); @@ -537,7 +553,63 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { } document.appendChild(root); - loadElement(reader, root); + // Start to parse the property + QName name = reader.getName(); // Should be sca:property + + // SCA 1.1 supports the @value for simple types + String valueAttr = getString(reader, VALUE); + if (valueAttr != null) { + Element valueElement = document.createElementNS(SCA11_NS, VALUE); + root.appendChild(valueElement); + valueElement.setTextContent(valueAttr); + } + + boolean isTextForProperty = true; + StringBuffer text = new StringBuffer(); + + int event = reader.getEventType(); + while (true) { + switch (event) { + case START_ELEMENT: + name = reader.getName(); + if (PROPERTY_QNAME.equals(name)) { + isTextForProperty = true; + break; + } + isTextForProperty = false; + // Read + if (VALUE_QNAME.equals(name)) { + loadElement(reader, root); + } else { + // Global elements + loadElement(reader, root); + } + break; + case XMLStreamConstants.CHARACTERS: + case XMLStreamConstants.CDATA: + if (isTextForProperty) { + text.append(reader.getText()); + } + break; + case END_ELEMENT: + name = reader.getName(); + if (PROPERTY_QNAME.equals(name)) { + if (root.getChildNodes().getLength() == 0) { + // Add the text as an + Element valueElement = document.createElementNS(SCA11_NS, VALUE); + root.appendChild(valueElement); + valueElement.setTextContent(text.toString()); + } + return document; + } + break; + } + if (reader.hasNext()) { + event = reader.next(); + } else { + break; + } + } return document; } @@ -607,8 +679,8 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { Document document = root.getOwnerDocument(); Node current = root; while (true) { - switch (reader.next()) { - case XMLStreamConstants.START_ELEMENT: + switch (reader.getEventType()) { + case START_ELEMENT: QName name = reader.getName(); Element child = createElement(document, name); @@ -644,20 +716,28 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { } break; - case XMLStreamConstants.CDATA: + case CDATA: current.appendChild(document.createCDATASection(reader.getText())); break; - case XMLStreamConstants.CHARACTERS: + case CHARACTERS: current.appendChild(document.createTextNode(reader.getText())); break; - case XMLStreamConstants.END_ELEMENT: + case COMMENT: + current.appendChild(document.createComment(reader.getText())); + break; + case END_ELEMENT: + // pop the element off the stack + current = current.getParentNode(); // if we are back at the root then we are done if (current == root) { return; } - // pop the element off the stack - current = current.getParentNode(); + } + if (reader.hasNext()) { + reader.next(); + } else { + return; } } } diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/WriteAllTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/WriteAllTestCase.java index 1877470cd3..cf86aec6f4 100644 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/WriteAllTestCase.java +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/WriteAllTestCase.java @@ -152,7 +152,7 @@ public class WriteAllTestCase { assertEquals(composite.getProperties().get(1).getName(),"prop2"); assertEquals(composite.getProperties().get(1).isMustSupply(), true); assertEquals(composite.getProperties().get(0).getXSDType(), new QName("http://foo", "MyComplexType")); - assertEquals(composite.getProperties().get(1).getXSDElement(), new QName("http://docs.oasis-open.org/ns/opencsa/sca/200903", "MyComplexPropertyValue1")); + assertEquals(composite.getProperties().get(1).getXSDElement(), new QName("http://foo", "MyComplexPropertyValue1")); } } diff --git a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/TestAllCalculator.composite b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/TestAllCalculator.composite index 2f1c2e333a..65e709f9b1 100644 --- a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/TestAllCalculator.composite +++ b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/TestAllCalculator.composite @@ -107,17 +107,21 @@ - - + AValue InterestingURI - - + + BValue BoringURI - + - + + 123 + + + 123 + 123 diff --git a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/TestAllPolicyCalculator.composite b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/TestAllPolicyCalculator.composite index 9d5dcead7c..661270d341 100644 --- a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/TestAllPolicyCalculator.composite +++ b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/TestAllPolicyCalculator.composite @@ -112,15 +112,14 @@ - - + AValue InterestingURI - - + + BValue BoringURI - + -- cgit v1.2.3