NXvalidate  1
 All Classes Namespaces Files Functions Variables
SVRLitem.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  * SVRLitem.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.util.ArrayList;
29 import java.util.Vector;
30 
31 import org.w3c.dom.NamedNodeMap;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34 
35 public class SVRLitem {
36  private static String NS_START = "[namespace-uri";
37  private static String NS_STOP = "']";
38 
39  private String[] location;
40  private String xpath_str;
41  private String type;
42  private String test;
43  private String message;
44 
45  public SVRLitem(final Node node) {
46  this.setLocation(node);
47  this.setType(node.getNodeName());
48  this.setAttrs(node.getAttributes());
49  this.setMessage(node);
50  }
51 
52  public String toString() {
53  StringBuilder buffer = new StringBuilder();
54  buffer.append(this.test + " failed at " + this.getLocation());
55  if (this.message.length() > 0) {
56  buffer.append("\n");
57  buffer.append(this.message);
58  }
59  return buffer.toString();
60  }
61 
62  ArrayList<String> getLocationArray() {
63  ArrayList<String> result = new ArrayList<String>();
64  for (int i = 0; i < this.location.length; i++) {
65  result.add(this.location[i]);
66  }
67  return result;
68  }
69 
70  public String getLocation() {
71  StringBuilder buffer = new StringBuilder();
72  for (String item: this.location) {
73  buffer.append("/");
74  buffer.append(item);
75  }
76  return buffer.toString();
77  }
78 
79  public String getType() {
80  return this.type;
81  }
82 
83  public String getTest() {
84  return this.test;
85  }
86 
87  public String getMessage() {
88  return this.message;
89  }
90 
91  private void setType(final String type) {
92  if (type.equals("svrl:failed-assert")) {
93  this.type = "failed assert";
94  } else {
95  throw new Error("Do not understand type " + type);
96  }
97  }
98 
99  private void setAttrs(final NamedNodeMap attrs) {
100  Node attr;
101  // get the test name
102  attr = attrs.getNamedItem("test");
103  if (attr != null) {
104  this.test = attr.getNodeValue();
105  }
106  }
107  private void setMessage(final Node node) {
108  if (node.getNodeName().equals("svrl:text")) {
109  NodeList nodes = node.getChildNodes();
110  int size = nodes.getLength();
111  if (size == 0) {
112  this.message = "";
113  } else if(size == 1) {
114  this.message = nodes.item(0).getNodeValue();
115  this.message = this.message.trim();
116  } else {
117  throw new Error("Expected only 1 node, found "
118  + nodes.getLength());
119  }
120  } else {
121  NodeList nodes = node.getChildNodes();
122  int size = nodes.getLength();
123  Node temp;
124  for (int i = 0; i < size; i++) {
125  temp = nodes.item(i);
126  if (temp.getNodeName().equals("svrl:text")) {
127  this.setMessage(temp);
128  return;
129  }
130  }
131  }
132  }
133 
134  private void setLocation(final Node node) {
135  // deal with the root splitting
136  this.xpath_str = getLocation(node);
137  String temp = this.xpath_str.substring(3);
138  this.location = temp.split("/\\*:");
139 
140  // trim out the namespace junk
141  for (int i = 0; i < this.location.length; i++) {
142  this.location[i] = removeNS(this.location[i]);
143  }
144  }
145  static private String removeNS(final String orig) {
146  int left = orig.indexOf(NS_START);
147  int right = orig.indexOf(NS_STOP, left + 1);
148 
149  return orig.substring(0, left)
150  + orig.substring(right + NS_STOP.length());
151  }
152 
153  public static boolean hasLocation(final Node candidate) {
154  if (candidate.getNodeType() != Node.ELEMENT_NODE) {
155  return false;
156  }
157  String location = getLocation(candidate);
158  if (location == null) {
159  return false;
160  }
161  if (location.length() <= 0) {
162  return false;
163  }
164  return true;
165  }
166 
167  public static String getLocation(final Node node) {
168  NamedNodeMap attrs = node.getAttributes();
169  Node attr = attrs.getNamedItem("location");
170  if (attr == null) {
171  return "";
172  }
173  else {
174  return attr.getNodeValue();
175  }
176  }
177 
178 }