NXvalidate  1
 All Classes Namespaces Files Functions Variables
FileActions.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 Stephen Rankin
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  * FileActions.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.util.ArrayList;
35 import java.util.Enumeration;
36 import java.util.ResourceBundle;
37 import org.w3c.dom.Document;
38 import org.xml.sax.SAXException;
39 import java.util.logging.Level;
40 import java.util.logging.Logger;
41 import javax.swing.JOptionPane;
42 import javax.swing.JTree;
43 import javax.xml.parsers.DocumentBuilder;
44 import javax.xml.parsers.DocumentBuilderFactory;
45 import org.nexusformat.nxvalidate.exceptions.NXvalidateException;
46 
51 public class FileActions implements Runnable {
52 
53  private File nxsFile = null;
54  private File nxdlFile = null;
55  private File reducedFile = null;
56  private File resultsFile = null;
57  private String nxconvertCommand = null;
58  private File saveDirectory = null;
59  private DocumentBuilderFactory factory = null;
60  private DocumentBuilder builder = null;
61  private NXReducedToTree domTree = null;
62  private NXNodeMapper root = null;
63  private int which = 1;
64  private TreeUtils treeUtils = null;
65  private ValidatorUtils validator = null;
66  private Document reducedDoc = null;
67  private Document resultsDoc = null;
68  private JTree jTree = null;
69  private boolean validateResult = false;
70  private ResourceBundle bundle = null;
71  private JOptionPane dialogReportProblem;
72  private NXvalidateFrame frame = null;
73  private File[] dataFiles = null;
74  private ArrayList<String> dataFileList = null;
75  private ArrayList<String> badDataFileList = null;
76  private boolean conversionFail = false;
77  private boolean isNotBulk = false;
78 
79  public FileActions(NXvalidateFrame frame, JTree jTree,
80  DocumentBuilder builder, NXReducedToTree domTree,
81  NXNodeMapper root) {
82 
83  this.domTree = domTree;
84  this.root = root;
85  this.builder = builder;
86  this.frame = frame;
87  this.jTree = jTree;
88 
89  bundle = ResourceBundle.getBundle(
90  "org/nexusformat/nxvalidate/resources/nxvalidate");
91  dialogReportProblem = new JOptionPane();
92 
93  treeUtils = new TreeUtils();
94  }
95 
96  public void setWhich(int which) {
97  this.which = which;
98  }
99 
100  public void setNXSFile(File nxsFile) {
101  this.nxsFile = nxsFile;
102  }
103 
104  public void setNXDLFile(File nxdlFile) {
105  this.nxdlFile = nxdlFile;
106  }
107 
108  public void setNXConvertFile(String nxconvertCommand) {
109  this.nxconvertCommand = nxconvertCommand;
110  }
111 
112  public String getNXConvertFile() {
113  return nxconvertCommand;
114  }
115 
116  public void setReducedFile(File reducedFile) {
117  this.reducedFile = reducedFile;
118  }
119 
120  public void setResultsDoc(Document resultsDoc) {
121  this.resultsDoc = resultsDoc;
122  }
123 
124  public void setReducedDoc(Document reducedDoc) {
125  this.reducedDoc = reducedDoc;
126  }
127 
128  public void setDataFiles(File[] dataFiles) {
129  this.dataFiles = dataFiles;
130  }
131 
132  public boolean getValidateResult() {
133  return validateResult;
134  }
135 
136  public ArrayList<String> getDataFileList() {
137  return dataFileList;
138  }
139 
140  public void setDataFileList(ArrayList<String> dataFileList) {
141  this.dataFileList = dataFileList;
142  }
143 
144  public File getSaveDirectory() {
145  return saveDirectory;
146  }
147 
148  public void setSaveDirectory(File saveDirectory) {
149  this.saveDirectory = saveDirectory;
150  }
151 
152  public boolean getConversionResult() {
153  return conversionFail;
154  }
155 
156  public ArrayList<String> getBadDataFileList() {
157  return badDataFileList;
158  }
159 
160  public void validateFile() {
161 
162  try {
163 
164  //Validation has already been done so we reset the tree
165  //before doing the validation again.
166  if (resultsDoc != null) {
167  SVRLNodeFilter filter = new SVRLNodeFilter();
168  filter.setFilterDocument(resultsDoc);
169  filter.setDocument(reducedDoc);
170  filter.resetNodes();
171  }
172 
173  //Do the validation.
174  if (nxconvertCommand != null) {
175  validator = new ValidatorUtils(nxsFile,nxconvertCommand);
176  } else {
177  dialogReportProblem.showMessageDialog(
178  frame,
179  "Problem Validating file, nxconvert command is not set.");
180  }
181  validator.setSchematron(nxdlFile);
182  validator.setReduced(reducedFile);
183  resultsFile = validator.validate();
184 
185  SVRLNodeFilter filter = new SVRLNodeFilter();
186 
187  resultsDoc = builder.parse(resultsFile);
188  filter.setFilterDocument(resultsDoc);
189  filter.setDocument(reducedDoc);
190  filter.getBadNodeList();
191 
192  treeUtils.setResultsDoc(jTree, resultsDoc);
193  treeUtils.setResultsFile(jTree, resultsFile);
194 
195  treeUtils.setValidated(jTree, resultsFile);
196 
197  //domTree.updateTree();
198  Logger.getLogger(NXvalidateFrame.class.getName()).log(
199  Level.INFO, "Finished Validating.");
200 
201  if(isNotBulk){
202  dialogReportProblem.showMessageDialog(
203  frame,
204  bundle.getString("validationCompleteMessage"));
205  }
206  } catch (NXvalidateException ex) {
207  dialogReportProblem.showMessageDialog(
208  frame,
209  "Problem Validating file: " + nxsFile);
210  Logger.getLogger(FileActions.class.getName()).log(
211  Level.WARNING, null, ex);
212  } catch (SAXException ex) {
213  dialogReportProblem.showMessageDialog(
214  frame,
215  "Problem Validating file: " + nxsFile);
216  Logger.getLogger(FileActions.class.getName()).log(
217  Level.WARNING, null, ex);
218  } catch (IOException ex) {
219  dialogReportProblem.showMessageDialog(
220  frame,
221  "Problem Validating file: " + nxsFile);
222  Logger.getLogger(FileActions.class.getName()).log(
223  Level.WARNING, null, ex);
224  } catch (Exception ex) {
225  dialogReportProblem.showMessageDialog(
226  frame,
227  "Problem Validating file: " + nxsFile);
228  Logger.getLogger(FileActions.class.getName()).log(
229  Level.WARNING, null, ex);
230  }
231 
232  }
233 
234  public void loadFile() {
235 
236  try {
237 
238  //Reduce the file with NXConvert.
239  NXconvert convert = new NXconvert(nxsFile, true, nxconvertCommand);
240  File reducedFile = convert.convert();
241 
242  if(reducedFile==null){
243  return;
244  }
245 
246  //Display reduced file
247  Document document = builder.parse(reducedFile);
248  document.setUserData("file", nxsFile, null);
249  NXNodeMapper node = new NXNodeMapper(
250  document, true, nxsFile);
251  node.setReducedDoc(document);
252  this.reducedDoc = document;
253 
254  if (nxdlFile != null) {
255  node.setNXDLFile(nxdlFile);
256  }
257  this.reducedFile = reducedFile;
258  node.setReducedFile(reducedFile);
259  node.setRoot(root);
260  root.insert(node,0);
261  //domTree.updateTree();
262 
263 
264  } catch (NXvalidateException ex) {
265  conversionFail = true;
266  badDataFileList.add(nxsFile.getAbsolutePath());
267  } catch (InterruptedException ex) {
268  Logger.getLogger(
269  FileActions.class.getName()).log(Level.SEVERE,
270  null, ex);
271  } catch (SAXException ex) {
272  Logger.getLogger(
273  FileActions.class.getName()).log(Level.SEVERE,
274  null, ex);
275  } catch (IOException ex) {
276  Logger.getLogger(
277  FileActions.class.getName()).log(Level.SEVERE,
278  null, ex);
279  }
280  }
281 
282  private ArrayList<String> getSubFiles(String file) {
283 
284  ArrayList<String> fileList = new ArrayList<String>();
285 
286  File tmpFile = new File(file);
287 
288  String[] files = tmpFile.list();
289 
290  if (files == null) {
291  return fileList;
292  }
293 
294  for (int i = 0; i < files.length; ++i) {
295  if (new File(tmpFile.getAbsolutePath() + tmpFile.separator + files[i]).isDirectory()) {
296  fileList.addAll(getSubFiles(tmpFile.getAbsolutePath() + tmpFile.separator + files[i]));
297  } else {
298  fileList.add(tmpFile.getAbsolutePath() + tmpFile.separator + files[i]);
299  }
300  }
301 
302  return fileList;
303  }
304 
305  public void bulkCheck() {
306 
307  if (dataFiles != null) {
308 
309  dataFileList = new ArrayList<String>();
310 
311  for (int i = 0; i < dataFiles.length; ++i) {
312 
313  if (dataFiles[i].isDirectory()) {
314  dataFileList.addAll(getSubFiles(dataFiles[i].getAbsolutePath()));
315  } else {
316  dataFileList.add(dataFiles[i].getAbsolutePath());
317  }
318 
319  }
320  this.dataFileList = dataFileList;
321  }
322 
323  }
324 
325  public void bulkLoad() {
326  //Do conversion for each selected file.
327  badDataFileList = new ArrayList<String>();
328  if (dataFileList != null) {
329  for (int i = 0; i < dataFileList.size(); ++i) {
330  nxsFile = new File(dataFileList.get(i));
331  loadFile();
332  }
333  domTree.updateTree();
334  }
335  }
336 
337  public void bulkValidate() {
338 
339  //Do Conversion and validation.
340  badDataFileList = new ArrayList<String>();
341  if (dataFileList != null) {
342  for (int i = 0; i < dataFileList.size(); ++i) {
343  nxsFile = new File(dataFileList.get(i));
344  loadFile();
345  validateFile();
346  }
347  domTree.updateTree();
348  dialogReportProblem.showMessageDialog(
349  frame,
350  bundle.getString("validationCompleteMessage"));
351  }
352 
353  }
354 
355  private void copy(File src, File dst) throws IOException {
356  InputStream in = new FileInputStream(src);
357  OutputStream out = new FileOutputStream(dst);
358 
359  byte[] buf = new byte[1024];
360  int len;
361  while ((len = in.read(buf)) > 0) {
362  out.write(buf, 0, len);
363  }
364  in.close();
365  out.close();
366  }
367 
368  public void saveResults(File directory){
369 
370  NXNodeMapper tmpNode = null;
371  File tmpReduced = null;
372  File tmpResults = null;
373  Enumeration children = root.children();
374 
375  while(children.hasMoreElements()){
376 
377  tmpNode = (NXNodeMapper)children.nextElement();
378 
379  if(tmpNode.getReducedFile()!=null){
380 
381  tmpReduced = new File(directory.getAbsolutePath() +
382  directory.separator + tmpNode.getReducedFile().getName());
383  tmpResults = new File(directory.getAbsolutePath() +
384  directory.separator + tmpNode.getResultsFile().getName());
385  try{
386  copy(tmpNode.getReducedFile(),tmpReduced);
387  copy(tmpNode.getResultsFile(),tmpResults);
388  } catch (IOException ex) {
389  Logger.getLogger(
390  FileActions.class.getName()).log(Level.SEVERE, null, ex);
391  }
392  }
393  }
394  }
395 
396  public void run() {
397 
398  if (which == 1) {
399  isNotBulk = true;
400  loadFile();
401  domTree.updateTree();
402  isNotBulk = false;
403  } else if (which == 2) {
404  isNotBulk = true;
405  validateFile();
406  domTree.updateTree();
407  isNotBulk = false;
408  } else if (which == 3) {
409  bulkCheck();
410  } else if (which == 4) {
411  bulkLoad();
412  }else if (which == 5) {
413  bulkValidate();
414  } else if (which == 6) {
415  saveResults(saveDirectory);
416  }
417  }
418 }