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.XBoolean; 28 import org.apache.xpath.objects.XObject; 29 import org.w3c.dom.Node ; 30 31 34 public class TrueCheck implements LayoutEngineCheck { 35 36 private String xpath; 37 private String failureMessage; 38 private PrefixResolver prefixResolver; 39 40 44 public TrueCheck(String xpath) { 45 this.xpath = xpath; 46 } 47 48 52 public TrueCheck(Node node) { 53 this.xpath = node.getAttributes().getNamedItem("xpath").getNodeValue(); 54 Node nd = node.getAttributes().getNamedItem("fail-msg"); 55 if (nd != null) { 56 this.failureMessage = nd.getNodeValue(); 57 } 58 this.prefixResolver = new PrefixResolverDefault(node); 59 } 60 61 62 public void check(LayoutResult result) { 63 XObject res; 64 try { 65 res = XPathAPI.eval(result.getAreaTree(), xpath, prefixResolver); 66 } catch (TransformerException e) { 67 throw new RuntimeException ("XPath evaluation failed: " + e.getMessage()); 68 } 69 if (!XBoolean.S_TRUE.equals(res)) { 70 if (failureMessage != null) { 71 throw new RuntimeException (failureMessage); 72 } else { 73 throw new RuntimeException ( 74 "Expected XPath expression to evaluate to 'true', but got '" 75 + res + "' (" + this + ")"); 76 } 77 } 78 79 } 80 81 82 public String toString() { 83 return "XPath: " + xpath; 84 } 85 86 } 87 | Popular Tags |