You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jason Vinson <vi...@charter.net> on 2005/02/17 17:29:43 UTC

[Digester] XSD Support?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

After reading some docs around the web, i found this:

http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg31474.html

And then found some other docs (not sure of the age) that mention
Xerces having limited XML Schema support.  So I guess my questions are
1.) Do any of you use XSD's with your digesters, and 2.) how can i use
something other than Xerces for my content handler in the digester?

Thanks for any pointers,
Jason
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCFMZ3AQ71cZOfvEQRAlnVAJ9JGl0mwj3/X0NXrVgNXPxKeIkBgQCfV/KT
zWuVLa4TNHpvoxeORGg/GlQ=
=ydp4
-----END PGP SIGNATURE-----


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] XSD Support?

Posted by Simon Kitching <sk...@apache.org>.
On Fri, 2005-02-18 at 12:39 +1300, Simon Kitching wrote:
> On Thu, 2005-02-17 at 12:25 -0500, Jason Vinson wrote:
> > For the benefit of anyone who is looking for the answers to these
> > questions, here's what i've found.  Please correct any glaring errors
> > if you see them.
> > 
> > 1.) AFAIK, XSD is supported in the digester if you use a non-Xerces
> > parser (i.e. Crimson)
> 
> No, it's supported no matter what parser you use, once it's enabled.
> It's just enabling schema validation that may be a little tricky.

To be more precise: digester doesn't *care* whether validation is
enabled or not; it does not affect digester in any way[1].

As a favour to users, Digester does try to provide some methods to make
it easy to set up schema validation. However this is really quite
outside Digester's main functionality [2].

Schema validation can be enabled on the parser by making direct calls on
it before commencing parsing with Digester.

[1] Well, the ErrorHandler associated with the parser may be invoked to
report a validation problem. And by default Digester provides an
ErrorHandler that logs then swallows reported errors. So when using
validation, you really need to specify a custom error handler too. But
again, a default ErrorHandler is provided by Digester just as a
convenience to users and you can set your own very easily.

[2] And I personally would prefer that digester didn't try to do this at
all..

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] XSD Support?

Posted by Simon Kitching <sk...@apache.org>.
On Thu, 2005-02-17 at 12:25 -0500, Jason Vinson wrote:
> For the benefit of anyone who is looking for the answers to these
> questions, here's what i've found.  Please correct any glaring errors
> if you see them.
> 
> 1.) AFAIK, XSD is supported in the digester if you use a non-Xerces
> parser (i.e. Crimson)

No, it's supported no matter what parser you use, once it's enabled.
It's just enabling schema validation that may be a little tricky.

Note that crimson is an old, obsolete xml parser that was bundled with
Sun's JAXP downloads for java 1.3 and earlier. I would not recommend its
use for *any* purpose. When using sun java < 1.3, just download a recent
release of xerces or any other xml parser. Sun java 1.4 and later comes
bundled with xerces.

> 
> 2.) In order to setup validation with crimson in the digester, you
> have to do something similar to the following:
> 
> ~        SAXParserFactoryImpl crimsonFactory = new SAXParserFactoryImpl();
> ~        crimsonFactory.setValidating(true);
> ~        URL url =
> Digester.class.getClassLoader().getResource("myFunSchema.xsd");
> ~        String schemaPath = url.toString();
> 
> ~        Digester digester = new Digester(crimsonFactory.newSAXParser());
> ~        digester.setSchema(schemaPath);


The Digester.setSchema method will have no effect at all if you use the
digester constructor variant that takes a parser as a parameter. So I
don't think the above code sets the schema path at all.

Also, I would be very surprised if the parser you are instantiating is
really crimson. As noted above, crimson is old and obsolete.

Re the code above to create the parser: if you want to create your own
parser, then this is indeed the way to do it. However this code is
exactly what will happen if you just let digester create the parser
anyway - except that when Digester creates the parser, the
Digester.setSchema and Digester.setValidating methods are not ignored.

> ~        digester.setErrorHandler(
> ~                  new ErrorHandler() {
> ~                      public void error(SAXParseException exception)
> throws SAXException {
> ~                          throw new SAXExceptionSubclass("Error
> validating XML - " + exception.getMessage());
> ~                      }
> ~                      public void fatalError(SAXParseException
> exception) throws SAXException {
> ~                          throw new SAXExceptionSubclass("Fatal Error
> validating XML - " + exception.getMessage());
> ~                      }
> ~                      public void warning(SAXParseException exception)
> throws SAXException {
> ~                          throw new SAXExceptionSubclass("Warning
> validating XML - " + exception.getMessage());
> ~                      }
> ~                  }
> ~                 );

Yep. You definitely need to do this, otherwise validation errors are
logged but not reported. There must be some historical reason for this;
it predates my involvement with Digester.


Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] XSD Support?

Posted by Jason Vinson <vi...@charter.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

For the benefit of anyone who is looking for the answers to these
questions, here's what i've found.  Please correct any glaring errors
if you see them.

1.) AFAIK, XSD is supported in the digester if you use a non-Xerces
parser (i.e. Crimson)

2.) In order to setup validation with crimson in the digester, you
have to do something similar to the following:

~        SAXParserFactoryImpl crimsonFactory = new SAXParserFactoryImpl();
~        crimsonFactory.setValidating(true);
~        URL url =
Digester.class.getClassLoader().getResource("myFunSchema.xsd");
~        String schemaPath = url.toString();

~        Digester digester = new Digester(crimsonFactory.newSAXParser());
~        digester.setSchema(schemaPath);
~        digester.setErrorHandler(
~                  new ErrorHandler() {
~                      public void error(SAXParseException exception)
throws SAXException {
~                          throw new SAXExceptionSubclass("Error
validating XML - " + exception.getMessage());
~                      }
~                      public void fatalError(SAXParseException
exception) throws SAXException {
~                          throw new SAXExceptionSubclass("Fatal Error
validating XML - " + exception.getMessage());
~                      }
~                      public void warning(SAXParseException exception)
throws SAXException {
~                          throw new SAXExceptionSubclass("Warning
validating XML - " + exception.getMessage());
~                      }
~                  }
~                 );
~        // Add rules here

~        Object myObj = digester.parse(new StringReader(xml));

If anyone knows of a cleaner/more efficient way to do this, i'm all
ears  :)

Thanks,
Jason



Jason Vinson wrote:

| After reading some docs around the web, i found this:
|
| http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg31474.html
|
|
|
| And then found some other docs (not sure of the age) that mention
| Xerces having limited XML Schema support.  So I guess my questions
| are 1.) Do any of you use XSD's with your digesters, and 2.) how
| can i use something other than Xerces for my content handler in the
|  digester?
|
| Thanks for any pointers, Jason


- ---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCFNOLAQ71cZOfvEQRAo6BAJsF+8uC/gcY93hPxTHaQpPYXliuvQCePjzO
r2iPhXLVd4wBpHHdzfuYOh8=
=6BdI
-----END PGP SIGNATURE-----


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] XSD Support?

Posted by Jason Vinson <vi...@charter.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

After many struggles yesterday, i figured out how to do it from
scanning Sun's java forums.  As you mentioned and as was mentioned in
the GMANE link i sent, passing in a pre-configured parser seems like
the cleanest way to handle it.

I appreciate the feedback.  Validation with schemas is something that
is relatively new to me.
Jason


Simon Kitching wrote:

| On Fri, 2005-02-18 at 12:22 +1300, Simon Kitching wrote:
|
|> (a) selecting a parser for digester to use:
|>
|> Digester can use an xml parser you explicitly provide: // somehow
|> instantiate and configure the parser you want Digester d = new
|> Digester(myparser);
|>
|> Alternatively, you can allow Digester to create the xml parser
|> instance (using the default java discovery mechanism to find it),
|> then tweak its settings: Digester d = new Digester(); XMLReader
|> reader = d.getXMLReader(); reader.setFeature(....);
|> reader.setProperty(...); etc
|
|
| Or (c) use the Digester as a ContentHandler: parser = ...; //
| somehow create your own parser Digester = new Digester();
| d.addRule(.....); parser.setContentHandler(d);
| parser.setErrorHandler(myErrorHandler); parser.parse(...);
|
|
|
| ---------------------------------------------------------------------
|  To unsubscribe, e-mail:
| commons-user-unsubscribe@jakarta.apache.org For additional
| commands, e-mail: commons-user-help@jakarta.apache.org
|
|

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCFeoiAQ71cZOfvEQRAmYqAJ9SXmraWtKHxXTcxt979vPnG63g9ACfa6gm
0mNydkqg45lTB8Pbu4BUahc=
=eSq2
-----END PGP SIGNATURE-----


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] XSD Support?

Posted by Simon Kitching <sk...@apache.org>.
On Fri, 2005-02-18 at 12:22 +1300, Simon Kitching wrote:

> (a) selecting a parser for digester to use:
> 
> Digester can use an xml parser you explicitly provide:
>   // somehow instantiate and configure the parser you want
>   Digester d = new Digester(myparser);
> 
> Alternatively, you can allow Digester to create the xml parser instance
> (using the default java discovery mechanism to find it), then tweak its
> settings:
>   Digester d = new Digester();
>   XMLReader reader = d.getXMLReader();
>   reader.setFeature(....);
>   reader.setProperty(...);
>   etc

Or (c) use the Digester as a ContentHandler:
   parser = ...; // somehow create your own parser
   Digester = new Digester();
   d.addRule(.....);
   parser.setContentHandler(d);
   parser.setErrorHandler(myErrorHandler);
   parser.parse(...);



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] XSD Support?

Posted by Simon Kitching <sk...@apache.org>.
On Thu, 2005-02-17 at 11:29 -0500, Jason Vinson wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> After reading some docs around the web, i found this:
> 
> http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg31474.html
> 
> And then found some other docs (not sure of the age) that mention
> Xerces having limited XML Schema support.  So I guess my questions are
> 1.) Do any of you use XSD's with your digesters, and 2.) how can i use
> something other than Xerces for my content handler in the digester?

It should be quite possible to do schema validation when using Digester.

(a) selecting a parser for digester to use:

Digester can use an xml parser you explicitly provide:
  // somehow instantiate and configure the parser you want
  Digester d = new Digester(myparser);

Alternatively, you can allow Digester to create the xml parser instance
(using the default java discovery mechanism to find it), then tweak its
settings:
  Digester d = new Digester();
  XMLReader reader = d.getXMLReader();
  reader.setFeature(....);
  reader.setProperty(...);
  etc

If you let Digester create the parser, then you can control which
implementation it finds by using the standard JDK mechanisms. See the
javadoc for SAXParserFactory.newInstance() for more information.

The default parser in java 1.4 is org.apache.xerces. The default parser
in java 1.5 is com.sun.org.apache.xerces, ie still xerces but with the
package name tweaked slightly to avoid problems that plagued 1.4 when
attempting to use a newer version of xerces. You mention the "crimson"
parser in another email; I believe this is the default only if you
download an old JAXP implementation for jdks <= 1.3.

Note also that using an "upgraded" version of xerces with java 1.4 is
very difficult; core classes in the java runtime override classes in the
classpath - and they bundled o.a.xerces in the runtime! There are ways
around this - see the xerces site, or google.

(b) enabling schema validation on the parser:

The W3C standards mandate a couple of features/properties that all
parsers must recognise. But unfortunately there are many useful features
that are *not* standardised, so you need to know what actual parser
implementation is being used in order to configure those. And sadly
enabling schema validation is one of those.

The email you refer provides an API for configuring some
commonly-used-but-not-standardised parser features, by having a set of
per-parser-implementation modules that can map the calls onto the
parser-specific property or feature. This code was committed to
Digester, and is present in the 1.6 release, in the o.a.c.d.parsers
package. Of course this only works if the actual parser being used by
digester is one with support code present in the o.a.c.d.parsers
package.

In an separate email, you wrote:
On Thu, 2005-02-17 at 10:24 -0500, Jason Vinson wrote:
> I am reworking a digester module in a project to use an XSD for
> validation, and I am getting an interesting exception from the
> org.apache.commons.digester.parser.XercesParser class.  In the
> configureXerces method on the line that says:
> 
> factory.setFeature(XERCES_DYNAMIC, true);
> 
> i get the following:
> 
> org.xml.sax.SAXNotRecognizedException: Feature:

I don't know why that is happening. I presume you're calling the
Digester.setSchema method? That method calls into the o.a.c.d.parsers
code, which auto-detects what concrete parser type you have (xerces it
would appear) then performs the appropriate parser-specific setup to
enable schema validation. It looks to me like either you have some odd
parser in your path that is being detected as xerces when it isn't, or
you have a very old or new version of xerces that doesn't support the
feature "http://apache.org/xml/features/validation/dynamic", or uses
some other name for it.

I can't help you with any problems in the o.a.c.d.parser package. You
may wish to contact Jean-Francois Arcand who wrote that code. 

But personally I wouldn't bother with calling Digester.setSchema anyway;
just set up the parser to do schema validation manually (ie *not*
through the digester APIs). See section (a) above for ways of doing
that. Of course you'll then have to include parser-specific code in your
application - thank the W3C and Sun for not providing a standard way to
enable schema validation!

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org