01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19: package de.schlund.pfixxml.util;
20:
21: import java.util.List;
22:
23: import javax.xml.transform.TransformerException;
24:
25: import org.apache.log4j.Logger;
26: import org.w3c.dom.Node;
27:
28: /**
29: * Evaluates XPath-expressions.
30: */
31: public class XPath {
32:
33: final static Logger LOG = Logger.getLogger(XPath.class);
34:
35: static XPathSupport defaultSupport = new XPathDefault();
36:
37: static XPathSupport getXPathSupport(Node context)
38: throws TransformerException {
39: for (XPathSupport xps : XsltProvider.getXpathSupport().values()) {
40: if (xps.isModelSupported(context))
41: return xps;
42: }
43: return defaultSupport;
44: }
45:
46: public static List<Node> select(Node context, String xpath)
47: throws TransformerException {
48: XPathSupport xps = getXPathSupport(context);
49: return xps.select(context, xpath);
50: }
51:
52: public static Node selectOne(Node context, String xpath)
53: throws TransformerException {
54: Node node;
55: node = selectNode(context, xpath);
56: if (node == null) {
57: throw new TransformerException("xpath '" + xpath
58: + "' not found in " + context.getClass().getName()
59: + " " + Xml.serialize(context, true, false));
60: }
61: return node;
62: }
63:
64: public static Node selectNode(Node context, String xpath)
65: throws TransformerException {
66: List<Node> result;
67: XPathSupport xps = getXPathSupport(context);
68: result = xps.select(context, xpath);
69: if (result.size() == 0) {
70: return null;
71: } else {
72: return result.get(0);
73: }
74: }
75:
76: /** computes the 'effective boolean value' **/
77: public static boolean test(Node context, String test)
78: throws TransformerException {
79: XPathSupport xps = getXPathSupport(context);
80: return xps.test(context, test);
81: }
82:
83: }
|