You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Mukul Gandhi <ga...@gmail.com> on 2009/04/11 18:36:35 UTC

unable to generate user friendly error messages, for assertions evaluations

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: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org


Re: unable to generate user friendly error messages, for assertions evaluations

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,
  Thanks for your reply.

I think you are right, that I was not registering an error handler.

Below are the latest findings from my code (with the modifications, as
per your suggestions).

In my main driver class (using which I am testing an end to end
validation process), I do something like (now I register an error
handler) following:

public class XMLSchema11Test implements ErrorHandler {

    // some code

    Validator v = s.newValidator();
    v.setErrorHandler(this);

    // some code

   public void error(SAXParseException exception)
           throws SAXException {
       throw exception;
    }

    public void fatalError(SAXParseException exception)
                throws SAXException {
      throw exception;
    }

    public void warning(SAXParseException exception)
             throws SAXException {
      throw exception;
    }
}

In the 'endElement' method of, XMLSchemaValidator.java I do:

if (assertionProcessor != null) {
   try {
      assertionProcessor.endElementDom(element);
    } catch (Exception ex) {
         throw new XNIException(
             "error occured while evaluating assertions", ex);
    }
}

I have to write, try/catch block here because some statements in
'endElementDom' method of class XMLAssertPsychopathImpl.java can throw
an exception.

Please note, that I am now passing the exception object 'ex' to the
higher API layers (earlier I wasn't doing this, as you pointed). Do
you think this is, OK?

In the method, 'endElementDom' of XMLAssertPsychopathImpl.java, I do
something like:

try {
   ResultSequence rs = eval.evaluate(xp);
   boolean result = false;

   // set value to result. some code here to do this.

   if (!result) {
       String typeName = validator.fCurrentPSVI.getTypeDefinition()
                        .getAnonymous() ? "anonymous type"
                        : validator.fCurrentPSVI.getTypeDefinition().getName();
                validator.reportSchemaError("cvc-xpath.3.13", new Object[] {
                        element.rawname,
                        assertImpl.getTest().getXPath().toString(), typeName });
    }
}
catch(Exception ex) {
   String typeName = validator.fCurrentPSVI.getTypeDefinition()
                    .getAnonymous() ? "anonymous type" : validator.fCurrentPSVI
                    .getTypeDefinition().getName();
            validator.reportSchemaError("cvc-xpath.3.13", new Object[] {
                    element.rawname,
                    assertImpl.getTest().getXPath().toString(), typeName });
            throw new XNIException("error occured while evaluating assertion",
                    ex);
}

Now when I run the validation process, I get error message as follows:

::file:///C:/xml/apache-xml-src/test.xml::15:8:-1:cvc-xpath.3.13:
Assertion evaluation ('1=2') for element 'root' with type 'anonymous
type' did not succeed.

Now the user has sufficient context information about the error. I
wanted to achieve this, and it seems to work fine.

Do you think, this flow is fine? Or, we could do it even better?

On Sun, Apr 12, 2009 at 1:54 AM, Michael Glavassevich
<mr...@ca.ibm.com> wrote:
> 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: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org



-- 
Regards,
Mukul Gandhi

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


Re: unable to generate user friendly error messages, for assertions evaluations

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
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: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Mukul Gandhi <ga...@gmail.com> 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: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org