26 package org.nexusformat.nxvalidate;
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;
36 import org.nexusformat.nxvalidate.exceptions.NXvalidateException;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Node;
40 import org.w3c.dom.NodeList;
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;
57 nodes =
new ArrayList<Node>();
58 badNodes =
new ArrayList<Node>();
59 warnNodes =
new ArrayList<Node>();
69 this.filterDoc = filterDoc;
95 Level.SEVERE, null, ex);
96 }
catch (XPathExpressionException ex) {
98 Level.SEVERE, null, ex);
114 Level.SEVERE, null, ex);
115 }
catch (XPathExpressionException ex) {
117 Level.SEVERE, null, ex);
134 Level.SEVERE, null, ex);
135 }
catch (XPathExpressionException ex) {
137 Level.SEVERE, null, ex);
144 XPathExpressionException {
146 NodeList failed = null;
147 String location = null;
148 Element element = null;
149 XPathFactory factory = XPathFactory.newInstance();
150 NodeList nodeList = null;
152 if (filterDoc != null) {
153 if (filterDoc.hasChildNodes()) {
159 failed = filterDoc.getElementsByTagName(
"svrl:failed-assert");
162 failed = filterDoc.getElementsByTagName(
"svrl:successful-report");
164 if (failed.getLength() > 0) {
166 for (
int i = 0; i < failed.getLength(); ++i) {
168 element = (Element) failed.item(i);
170 if (element.hasChildNodes()) {
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);
183 Object result = expr.evaluate(doc,
184 XPathConstants.NODESET);
185 nodeList = (NodeList) result;
188 for (
int j = 0; j < nodeList.getLength(); j++) {
190 nodeList.item(j).setUserData(
"validated", null, null);
193 nodeList.item(j).setUserData(
"bad", null, null);
196 nodeList.item(j).setUserData(
"warn", null, null);
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);
221 private void getReportingNode(
int type, NodeList nodeListInput)
throws NXvalidateException,
222 XPathExpressionException {
225 String location = 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;
240 for (
int i = 0; i < nodeListInput.getLength(); ++i) {
247 element = (Element) nodeListInput.item(i);
249 if (element.hasChildNodes()) {
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();
261 if (element.getElementsByTagName(
262 "svrl:diagnostic-reference").getLength() > 0) {
264 diag = element.getElementsByTagName(
265 "svrl:diagnostic-reference").item(0).getTextContent().trim();
267 diagAtt = ((Element) element.getElementsByTagName(
268 "svrl:diagnostic-reference").item(0)).getAttribute(
"diagnostic");
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);
278 Object result = expr.evaluate(doc,
279 XPathConstants.NODESET);
280 nodeList = (NodeList) result;
282 for (
int j = 0; j < nodeList.getLength(); j++) {
284 nodeList.item(j).setUserData(
"validated",
new Boolean(
true), null);
286 nodeList.item(j).setUserData(
"bad",
new Boolean(
true), null);
287 badNodes.add(nodeList.item(j));
290 nodeList.item(j).setUserData(
"warn",
new Boolean(
true), null);
291 warnNodes.add(nodeList.item(j));
294 if (nodeList.item(j).getUserData(
"tests")
295 != null && test != null) {
296 tests = (ArrayList<String>) nodeList.item(j).getUserData(
300 tests =
new ArrayList<String>();
304 if (nodeList.item(j).getUserData(
"texts")
305 != null && text != null) {
306 texts = (ArrayList<String>) nodeList.item(j).getUserData(
310 texts =
new ArrayList<String>();
314 if (nodeList.item(j).getUserData(
"diags")
315 != null && diag != null) {
316 diags = (ArrayList<String>) nodeList.item(j).getUserData(
320 diags =
new ArrayList<String>();
324 if (nodeList.item(j).getUserData(
"diagatts")
325 != null && diagAtt != null) {
326 diagAtts = (ArrayList<String>) nodeList.item(j).getUserData(
328 diagAtts.add(diagAtt);
330 diagAtts =
new ArrayList<String>();
331 diagAtts.add(diagAtt);
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);
343 nodes.add(nodeList.item(j));
353 private void getXPathList() throws NXvalidateException, XPathExpressionException {
355 NodeList list = null;
357 if (filterDoc != null) {
358 if (filterDoc.hasChildNodes()) {
362 list = filterDoc.getElementsByTagName(
"svrl:failed-assert");
364 if (list.getLength() > 0) {
365 getReportingNode(0, list);
370 list = filterDoc.getElementsByTagName(
"svrl:successful-report");
372 if (list.getLength() > 0) {
373 getReportingNode(1, list);