26 package org.nexusformat.nxvalidate;
28 import org.nexusformat.nxvalidate.filter.SchematronFilter;
29 import org.nexusformat.nxvalidate.filter.XmlFilter;
30 import org.nexusformat.nxvalidate.filter.NeXusFilter;
31 import org.nexusformat.nxvalidate.filter.HdfFilter;
32 import org.nexusformat.nxvalidate.filter.AllNeXusFilter;
33 import java.awt.GridBagConstraints;
34 import java.awt.GridBagLayout;
35 import java.awt.Insets;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
39 import java.util.ArrayList;
40 import java.util.Iterator;
41 import java.util.Vector;
42 import javax.swing.JButton;
43 import javax.swing.JFileChooser;
44 import javax.swing.JFrame;
45 import javax.swing.JLabel;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.JTextArea;
49 import javax.swing.JTextField;
50 import javax.swing.JTree;
51 import javax.swing.SwingUtilities;
52 import javax.swing.ToolTipManager;
53 import javax.swing.UIManager;
54 import javax.swing.tree.DefaultTreeCellRenderer;
55 import javax.swing.tree.DefaultTreeModel;
56 import javax.swing.tree.TreeModel;
57 import javax.xml.parsers.SAXParser;
58 import javax.xml.parsers.SAXParserFactory;
65 private static final long serialVersionUID = -2935498610579762249L;
66 static private final String newline =
"\n";
67 JButton browseFileButton;
68 JButton validateButton;
70 JTextField filenameText;
72 JButton browseNxdlButton;
77 JFileChooser nxdlChooser;
78 String reducedNeXusFilename;
79 String schematronFilename =
"schematron.sch";
81 File schematronFile = null;
84 ArrayList<Report> reports;
88 super(
new GridBagLayout());
90 GridBagConstraints c =
new GridBagConstraints();
94 log =
new JTextArea(5, 20);
95 log.setMargin(
new Insets(5, 5, 5, 5));
96 log.setEditable(
false);
97 JScrollPane logScrollPane =
new JScrollPane(log);
100 fc =
new JFileChooser();
102 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
104 this.fc.addChoosableFileFilter(nxsFilter);
106 this.fc.addChoosableFileFilter(
new HdfFilter());
107 this.fc.addChoosableFileFilter(
new XmlFilter());
108 this.fc.setFileFilter(nxsFilter);
111 this.setDefaultDefinition();
114 this.nxdlChooser =
new JFileChooser();
115 nxdlChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
116 nxdlChooser.setSelectedFile(
new File(this.schematronFilename));
121 filenameLabel =
new JLabel(
"NXS File:");
124 filenameText =
new JTextField(30);
125 filenameText.setToolTipText(
"The NeXus file to validate");
128 browseFileButton =
new JButton(
"Browse...");
129 browseFileButton.addActionListener(
this);
133 nxdlLabel =
new JLabel(
"NXDL File:");
136 nxdlText =
new JTextField(30);
137 nxdlText.setToolTipText(
"The definition to validate against");
138 nxdlText.setText(this.schematronFilename);
141 browseNxdlButton =
new JButton(
"Browse...");
142 browseNxdlButton.addActionListener(
this);
146 validateButton =
new JButton(
"Perform Validation");
147 validateButton.setToolTipText(
"Perform Validation");
148 validateButton.addActionListener(
this);
151 tree =
new JTree(
new NXSnode());
153 DefaultTreeCellRenderer rend1 =
new DefaultTreeCellRenderer();
158 ToolTipManager.sharedInstance().registerComponent(tree);
163 c.fill = GridBagConstraints.HORIZONTAL;
167 c.insets =
new Insets(0, 10, 0, 0);
168 add(filenameLabel, c);
170 c.fill = GridBagConstraints.HORIZONTAL;
171 c.insets =
new Insets(0, 0, 0, 0);
175 add(filenameText, c);
177 c.fill = GridBagConstraints.HORIZONTAL;
181 add(browseFileButton, c);
184 c.fill = GridBagConstraints.HORIZONTAL;
188 c.insets =
new Insets(0, 10, 0, 0);
191 c.fill = GridBagConstraints.HORIZONTAL;
192 c.insets =
new Insets(0, 0, 0, 0);
198 c.fill = GridBagConstraints.HORIZONTAL;
202 add(browseNxdlButton, c);
205 c.fill = GridBagConstraints.HORIZONTAL;
210 add(validateButton, c);
212 c.fill = GridBagConstraints.BOTH;
216 add(
new JScrollPane(tree), c);
218 c.fill = GridBagConstraints.HORIZONTAL;
222 add(logScrollPane, c);
225 private void setDefaultDefinition() {
226 schematronFile =
new File(this.schematronFilename);
229 public TreeModel
parseXML(String filename)
throws Exception {
230 SAXParserFactory factory = SAXParserFactory.newInstance();
232 SAXParser saxParser = factory.newSAXParser();
233 saxParser.parse(
new File(filename), handler);
234 return new DefaultTreeModel(handler.
getRoot());
239 if (e.getSource() == browseFileButton) {
242 if (returnVal == JFileChooser.APPROVE_OPTION) {
243 File file = fc.getSelectedFile();
246 log.append(
"Selected NeXus File: " + file.getName() +
"." + newline);
250 log.append(
"Browse nexus cancelled by user." + newline);
252 log.setCaretPosition(log.getDocument().getLength());
254 }
else if (e.getSource() == browseNxdlButton) {
256 if (returnVal == JFileChooser.APPROVE_OPTION) {
257 File file = nxdlChooser.getSelectedFile();
258 nxdlText.setText(file.getAbsolutePath());
259 schematronFile = file;
260 log.append(
"Selected Definition File: " + file.getName() +
"." + newline);
262 log.append(
"Browse definition cancelled by user." + newline);
264 }
else if (e.getSource() == validateButton) {
282 if (this.reports.size() > 1) {
284 "We only sent a single file to be processed, but have received multiple reports back.");
286 if (this.reports.size() < 1) {
288 "Failed to generate a report for " + filenameText.getText());
318 log.append(
"Finished Validating." + newline);
319 Report report = reports.get(0);
320 tree.setModel(report.getTree());
322 log.append(
"There were " + report.numErrors() +
" errors found."
325 ArrayList<SVRLitem> messages = report.getReport();
327 Iterator i = messages.iterator();
328 while (i.hasNext()) {
329 log.append(
"->" + i.next() + newline);
332 }
catch (Throwable e1) {
333 log.append(e1.getMessage() + newline);
334 e1.printStackTrace();
337 log.setCaretPosition(log.getDocument().getLength());
355 private static void createAndShowGUI() {
357 JFrame frame =
new JFrame(
"NeXus Validation Tool");
358 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
365 frame.setVisible(
true);
368 public static void main(String[] args) {
371 SwingUtilities.invokeLater(
new Runnable() {
375 UIManager.put(
"swing.boldMetal", Boolean.FALSE);