You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mu...@apache.org on 2009/11/25 02:45:18 UTC

svn commit: r883954 - in /xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs: AbstractPsychoPathImpl.java XMLAssertPsychopathImpl.java

Author: mukulg
Date: Wed Nov 25 01:45:17 2009
New Revision: 883954

URL: http://svn.apache.org/viewvc?rev=883954&view=rev
Log:
improvements to assertions implementation. this fix provides, better assert XDM tree construction, and improves typing support for assertions xpath2 "dynamic context" variable, $value.

Modified:
    xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/AbstractPsychoPathImpl.java
    xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLAssertPsychopathImpl.java

Modified: xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/AbstractPsychoPathImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/AbstractPsychoPathImpl.java?rev=883954&r1=883953&r2=883954&view=diff
==============================================================================
--- xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/AbstractPsychoPathImpl.java (original)
+++ xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/AbstractPsychoPathImpl.java Wed Nov 25 01:45:17 2009
@@ -17,6 +17,8 @@
 
 package org.apache.xerces.impl.xs;
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.Enumeration;
 import java.util.Map;
 
@@ -45,6 +47,16 @@
 import org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType;
 import org.eclipse.wst.xml.xpath2.processor.internal.types.ElementType;
 import org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSDate;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSDateTime;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSFloat;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSInt;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSLong;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSString;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSTime;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -153,6 +165,65 @@
     }
     
     /*
+     * Get psychopath xpath2 typed value, corresponding to the XSD language
+     * type.
+     * 
+     * @param xsdTypeName a XSD built in type name, like "string", "date" etc.
+     * @param value a "string value", that need to be converted to, the
+     *        psychopath xpath2 XSD typed value.  
+     */
+    protected Object getPsychoPathTypeForXSDType(String xsdTypeName,
+                                                 String value) {
+        Object psychoPathType = null;
+        
+        if ("string".equals(xsdTypeName)) {
+            psychoPathType = new XSString(value);   
+        }
+        else if ("date".equals(xsdTypeName)) {       
+            psychoPathType = XSDate.parse_date(value);
+        }
+        else if ("int".equals(xsdTypeName)) {      
+            psychoPathType = new XSInt(new BigInteger(value));
+        }
+        else if ("long".equals(xsdTypeName)) {     
+           psychoPathType = new XSLong(new BigInteger(value));
+        }
+        else if ("integer".equals(xsdTypeName)) {      
+           psychoPathType = new XSInteger(new BigInteger(value));
+        }
+        else if ("double".equals(xsdTypeName)) {       
+           psychoPathType = new XSDouble(Double.parseDouble(value));
+        }
+        else if ("float".equals(xsdTypeName)) {        
+           psychoPathType = new XSFloat(Float.parseFloat(value));
+        }
+        else if ("decimal".equals(xsdTypeName)) {      
+           psychoPathType = new XSDecimal(new BigDecimal(value));
+        }
+        else if ("dateTime".equals(xsdTypeName)) {
+           psychoPathType = XSDateTime.parseDateTime(value);
+        }
+        else if ("time".equals(xsdTypeName)) {
+           psychoPathType = XSTime.parse_time(value);
+        }
+        else if ("date".equals(xsdTypeName)) {
+           psychoPathType = XSDate.parse_date(value);
+        }
+        else if ("boolean".equals(xsdTypeName)) {
+           psychoPathType = new XSBoolean(Boolean.valueOf(value).booleanValue());
+        }
+        else if ("NOTATION".equals(xsdTypeName)) {
+           psychoPathType = new XSString(value);
+        }
+        else {
+           // create a XSString value, as fallback option 
+           psychoPathType = new XSString(value);
+        } 
+        
+        return psychoPathType;
+    }
+    
+    /*
      * Method to report error messages
      */
     private void reportError(String key, XSAssertImpl assertImpl,

Modified: xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLAssertPsychopathImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLAssertPsychopathImpl.java?rev=883954&r1=883953&r2=883954&view=diff
==============================================================================
--- xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLAssertPsychopathImpl.java (original)
+++ xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xs/XMLAssertPsychopathImpl.java Wed Nov 25 01:45:17 2009
@@ -21,6 +21,7 @@
 import java.util.Stack;
 import java.util.Vector;
 
+import org.apache.xerces.dom.CoreDocumentImpl;
 import org.apache.xerces.dom.PSVIAttrNSImpl;
 import org.apache.xerces.dom.PSVIDocumentImpl;
 import org.apache.xerces.dom.PSVIElementNSImpl;
@@ -37,7 +38,7 @@
 import org.apache.xerces.xs.XSTypeDefinition;
 import org.eclipse.wst.xml.xpath2.processor.DynamicContext;
 import org.eclipse.wst.xml.xpath2.processor.ast.XPath;
-import org.eclipse.wst.xml.xpath2.processor.internal.types.XSString;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -105,20 +106,9 @@
                                                       assertDocument,
                                                       assertParams);
         
-        // assign value to variable, "value" in XPath context
+        // create variable, $value in XPath dynamic context
         fDynamicContext.add_variable(new org.eclipse.wst.xml.xpath2.processor.internal.types.QName(
                                          "value"));
-        String value = "";
-        NodeList childList = currentAssertDomNode.getChildNodes();
-        if (childList.getLength() == 1) {
-            Node node = childList.item(0);
-            if (node.getNodeType() == Node.TEXT_NODE) {
-                value = node.getNodeValue();
-            }
-        }
-        fDynamicContext.set_variable(
-                new org.eclipse.wst.xml.xpath2.processor.internal.types.QName(
-                        "value"), new XSString(value));
     }
 
     /*
@@ -126,13 +116,12 @@
      * @see org.apache.xerces.xni.parser.XMLAssertAdapter#startElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.XMLAttributes, org.apache.xerces.xni.Augmentations)
      */
     public void startElement(QName element, XMLAttributes attributes,
-                                               Augmentations augs) {
+                                              Augmentations augs) {
         if (currentAssertDomNode == null) {
-           currentAssertDomNode = assertDocument.createElementNS(
-                                              element.uri, element.rawname);
+           currentAssertDomNode = new PSVIElementNSImpl((CoreDocumentImpl) assertDocument, element.uri, element.rawname);
            assertDocument.appendChild(currentAssertDomNode);
         } else {
-            Element elem = assertDocument.createElementNS(element.uri, element.rawname);
+            Element elem = new PSVIElementNSImpl((CoreDocumentImpl) assertDocument, element.uri, element.rawname);
             currentAssertDomNode.appendChild(elem);
             currentAssertDomNode = elem;
         }
@@ -143,20 +132,20 @@
             String attQName = attributes.getQName(attIndex);
             String attValue = attributes.getValue(attIndex);
             
-            PSVIAttrNSImpl attrNode = new PSVIAttrNSImpl((PSVIDocumentImpl)assertDocument, attrUri, attQName);
+            PSVIAttrNSImpl attrNode = new PSVIAttrNSImpl((PSVIDocumentImpl) assertDocument, attrUri, attQName);
             attrNode.setNodeValue(attValue);
             
             // set PSVI information for the attribute
             Augmentations attrAugs = attributes.getAugmentations(attIndex);
-            AttributePSVImpl attrPSVI = (AttributePSVImpl)attrAugs.getItem(Constants.ATTRIBUTE_PSVI);
+            AttributePSVImpl attrPSVI = (AttributePSVImpl) attrAugs.getItem(Constants.ATTRIBUTE_PSVI);
             attrNode.setPSVI(attrPSVI);
             
             currentAssertDomNode.setAttributeNode(attrNode);
         }
 
         Object assertion = augs.getItem("ASSERT");
-        // if we have assertion on this element, store the element reference
-        // and the assertions on it, on the stack objects
+        // if we have assertion applicable to this element, store the element
+        // reference and the assertions on it, on the runtime stacks
         if (assertion != null) {
             assertRootStack.push(currentAssertDomNode);
             assertListStack.push(assertion);
@@ -170,37 +159,19 @@
     public void endElement(QName element, Augmentations augs) throws Exception {
         if (currentAssertDomNode != null) {
             // set PSVI information on the element
-            ElementPSVI elemPSVI = (ElementPSVI)augs.getItem(Constants.ELEMENT_PSVI);
+            ElementPSVI elemPSVI = (ElementPSVI) augs.getItem(Constants.ELEMENT_PSVI);
             ((PSVIElementNSImpl)currentAssertDomNode).setPSVI(elemPSVI);
             
             if (!assertRootStack.empty() && (currentAssertDomNode == assertRootStack.peek())) {               
                  // get XSModel                
-                 fSchema =  ((PSVIElementNSImpl)currentAssertDomNode).getSchemaInformation();
+                 fSchema =  ((PSVIElementNSImpl) currentAssertDomNode).getSchemaInformation();
                  
                  // pop the stack, to go one level up
                  assertRootStack.pop();
                  // get assertions, and go one level up
                  Object assertions = assertListStack.pop(); 
-                
-                 // initialize the XPath engine
-                 initXPathProcessor();
                  
-                 // evaluate assertions
-                 if (assertions instanceof XSObjectList) {
-                    // assertions from a complex type definition
-                    XSObjectList assertList = (XSObjectList) assertions;
-                    for (int i = 0; i < assertList.size(); i++) {
-                        XSAssertImpl assertImpl = (XSAssertImpl) assertList.get(i);
-                        evaluateAssertion(element, assertImpl);
-                    }
-                } else if (assertions instanceof Vector) {
-                    // assertions from a simple type definition
-                    Vector assertList = (Vector) assertions;                    
-                    for (int i = 0; i < assertList.size(); i++) {
-                        XSAssertImpl assertImpl = (XSAssertImpl) assertList.get(i);
-                        evaluateAssertion(element, assertImpl);
-                    }
-                }
+                 processAllAssertionsOnElement(element, assertions);
             }
 
             if (currentAssertDomNode.getParentNode() instanceof Element) {
@@ -210,20 +181,63 @@
     }
 
     /*
+     * Method to evaluate all assertions for the element tree
+     */
+    private void processAllAssertionsOnElement(QName element, Object assertions)
+            throws Exception {
+         // initialize the XPath engine
+         initXPathProcessor();
+         
+         // determine value of variable, $value
+         String value = null;
+         NodeList childList = currentAssertDomNode.getChildNodes();
+         if (childList.getLength() == 1) {
+             Node node = childList.item(0);
+             if (node.getNodeType() == Node.TEXT_NODE) {
+                 value = node.getNodeValue();
+             }
+         }
+         
+         // evaluate assertions
+         if (assertions instanceof XSObjectList) {
+            // assertions from a complex type definition
+            if (value != null) {
+              // complex type with simple content
+              setValueOf$value(value);
+            } else {
+              // complex type with complex content                
+              // $value should be, the XPath2 "empty sequence" ... TO DO 
+            }
+            XSObjectList assertList = (XSObjectList) assertions;
+            for (int i = 0; i < assertList.size(); i++) {
+               XSAssertImpl assertImpl = (XSAssertImpl) assertList.get(i);
+               evaluateAssertion(element, assertImpl);
+            }
+        } else if (assertions instanceof Vector) {
+            // assertions from a simple type definition
+            setValueOf$value(value);
+            Vector assertList = (Vector) assertions;                    
+            for (int i = 0; i < assertList.size(); i++) {
+                XSAssertImpl assertImpl = (XSAssertImpl) assertList.get(i);
+                evaluateAssertion(element, assertImpl);
+            }
+        }
+    }
+
+    /*
      * (non-Javadoc)
      * @see org.apache.xerces.xni.parser.XMLAssertAdapter#characters(org.apache.xerces.xni.XMLString)
      */
     public void characters(XMLString text) {
         // add a child text node to the assertions, DOM tree
         if (currentAssertDomNode != null) {
-            currentAssertDomNode.appendChild(assertDocument
-                    .createTextNode(new String(text.ch, text.offset,
-                            text.length)));
+            currentAssertDomNode.appendChild(assertDocument.createTextNode(new 
+                                   String(text.ch, text.offset, text.length)));
         }
     }
 
     /*
-     * Method to evaluate an assertions, XPath expression
+     * Method to evaluate an assertion for the element
      */
     private void evaluateAssertion(QName element, XSAssertImpl assertImpl) {
         try {  
@@ -259,4 +273,26 @@
                                assertImpl.getTest().getXPath().toString(),
                                typeString } );
     }
+    
+    // assign value to the XPath2 "dynamic context" variable, $value
+    private void setValueOf$value(String value) {
+       PSVIElementNSImpl currentAssertPSVINode = (PSVIElementNSImpl) currentAssertDomNode;
+       
+       String typeName = "";
+       if (Constants.NS_XMLSCHEMA.equals(currentAssertPSVINode.getTypeNamespace())) {
+           typeName = currentAssertPSVINode.getTypeDefinition().getName();    
+       } else if (Constants.NS_XMLSCHEMA.equals(currentAssertPSVINode.
+                                         getTypeDefinition().
+                                         getBaseType().getNamespace())) {
+           typeName = currentAssertPSVINode.getTypeDefinition().getBaseType().
+                                         getName();
+       }
+       
+       Object psychoPathType = abstrPsychopathImpl.getPsychoPathTypeForXSDType
+                                                             (typeName, value);
+       
+       fDynamicContext.set_variable(
+               new org.eclipse.wst.xml.xpath2.processor.internal.types.QName(
+                       "value"), (AnyType) psychoPathType);
+    }
 }



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