Hey Mukul,

Yup, you pointed me in the right direction -- thanks...but I still think 
there's a bug.

If you look at the docs to ErrorHandler:

http://docs.oracle.com/javase/6/docs/api/org/xml/sax/ErrorHandler.html

It says that:  " it is up to the application to decide whether to throw an 
exception for different types of errors and warnings."

In other words, the error handler gives an app the ability to decide whether or 
not to throw an exception when an error occurs. The issue that I was having was 
that I registered an error handler that threw an exception. I couldn't 
replicate it from the SourceValidator sample because it reports errors directly 
from the error handler and doesn't throw an exception there at all.

The problem that I have is that in this case:

<a xmlns="http://www.rackspace.com/xerces/test"; even="23"/>

The SAXParseException propagated all the way up to my app as expected.

In this case, however:

<e xmlns="http://www.rackspace.com/xerces/test";>
    <even>23</even>
 </e>

The exception never propagated up -- it got lost!  I totally see that as a bug.

I've reworked the code to not throw an exception in the ErrorHandler -- but I 
don't feel this is very efficient. I want to fail fast on the first parse error 
I see and not have to wait for the entire message to finish parsing.

Thanks,

-jOrGe W.

On Jun 24, 2012, at 2:55 AM, Mukul Gandhi wrote:

> Hi Jorge,
>   I think, this error is not specific to XSD assertions. Setting a
> error handler on the JAXP validator object would solve this problem.
> 
> Following are the proposed changes to solve this issue,
> 
> Validator v = s.newValidator();
> v.setErrorHandler(new ErrHandler());
> v.validate(new StreamSource(instance));
> 
> and define a suitable error handler as following,
> 
> class ErrHandler implements ErrorHandler {
>    ...
> }
> 
> On Fri, Jun 22, 2012 at 12:48 AM, Jorge Williams
> <jorge.willi...@rackspace.com> wrote:
>> Hey Guys,
>> 
>> Can you give me a hand. I'm getting a weird error that I cannot replicate 
>> with jaxp.SourceValidator  so I know I must be doing something wrong, but I 
>> can't tell what.
>> 
>> My code looks like this:
>> 
>> package com.rackspace.xerces;
>> 
>> import javax.xml.validation.*;
>> import javax.xml.transform.stream.*;
>> 
>> public class Test {
>>   public static void main(String[] args) {
>>      if (args.length != 2) {
>>         System.err.println ("Usage: need XSD and instance doc");
>>         return;
>>      }
>> 
>>      try {
>>         System.setProperty 
>> ("javax.xml.validation.SchemaFactory:http://www.w3.org/XML/XMLSchema/v1.1";, 
>> "org.apache.xerces.jaxp.validation.XMLSchema11Factory");
>> 
>>         String xsd = args[0];
>>         String instance = args[1];
>> 
>>         SchemaFactory factory = 
>> SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1";);
>>         factory.setFeature 
>> ("http://apache.org/xml/features/validation/cta-full-xpath-checking";, true);
>> 
>>         Schema s = factory.newSchema(new StreamSource(xsd));
>>         s.newValidator().validate(new StreamSource(instance));
>> 
>>      } catch (Exception e) {
>>         e.printStackTrace();
>>      }
>>   }
>> }
>> 
>> 
>> You pass a schema in param 1 and an instance document in param 2, the code 
>> validates and returns errors.  Here's the schema I'm using to test:
>> 
>> 
>> <schema
>>    elementFormDefault="qualified"
>>    attributeFormDefault="unqualified"
>>    xmlns="http://www.w3.org/2001/XMLSchema";
>>    xmlns:xsd="http://www.w3.org/2001/XMLSchema";
>>    xmlns:tst="http://www.rackspace.com/xerces/test";
>>    targetNamespace="http://www.rackspace.com/xerces/test";>
>> 
>>    <element name="e" type="tst:SampleElement"/>
>>    <element name="a" type="tst:SampleAttribute"/>
>> 
>>    <complexType name="SampleElement">
>>        <sequence>
>>            <element name="even" type="tst:EvenInt100" minOccurs="0"/>
>>        </sequence>
>>    </complexType>
>> 
>>    <complexType name="SampleAttribute">
>>        <attribute name="even" type="tst:EvenInt100" use="optional"/>
>>    </complexType>
>> 
>>    <!-- XSD 1.1 assert -->
>>    <simpleType name="EvenInt100">
>>        <restriction base="xsd:integer">
>>           <minInclusive value="0" />
>>           <maxInclusive value="100" />
>>           <assertion test="$value mod 2 = 0" />
>>        </restriction>
>>    </simpleType>
>> </schema>
>> 
>> 
>> When I pass the following instance document, the assertion in EvenInt100 
>> trips and things fail as expected:
>> 
>>  <a xmlns="http://www.rackspace.com/xerces/test"; even="23"/>
>> 
>> When I pass *this* instance document however, I'd expect the same assertion 
>> to fail, but it doesn't.
>> 
>>  <e xmlns="http://www.rackspace.com/xerces/test";>
>>     <even>23</even>
>>  </e>
>> 
>> 
>> The very weird bit is that  jaxp.SourceValidator   works correctly on the 
>> example, so i must be doing something wrong...but I can't tell what it is.  
>> Any ideas?
>> 
>> BTW my code is on github here:
>> 
>> https://github.com/RackerWilliams/xerces-tests/tree/master/schema_tests
>> 
>> You should be able to do a
>> 
>> mvn clean install
>> 
>> Then
>> 
>> java -jar target/xerces-test-1.0.0-SNAPSHOT-jar-with-dependencies.jar 
>> test.xsd test.xml
>> 
>> to illustrate the issue.
>> 
>> Thanks,
>> 
>> -jOrGe W.
> 
> 
> 
> 
> -- 
> Regards,
> Mukul Gandhi
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscr...@xerces.apache.org
> For additional commands, e-mail: j-users-h...@xerces.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscr...@xerces.apache.org
For additional commands, e-mail: j-users-h...@xerces.apache.org

Reply via email to