KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutengine > EvalCheck


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: EvalCheck.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutengine;
21
22 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
29
30 /**
31  * Simple check that requires an XPath expression to evaluate to true.
32  */

33 public class EvalCheck implements LayoutEngineCheck {
34
35     private String JavaDoc expected;
36     private String JavaDoc xpath;
37     private double tolerance;
38     private PrefixResolver prefixResolver;
39     
40     /**
41      * Creates a new instance
42      * @param expected expected value
43      * @param xpath XPath statement that needs to be evaluated
44      */

45     public EvalCheck(String JavaDoc expected, String JavaDoc xpath) {
46         this.expected = expected;
47         this.xpath = xpath;
48     }
49     
50     /**
51      * Creates a new instance from a DOM node.
52      * @param node DOM node that defines this check
53      */

54     public EvalCheck(Node JavaDoc node) {
55         this.expected = node.getAttributes().getNamedItem("expected").getNodeValue();
56         this.xpath = node.getAttributes().getNamedItem("xpath").getNodeValue();
57         Node JavaDoc 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     /** @see org.apache.fop.layoutengine.LayoutEngineCheck */
65     public void check(LayoutResult result) {
66         XObject res;
67         try {
68             res = XPathAPI.eval(result.getAreaTree(), xpath, prefixResolver);
69         } catch (TransformerException JavaDoc e) {
70             throw new RuntimeException JavaDoc("XPath evaluation failed: " + e.getMessage());
71         }
72         String JavaDoc actual = res.str(); //Second str() seems to fail. D'oh!
73
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 JavaDoc(
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 JavaDoc(
84                         "Expected XPath expression to evaluate to '" + expected + "', but got '"
85                         + actual + "' (" + this + ")");
86             }
87         }
88     }
89
90     /** @see java.lang.Object#toString() */
91     public String JavaDoc toString() {
92         return "XPath: " + xpath;
93     }
94
95 }
96
Popular Tags