Author: mukulg
Date: Sat Oct 30 06:16:09 2010
New Revision: 1029000

URL: http://svn.apache.org/viewvc?rev=1029000&view=rev
Log:
doing a little improvement (thought this was useful, and is optional) to 
assertions error messages (which uses Xerces extensions namespace). we are now 
able to provide an atomic value available in validation context (the value of 
assertions XPath2 context variable $value) to assertions error messages.

for example, if there is an assertion facet defined like following:
<xs:assertion test="$value mod 2 = 0" xerces:message="The value must be 
divisible by 2. Atomic value {$value} is not divisible by 2." />

and if this assertion fails in an validation episode, the following error 
message is generated by Xerces:
"The value must be divisible by 2. Atomic value 101 is not divisible by 2."
(the instance document had value 101)

The placeholder for this feature must be {$value} and there can be multiple 
instances of this placeholder in single message, and all of them will be 
replace by the run-time value of $value. 

Modified:
    
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/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=1029000&r1=1028999&r2=1029000&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
 Sat Oct 30 06:16:09 2010
@@ -113,6 +113,9 @@ public class XMLAssertPsychopathImpl ext
     // an instance variable to track the name of an attribute currently
     // been processed for assertions.
     private String fAttrName = null;
+    
+    // a placeholder definition used for assertions error messages.
+    private final String ERROR_PLACEHOLDER_REGEX = "\\{\\$value\\}";
 
     
     /*
@@ -258,7 +261,7 @@ public class XMLAssertPsychopathImpl ext
 
     /*
      * Method to evaluate all of XML schema 1.1 assertions for an element tree.
-     * This is the root method, which evaluates all XML schema assertions, in
+     * This is the root method which evaluates all XML schema assertions, in
      * a single XML instance validation episode.
      */
     private void processAllAssertionsOnElement(QName element,
@@ -921,6 +924,7 @@ public class XMLAssertPsychopathImpl ext
         QName element = assertError.getElement();
         XSAssertImpl assertImpl = assertError.getAssertion();
         boolean isList = assertError.isList();
+        String value = assertError.getValue();
         
         XSTypeDefinition typeDef = assertImpl.getTypeDefinition();        
         String typeString = "";
@@ -948,6 +952,12 @@ public class XMLAssertPsychopathImpl ext
             
         String message = assertImpl.getMessage();
         if (message != null) {
+           // substitute all placeholder macro instances of "{$value}" with 
atomic value
+           // stored in variable "value".
+           if (value != null && !"".equals(value)) {
+              message = message.replaceAll(ERROR_PLACEHOLDER_REGEX, value);
+           }
+           
            if (!message.endsWith(".")) {
               message = message + ".";    
            }
@@ -958,7 +968,7 @@ public class XMLAssertPsychopathImpl ext
            else {
               message = "Assertion failed for schema type '" + typeString + 
"'. "
                          + message; 
-           }
+           }           
            fValidator.reportSchemaError("cvc-assertion.failure", 
                                new Object[] { message, listAssertErrMessage } 
);    
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to