NXvalidate  1
 All Classes Namespaces Files Functions Variables
NXvalidate.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  * NXvalidate.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.io.File;
29 import java.util.ArrayList;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 
33 public class NXvalidate {
34 
35  static final String VERSION = "0.1 alpha";
36  private ArrayList<File> files;
37  private File schematronFile;
38  private boolean keepTemp;
39  private boolean convertNxs;
40  private int verbose;
41  private ArrayList<Report> reports;
42  private File reduced = null;
43 
44  NXvalidate() {
45  files = new ArrayList<File>();
46  this.schematronFile = null;
47  this.keepTemp = false;
48  this.convertNxs = true;
49  this.verbose = 0;
50  this.reports = new ArrayList<Report>();
51  }
52 
53  public ArrayList<Report> getReports() {
54  return reports;
55  }
56 
57  public ArrayList<File> getFilenames() {
58  return files;
59  }
60 
61  public void setFilenames(ArrayList<String> filenames) {
62  this.files = files;
63  }
64 
65  public void setNXSFile(File file) {
66  files.add(file);
67  }
68 
69  public File getSchematron() {
70  return schematronFile;
71  }
72 
73  public void setSchematron(File schematronFile) {
74  this.schematronFile = schematronFile;
75  }
76 
77  public boolean isKeepTemp() {
78  return keepTemp;
79  }
80 
81  public void setKeepTemp(boolean keepTemp) {
82  this.keepTemp = keepTemp;
83  }
84 
85  public boolean isConvertNxs() {
86  return convertNxs;
87  }
88 
89  public void setConvertNxs(boolean convertNxs) {
90  this.convertNxs = convertNxs;
91  }
92 
93  public int getVerbose() {
94  return verbose;
95  }
96 
97  public void setVerbose(int verbose) {
98  this.verbose = verbose;
99  }
100 
101  public static String getVersion() {
102  return VERSION;
103  }
104 
105  public File getReduced() {
106  return reduced;
107  }
108 
109  public File setReduced(File reduced) {
110  return this.reduced = reduced;
111  }
112 
113  void parseArgs(final String[] args) {
114  // check that the help and version arguments aren't specified
115  for (int i = 0; i < args.length; i++) {
116  if (args[i].equals("-h") || args[i].equals("--help")) {
117  this.printHelp(2);
118  System.exit(0);
119  }
120  if (args[i].equals("--version")) {
121  this.printVersion();
122  System.exit(0);
123  }
124  }
125 
126  // go through the arguments for real
127  for (int i = 0; i < args.length; i++) {
128  if (args[i].equals("-v") || args[i].equals("--verbose")) {
129  this.verbose += 1;
130  } else if (args[i].equals("-k") || args[i].equals("--keep")) {
131  this.keepTemp = true;
132  } else if (args[i].equals("-d") || args[i].equals("--dfn")) {
133  schematronFile = new File(args[i + 1]);
134  i++;
135  } else if (args[i].equals("--noconvert")) {
136  this.convertNxs = false;
137  } else {
138  files.add(new File(args[i]));
139  }
140  }
141 
142 
143  // confirm that the manditory arguments are there
144  if (this.files.size() <= 0) {
145  System.out.println("Must specify at least one nexus file");
146  this.printHelp(0);
147  System.exit(-1);
148  }
149  if (!schematronFile.exists()) {
150  System.out.println("Must specify a schematron file");
151  this.printHelp(0);
152  System.exit(-1);
153  }
154  }
155 
156 
157 
158  void process() {
159  if (this.verbose > 0) {
160  System.out.println("Running NXvalidate (version:" + VERSION + ")");
161  }
162  int size = this.files.size();
163  for (int i = 0; i < size; i++) {
164  this.process(this.files.get(i));
165  }
166  }
167 
168  private static File toAbsFile(final String filename) {
169  File file = new File(filename);
170  return file;
171  }
172 
173  private File process(final File file) throws Error {
174 
175  File result = null;
176 
177  if (convertNxs) {
178  try {
179  NXconvert converter = new NXconvert(file, keepTemp,null);
180  reduced = converter.convert();
181  } catch (Exception e) {
182  Logger.getLogger(NXvalidate.class.getName()).log(Level.SEVERE,
183  "While converting \"" + file +
184  "\" to reduced xml format",e);
185  throw new Error("While converting \"" + file +
186  "\" to reduced xml format", e);
187  }
188  }
189 
190  if (reduced != null && schematronFile !=null) {
191 
192  // create the validation setup
193  NXschematron schematron = new NXschematron(file,reduced,
194  schematronFile, keepTemp);
195 
196  try {
197  result = schematron.validate();
198  } catch (Exception e) {
199  Logger.getLogger(NXvalidate.class.getName()).log(Level.SEVERE,
200  "While creating validation report",e);
201  throw new Error("While creating validation report", e);
202  }
203 
204  // create the report
205  Report report = null;
206  try {
207  report = new Report(reduced, result);
208  } catch (Exception e) {
209  Logger.getLogger(NXvalidate.class.getName()).log(Level.SEVERE,
210  "While generating the report object",e);
211  throw new Error("While generating the report object", e);
212  }
213 
214  // Add to vector of reports (one for each input file)
215  reports.add(report);
216 
217  report.printTree();
218  int numErrors = report.numErrors();
219  if (numErrors > 0) {
220  report.printReport();
221  }
222 
223  }
224  return result;
225  }
226 
227  private void printVersion() {
228  System.out.println("NXvalidate version " + VERSION);
229  }
230 
231  private void printHelp(final int level) {
232  System.out.println("usage: nxvalidate [options] <nxsfile>");
233  if (level <= 0) {
234  return;
235  }
236 
237  System.out.println();
238  System.out.println("Validate nexus files against the nexus definitions");
239  this.printVersion();
240  if (level <= 1) {
241  return;
242  }
243 
244  System.out.println();
245  System.out.println("-h, --help print this help information");
246  System.out.println("-v, --verbose increase verbose printing");
247  System.out.println("-d, --dfn specify the definition file");
248  System.out.println("-k, --keep keep temporary files");
249  System.out.println("--noconvert do not reduce the nexus file");
250  }
251 
252  public static void main(String[] args) {
253  NXvalidate validate = new NXvalidate();
254  validate.parseArgs(args);
255  validate.process();
256  }
257 }