01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.core.xml.dom4j;
18:
19: import java.util.List;
20:
21: import org.compass.core.xml.XmlObject;
22: import org.compass.core.xml.XmlXPathExpression;
23: import org.dom4j.Node;
24: import org.dom4j.xpath.DefaultXPath;
25:
26: /**
27: * A dom4j (http://www.dom4j.org) implementation of {@link XmlObject}.
28: *
29: * @author kimchy
30: */
31: public class Dom4jXmlObject implements XmlObject {
32:
33: private Node node;
34:
35: /**
36: * Constructs a new xml object based on a dom4j <code>Node</code>.
37: *
38: * @param node The node to construct the dom4j xml object with
39: */
40: public Dom4jXmlObject(Node node) {
41: this .node = node;
42: }
43:
44: /**
45: * Returns the dom4j node name.
46: */
47: public String getName() {
48: return node.getName();
49: }
50:
51: /**
52: * Returns the dom4j node text.
53: */
54: public String getValue() {
55: return node.getText();
56: }
57:
58: public XmlObject[] selectPath(String path) {
59: List nodes = node.selectNodes(path);
60: XmlObject[] xmlObjects = new XmlObject[nodes.size()];
61: for (int i = 0; i < xmlObjects.length; i++) {
62: xmlObjects[i] = new Dom4jXmlObject((Node) nodes.get(i));
63: }
64: return xmlObjects;
65: }
66:
67: /**
68: * Return <code>true</code> since dom4j supports xml compilation.
69: */
70: public boolean canCompileXpath() {
71: return true;
72: }
73:
74: /**
75: * Compiles the given xpath expression using dom4j <code>DefaultXPath</code>.
76: */
77: public XmlXPathExpression compile(String path) {
78: return new Dom4jXmlXPathExpression(new DefaultXPath(path));
79: }
80:
81: /**
82: * Returns the dom4j Node that this xml object wraps.
83: */
84: public Node getNode() {
85: return node;
86: }
87: }
|