You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by Eric SCHAEFFER <es...@posterconseil.com> on 2000/04/07 17:17:10 UTC

How to validate an "org.w3c.dom.Document"

Hi,

I want to validate a document (ie an org.w3c.dom.Document object, not an XML
file) with a DTD. What is the simplest way of doing this ?

(The way I've found is to serialize the document to a string and to parse the
string, with an ErrorHandler. Not very simple, and a bit stupid ...)

Thank's
Eric.

_______________________________________

Eric SCHAEFFER
eschaeffer@posterconseil.com

POSTER CONSEIL
118 rue de Tocqueville
75017 PARIS
FRANCE
Tel. : 33-140541058
Fax : 33-140541059
_______________________________________

-------------------------------------------------------------------
 Missed us in Orlando? Make it up with ApacheCON Europe in London!
------------------------- http://ApacheCon.Com --------------------



xerces and w3c schemas

Posted by David Bourget <ro...@dbourget.com>.
Hello all,

I'd like to know what is the current state of the support for w3c schemas in
xerces. I adventured myself in the code to get an idea and saw big chunks of
code commented, and few real english comments :) (to takes this badly, i'm
joking)

More precisely, I'd like to know what have to be done to support the current
draft, and possibly what could I do to help. I will be in summer vacancy
soon so I'm looking for some entertaining project to jump in :)

I would also appreciate if someone could point me to plans / readme / status
files / whatever could help to understand the big picture, the weak spots
etc of the current implementation.
(I already saw the java/STATUS file, is it current ? What happened since 19
feb ?)


thank you,
David


Re: How to validate an "org.w3c.dom.Document"

Posted by Andy Clark <an...@apache.org>.
Eric SCHAEFFER wrote:
> Can you be more explicit ?

Look at java.io.PipedReader/Writer for the streaming.

A little more on the point I made about being able to associate 
errors with the actual DOM node that caused the error. This can
be done but requires a little more work on your part. First of
all, you need a serializer that can walk the DOM tree, writing 
to the PipedWriter *and* knows about the parser instance used
to parse the stream and register itself as an error handler.
Does this make sense?

In addition, the parser is setup, by default, to buffer large
portions of the document stream for performance reasons. If you
want to have parsing errors occur when the associated nodes are
being serialized, then you have to use a parser instance that
doesn't buffer its input. We don't have an example of doing 
this but I can point you in the right direction. Check out the
org.apache.xerces.readers.StreamingCharReader class as well as
the org.apache.xerces.readers.DefaultReaderFactory class. 

The DefaultReaderFactory is used by the parser to recognize
the XML or Text decl lines and construct an appropriate 
reader to handle the document. By default, the reader factory
always returns buffered readers. You can create a subclass of
this factory to return StreamingCharReader objects instead.

Then it's just a simple matter of making a parser subclass
that uses your new reader factory instead of the default one.

I hope this is enough information to get you started. If you
write the code for the streaming parser instance, please
contribute the code so we can add it to either a) the
primary Xerces package or b) as a contributed extension.

Perhaps the whole issue of contributions needs to be thought
about and decided upon. Anyone with thoughts on this?

-- 
Andy Clark * IBM, JTC - Silicon Valley * andyc@apache.org

Re: How to validate an "org.w3c.dom.Document"

Posted by Eric SCHAEFFER <es...@posterconseil.com>.
Can you be more explicit ?

I'm not familiar with Java Streams. And the only stream I can create is a
ByteArrayInputStream/ByteArrayOutputStream (all others are created from
another stream). If I do so, I still need to write all the data in the
ByteArrayOutputStream buffer before parsing it...

Maybe if I use another stream type ?

Thank's a lot
Eric.

PS: my code (with streams)

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  org.apache.xml.serialize.OutputFormat outputFormat = new
org.apache.xml.serialize.OutputFormat(document);
  outputFormat.setOmitXMLDeclaration(false);
  outputFormat.setDoctype(null, "/test/territoire_posterscan.dtd");
  outputFormat.setIndenting(true);
  outputFormat.setIndent(1);
  org.apache.xml.serialize.XMLSerializer serializer = new
org.apache.xml.serialize.XMLSerializer(out, outputFormat);
  serializer.serialize(document);

  org.apache.xerces.parsers.DOMParser parser = new
org.apache.xerces.parsers.DOMParser();
  ParserErrorHandler errorHandler = new ParserErrorHandler();
  parser.setErrorHandler(errorHandler);
  parser.setFeature("http://xml.org/sax/features/validation", true);
  parser.setLocale(new Locale(language, country));
  parser.parse(new org.xml.sax.InputSource(new
ByteArrayInputStream(out.toByteArray()))); // SAXParseException


----- Original Message -----
From: Andy Clark <an...@apache.org>
To: <xe...@xml.apache.org>
Sent: Monday, April 10, 2000 11:53 PM
Subject: Re: How to validate an "org.w3c.dom.Document"


> Eric SCHAEFFER wrote:
> > (The way I've found is to serialize the document to a string and to parse
the
> > string, with an ErrorHandler. Not very simple, and a bit stupid ...)
>
> That sounds about right. Except that I would serialize it to a
> stream that is parsed instead of wasting memory by building a
> string. And by doing so you could trap the validation errors
> and associate them to the current node being serialized.
>
> --
> Andy Clark * IBM, JTC - Silicon Valley * andyc@apache.org
>


Re: How to validate an "org.w3c.dom.Document"

Posted by Andy Clark <an...@apache.org>.
Eric SCHAEFFER wrote:
> (The way I've found is to serialize the document to a string and to parse the
> string, with an ErrorHandler. Not very simple, and a bit stupid ...)

That sounds about right. Except that I would serialize it to a
stream that is parsed instead of wasting memory by building a
string. And by doing so you could trap the validation errors 
and associate them to the current node being serialized.

-- 
Andy Clark * IBM, JTC - Silicon Valley * andyc@apache.org

Re: How to validate an "org.w3c.dom.Document"

Posted by Andy Clark <an...@apache.org>.
This has turned into a pretty good discussion. Perhaps we should
cross-post the whole thread to the new xerces-j-dev mailing list.

-- 
Andy Clark * IBM, JTC - Silicon Valley * andyc@apache.org