Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » XML Validation
XML Validation [message #426106] Tue, 16 December 2008 14:47 Go to next message
LoÃ?¯c QuÃ?©ran is currently offline LoÃ?¯c QuÃ?©ranFriend
Messages: 45
Registered: July 2009
Member
Hi,

I want to validate an XML file against its XML schema. To be more
precise, I want to validate different XML files that should conform to
different XML schemas, which represent different versions of the same
business model. Each xsd is located in its corresponding model EMF
plugin, in directory model.

I had hoped that specifying xsi:schemaLocation in the XML would be
sufficient in conjunction with an XMLResource option to activate
validation. But when I look at the schema location handling, I can't see
how this could work. Is it correct ?

As I have heavily tuned the serialization/deserialization process and as
this process is independant of the different versions, I am reluctant to
modify XMLLoad, for example to change the SAXParserFactory in makeParser().

So I have tried to use XMLResource.OPTION_PARSER_FEATURES and
XMLResource.OPTION_PARSER_PROPERTIES (see code below).

But this raises an exception org.xml.sax.SAXNotSupportedException:
Property 'http://java.sun.com/xml/jaxp/properties/schemaLanguage' must
be set before setting property
'http://java.sun.com/xml/jaxp/properties/schemaSource'. This exception
comes from the fact that Xerces implementation, which is the default SAX
Parser in Sun's JDK 1.6 expects the properties in a precise order, which
is not enforced by EMF map options.

How can I simply validate the files I load ? Must I use the parser pool
to explicitly define the parser ? Isn't there anything simpler ?

To complicate things, I would like to load with XML validation errors in
resource.getErrors(), without failing if such errors occur.

Thanks in advance.

Loïc




// Turn on parser validation
Map<String, Boolean> parserFeatures = new HashMap<String, Boolean>();
parserFeatures.put("http://xml.org/sax/features/validation",
Boolean.TRUE); //$NON-NLS-1$
result.getDefaultLoadOptions().put(XMLResource.OPTION_PARSER _FEATURES,
parserFeatures);

// Define the schema to use. See JAXPConstants for evolution using
// public constants.
SchemaFactory factory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema;
InputStream stream = null;
try {
URL find = FileLocator.find(Activator.getDefault().getBundle(),
new Path("model/<mymodel>.xsd"), null);
stream = find.openStream();
Source source = new StreamSource(stream);
schema = factory.newSchema(source);
Map<String, Object> parserProperties = new HashMap<String, Object>();
parserProperties.put("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");

parserProperties.put("http://java.sun.com/xml/jaxp/properties/schemaSource",
schema);

result.getDefaultLoadOptions().put(XMLResource.OPTION_PARSER _PROPERTIES,
parserProperties);
} catch (Exception ex) {
...
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException ex) {
...
}
}
}
Re: XML Validation [message #426108 is a reply to message #426106] Tue, 16 December 2008 15:20 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30892
Registered: July 2009
Senior Member
Loïc,

Comments below.

Loïc Quéran wrote:
> Hi,
>
> I want to validate an XML file against its XML schema. To be more
> precise, I want to validate different XML files that should conform to
> different XML schemas, which represent different versions of the same
> business model. Each xsd is located in its corresponding model EMF
> plugin, in directory model.
>
> I had hoped that specifying xsi:schemaLocation in the XML would be
> sufficient in conjunction with an XMLResource option to activate
> validation. But when I look at the schema location handling, I can't
> see how this could work. Is it correct ?
EMF does validation itself. Part of that happens while reading and the
rest when you invoke Diagnostician.INSTANCE.validate for the root object
of the resource.
>
> As I have heavily tuned the serialization/deserialization process and
> as this process is independant of the different versions, I am
> reluctant to modify XMLLoad, for example to change the
> SAXParserFactory in makeParser().
To do true XML validation, you'd need to specialize the SAXParser to do
that as it parses.
>
> So I have tried to use XMLResource.OPTION_PARSER_FEATURES and
> XMLResource.OPTION_PARSER_PROPERTIES (see code below).
>
> But this raises an exception org.xml.sax.SAXNotSupportedException:
> Property 'http://java.sun.com/xml/jaxp/properties/schemaLanguage' must
> be set before setting property
> 'http://java.sun.com/xml/jaxp/properties/schemaSource'. This exception
> comes from the fact that Xerces implementation, which is the default
> SAX Parser in Sun's JDK 1.6 expects the properties in a precise order,
> which is not enforced by EMF map options.
Jeesh, that's kind of a bad joke. I suppose you could use a
LinkedHashMap to control the order.
>
> How can I simply validate the files I load ? Must I use the parser
> pool to explicitly define the parser ? Isn't there anything simpler ?
There's no simpler way.
>
> To complicate things, I would like to load with XML validation errors
> in resource.getErrors(), without failing if such errors occur.
I think the errors will be recorded as long as they aren't treated as fatal.
>
> Thanks in advance.
>
> Loïc
>
>
>
>
> // Turn on parser validation
> Map<String, Boolean> parserFeatures = new HashMap<String, Boolean>();
> parserFeatures.put("http://xml.org/sax/features/validation",
> Boolean.TRUE); //$NON-NLS-1$
>
> result.getDefaultLoadOptions().put(XMLResource.OPTION_PARSER _FEATURES,
> parserFeatures);
>
> // Define the schema to use. See JAXPConstants for evolution using
> // public constants.
> SchemaFactory factory =
> SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
> Schema schema;
> InputStream stream = null;
> try {
> URL find =
> FileLocator.find(Activator.getDefault().getBundle(),
> new Path("model/<mymodel>.xsd"), null);
> stream = find.openStream();
> Source source = new StreamSource(stream);
> schema = factory.newSchema(source);
> Map<String, Object> parserProperties = new HashMap<String, Object>();
> parserProperties.put("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
> "http://www.w3.org/2001/XMLSchema");
>
> parserProperties.put("http://java.sun.com/xml/jaxp/properties/schemaSource",
> schema);
>
> result.getDefaultLoadOptions().put(XMLResource.OPTION_PARSER _PROPERTIES,
> parserProperties);
> } catch (Exception ex) {
> ...
> } finally {
> if (stream != null) {
> try {
> stream.close();
> } catch (IOException ex) {
> ...
> }
> }
> }
Previous Topic:[CDO] CDORevisionImpl.eIsSet() works incorrectly
Next Topic:[CDO] About revisions
Goto Forum:
  


Current Time: Thu Feb 20 14:17:02 GMT 2020

Powered by FUDForum. Page generated in 0.03436 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top