NXvalidate  1
 All Classes Namespaces Files Functions Variables
NXconvert.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  * NXconvert.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import org.nexusformat.nxvalidate.exceptions.NXvalidateException;
33 
34 public class NXconvert {
35 
36  private static final String NXCONVERT = "nxconvert";
37  private static final String EXTENSION = ".reduced";
38  private File rawfile;
39  private File redfile;
40  private NXproperties props = null;
41  private String convertCommand = null;
42 
43  public NXconvert(final File rawfile, final boolean keepTemp,
44  final String convertCommand)
45  throws IOException, InterruptedException {
46 
47  this.rawfile = rawfile;
48  redfile = File.createTempFile(rawfile.getName() + ".",
49  EXTENSION);
50  if (!keepTemp) {
51  redfile.deleteOnExit();
52  }
53 
54  this.convertCommand = convertCommand;
55 
56  props = new NXproperties();
57 
58  }
59 
60  private void printStd(final String command, final String out,
61  final String err) {
62 
63  Logger.getLogger(NXconvert.class.getName()).log(Level.INFO, command);
64  Logger.getLogger(NXconvert.class.getName()).log(Level.INFO, out);
65  Logger.getLogger(NXconvert.class.getName()).log(Level.WARNING, err);
66 
67  }
68 
69  public File convert() throws IOException, InterruptedException, NXvalidateException {
70 
71  Logger.getLogger(NXconvert.class.getName()).log(
72  Level.INFO, "Creating " + redfile.getAbsolutePath());
73 
74  String command = "";
75  if(convertCommand!=null){
76  command = convertCommand +
77  " -d " + this.rawfile + " " + redfile;;
78  }
79  else{
80  return null;
81  }
82 
83  // execute the command
84  Runtime rt = Runtime.getRuntime();
85  Process proc = rt.exec(command);
86 
87  // get the output and error
88  StreamGobbler out = new StreamGobbler(proc.getInputStream(), "OUTPUT");
89  StreamGobbler err = new StreamGobbler(proc.getErrorStream(), "ERROR");
90  out.start();
91  err.start();
92  int exitValue = proc.waitFor();
93 
94  // cleanup and return result
95  printStd(command, out.getOutput(), err.getOutput());
96  if (exitValue != 0) {
97  StringBuilder buffer = new StringBuilder("Execute command \"");
98  buffer.append(command);
99  buffer.append("\" failed. Returned ");
100  buffer.append(exitValue);
101  throw new NXvalidateException(buffer.toString());
102  }
103  return redfile;
104  }
105 
106  public static void main(String[] args) {
107 
108  if (args.length != 1) {
109  Logger.getLogger(NXconvert.class.getName()).log(Level.SEVERE,
110  "You must specify one input file");
111  return;
112  }
113  try {
114  NXconvert convert = new NXconvert(new File(args[0]), false, args[1]);
115  convert.convert();
116  } catch (Exception e) {
117  e.printStackTrace();
118  }
119  }
120 }