1 17 18 19 20 package org.apache.fop.layoutengine; 21 22 import javax.xml.transform.TransformerException ; 23 24 import org.apache.xml.utils.PrefixResolver; 25 import org.apache.xml.utils.PrefixResolverDefault; 26 import org.apache.xpath.XPathAPI; 27 import org.apache.xpath.objects.XObject; 28 import org.w3c.dom.Node ; 29 30 33 public class EvalCheck implements LayoutEngineCheck { 34 35 private String expected; 36 private String xpath; 37 private double tolerance; 38 private PrefixResolver prefixResolver; 39 40 45 public EvalCheck(String expected, String xpath) { 46 this.expected = expected; 47 this.xpath = xpath; 48 } 49 50 54 public EvalCheck(Node node) { 55 this.expected = node.getAttributes().getNamedItem("expected").getNodeValue(); 56 this.xpath = node.getAttributes().getNamedItem("xpath").getNodeValue(); 57 Node nd = node.getAttributes().getNamedItem("tolerance"); 58 if (nd != null) { 59 this.tolerance = Double.parseDouble(nd.getNodeValue()); 60 } 61 this.prefixResolver = new PrefixResolverDefault(node); 62 } 63 64 65 public void check(LayoutResult result) { 66 XObject res; 67 try { 68 res = XPathAPI.eval(result.getAreaTree(), xpath, prefixResolver); 69 } catch (TransformerException e) { 70 throw new RuntimeException ("XPath evaluation failed: " + e.getMessage()); 71 } 72 String actual = res.str(); if (tolerance != 0) { 74 double v1 = Double.parseDouble(expected); 75 double v2 = Double.parseDouble(actual); 76 if (Math.abs(v1 - v2) > tolerance) { 77 throw new RuntimeException ( 78 "Expected XPath expression to evaluate to '" + expected + "', but got '" 79 + actual + "' (" + this + ", outside tolerance)"); 80 } 81 } else { 82 if (!expected.equals(actual)) { 83 throw new RuntimeException ( 84 "Expected XPath expression to evaluate to '" + expected + "', but got '" 85 + actual + "' (" + this + ")"); 86 } 87 } 88 } 89 90 91 public String toString() { 92 return "XPath: " + xpath; 93 } 94 95 } 96 | Popular Tags |