Hi,
There is no Oxygen API for schematron validation from a plugin or from an Author extension.
Schematron validation basically consists of applying an XSL stylesheet over the schematron file and then applying the result stylesheet over the XML content.
See this site for more details:
http://www.schematron.com/
I will assume you want to use ISO schematron with XPath 1.0.
Here is a sample code which does schematron validation based on the schematron schemas available at:
http://www.schematron.com/tmp/iso-schematron-xslt1.zip
The validation is performed on two Oxygen sample files.
Code: Select all
public static void main(String[] args) throws Exception{
URL xmlURL = new URL("file:/C:/Users/developer/Documents/OxygenXMLEditor/samples/schematron/iso/tournament/Tournament.xml");
URL schURL = new URL("file:/C:/Users/developer/Documents/OxygenXMLEditor/samples/schematron/iso/tournament/tournament-schema.sch");
URL xslSchMessagesURL = new URL("file:/C:/Users/developer/Desktop/iso-schematron-xslt1/iso_schematron_message.xsl");
System.err.println(checkValid(xmlURL, null, schURL, null, xslSchMessagesURL));
}
/**
* Check if an XML is valid
* @param xmlURL The XML URL
* @param xmlContent The XML Content
* @param schematronURL URL to schematron file location
* @param schematronContent The Schematron Content
* @param urlToISOSchematronMessages
* @return The schematron validation result
* @throws TransformerFactoryConfigurationError
* @throws IOException
* @throws TransformerException
*/
public static String checkValid(URL xmlURL, String xmlContent,
URL schematronURL, String schematronContent, URL urlToISOSchematronMessages) throws TransformerFactoryConfigurationError, IOException, TransformerException {
//First apply the XSL over the schematron content
InputSource xslSCHMessages = new InputSource();
if(urlToISOSchematronMessages != null) {
xslSCHMessages.setSystemId(urlToISOSchematronMessages.toString());
}
Transformer newTransformer = TransformerFactory.newInstance().newTransformer(new SAXSource(xslSCHMessages));
//Apply over the schematron content
File xslFromSchematron = File.createTempFile("sch", ".xsl");
Result result = new StreamResult(xslFromSchematron);
InputSource schematronIS = new InputSource();
if(schematronURL != null) {
schematronIS.setSystemId(schematronURL.toString());
}
if(schematronContent != null) {
schematronIS.setCharacterStream(new StringReader(schematronContent));
}
newTransformer.transform(new SAXSource(schematronIS), result);
//Now apply the XSL obtained from applying the Schematron XSL over the SCH file
newTransformer = TransformerFactory.newInstance().newTransformer(new SAXSource(new InputSource(xslFromSchematron.toURL().toString())));
StringWriter resultWriter = new StringWriter();
result = new StreamResult(resultWriter);
InputSource xmlIS = new InputSource();
if(xmlURL != null) {
xmlIS.setSystemId(xmlURL.toString());
}
if(xmlContent != null) {
xmlIS.setCharacterStream(new StringReader(xmlContent));
}
newTransformer.transform(new SAXSource(xmlIS), result);
return resultWriter.toString();
}
This code can be used either from an Author custom operation (which can access both the URL of the current file and its XML content through the AuthorAccess) or from the custom protocol plugin when the save is performed.
How to detect save is performed on an URL using the custom URL Handler:
The output stream will be requested from the custom URLConnection implementation. You can then return your own OutputStream implementation which collects all the written bytes in a string buffer.
When
close() is called on the OutputStream implementation you can call the
validate method. If validation is not succesful you can throw an IOException which will interrupt the Save operation. If it is succesful you can save the content to the real destination.
Regards,
Radu