NXvalidate  1
 All Classes Namespaces Files Functions Variables
NXschematron.java
Go to the documentation of this file.
1 /* NeXus - Neutron & X-ray Common Data Format
2  *
3  * NeXus file validation GUI tool.
4  *
5  * Copyright (C) 2010 Nexus Team
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * For further information, see <http://www.nexusformat.org/>
22  *
23  * NXschematron.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import org.nexusformat.nxvalidate.resources.XSLTResolver;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 
34 import java.io.InputStream;
35 import java.io.OutputStream;
36 import javax.xml.transform.Transformer;
37 import javax.xml.transform.TransformerException;
38 import javax.xml.transform.TransformerFactory;
39 import javax.xml.transform.stream.StreamResult;
40 import javax.xml.transform.stream.StreamSource;
41 
42 public class NXschematron {
43 
44  private File reducedNeXusFile;
45  private File schematronFile;
46  private File inputNexusFile;
47  private boolean keepTemp;
48  private InputStream dsdlIncludeXSLTStream = null;
49  private InputStream abstractExpandXSLTStream = null;
50  private InputStream svrlForXslt2XSLTStream = null;
51  private InputStream nxdl2schXSLTStream = null;
52  //protected String schematronxslt[] = new String[]{"iso_dsdl_include.xsl",
53  // "iso_abstract_expand.xsl", "iso_svrl_for_xslt2.xsl"};
54 
55  public NXschematron(File inputNexusFile, File reducedNeXusFile, File schematronFile,
56  final boolean keepTemp) {
57 
58  this.reducedNeXusFile = reducedNeXusFile;
59  this.schematronFile = schematronFile;
60  this.inputNexusFile = inputNexusFile;
61  this.keepTemp = keepTemp;
62 
63  // The Schematron files...
64  dsdlIncludeXSLTStream =
65  NXschematron.class.getResourceAsStream(
66  "resources/iso_dsdl_include.xsl");
67  abstractExpandXSLTStream =
68  NXschematron.class.getResourceAsStream(
69  "resources/iso_abstract_expand.xsl");
70  svrlForXslt2XSLTStream =
71  NXschematron.class.getResourceAsStream(
72  "resources/iso_svrl_for_xslt2.xsl");
73  //XSLT for NXDL to sch
74  nxdl2schXSLTStream =
75  NXschematron.class.getResourceAsStream(
76  "resources/xslt/nxdl2sch.xsl");
77 
78  }
79 
88  public static void TransformoMatic(String inputFilename,
89  String xslFilename, String outputFilename)
90  throws TransformerException {
91  TransformoMatic(new File(inputFilename), new File(xslFilename),
92  new File(outputFilename));
93  }
94 
102  public static void TransformoMatic(File inputFile, File xslFile,
103  File outputFile) throws TransformerException {
104 
105  // Create an instance of the transform factory
106  TransformerFactory myFactory = TransformerFactory.newInstance(
107  "net.sf.saxon.TransformerFactoryImpl", null);
108  myFactory.setURIResolver(new XSLTResolver());
109  // Create a transformer for the xsl stylesheet
110  Transformer transformer = myFactory.newTransformer(new StreamSource(
111  xslFile));
112  transformer.setParameter("generate-paths", "yes");
113  transformer.setURIResolver(new XSLTResolver());
114  // Do the transform
115  transformer.transform(new StreamSource(inputFile), new StreamResult(
116  outputFile));
117 
118  }
119 
120  public static void TransformoMatic(InputStream inputFileStream,
121  InputStream xslFileStream, OutputStream outputFileStream)
122  throws TransformerException {
123 
124  // Create an instance of the transform factory
125  TransformerFactory myFactory = TransformerFactory.newInstance(
126  "net.sf.saxon.TransformerFactoryImpl", null);
127 
128  myFactory.setURIResolver(new XSLTResolver());
129  // Create a transformer for the xsl stylesheet
130  Transformer transformer = myFactory.newTransformer(new StreamSource(
131  xslFileStream));
132 
133  transformer.setParameter("generate-paths", "yes");
134  transformer.setURIResolver(new XSLTResolver());
135  // Do the transform
136  transformer.transform(new StreamSource(inputFileStream),
137  new StreamResult(outputFileStream));
138 
139  }
140 
141  File validate() throws IOException, TransformerException {
142  return validate(reducedNeXusFile, schematronFile);
143  }
144 
145  File validate(File reducedNeXusFile, File schematronFile) throws
146  IOException, TransformerException {
147 
148  // Step 0
149  File schematron0 = File.createTempFile("nxdlFile", ".step0");
150  if (!this.keepTemp) {
151  schematron0.deleteOnExit();
152  }
153  TransformoMatic(new FileInputStream(schematronFile), nxdl2schXSLTStream,
154  new FileOutputStream(schematron0));
155 
156  // Step 1
157  File schematron1 = File.createTempFile("schematron", ".step1");
158  if (!this.keepTemp) {
159  schematron1.deleteOnExit();
160  }
161  TransformoMatic(new FileInputStream(schematron0), dsdlIncludeXSLTStream,
162  new FileOutputStream(schematron1));
163 
164  // Step 2
165  File schematron2 = File.createTempFile("schematron", ".step2");
166  if (!this.keepTemp) {
167  schematron2.deleteOnExit();
168  }
169  TransformoMatic(new FileInputStream(schematron1),
170  abstractExpandXSLTStream,
171  new FileOutputStream(schematron2));
172 
173  // Step 3
174  File schemaFile = File.createTempFile("schema", ".xslt");
175  if (!this.keepTemp) {
176  schemaFile.deleteOnExit();
177  }
178  TransformoMatic(new FileInputStream(schematron2),svrlForXslt2XSLTStream,
179  new FileOutputStream(schemaFile));
180 
181  // Now lets validate the actual reduced file.
182  //File resultsFile = File.createTempFile(inputNexusFile.getName() + ".result", ".xml");
183 
184  //File resultsFile = File.createTempFile(inputNexusFile.getName(). + ".result", ".xml");
185 
186  File resultsFile = new File(reducedNeXusFile.getName().replaceAll(".reduced", "") + ".result");
187 
188  if (!this.keepTemp) {
189  resultsFile.deleteOnExit();
190  }
191  TransformoMatic(new FileInputStream(reducedNeXusFile),
192  new FileInputStream(schemaFile),
193  new FileOutputStream(resultsFile));
194 
195  // Return the filename
196  return resultsFile;
197  }
198 
199  public static void main(String[] args) {
200  if (args.length != 3) {
201  System.out.println("Must specify three input files");
202  return;
203  }
204  try {
205  NXschematron sch = new NXschematron(new File(args[0]),
206  new File(args[1]), new File(args[2]), false);
207  File results = sch.validate();
208  System.out.println(results.getAbsolutePath());
209  } catch (Exception e) {
210  e.printStackTrace();
211  }
212  }
213 }