Author: mukulg
Date: Sun Dec 6 08:54:35 2009
New Revision: 887651
URL: http://svn.apache.org/viewvc?rev=887651&view=rev
Log:
fixes to processing of xpath2 context variable, $value with context awareness,
as specified in the XSD 1.1 spec. improvement to error messages.
Modified:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/msg/XMLSchemaMessages.properties
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/msg/XMLSchemaMessages.properties
URL:
http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/msg/XMLSchemaMessages.properties?rev=887651&r1=887650&r2=887651&view=diff
==============================================================================
---
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/msg/XMLSchemaMessages.properties
(original)
+++
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/msg/XMLSchemaMessages.properties
Sun Dec 6 08:54:35 2009
@@ -106,6 +106,7 @@
cvc-type.3.1.2 = cvc-type.3.1.2: Element ''{0}'' is a simple type, so
it must have no element information item [children].
cvc-type.3.1.3 = cvc-type.3.1.3: The value ''{1}'' of element ''{0}''
is not valid.
cvc-assertion.3.13.4.1 = cvc-assertion.3.13.4.1: Assertion evaluation
(''{1}'') for element ''{0}'' with type ''{2}'' did not succeed.
+ cvc-assertion.4.3.15.3 = cvc-assertion.4.3.15.3: Assertion evaluation
(''{1}'') for element ''{0}'' with type ''{2}'' did not succeed (undefined
context).
cvc-xpath.3.13.4.2 = cvc-xpath.3.13.4.2: Assertion XPath expression,
(''{0}'') on the Schema type (''{1}'') couldn''t compile successfully.
#schema valid (3.X.3)
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=887651&r1=887650&r2=887651&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
Sun Dec 6 08:54:35 2009
@@ -103,9 +103,10 @@
fDynamicContext = new DefaultDynamicContext(schema, document);
- // populate the PsychoPath XPath 2.0 static context, with namespace
bindings
- // derived from the XSD Schema document
- NamespaceSupport xpath2NamespaceContext = (NamespaceSupport)
psychoPathParams.get("XPATH2_NS_CONTEXT");
+ // populate the PsychoPath XPath 2.0 static context, with namespace
+ // bindings derived from the XSD Schema document
+ NamespaceSupport xpath2NamespaceContext = (NamespaceSupport)
+ psychoPathParams.get("XPATH2_NS_CONTEXT");
Enumeration currPrefixes = xpath2NamespaceContext.getAllPrefixes();
while (currPrefixes.hasMoreElements()) {
String prefix = (String)currPrefixes.nextElement();
@@ -127,21 +128,27 @@
String xPathDefaultNamespace,
Element contextNode)
throws StaticError, DynamicError {
- if (xPathDefaultNamespace != null) {
- fDynamicContext.add_namespace(null, xPathDefaultNamespace);
- }
+
StaticChecker sc = new StaticNameResolver(fDynamicContext);
sc.check(xp);
- Evaluator eval = new DefaultEvaluator(fDynamicContext, domDoc);
-
- // change focus to the top most element
- ResultSequence nodeEvalRS = ResultSequenceFactory.create_new();
- nodeEvalRS.add(new ElementType(contextNode,
+ Evaluator eval = null;
+ if (contextNode != null) {
+ eval = new DefaultEvaluator(fDynamicContext, domDoc);
+ // change focus to the top most element
+ ResultSequence nodeEvalRS = ResultSequenceFactory.create_new();
+ nodeEvalRS.add(new ElementType(contextNode,
fDynamicContext.node_position(contextNode)));
-
- fDynamicContext.set_focus(new Focus(nodeEvalRS));
-
+ if (xPathDefaultNamespace != null) {
+ fDynamicContext.add_namespace(null, xPathDefaultNamespace);
+ }
+
+ fDynamicContext.set_focus(new Focus(nodeEvalRS));
+ }
+ else {
+ eval = new DefaultEvaluator(fDynamicContext, null);
+ }
+
ResultSequence rs = eval.evaluate(xp);
boolean result = false;
@@ -200,7 +207,8 @@
psychoPathType = new XSAnyURI(value);
}
else if ("boolean".equals(xsdTypeName)) {
- psychoPathType = new
XSBoolean(Boolean.valueOf(value).booleanValue());
+ psychoPathType = new XSBoolean(Boolean.valueOf(value).
+ booleanValue());
}
else if ("date".equals(xsdTypeName)) {
psychoPathType = XSDate.parse_date(value);
@@ -313,7 +321,8 @@
String typeString = "";
if (typeDef != null) {
- typeString = (typeDef.getName() != null) ? typeDef.getName() :
"#anonymous";
+ typeString = (typeDef.getName() != null) ? typeDef.getName() :
+ "#anonymous";
}
else {
typeString = "#anonymous";
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=887651&r1=887650&r2=887651&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
Sun Dec 6 08:54:35 2009
@@ -37,6 +37,8 @@
import org.apache.xerces.xs.XSObjectList;
import org.apache.xerces.xs.XSTypeDefinition;
import org.eclipse.wst.xml.xpath2.processor.DynamicContext;
+import org.eclipse.wst.xml.xpath2.processor.DynamicError;
+import org.eclipse.wst.xml.xpath2.processor.StaticError;
import org.eclipse.wst.xml.xpath2.processor.ast.XPath;
import org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType;
import org.w3c.dom.Document;
@@ -65,15 +67,15 @@
// a factory Document object to construct DOM tree nodes
Document assertDocument = null;
- // an element to track construction of assertion DOM tree. This object
changes
- // as per the XNI document events.
+ // an element to track construction of assertion DOM tree. This object
+ // changes as per the XNI document events.
Element currentAssertDomNode = null;
// a stack holding the DOM root for assertions evaluation
Stack assertRootStack = null;
- // a stack parallel to 'assertRootStack' storing all assertions for a
single
- // assert tree
+ // a stack parallel to 'assertRootStack' storing all assertions for a
+ // single assert tree.
Stack assertListStack = null;
// XMLSchemaValidator reference. set from the XMLSchemaValidator object
@@ -100,15 +102,13 @@
* Initialize the PsychoPath XPath processor
*/
private void initXPathProcessor() throws Exception {
- validator = (XMLSchemaValidator)
getProperty("http://apache.org/xml/properties/assert/validator");
+ validator = (XMLSchemaValidator) getProperty
+ ("http://apache.org/xml/properties/assert/validator");
abstrPsychopathImpl = new AbstractPsychoPathImpl();
- fDynamicContext = abstrPsychopathImpl.initDynamicContext(fSchema,
- assertDocument,
- assertParams);
-
- // create variable, $value in XPath dynamic context
- fDynamicContext.add_variable(new
org.eclipse.wst.xml.xpath2.processor.internal.types.QName(
- "value"));
+ fDynamicContext = abstrPsychopathImpl.initDynamicContext(
+ fSchema,
+ assertDocument,
+ assertParams);
}
/*
@@ -118,10 +118,12 @@
public void startElement(QName element, XMLAttributes attributes,
Augmentations augs) {
if (currentAssertDomNode == null) {
- currentAssertDomNode = new PSVIElementNSImpl((CoreDocumentImpl)
assertDocument, element.uri, element.rawname);
+ currentAssertDomNode = new PSVIElementNSImpl((CoreDocumentImpl)
+ assertDocument, element.uri, element.rawname);
assertDocument.appendChild(currentAssertDomNode);
} else {
- Element elem = new PSVIElementNSImpl((CoreDocumentImpl)
assertDocument, element.uri, element.rawname);
+ Element elem = new PSVIElementNSImpl((CoreDocumentImpl)
+ assertDocument, element.uri, element.rawname);
currentAssertDomNode.appendChild(elem);
currentAssertDomNode = elem;
}
@@ -132,12 +134,14 @@
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);
@@ -159,12 +163,15 @@
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())) {
+ 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();
@@ -175,7 +182,8 @@
}
if (currentAssertDomNode.getParentNode() instanceof Element) {
- currentAssertDomNode =
(Element)currentAssertDomNode.getParentNode();
+ currentAssertDomNode = (Element)currentAssertDomNode.
+ getParentNode();
}
}
}
@@ -183,8 +191,10 @@
/*
* Method to evaluate all assertions for the element tree
*/
- private void processAllAssertionsOnElement(QName element, Object
assertions)
- throws Exception {
+ private void processAllAssertionsOnElement(
+ QName element,
+ Object assertions)
+ throws Exception {
// initialize the XPath engine
initXPathProcessor();
@@ -197,7 +207,7 @@
value = node.getNodeValue();
}
}
-
+
// evaluate assertions
if (assertions instanceof XSObjectList) {
// assertions from a complex type definition
@@ -211,7 +221,10 @@
XSObjectList assertList = (XSObjectList) assertions;
for (int i = 0; i < assertList.size(); i++) {
XSAssertImpl assertImpl = (XSAssertImpl) assertList.get(i);
- evaluateAssertion(element, assertImpl);
+ evaluateAssertion(element,
+ assertImpl,
+ value,
+ xPathContextExistsForComplexType());
}
} else if (assertions instanceof Vector) {
// assertions from a simple type definition
@@ -219,12 +232,43 @@
Vector assertList = (Vector) assertions;
for (int i = 0; i < assertList.size(); i++) {
XSAssertImpl assertImpl = (XSAssertImpl) assertList.get(i);
- evaluateAssertion(element, assertImpl);
+ evaluateAssertion(element, assertImpl, value, false);
}
}
}
/*
+ * Determine, if XPath context should exist, for assertions on
+ * complex types.
+ * an XPath context should exist, if complex type has following
+ * content model:
+ * complexType -> simpleContent -> extension
+ * OR
+ * complexType -> mixed content type
+ * (i.e, <xs:complexType ... mixed="true")
+ */
+ private boolean xPathContextExistsForComplexType() {
+ boolean contextExistsForComplexType = false;
+
+ PSVIElementNSImpl psviElemCurrent = (PSVIElementNSImpl)
+ currentAssertDomNode;
+ if (psviElemCurrent.getTypeDefinition().getTypeCategory() ==
+ XSTypeDefinition.COMPLEX_TYPE) {
+ XSComplexTypeDecl compTypeDecl = (XSComplexTypeDecl)
+
psviElemCurrent.getTypeDefinition();
+ if (compTypeDecl.isDerivedFrom(SchemaSymbols.URI_SCHEMAFORSCHEMA,
+ SchemaSymbols.ATTVAL_ANYSIMPLETYPE,
+ XSComplexTypeDecl.DERIVATION_EXTENSION) ||
+ (compTypeDecl.getContentType() ==
+ compTypeDecl.CONTENTTYPE_MIXED)) {
+ contextExistsForComplexType = true;
+ }
+ }
+
+ return contextExistsForComplexType;
+ }
+
+ /*
* (non-Javadoc)
* @see
org.apache.xerces.xni.parser.XMLAssertAdapter#characters(org.apache.xerces.xni.XMLString)
*/
@@ -239,18 +283,47 @@
/*
* Method to evaluate an assertion for the element
*/
- private void evaluateAssertion(QName element, XSAssertImpl assertImpl) {
+ private void evaluateAssertion(QName element,
+ XSAssertImpl assertImpl,
+ String value,
+ boolean xPathContextExists) {
try {
- XPath xp = assertImpl.getCompiledXPath();
- boolean result = abstrPsychopathImpl.evaluatePsychoPathExpr(xp,
- assertImpl.getXPathDefaultNamespace(),
- currentAssertDomNode);
+ XPath xp = assertImpl.getCompiledXPath();
+
+ boolean result;
+ if ((value == null) ||
+ (xPathContextExists == true)) {
+ // assertion on complex type, with complex content
+ // OR, complex type -> simpleContent -> extension
+ // OR, complex type -> mixed
+ result = abstrPsychopathImpl.evaluatePsychoPathExpr(xp,
+ assertImpl.getXPathDefaultNamespace(),
+ currentAssertDomNode);
+ }
+ else {
+ // assertion on simple type
+ // OR, complex type -> simpleContent -> restriction.
+ // here, XPath context is "undefined"
+ result = abstrPsychopathImpl.evaluatePsychoPathExpr(xp,
+ assertImpl.getXPathDefaultNamespace(),
+ null);
+ }
if (!result) {
// assertion evaluation is false
reportError("cvc-assertion.3.13.4.1", element, assertImpl);
}
- } catch (Exception ex) {
+ }
+ catch (DynamicError ex) {
+ if (ex.code().equals("XPDY0002")) {
+ // ref: http://www.w3.org/TR/xpath20/#eval_context
+ reportError("cvc-assertion.4.3.15.3", element, assertImpl);
+ }
+ else {
+ reportError("cvc-assertion.3.13.4.1", element, assertImpl);
+ }
+ }
+ catch (StaticError ex) {
reportError("cvc-assertion.3.13.4.1", element, assertImpl);
}
}
@@ -258,12 +331,14 @@
/*
* Method to report error messages
*/
- private void reportError(String key, QName element, XSAssertImpl
assertImpl) {
+ private void reportError(String key, QName element,
+ XSAssertImpl assertImpl) {
XSTypeDefinition typeDef = assertImpl.getTypeDefinition();
String typeString = "";
if (typeDef != null) {
- typeString = (typeDef.getName() != null) ? typeDef.getName() :
"#anonymous";
+ typeString = (typeDef.getName() != null) ? typeDef.getName() :
+ "#anonymous";
}
else {
typeString = "#anonymous";
@@ -276,9 +351,11 @@
// assign value to the XPath2 "dynamic context" variable, $value
private void setValueOf$value(String value) {
- PSVIElementNSImpl currentAssertPSVINode = (PSVIElementNSImpl)
currentAssertDomNode;
+ PSVIElementNSImpl currentAssertPSVINode = (PSVIElementNSImpl)
+ currentAssertDomNode;
- String xsdTypeName =
getXSDtypeOf$Value(currentAssertPSVINode.getTypeDefinition());
+ String xsdTypeName = getXSDtypeOf$Value(currentAssertPSVINode.
+ getTypeDefinition());
Object psychoPathType = abstrPsychopathImpl.getPsychoPathTypeForXSDType
(xsdTypeName, value);
fDynamicContext.set_variable(
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]