26 package org.nexusformat.nxvalidate;
28 import java.util.ArrayList;
29 import java.util.Vector;
31 import org.w3c.dom.NamedNodeMap;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
36 private static String NS_START =
"[namespace-uri";
37 private static String NS_STOP =
"']";
39 private String[] location;
40 private String xpath_str;
43 private String message;
46 this.setLocation(node);
47 this.setType(node.getNodeName());
48 this.setAttrs(node.getAttributes());
49 this.setMessage(node);
53 StringBuilder buffer =
new StringBuilder();
54 buffer.append(this.test +
" failed at " + this.
getLocation());
55 if (this.message.length() > 0) {
57 buffer.append(this.message);
59 return buffer.toString();
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]);
71 StringBuilder buffer =
new StringBuilder();
72 for (String item: this.location) {
76 return buffer.toString();
91 private void setType(
final String type) {
92 if (type.equals(
"svrl:failed-assert")) {
93 this.type =
"failed assert";
95 throw new Error(
"Do not understand type " + type);
99 private void setAttrs(
final NamedNodeMap attrs) {
102 attr = attrs.getNamedItem(
"test");
104 this.test = attr.getNodeValue();
107 private void setMessage(
final Node node) {
108 if (node.getNodeName().equals(
"svrl:text")) {
109 NodeList nodes = node.getChildNodes();
110 int size = nodes.getLength();
113 }
else if(size == 1) {
114 this.message = nodes.item(0).getNodeValue();
115 this.message = this.message.trim();
117 throw new Error(
"Expected only 1 node, found "
118 + nodes.getLength());
121 NodeList nodes = node.getChildNodes();
122 int size = nodes.getLength();
124 for (
int i = 0; i < size; i++) {
125 temp = nodes.item(i);
126 if (temp.getNodeName().equals(
"svrl:text")) {
127 this.setMessage(temp);
134 private void setLocation(
final Node node) {
137 String temp = this.xpath_str.substring(3);
138 this.location = temp.split(
"/\\*:");
141 for (
int i = 0; i < this.location.length; i++) {
142 this.location[i] = removeNS(this.location[i]);
145 static private String removeNS(
final String orig) {
146 int left = orig.indexOf(NS_START);
147 int right = orig.indexOf(NS_STOP, left + 1);
149 return orig.substring(0, left)
150 + orig.substring(right + NS_STOP.length());
154 if (candidate.getNodeType() != Node.ELEMENT_NODE) {
158 if (location == null) {
161 if (location.length() <= 0) {
168 NamedNodeMap attrs = node.getAttributes();
169 Node attr = attrs.getNamedItem(
"location");
174 return attr.getNodeValue();