NXvalidate  1
 All Classes Namespaces Files Functions Variables
Report.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  * Report.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Vector;
32 
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
36 
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Node;
39 import org.w3c.dom.NodeList;
40 import org.xml.sax.SAXException;
41 
42 class Report {
43 
44  NXSnode report;
45  ArrayList<SVRLitem> errors;
46  private static String NXROOT = "NXroot";
47  private static String SVRLROOT = "svrl:schematron-output";
48 
49  Report(final File reduced, final File reportFile)
50  throws ParserConfigurationException, SAXException, IOException {
51 
52  // parse the reduced file and turn it into a node tree
53  Document redDoc = parse(reduced);
54  report = new NXSnode(getNXroot(redDoc));
55 
56  // add the svrl report to the reduced
57  Document svrlDoc = parse(reportFile);
58  this.errors = this.report.addSVRL(getSVRLroot(svrlDoc));
59  }
60 
61  public int numErrors() {
62  return this.errors.size();
63  }
64 
65  public ArrayList<SVRLitem> getReport() {
66  return this.errors;
67  }
68 
69  public void printReport() {
70  int size = this.numErrors();
71  for (int i = 0; i < size; i++) {
72  System.out.println(this.errors.get(i));
73  if (i + 1 < size) {
74  System.out.println("----------");
75  }
76  }
77  }
78 
79  public NXSnode getTree() {
80  return this.report;
81  }
82 
83  public void printTree() {
84  this.report.printTree();
85  }
86 
87  static private Node getNXroot(final Document doc) {
88  NodeList nodes = doc.getElementsByTagName(NXROOT);
89  int size = nodes.getLength();
90  if (size == 1) {
91  return nodes.item(0);
92  }
93  throw new Error(
94  "Failed to find one " + NXROOT + " (found " + size + ")");
95  }
96 
97  static private Node getSVRLroot(final Document doc) {
98  NodeList nodes = doc.getElementsByTagName(SVRLROOT);
99  int size = nodes.getLength();
100  if (size == 1) {
101  return nodes.item(0);
102  }
103  throw new Error(
104  "Failed to find one " + SVRLROOT + " (found " + size + ")");
105  }
106 
107  static private Document parse(final File file)
108  throws ParserConfigurationException, SAXException, IOException {
109 
110  DocumentBuilder builder =
111  DocumentBuilderFactory.newInstance().newDocumentBuilder();
112  return builder.parse(file);
113  }
114 }