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:
20: package de.schlund.pfixxml.util.xsltimpl;
21:
22: import java.util.ArrayList;
23: import java.util.List;
24:
25: import javax.xml.transform.TransformerException;
26: import javax.xml.xpath.XPathConstants;
27: import javax.xml.xpath.XPathException;
28: import javax.xml.xpath.XPathFactoryConfigurationException;
29:
30: import net.sf.saxon.Configuration;
31: import net.sf.saxon.dom.NodeOverNodeInfo;
32: import net.sf.saxon.om.Item;
33: import net.sf.saxon.om.NodeInfo;
34: import net.sf.saxon.xpath.XPathEvaluator;
35:
36: import org.w3c.dom.Node;
37:
38: import de.schlund.pfixxml.util.XPathSupport;
39:
40: /**
41: * @author mleidig@schlund.de
42: */
43: public class XPathSaxon2 implements XPathSupport {
44:
45: public boolean isModelSupported(Node node) {
46: return NodeOverNodeInfo.class.isAssignableFrom(node.getClass());
47: }
48:
49: public List<Node> select(Node context, String xpath)
50: throws TransformerException {
51: try {
52: XPathEvaluator xpe = createXPath(context);
53: List<Node> nodeList = new ArrayList<Node>();
54: List<?> list = (List<?>) xpe.evaluate(xpath, context,
55: XPathConstants.NODESET);
56: for (Object obj : list) {
57: Item item = (Item) obj;
58: if (item instanceof NodeInfo)
59: nodeList
60: .add(NodeOverNodeInfo.wrap((NodeInfo) item));
61: }
62: return nodeList;
63: } catch (XPathException x) {
64: throw new TransformerException("XPath error", x);
65: }
66: }
67:
68: public boolean test(Node context, String test)
69: throws TransformerException {
70: try {
71: XPathEvaluator xpe = createXPath(context);
72: Boolean res = (Boolean) xpe.evaluate(test, context,
73: XPathConstants.BOOLEAN);
74: return res.booleanValue();
75: } catch (XPathException x) {
76: throw new TransformerException("XPath error", x);
77: }
78: }
79:
80: private XPathEvaluator createXPath(Node context)
81: throws XPathFactoryConfigurationException {
82: NodeOverNodeInfo info = (NodeOverNodeInfo) context;
83: Configuration config = info.getUnderlyingNodeInfo()
84: .getConfiguration();
85: XPathEvaluator xpe = new XPathEvaluator(config);
86: return xpe;
87: }
88:
89: }
|