01: package org.compass.core.xml.javax;
02:
03: import javax.xml.xpath.XPathConstants;
04: import javax.xml.xpath.XPathExpression;
05:
06: import org.compass.core.xml.XmlObject;
07: import org.compass.core.xml.XmlXPathExpression;
08: import org.w3c.dom.NodeList;
09:
10: /**
11: * A java 5 implementation of {@link XmlXPathExpression} wrapping a {@link XPathExpression}.
12: *
13: * @author kimchy
14: */
15: public class XPathXmlXPathExpression implements XmlXPathExpression {
16:
17: private XPathExpression xPathExpression;
18:
19: public XPathXmlXPathExpression(XPathExpression xPathExpression) {
20: this .xPathExpression = xPathExpression;
21: }
22:
23: public XmlObject[] select(XmlObject xmlObject) throws Exception {
24: NodeList nodeList = (NodeList) xPathExpression.evaluate(
25: ((NodeXmlObject) xmlObject).getNode(),
26: XPathConstants.NODESET);
27: if (nodeList == null) {
28: return null;
29: }
30: XmlObject[] xmlObjects = new XmlObject[nodeList.getLength()];
31: for (int i = 0; i < xmlObjects.length; i++) {
32: xmlObjects[i] = new NodeXmlObject(nodeList.item(i));
33: }
34: return xmlObjects;
35: }
36: }
|