Hi Mukul,

Have you registered an error handler? I'm guessing by the result you're
seeing that you haven't. By default (in other words when no error handler
has been registered) the JAXP Valdiator is required to throw an exception
[1] when a validation error is reported. If you want the validator to
continue after the first error you need to have registered an error handler
which permits that.

Regarding the code you're writing... though I'm not sure for the need for
the try/catch block around the call to evaluateXPathExpression() we
shouldn't be catching exceptions from reportSchemaError(). Those are meant
to be thrown from the schema validator all the way up to the API layer
where they get converted to SAXExceptions. By catching it here and creating
a new XNIException with only the error message we lose the location
information (file name, line/column numbers) which would be useful for the
application to determine where the error occurred.

Thanks.

[1]
http://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/validation/Validator.html#setErrorHandler(org.xml.sax.ErrorHandler)

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [email protected]
E-mail: [email protected]

Mukul Gandhi <[email protected]> wrote on 04/11/2009 12:36:35 PM:

> Hi all,
>    I have made quite a bit of progress in implementing assertions, but
> facing a small difficulty at present. Kindly find below the problem I
> am facing.
>
> I am writing code into the class, XMLSchemaValidator.java and facing
> some difficulty generating error messages on the console.
>
> In the method, 'endElement' I am evaluating assertions as following:
>
> XMLAssert assertionProcessor = new XMLAssertPsychopathImpl();
>
> try {
>
>   // some code
>
>   boolean isAssertTrue = assertionProcessor
>
.evaluateXpathExpression(assertImpl.getTest().getXPath()
>                             .toString());
>   if (!isAssertTrue) {
>      String typeName = fCurrentPSVI.getTypeDefinition().getAnonymous()
> ? "anonymous type" : fCurrentPSVI.getTypeDefinition().getName();
>
>      reportSchemaError("cvc-xpath.3.13", new Object[] {
>                         element.rawname,
>                         assertImpl.getTest().getXPath().toString(),
>                         typeName});
>
>   }
>
>   // some code
>
> }
> catch (Exception ex) {
>   throw new XNIException(ex.getMessage());
> }
>
> In the file, XMLSchemaMessages.properties I have created a message key
> as following:
>
> cvc-xpath.3.13 = cvc-xpath.3.13: Assertion evaluation (''{1}'') for
> element ''{0}'' with type ''{2}'' did not succeed.
>
> But in the above code, the 'reportSchemaError' call is throwing a Java
> exception, and the control goes to the catch (Exception ex) block, and
> the program ends with following message on console (produced by the
> call, throw new XNIException):
>
> org.xml.sax.SAXException: cvc-xpath.3.13: Assertion evaluation ('1=2')
> for element 'X' with type 'myComplexType1
> ' did not succeed.
>         at
org.apache.xerces.jaxp.validation.Util.toSAXException(Util.java:63)
>         at org.apache.xerces.jaxp.validation.StreamValidatorHelper.
> validate(StreamValidatorHelper.java:187)
>         at org.apache.xerces.jaxp.validation.ValidatorImpl.
> validate(ValidatorImpl.java:129)
>         at javax.xml.validation.Validator.validate(Unknown Source)
>         at XMLSchema11Test.main(XMLSchema11Test.java:15)
>
> In this particular case, the following assert declaration in the
> Schema, fails: <xs:assert test="1=2" />. And it should indeed fail.
> But I want to display an user friendly error message in this case.
>
> Why does the 'reportSchemaError' method call fails, and how could I
> fix this problem?
>
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]

Reply via email to