Michael,
I finally understood your point (I could be slow sometimes ;o)). What I
have now:
- with LSParser:
// get DOM Implementation using DOM Registry
System.setProperty(DOMImplementationRegistry.PROPERTY,"org.apache.xerces
.dom.DOMXSImplementationSourceImpl");
DOMImplementationRegistry registry =
DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS)registry.getDOMImplementation("LS");
// create DOMBuilder
LSParser builder =
impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
DOMConfiguration config = builder.getDomConfig();
builder.setFilter(skipWhitespaceFilter);
// Create the document builder
JMSErrorHandler2 errorHandler = new JMSErrorHandler2();
config.setParameter("error-handler", errorHandler);
config.setParameter("schema-type",
"http://www.w3.org/2001/XMLSchema");
config.setParameter("http://xml.org/sax/features/validation", true);
config.setParameter("http://apache.org/xml/features/validation/schema",
true);
config.setParameter("http://apache.org/xml/features/validation/schema/el
ement-default", true);
// Define the catalog resolver
XMLCatalogResolver catalog = new XMLCatalogResolver();
String[] catList = {System.getProperty("CATALOG")};
catalog.setCatalogList(catList);
// Set the prefere
catalog.setPreferPublic(true);
config.setParameter("resource-resolver", catalog);
// Parse the xml file
Document doc = null;
LSInput input = impl.createLSInput();
if (xml instanceof File) {
input.setByteStream(new
FileInputStream((File)xml));
} else if (xml instanceof String) {
input.setCharacterStream(new
StringReader((String)xml));
}
doc = builder.parse(input);
// Error checking
if ( errorHandler.validationError == true ) {
// errors occured during the parsing
if (myLogger.isInfoEnabled()) {
myLogger.info(funcName + "XML file is
not valid - Got a parsing error");
}
} else {
if (myLogger.isInfoEnabled()) {
myLogger.info(funcName + "XML file is
valid");
}
// Return the augmented document
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer();
DOMSource domSource = new DOMSource(doc);
StringWriter message = new StringWriter();
StreamResult streamResult = new
StreamResult(message);
transformer.transform(domSource, streamResult);
System.out.println(message.toString());
}
- With DOM factory:
// Create the dom factory
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
// Set the namespace property
domFactory.setNamespaceAware(true);
// Ignore the comments
domFactory.setIgnoringComments(true);
// Eliminate whitespace in element content
domFactory.setIgnoringElementContentWhitespace(true);
domFactory.setCoalescing(true);
// Set the schema language property to be used for
validation
domFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaL
anguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
domFactory.setFeature("http://xml.org/sax/features/validation", true);
domFactory.setFeature("http://apache.org/xml/features/validation/schema"
, true);
domFactory.setFeature("http://apache.org/xml/features/validation/schema/
element-default",true);
// Define the catalog resolver
XMLCatalogResolver catalog = new XMLCatalogResolver();
String[] catList = {System.getProperty("CATALOG")};
catalog.setCatalogList(catList);
// Set the prefere
catalog.setPreferPublic(true);
// Add the catalog to the document builder factory
domFactory.setAttribute("http://apache.org/xml/properties/internal/entit
y-resolver", catalog);
// Create the document builder
DocumentBuilder builder =
domFactory.newDocumentBuilder();
// Set an error handler
JMSErrorHandler errorHandler = new JMSErrorHandler();
builder.setErrorHandler(errorHandler);
// Parse the xml file
Document doc = null;
if (xml instanceof File) {
doc = builder.parse((File)xml);
} else if (xml instanceof String) {
doc = builder.parse(new InputSource(new
StringReader((String)xml)));
}
// Error checking
if ( errorHandler.validationError == true ) {
// errors occured during the parsing
if (myLogger.isInfoEnabled()) {
myLogger.info(funcName + "XML file is
not valid - Got a parsing error");
myLogger.info(funcName +
errorHandler.saxParseException.getMessage());
}
} else {
if (myLogger.isInfoEnabled()) {
myLogger.info(funcName + "XML file is
valid");
}
// Return the augmented document
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer();
DOMSource domSource = new DOMSource(doc);
StringWriter message = new StringWriter();
StreamResult streamResult = new
StreamResult(message);
transformer.transform(domSource, streamResult);
System.out.println(message.toString());
}
Both methods are parsing, validating and augmenting with default values
:o) If you see something wrong, please tell me.
Thx!!!
Lydie.
-----Original Message-----
From: Decoker, Lydie (Lydie) [mailto:[EMAIL PROTECTED]
Sent: Friday, October 13, 2006 17:06
To: [email protected]
Subject: RE: element-default with DOM3
Michael,
Those 3 lines come from [1]:
> config.setParameter("http://xml.org/sax/features/validation", true);
>
config.setParameter("http://apache.org/xml/features/validation/schema",t
rue);
>
config.setParameter("http://apache.org/xml/features/validation/schema/el
ement-default", true);
I used the same features when I use a DOM factory. If you do not set all
those 3 to true, the element-default augmentation is not done. When
using LSParser, the augmentation is not done even if I use those 3
features. If I do something wrong, thx to help to correct my code. I am
quite new in this Java area.
[1]
http://xerces.apache.org/xerces2-j/features.html#validation.schema.eleme
nt-default
Regards,
Lydie.
-----Original Message-----
From: Michael Glavassevich [mailto:[EMAIL PROTECTED]
Sent: Friday, October 13, 2006 16:46
To: [email protected]
Subject: RE: element-default with DOM3
Not sure what's going on but your use of the JAXP validation API is
completely unnecessary. You're already telling the LSParser to validate
the input.
Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [EMAIL PROTECTED]
E-mail: [EMAIL PROTECTED]
"Decoker, Lydie \(Lydie\)" <[EMAIL PROTECTED]> wrote on 10/13/2006
03:09:22 AM:
> It did not add the default element value :o(
>
> What I have is:
>
> System.setProperty(DOMImplementationRegistry.PROPERTY,"org.apache.xerc
> es
> .dom.DOMXSImplementationSourceImpl");
> System.setProperty("javax.xml.validation.SchemaFactory:http://www.w3.o
> rg /2001/XMLSchema",
> "org.apache.xerces.jaxp.validation.XMLSchemaFactory");
> DOMImplementationRegistry registry =
> DOMImplementationRegistry.newInstance();
>
> DOMImplementationLS impl =
> (DOMImplementationLS)registry.getDOMImplementation("LS");
>
> // create DOMBuilder
> LSParser builder =
> impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
> DOMConfiguration config = builder.getDomConfig();
> builder.setFilter(skipWhitespaceFilter);
>
> // Create the document builder
> JMSErrorHandler2 errorHandler = new JMSErrorHandler2();
> config.setParameter("error-handler", errorHandler);
> config.setParameter("http://xml.org/sax/features/validation", true);
> config.setParameter("http://apache.org/xml/features/validation/schema"
> ,
> true);
> config.setParameter("http://apache.org/xml/features/validation/schema/
> el
> ement-default", true);
>
> // Parse the xml file
> Document doc = null;
> LSInput input = impl.createLSInput();
>
> if (xml instanceof File) {
> input.setByteStream(new FileInputStream((File)xml)); } else if (xml
> instanceof String) {
> input.setCharacterStream(new StringReader((String)xml)); } doc =
> builder.parse(input);
>
> // Create the schema factory
> SchemaFactory schemaFactory =
> SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
>
> // Create the schema object
> Schema schema = schemaFactory.newSchema(new File(schemaLocation));
>
> // Create the validator
> Validator validator = schema.newValidator();
>
> // Add an error handler to the validator JMSErrorHandler
> v_errorHandler = new JMSErrorHandler();
> validator.setErrorHandler(v_errorHandler);
>
> // Create the dom source and destination // The destination will
> contain the doc augmented with the default attribute/element DOMSource
> source = new DOMSource(doc); DOMResult result = new DOMResult();
>
> // Validate and augment the source
> validator.validate(source, result);
>
> // Error checking
> if ( v_errorHandler.validationError == true ) {
> // errors occured during the parsing
> if (myLogger.isInfoEnabled()) {
> myLogger.info(funcName + "XML file is not valid - Got a
> validation error");
> myLogger.info(v_errorHandler.saxParseException.getMessage());
> }
> Throw new NBIErrException("XML file not valid " +
> v_errorHandler.saxParseException.getMessage());
> } else {
> if (myLogger.isInfoEnabled()) {
> myLogger.info(funcName + "XML file is valid");
> }
> // Return the augmented document
> return (Document)result.getNode();
> }
> -----Original Message-----
> From: Michael Glavassevich
> Sent: Friday, October 13, 2006 06:20
> To: [email protected]
> Subject: RE: element-default with DOM3
>
> Try setting
> "http://apache.org/xml/features/validation/schema/element-default" as
> a parameter with a Boolean value and see what happens.
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: [EMAIL PROTECTED]
> E-mail: [EMAIL PROTECTED]
>
> "Decoker, Lydie \(Lydie\)" wrote on 10/12/2006
> 03:53:18 AM:
>
> > Thx Michael. But looking the links provided, I do not see what could
> > help me to add element default value in my DOM tree.
> >
> > I see that for LSSerialzer, you have an option
> > "discard-default-content". If you set it to false, I should get the
> > default value. But from what I understand LSSerializer will output a
> > string but I want to keep my DOM tree.
> >
> > Lydie.
> >
> > -----Original Message-----
> > From: Michael Glavassevich [mailto:[EMAIL PROTECTED]
> > Sent: Wednesday, October 11, 2006 19:00
> > To: [email protected]
> > Subject: Re: element-default with DOM3
> >
> > "Decoker, Lydie \(Lydie\)" wrote on 10/11/2006
> > 09:32:39 AM:
> >
> > > Hello!
> > >
> > > I am using now the LSParser (in order to get rid of the empty text
> > > node) adn LSParserFilter.
> > > I am trying to replicate what I was doing with the
DocumentBuilder.
> > > To augment my xml with the default values, the feature I had set
> was:
> > >
> > > domFactory.setFeature("http://apache.
> > > org/xml/features/validation/schema/element-default",true);
> > > Is there something similar for DOMImplementationLS used to create
> > > the LSParser?
> >
> > See DOMConfiguration [1]. This is what you use to configure [2] an
> > LSParser.
> >
> > > Thx in advance,
> > >
> > > Lydie
> >
> > [1] http://www.w3.org/TR/DOM-Level-3-Core/core.html#DOMConfiguration
> > [2]
> > http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/load-save.html
> > #L
> > S-
> > LSParser-config
> >
> > Michael Glavassevich
> > XML Parser Development
> > IBM Toronto Lab
> > E-mail: [EMAIL PROTECTED]
> > E-mail: [EMAIL PROTECTED]
> >
> > --------------------------------------------------------------------
> > - To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> > --------------------------------------------------------------------
> > - To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]