NXvalidate  1
SVRLNodeFilter.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  * SVRLNodeFilter.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.util.ArrayList;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import javax.xml.xpath.XPath;
32 import javax.xml.xpath.XPathConstants;
33 import javax.xml.xpath.XPathExpression;
34 import javax.xml.xpath.XPathExpressionException;
35 import javax.xml.xpath.XPathFactory;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Node;
40 import org.w3c.dom.NodeList;
41 
48 public class SVRLNodeFilter implements NodeFilterInterface {
49 
50  private Document filterDoc = null;
51  private Document doc = null;
52  private ArrayList<Node> nodes = null;
53  private ArrayList<Node> badNodes = null;
54  private ArrayList<Node> warnNodes = null;
55 
56  public SVRLNodeFilter() {
57  nodes = new ArrayList<Node>();
58  badNodes = new ArrayList<Node>();
59  warnNodes = new ArrayList<Node>();
60  }
61 
67  public void setFilterDocument(Document filterDoc) {
68 
69  this.filterDoc = filterDoc;
70 
71  }
72 
78  public void setDocument(Document doc) {
79 
80  this.doc = doc;
81 
82  }
83 
88  public ArrayList<Node> getBadNodeList() {
89  try {
90 
91  getXPathList();
92 
93  } catch (NXvalidateException ex) {
94  Logger.getLogger(SVRLNodeFilter.class.getName()).log(
95  Level.SEVERE, null, ex);
96  } catch (XPathExpressionException ex) {
97  Logger.getLogger(SVRLNodeFilter.class.getName()).log(
98  Level.SEVERE, null, ex);
99  }
100  return badNodes;
101  }
102 
107  public ArrayList<Node> getWarnNodeList(){
108  try {
109 
110  getXPathList();
111 
112  } catch (NXvalidateException ex) {
113  Logger.getLogger(SVRLNodeFilter.class.getName()).log(
114  Level.SEVERE, null, ex);
115  } catch (XPathExpressionException ex) {
116  Logger.getLogger(SVRLNodeFilter.class.getName()).log(
117  Level.SEVERE, null, ex);
118  }
119  return warnNodes;
120  }
121 
126  public void resetNodes() {
127  try {
128 
129  resetNodes(0);
130  resetNodes(1);
131 
132  } catch (NXvalidateException ex) {
133  Logger.getLogger(SVRLNodeFilter.class.getName()).log(
134  Level.SEVERE, null, ex);
135  } catch (XPathExpressionException ex) {
136  Logger.getLogger(SVRLNodeFilter.class.getName()).log(
137  Level.SEVERE, null, ex);
138  }
139 
140  }
141 
142 
143  private void resetNodes(int type) throws NXvalidateException,
144  XPathExpressionException {
145 
146  NodeList failed = null;
147  String location = null;
148  Element element = null;
149  XPathFactory factory = XPathFactory.newInstance();
150  NodeList nodeList = null;
151 
152  if (filterDoc != null) {
153  if (filterDoc.hasChildNodes()) {
154 
155  //Get the xml elements svrl:failed-assert and
156  //svrl:successful-report from the XML
157  //validation results file.
158  if(type == 0){
159  failed = filterDoc.getElementsByTagName("svrl:failed-assert");
160  }
161  else if(type == 1){
162  failed = filterDoc.getElementsByTagName("svrl:successful-report");
163  }
164  if (failed.getLength() > 0) {
165 
166  for (int i = 0; i < failed.getLength(); ++i) {
167 
168  element = (Element) failed.item(i);
169 
170  if (element.hasChildNodes()) {
171 
172  //Each svrl:failed-assert element has child element
173  //location which specifies the XPATH location of the
174  //bad nodes.
175  location = element.getAttribute("location");
176  XPath xpath = factory.newXPath();
177  location = location.replaceAll(
178  "\\[namespace-uri\\(\\)="
179  + "'http://definition.nexusformat.org/"
180  + "schema/3.1'\\]", "");
181  XPathExpression expr = xpath.compile(location);
182 
183  Object result = expr.evaluate(doc,
184  XPathConstants.NODESET);
185  nodeList = (NodeList) result;
186 
187  //Set the node back to its original state.
188  for (int j = 0; j < nodeList.getLength(); j++) {
189 
190  nodeList.item(j).setUserData("validated", null, null);
191 
192  if(type == 0){
193  nodeList.item(j).setUserData("bad", null, null);
194  }
195  else if(type == 1){
196  nodeList.item(j).setUserData("warn", null, null);
197  }
198 
199  nodeList.item(j).setUserData(
200  "tests", null, null);
201  nodeList.item(j).setUserData(
202  "texts", null, null);
203  nodeList.item(j).setUserData(
204  "diags", null, null);
205  nodeList.item(j).setUserData(
206  "diagatts", null, null);
207  }
208 
209  }
210 
211  }
212 
213  }
214 
215  }
216 
217  }
218 
219  }
220 
221  private void getReportingNode(int type, NodeList nodeListInput) throws NXvalidateException,
222  XPathExpressionException {
223 
224 
225  String location = null;
226  String test = null;
227  String text = null;
228  String diag = null;
229  String diagAtt = null;
230  ArrayList<String> tests = null;
231  ArrayList<String> texts = null;
232  ArrayList<String> diags = null;
233  ArrayList<String> diagAtts = null;
234  Element element = null;
235  XPathFactory factory = XPathFactory.newInstance();
236  NodeList nodeList = null;
237 
238  //Loop through the nodes and get
239  //the test, text diagnostic and diagnostic attribute.
240  for (int i = 0; i < nodeListInput.getLength(); ++i) {
241 
242  test = null;
243  text = null;
244  diag = null;
245  diagAtt = null;
246 
247  element = (Element) nodeListInput.item(i);
248 
249  if (element.hasChildNodes()) {
250 
251  //Each element has child element
252  //location which specifies the XPATH location of the
253  //bad nodes.
254  location = element.getAttribute("location");
255  test = element.getAttribute("test");
256  if (element.getElementsByTagName("svrl:text").getLength() > 0) {
257  text = element.getElementsByTagName(
258  "svrl:text").item(0).getTextContent().trim();
259  }
260 
261  if (element.getElementsByTagName(
262  "svrl:diagnostic-reference").getLength() > 0) {
263 
264  diag = element.getElementsByTagName(
265  "svrl:diagnostic-reference").item(0).getTextContent().trim();
266 
267  diagAtt = ((Element) element.getElementsByTagName(
268  "svrl:diagnostic-reference").item(0)).getAttribute("diagnostic");
269  }
270 
271  XPath xpath = factory.newXPath();
272  location = location.replaceAll(
273  "\\[namespace-uri\\(\\)="
274  + "'http://definition.nexusformat.org/"
275  + "schema/3.1'\\]", "");
276  XPathExpression expr = xpath.compile(location);
277 
278  Object result = expr.evaluate(doc,
279  XPathConstants.NODESET);
280  nodeList = (NodeList) result;
281 
282  for (int j = 0; j < nodeList.getLength(); j++) {
283 
284  nodeList.item(j).setUserData("validated", new Boolean(true), null);
285  if(type == 0){
286  nodeList.item(j).setUserData("bad", new Boolean(true), null);
287  badNodes.add(nodeList.item(j));
288  }
289  else if(type == 1){
290  nodeList.item(j).setUserData("warn", new Boolean(true), null);
291  warnNodes.add(nodeList.item(j));
292  }
293 
294  if (nodeList.item(j).getUserData("tests")
295  != null && test != null) {
296  tests = (ArrayList<String>) nodeList.item(j).getUserData(
297  "tests");
298  tests.add(test);
299  } else {
300  tests = new ArrayList<String>();
301  tests.add(test);
302  }
303 
304  if (nodeList.item(j).getUserData("texts")
305  != null && text != null) {
306  texts = (ArrayList<String>) nodeList.item(j).getUserData(
307  "texts");
308  texts.add(text);
309  } else {
310  texts = new ArrayList<String>();
311  texts.add(text);
312  }
313 
314  if (nodeList.item(j).getUserData("diags")
315  != null && diag != null) {
316  diags = (ArrayList<String>) nodeList.item(j).getUserData(
317  "diags");
318  diags.add(diag);
319  } else {
320  diags = new ArrayList<String>();
321  diags.add(diag);
322  }
323 
324  if (nodeList.item(j).getUserData("diagatts")
325  != null && diagAtt != null) {
326  diagAtts = (ArrayList<String>) nodeList.item(j).getUserData(
327  "diagatts");
328  diagAtts.add(diagAtt);
329  } else {
330  diagAtts = new ArrayList<String>();
331  diagAtts.add(diagAtt);
332  }
333 
334  nodeList.item(j).setUserData(
335  "tests", tests, null);
336  nodeList.item(j).setUserData(
337  "texts", texts, null);
338  nodeList.item(j).setUserData(
339  "diags", diags, null);
340  nodeList.item(j).setUserData(
341  "diagatts", diagAtts, null);
342 
343  nodes.add(nodeList.item(j));
344  }
345 
346  }
347 
348  }
349 
350 
351  }
352 
353  private void getXPathList() throws NXvalidateException, XPathExpressionException {
354 
355  NodeList list = null;
356 
357  if (filterDoc != null) {
358  if (filterDoc.hasChildNodes()) {
359 
360  //Get the xml elements svrl:failed-assert from the XML
361  //validation results file.
362  list = filterDoc.getElementsByTagName("svrl:failed-assert");
363 
364  if (list.getLength() > 0) {
365  getReportingNode(0, list);
366  }
367 
368  //Get the xml elements successful-report from the XML
369  //validation results file.
370  list = filterDoc.getElementsByTagName("svrl:successful-report");
371 
372  if (list.getLength() > 0) {
373  getReportingNode(1, list);
374  }
375 
376  }
377 
378  }
379 
380  }
381 }
Implementation of NodeFilterInterface which filters out elements in a NEXUS reduced file if they have...
void setDocument(Document doc)
Sets the document to which to apply the filter.
void resetNodes()
Reset the nodes to indicate that they are now good nodes, i.e.
ArrayList< Node > getWarnNodeList()
A list of nodes which have a warning.
void setFilterDocument(Document filterDoc)
Sets a DOM document (filter document) that contains a list of nodes that have failed any validation t...
ArrayList< Node > getBadNodeList()
A list of nodes which are bad.
An interface which represents the concept that the elements in an XML Document can be good or bad aft...