KickJava   Java API By Example, From Geeks To Geeks.

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


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: TrueCheck.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.XBoolean;
28 import org.apache.xpath.objects.XObject;
29 import org.w3c.dom.Node JavaDoc;
30
31 /**
32  * Simple check that requires an XPath expression to evaluate to true.
33  */

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

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

52     public TrueCheck(Node JavaDoc node) {
53         this.xpath = node.getAttributes().getNamedItem("xpath").getNodeValue();
54         Node JavaDoc 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     /** @see org.apache.fop.layoutengine.LayoutEngineCheck */
62     public void check(LayoutResult result) {
63         XObject res;
64         try {
65             res = XPathAPI.eval(result.getAreaTree(), xpath, prefixResolver);
66         } catch (TransformerException JavaDoc e) {
67             throw new RuntimeException JavaDoc("XPath evaluation failed: " + e.getMessage());
68         }
69         if (!XBoolean.S_TRUE.equals(res)) {
70             if (failureMessage != null) {
71                 throw new RuntimeException JavaDoc(failureMessage);
72             } else {
73                 throw new RuntimeException JavaDoc(
74                         "Expected XPath expression to evaluate to 'true', but got '"
75                         + res + "' (" + this + ")");
76             }
77         }
78
79     }
80
81     /** @see java.lang.Object#toString() */
82     public String JavaDoc toString() {
83         return "XPath: " + xpath;
84     }
85     
86 }
87
Popular Tags