diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3cdfa19 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.log +out/* diff --git a/bin/fr/inist/SaxonXSLT$1.class b/bin/fr/inist/SaxonXSLT$1.class index 7304199..968320f 100644 --- a/bin/fr/inist/SaxonXSLT$1.class +++ b/bin/fr/inist/SaxonXSLT$1.class Binary files differ diff --git a/bin/fr/inist/SaxonXSLT.class b/bin/fr/inist/SaxonXSLT.class index 546de15..1a7a459 100644 --- a/bin/fr/inist/SaxonXSLT.class +++ b/bin/fr/inist/SaxonXSLT.class Binary files differ diff --git a/lib/saxon-license.lic b/lib/saxon-license.lic new file mode 100644 index 0000000..407f211 --- /dev/null +++ b/lib/saxon-license.lic @@ -0,0 +1,18 @@ +Licensor=Saxonica +Licensee=Claude Niederlender +Company=SITE CNRS +Email=claude.niederlender@inist.fr +Edition=EE +SAT=yes +SAQ=no +SAV=yes +Issued=2017-03-31 +Series=S +Serial=S005653 +User=P0001 +Evaluation=no +Expiration=never +UpgradeDays=366 +MaintenanceDays=366 + +Signature=302D0214141E5BE8AB4FE11A4E870F573C3D31FDF5E8036F02150096327AFFDF4A78C70FD0B726E9CDE5A3875B3D44 \ No newline at end of file diff --git a/src/fr/inist/SaxonXSLT.java b/src/fr/inist/SaxonXSLT.java index 69edda0..d421b72 100644 --- a/src/fr/inist/SaxonXSLT.java +++ b/src/fr/inist/SaxonXSLT.java @@ -4,20 +4,18 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.nio.file.spi.FileTypeDetector; -import java.util.Enumeration; import java.util.HashMap; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import javax.xml.parsers.SAXParserFactory; -import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; @@ -26,7 +24,12 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; +import com.saxonica.config.EnterpriseTransformerFactory; + +import net.sf.saxon.lib.FeatureKeys; +import net.sf.saxon.lib.Validation; import net.sf.saxon.trans.XPathException; public class SaxonXSLT { @@ -37,9 +40,11 @@ String source = args[0]; String style = args[1]; String output = args[2]; + String schema = args[3]; File fIn = new File(source); File fOut = new File(output); File fXslt = new File(style); + File fSchema = new File(schema); if (style==null) { @@ -71,21 +76,35 @@ System.out.println("output must be a valid file"); usage(); return; + } + SaxonXSLT saxonXslt; + saxonXslt = new SaxonXSLT(fIn, fXslt, fOut); + if (schema != null) { + System.out.println("We will try to validate generated files thanks to "+schema); + if (!fSchema.exists()) { + System.out.println("schema must be a valid file"); + usage(); + return; + } else { + saxonXslt.setSchema(fSchema); + System.out.println("schema : "+fSchema.getAbsolutePath()); + System.out.println("hasSchema() : "+saxonXslt.hasSchema()); + + } } - SaxonXSLT saxonXslt = new SaxonXSLT(fIn, fXslt, fOut); saxonXslt.processInput(); - } public static void usage() { - System.out.print("usage : java SaxonXSLT (input can be file or dir, output must be a dir)"); + System.out.print("usage : java SaxonXSLT [] (input can be file or dir, output must be a dir)"); } - private File fIn, fXslt, fOut; + private File fIn, fXslt, fOut, fSchema; private String outputPath; private String inputPath; + public SaxonXSLT(File in, File xslt, File out) { this.fIn = in; this.fXslt = xslt; @@ -147,17 +166,48 @@ transformer.transform(xmlSource, new StreamResult(xmlOut)); System.out.println("Transformation OK, consulter le fichier "+xmlOut); + + if (hasSchema()) { + return validate(xmlOut); + } } catch (Exception err) { - System.out.println(err.getMessage()); + System.out.println("Transformation KO sur le fichier "+source); + System.err.println("Echec XSLT sur le fichier "+source); + System.err.println(err.getMessage()); err.printStackTrace(); return 1; } return 0; - } + + public int validate(File fileToValidate) { + try { + System.setProperty("javax.xml.transform.TransformerFactory", + "com.saxonica.config.EnterpriseTransformerFactory"); + TransformerFactory factory = TransformerFactory.newInstance(); + factory.setAttribute(FeatureKeys.SCHEMA_VALIDATION, new Integer( + Validation.STRICT)); + StreamSource schema = new StreamSource(fSchema.toURI().toString()); + ((EnterpriseTransformerFactory) factory).addSchema(schema); + Transformer trans = factory.newTransformer(); + StreamSource source = new StreamSource(fileToValidate.toURI().toString()); + SAXResult sink = new SAXResult(new DefaultHandler()); + trans.transform(source, sink); + + } catch (TransformerException err) { + System.out.println("Validation KO sur le fichier "+fileToValidate.getAbsolutePath()); + System.err.println("Erreur de validation sur le fichier "+fileToValidate.getAbsolutePath()); + System.err.println(err.getMessage()); + return 1; + } + System.out.println("Validation OK sur le fichier "+fileToValidate.getAbsolutePath()); + return 0; + } + + /** * Maintain prepared stylesheets in memory for reuse */ @@ -177,16 +227,19 @@ } return x; } - - /** - * Clear the cache. Useful if stylesheets have been modified, or simply if space is - * running low. We let the garbage collector do the work. - */ - - private synchronized void clearCache() { - cache = new HashMap(20); - } - - private static HashMap cache = new HashMap(20); + + public File getSchema() { + return fSchema; + } + + public void setSchema(File fSchema) { + this.fSchema = fSchema; + } + + public boolean hasSchema() { + return (this.fSchema != null); + } + + private static HashMap cache = new HashMap(20); }