NXvalidate  1
 All Classes Namespaces Files Functions Variables
CheckNexusFileType.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  * CheckNexusFileType.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.net.ConnectException;
34 import java.net.MalformedURLException;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37 import javax.xml.parsers.DocumentBuilder;
38 import javax.xml.parsers.DocumentBuilderFactory;
39 import javax.xml.parsers.ParserConfigurationException;
40 import org.w3c.dom.Document;
41 import org.xml.sax.SAXException;
42 
47 public class CheckNexusFileType {
48 
49  private DocumentBuilderFactory factory = null;
50  private DocumentBuilder builder = null;
51 
52  public CheckNexusFileType() {
53  factory = DocumentBuilderFactory.newInstance();
54  factory.setNamespaceAware(true);
55  try {
56  builder = factory.newDocumentBuilder();
57  } catch (ParserConfigurationException ex) {
58  Logger.getLogger(CheckNexusFileType.class.getName()).log(
59  Level.SEVERE, null, ex);
60  }
61  }
62 
71  public boolean checkNexusFile(File file) {
72  try {
73  if (checkHDF5(file)) {
74  return true;
75  } else if (checkHDF4(file)) {
76  return true;
77  } else if (checkNexusXML(file)) {
78  return true;
79  }
80  } catch (FileNotFoundException ex) {
81  Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.SEVERE, null, ex);
82  } catch (IOException ex) {
83  Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.SEVERE, null, ex);
84  }
85  return false;
86  }
87 
95  public boolean checkHDF5(File file) throws FileNotFoundException,
96  IOException {
97 
98  byte[] b = new byte[7];
99  FileInputStream stream = new FileInputStream(file);
100  stream.read(b);
101  stream.close();
102  if (b[0] != -119) {
103  return false;
104  } else if (b[1] != 72) {
105  return false;
106  } else if (b[2] != 68) {
107  return false;
108  } else if (b[3] != 70) {
109  return false;
110  } else if (b[4] != 13) {
111  return false;
112  } else if (b[5] != 10) {
113  return false;
114  } else if (b[6] != 26) {
115  return false;
116  }
117 
118  return true;
119  }
120 
128  public boolean checkHDF4(File file) throws FileNotFoundException,
129  IOException {
130 
131  byte[] b = new byte[4];
132  FileInputStream stream = new FileInputStream(file);
133  stream.read(b);
134  stream.close();
135  if (b[0] != 14) {
136  return false;
137  } else if (b[1] != 3) {
138  return false;
139  } else if (b[2] != 19) {
140  return false;
141  } else if (b[3] != 1) {
142  return false;
143  }
144 
145  return true;
146 
147  }
148 
156  private boolean checkNexusXML(File file) {
157 
158  boolean result = false;
159 
160  try {
161  Document resultsDoc = builder.parse(file);
162  if (resultsDoc.getDocumentElement().getNodeName().equals("NXroot")) {
163  result = true;
164  }
165  } catch (SAXException ex) {
166  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
167  // "SAXException: " + file.getAbsolutePath(), ex);
168  return result;
169  } catch (MalformedByteSequenceException ex) {
170  // Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
171  // "MalformedByteSequenceException: " + file.getAbsolutePath(), ex);
172  return result;
173  } catch (ConnectException ex) {
174  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
175  // "ConnectException: " + file.getAbsolutePath(), ex);
176  return result;
177  } catch (MalformedURLException ex) {
178  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
179  // "MalformedURLException: " + file.getAbsolutePath(), ex);
180  return result;
181  } catch (FileNotFoundException ex) {
182  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
183  // "FileNotFoundException: " + file.getAbsolutePath(), ex);
184  return result;
185  } catch (IOException ex) {
186  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
187  // "IOException: " + file.getAbsolutePath(), ex);
188  return result;
189  }
190 
191  return result;
192 
193  }
194 
203  public boolean checkNXDLFile(File file) {
204 
205  boolean result = false;
206 
207  try {
208  Document resultsDoc = builder.parse(file);
209  if (resultsDoc.getDocumentElement().getNodeName().equals("definition")) {
210  result = true;
211  }
212  if (resultsDoc.getDocumentElement().getNodeName().equals("definitions")) {
213  result = true;
214  }
215  } catch (SAXException ex) {
216  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
217  // "SAXException: " + file.getAbsolutePath(), ex);
218  return result;
219  } catch (MalformedByteSequenceException ex) {
220  // Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
221  // "MalformedByteSequenceException: " + file.getAbsolutePath(), ex);
222  return result;
223  } catch (ConnectException ex) {
224  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
225  // "ConnectException: " + file.getAbsolutePath(), ex);
226  return result;
227  } catch (MalformedURLException ex) {
228  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
229  // "MalformedURLException: " + file.getAbsolutePath(), ex);
230  return result;
231  } catch (FileNotFoundException ex) {
232  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
233  // "FileNotFoundException: " + file.getAbsolutePath(), ex);
234  return result;
235  } catch (IOException ex) {
236  //Logger.getLogger(CheckNexusFileType.class.getName()).log(Level.INFO,
237  // "IOException: " + file.getAbsolutePath(), ex);
238  return result;
239  }
240 
241  return result;
242 
243  }
244 
245 }