You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2006/10/05 00:26:17 UTC

svn commit: r453052 - in /incubator/tuscany/java/sca: kernel/core/src/main/java/org/apache/tuscany/core/loader/ kernel/core/src/main/java/org/apache/tuscany/core/property/ kernel/core/src/test/java/org/apache/tuscany/core/property/ services/databinding...

Author: rfeng
Date: Wed Oct  4 15:26:16 2006
New Revision: 453052

URL: http://svn.apache.org/viewvc?view=rev&rev=453052
Log:
Fix the xpath expression by removing trailing /

Modified:
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java
    incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/property/PropertyHelper.java
    incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/property/PropertyHelperTestCase.java
    incubator/tuscany/java/sca/services/databinding/databinding-axiom/src/main/java/org/apache/tuscany/databinding/axiom/XMLStreamReader2OMElement.java

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java?view=diff&rev=453052&r1=453051&r2=453052
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/loader/StAXUtil.java Wed Oct  4 15:26:16 2006
@@ -20,6 +20,9 @@
 
 import java.util.HashMap;
 import java.util.Map;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.stream.XMLStreamConstants;
@@ -36,7 +39,7 @@
 
 /**
  * Utility classes to support StAX-based loaders
- *
+ * 
  * @version $Rev$ $Date$
  */
 public final class StAXUtil {
@@ -54,9 +57,9 @@
 
     /**
      * Convert a "multiplicity" attribute to the equivalent enum value.
-     *
+     * 
      * @param multiplicity the attribute to convert
-     * @param def          the default value
+     * @param def the default value
      * @return the enum equivalent
      */
     public static Multiplicity multiplicity(String multiplicity, Multiplicity def) {
@@ -64,9 +67,10 @@
     }
 
     /**
-     * Convert a "scope" attribute to the equivalent enum value. Returns CONVERSATIONAL if the value equals (ignoring
-     * case) "conversational", otherwise returns NONCONVERSATIONAL.
-     *
+     * Convert a "scope" attribute to the equivalent enum value. Returns
+     * CONVERSATIONAL if the value equals (ignoring case) "conversational",
+     * otherwise returns NONCONVERSATIONAL.
+     * 
      * @param scope the attribute to convert
      * @return the enum equivalent
      */
@@ -78,42 +82,42 @@
         }
     }
 
-    public static Document createPropertyValue(XMLStreamReader reader,
-                                               QName type,
-                                               DocumentBuilder builder) throws XMLStreamException {
+    public static Document createPropertyValue(XMLStreamReader reader, QName type, DocumentBuilder builder)
+        throws XMLStreamException {
         Document doc = builder.newDocument();
 
         // root element has no namespace and local name "value"
         Element root = doc.createElementNS(null, "value");
         if (type != null) {
-            Attr xsi = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi");
-            xsi.setValue("http://www.w3.org/2001/XMLSchema-instance");
+            Attr xsi = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:xsi");
+            xsi.setValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
             root.setAttributeNodeNS(xsi);
 
             String prefix = type.getPrefix();
             if (prefix == null || prefix.length() == 0) {
                 prefix = "ns";
             }
-            Attr typeXmlns = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix);
+            Attr typeXmlns = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix);
             typeXmlns.setValue(type.getNamespaceURI());
             root.setAttributeNodeNS(typeXmlns);
 
-            Attr xsiType = doc.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:type");
+            Attr xsiType = doc.createAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:type");
             xsiType.setValue(prefix + ":" + type.getLocalPart());
             root.setAttributeNodeNS(xsiType);
         }
         doc.appendChild(root);
-        
+
         loadPropertyValue(reader, root);
         return doc;
     }
-    
+
     /**
-     * Load a property value specification from an StAX stream into a DOM Document. Only elements, text and attributes
-     * are processed; all comments and other whitespace are ignored.
-     *
+     * Load a property value specification from an StAX stream into a DOM
+     * Document. Only elements, text and attributes are processed; all comments
+     * and other whitespace are ignored.
+     * 
      * @param reader the stream to read from
-     * @param root   the DOM node to load
+     * @param root the DOM node to load
      */
     public static void loadPropertyValue(XMLStreamReader reader, Node root) throws XMLStreamException {
         Document document = root.getOwnerDocument();

Modified: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/property/PropertyHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/property/PropertyHelper.java?view=diff&rev=453052&r1=453051&r2=453052
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/property/PropertyHelper.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/property/PropertyHelper.java Wed Oct  4 15:26:16 2006
@@ -51,7 +51,7 @@
 public class PropertyHelper {
 
     private static final XPathFactory FACTORY = XPathFactory.newInstance();
-    
+
     private PropertyHelper() {
     }
 
@@ -63,11 +63,18 @@
         }
         XPathExpression expression = path.compile(xPathExpression);
         Node result = (Node)expression.evaluate(node, XPathConstants.NODE);
+        if (result == null) {
+            return null;
+        }
 
         // TODO: How to wrap the result into a Document?
         Document document = DOMHelper.newDocument();
-        document.appendChild(document.importNode(result, true));
-        return document;
+        if (result instanceof Document) {
+            return document;
+        } else {
+            document.appendChild(document.importNode(result, true));
+            return document;
+        }
     }
 
     public static Document loadFromFile(String file, DeploymentContext deploymentContext)
@@ -115,13 +122,21 @@
                         Property<?> compositeProp = parent.getProperties().get(name);
                         if (compositeProp == null) {
                             InvalidValueException ex =
-                                new InvalidValueException("The 'source' cannot be resolved to a composite property");
+                                new InvalidValueException(
+                                                          "The 'source' cannot be resolved to a composite property");
                             ex.addContextName(source);
                             throw ex;
                         }
                         Document document = compositeProp.getDefaultValue();
                         // Adding /value because the document root is "value"
-                        String xpath = "/value" + source.substring(index);
+                        String path = source.substring(index);
+                        String xpath = null;
+                        if ("/".equals(path)) {
+                            // trailing / is not legal for xpath
+                            xpath = "/value";
+                        } else {
+                            xpath = "/value" + path;
+                        }
 
                         // FIXME: How to deal with namespaces?
                         node = evaluate(null, document, xpath);

Modified: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/property/PropertyHelperTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/property/PropertyHelperTestCase.java?view=diff&rev=453052&r1=453051&r2=453052
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/property/PropertyHelperTestCase.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/property/PropertyHelperTestCase.java Wed Oct  4 15:26:16 2006
@@ -83,6 +83,10 @@
         assertNotNull(doc);
         doc = PropertyHelper.evaluate(context, node, "/ipo:purchaseOrder/billTo");
         assertNotNull(doc);
+        doc = PropertyHelper.evaluate(context, node, "/");
+        assertNotNull(doc);
+        doc = PropertyHelper.evaluate(context, node, "/ipo:purchaseOrder/billTo1");
+        assertNull(doc);
     }
 
     public void testFile() throws Exception {

Modified: incubator/tuscany/java/sca/services/databinding/databinding-axiom/src/main/java/org/apache/tuscany/databinding/axiom/XMLStreamReader2OMElement.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-axiom/src/main/java/org/apache/tuscany/databinding/axiom/XMLStreamReader2OMElement.java?view=diff&rev=453052&r1=453051&r2=453052
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-axiom/src/main/java/org/apache/tuscany/databinding/axiom/XMLStreamReader2OMElement.java (original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-axiom/src/main/java/org/apache/tuscany/databinding/axiom/XMLStreamReader2OMElement.java Wed Oct  4 15:26:16 2006
@@ -35,7 +35,8 @@
 import org.osoa.sca.annotations.Service;
 
 @Service(Transformer.class)
-public class XMLStreamReader2OMElement extends TransformerExtension<XMLStreamReader, OMElement> implements PullTransformer<XMLStreamReader, OMElement> {
+public class XMLStreamReader2OMElement extends TransformerExtension<XMLStreamReader, OMElement> implements
+    PullTransformer<XMLStreamReader, OMElement> {
 
     public XMLStreamReader2OMElement() {
         super();
@@ -51,29 +52,28 @@
             throw new TransformationException(e);
         }
     }
-    
+
     /**
-     * For data trasnformation purpose, we may only care about the content of the OMElement.
-     * If the incoming OMElement is under a different name, then we try to adjust it based on the target
-     * data type
      * @param context
      * @param element
      */
     private void adjustElementName(TransformationContext context, OMElement element) {
         if (context != null) {
-            DataType<QName> dataType = context.getTargetDataType();
-            QName targetQName = dataType == null ? null : dataType.getLogical();
-            if (targetQName != null && !element.getQName().equals(targetQName)) {
+            DataType dataType = context.getTargetDataType();
+            Object targetQName = dataType == null ? null : dataType.getLogical();
+            if (!(targetQName instanceof QName)) {
+                return;
+            }
+            if (!element.getQName().equals(targetQName)) {
                 // TODO: Throw expection or switch to the new Element
                 OMFactory factory = OMAbstractFactory.getOMFactory();
-                OMNamespace namespace =
-                        factory.createOMNamespace(targetQName.getNamespaceURI(), targetQName.getPrefix());
+                QName name = (QName)targetQName;
+                OMNamespace namespace = factory.createOMNamespace(name.getNamespaceURI(), name.getPrefix());
                 element.setNamespace(namespace);
-                element.setLocalName(targetQName.getLocalPart());
+                element.setLocalName(name.getLocalPart());
             }
         }
     }
-    
 
     public Class getTargetType() {
         return OMElement.class;



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org