Validating against an xml schema with a plugin

Having trouble installing Oxygen? Got a bug to report? Post it all here.
micah.hainline
Posts: 3
Joined: Fri Apr 12, 2013 11:19 pm

Validating against an xml schema with a plugin

Post by micah.hainline »

I have created a plugin for use in Oxygen Author. Inside that plugin I want to validate a piece of XML that I receive from the user, and so I use the standard SchemaFactory mechanism to do this:

Code: Select all


SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaSource);
Validator validator = schema.newValidator();
validator.validate(xmlSource);
In a normal Java environment this does exactly what I want. Inside Oxygen, however, it throws an exception. It seems that Saxon has been registered with the SchemaFactory, but it throws a license-related exception when I try to use the validator.

Code: Select all

net.sf.saxon.trans.LicenseException: License file saxon-license.lic not found.
If I explicitly use the schema factory found in the JVM, everything works fine:

Code: Select all


SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI, "com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory", getClass().getClassLoader());
However, the factory class string is a private implementation detail in the JVM, and I feel uncomfortable using this. As far as I know the next version of Java may decide to include a different schema factory and my code would no longer function.

My question is this: how am I supposed to do a basic xml schema validation from inside the Oxygen environment? I find it ironic that the standard Java mechanism doesn't work.
Radu
Posts: 9054
Joined: Fri Jul 09, 2004 5:18 pm

Re: Validating against an xml schema with a plugin

Post by Radu »

Hi,

Oxygen comes with the Saxon 9 XSLT processor saxon9ee.jar library in the classpath and indeed the library registers in its META-INF/services as a schema validator factory.
Your current approach is the correct one.
What we could do on our side:
We will consider repacking the library and remove from it the service which registers it as the schema validator factory.
Also in the next version of Oxygen (15.0) the Saxon 9 EE validator will be considered licensed even when used via a factory.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
micah.hainline
Posts: 3
Joined: Fri Apr 12, 2013 11:19 pm

Re: Validating against an xml schema with a plugin

Post by micah.hainline »

Either of the two options you outline would make sense. It's not a problem if it's registered with the SchemaFactory as long as it doesn't throw license exceptions, and it's not a problem if it's not registered at all. It's only a problem when it's registered and also doesn't work.
yann1806
Posts: 22
Joined: Fri Aug 09, 2013 11:03 am

Re: Validating against an xml schema with a plugin

Post by yann1806 »

Hi guys,

reusing this topic because it's so close to my question:

how can I validate an XSD that is part of a framework? I want to intercept the save so that the XSD is validated (against the XSD language definition).

I am not trying to validate an XML against an XSD (the example given above), I'm trying to validate an XSD.

Also (not sure if it matters), because the XSD is part of a framework, it starts with something like:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://www.oxygenxml.com/ns/myxsd"
xmlns="http://www.oxygenxml.com/ns/myxsd">

Help greatly appreciated,

Yannick
iulian_velea
Posts: 63
Joined: Fri Dec 12, 2003 6:34 pm
Contact:

Re: Validating against an xml schema with a plugin

Post by iulian_velea »

Hello Yannick,

I have a few questions related to your post.
First, did you know that oXygen already performs automatic validation when resources are edited and saved? The option controlling this behavior can be found in the Editor / Document checking option page and it's called Enable automatic validation. This feature performs a full XSD validation.
Another guess would be that maybe you want to perform the schema file validation on your own with an oXygen plugin. In this case we will need more information about what exactly you want to achieve.
Have you tried the SchemaFactory instanciation approach?

Code: Select all

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaSource);
Are there any special options or features you want to set for this custom validation?

Best regards,
Iulian

--
Iulian Velea
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger http://www.oxygenxml.com
yann1806
Posts: 22
Joined: Fri Aug 09, 2013 11:03 am

Re: Validating against an xml schema with a plugin

Post by yann1806 »

Hi Lulian,

thanks for your answer.

I want to programatically validate the XSD document itself, for example:

<xs:sequence>
<xs:element maxOccurs="unbounded" ref="block"/>
</xs:sequence>

is valid in an XSD, but something like:

<xs:sequenceABCDEF>
<xs:element maxOccurs="unbounded" ref="block"/>
</xs:sequenceABCDEF>

is not valid in an XSD (even if the XML remains well-formed).

I think the suggestion you're giving has to do with validating XML documents against their schemas, but I want to validate the schema itself.

I know Oxygen can do it by default (I see "xs:sequenceABCDEF" underlined in red when I edit my schema), but I'm not sure how to do it programatically in my own save function that saves to a remote CMS.

Thanks a lot!

Yannick
iulian_velea
Posts: 63
Joined: Fri Dec 12, 2003 6:34 pm
Contact:

Re: Validating against an xml schema with a plugin

Post by iulian_velea »

Hello,

If you want to make sure the user is aware that the schema file is invalid before saving it, there is an option in the Editor / Open/Save option page named Check errors on save controlling this. If this option is checked, a dialog is shown when an invalid file is saved, giving the user the possibility to stop the save operation.
However if you want to completely stop the user from saving invalid schema on the CMS you should catch the exception that should be thrown from the snippet of code from my previous post, when the schema is invalid, and programatically cancel the save operation.

Regards,
Iulian
yann1806
Posts: 22
Joined: Fri Aug 09, 2013 11:03 am

Re: Validating against an xml schema with a plugin

Post by yann1806 »

Hi Lulian,

thanks - got it, it works.

My last question is: to validate the content before saving: how can I transform the current editor content Reader object returned by createContentReader() to a Source object for the newSchema() method?

I can't find the right API.

Thanks,

Yannick
yann1806
Posts: 22
Joined: Fri Aug 09, 2013 11:03 am

Re: Validating against an xml schema with a plugin

Post by yann1806 »

Ah never mind, got it:

new StreamSource(new StringReader(IOUtils.toString(editor.createContentReader())))

Thanks for the help, much apreciated!

Yannick
iulian_velea
Posts: 63
Joined: Fri Dec 12, 2003 6:34 pm
Contact:

Re: Validating against an xml schema with a plugin

Post by iulian_velea »

Hi Yannick,

I don't think you need to read the editor content into a String and then create a StringReader over it.
Why don't you use the reader created from the editor as the argument of the StreamSource constructor directly?

Code: Select all

Schema schema = schemaFactory.newSchema(new StreamSource(editor.createReader()));
Cheers,
Iulian
yann1806
Posts: 22
Joined: Fri Aug 09, 2013 11:03 am

Re: Validating against an xml schema with a plugin

Post by yann1806 »

Brilliant!

Thanks :)

Yannick
Post Reply