1
2
3
4
5
6
7
8 package org.dom4j.tree;
9
10 import java.io.IOException;
11 import java.io.Serializable;
12 import java.io.Writer;
13 import java.util.List;
14
15 import org.dom4j.Document;
16 import org.dom4j.DocumentFactory;
17 import org.dom4j.Element;
18 import org.dom4j.Node;
19 import org.dom4j.NodeFilter;
20 import org.dom4j.XPath;
21 import org.dom4j.rule.Pattern;
22
23 /***
24 * <p>
25 * <code>AbstractNode</code> is an abstract base class for tree implementors
26 * to use for implementation inheritence.
27 * </p>
28 *
29 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
30 * @version $Revision: 1.31 $
31 */
32 public abstract class AbstractNode implements Node, Cloneable, Serializable {
33 protected static final String[] NODE_TYPE_NAMES = {"Node", "Element",
34 "Attribute", "Text", "CDATA", "Entity", "Entity",
35 "ProcessingInstruction", "Comment", "Document", "DocumentType",
36 "DocumentFragment", "Notation", "Namespace", "Unknown" };
37
38 /*** The <code>DocumentFactory</code> instance used by default */
39 private static final DocumentFactory DOCUMENT_FACTORY = DocumentFactory
40 .getInstance();
41
42 public AbstractNode() {
43 }
44
45 public short getNodeType() {
46 return UNKNOWN_NODE;
47 }
48
49 public String getNodeTypeName() {
50 int type = getNodeType();
51
52 if ((type < 0) || (type >= NODE_TYPE_NAMES.length)) {
53 return "Unknown";
54 }
55
56 return NODE_TYPE_NAMES[type];
57 }
58
59 public Document getDocument() {
60 Element element = getParent();
61
62 return (element != null) ? element.getDocument() : null;
63 }
64
65 public void setDocument(Document document) {
66 }
67
68 public Element getParent() {
69 return null;
70 }
71
72 public void setParent(Element parent) {
73 }
74
75 public boolean supportsParent() {
76 return false;
77 }
78
79 public boolean isReadOnly() {
80 return true;
81 }
82
83 public boolean hasContent() {
84 return false;
85 }
86
87 public String getPath() {
88 return getPath(null);
89 }
90
91 public String getUniquePath() {
92 return getUniquePath(null);
93 }
94
95 public Object clone() {
96 if (isReadOnly()) {
97 return this;
98 } else {
99 try {
100 Node answer = (Node) super.clone();
101 answer.setParent(null);
102 answer.setDocument(null);
103
104 return answer;
105 } catch (CloneNotSupportedException e) {
106
107 throw new RuntimeException("This should never happen. Caught: "
108 + e);
109 }
110 }
111 }
112
113 public Node detach() {
114 Element parent = getParent();
115
116 if (parent != null) {
117 parent.remove(this);
118 } else {
119 Document document = getDocument();
120
121 if (document != null) {
122 document.remove(this);
123 }
124 }
125
126 setParent(null);
127 setDocument(null);
128
129 return this;
130 }
131
132 public String getName() {
133 return null;
134 }
135
136 public void setName(String name) {
137 throw new UnsupportedOperationException("This node cannot be modified");
138 }
139
140 public String getText() {
141 return null;
142 }
143
144 public String getStringValue() {
145 return getText();
146 }
147
148 public void setText(String text) {
149 throw new UnsupportedOperationException("This node cannot be modified");
150 }
151
152 public void write(Writer writer) throws IOException {
153 writer.write(asXML());
154 }
155
156
157 public Object selectObject(String xpathExpression) {
158 XPath xpath = createXPath(xpathExpression);
159
160 return xpath.evaluate(this);
161 }
162
163 public List selectNodes(String xpathExpression) {
164 XPath xpath = createXPath(xpathExpression);
165
166 return xpath.selectNodes(this);
167 }
168
169 public List selectNodes(String xpathExpression,
170 String comparisonXPathExpression) {
171 return selectNodes(xpathExpression, comparisonXPathExpression, false);
172 }
173
174 public List selectNodes(String xpathExpression,
175 String comparisonXPathExpression, boolean removeDuplicates) {
176 XPath xpath = createXPath(xpathExpression);
177 XPath sortBy = createXPath(comparisonXPathExpression);
178
179 return xpath.selectNodes(this, sortBy, removeDuplicates);
180 }
181
182 public Node selectSingleNode(String xpathExpression) {
183 XPath xpath = createXPath(xpathExpression);
184
185 return xpath.selectSingleNode(this);
186 }
187
188 public String valueOf(String xpathExpression) {
189 XPath xpath = createXPath(xpathExpression);
190
191 return xpath.valueOf(this);
192 }
193
194 public Number numberValueOf(String xpathExpression) {
195 XPath xpath = createXPath(xpathExpression);
196
197 return xpath.numberValueOf(this);
198 }
199
200 public boolean matches(String patternText) {
201 NodeFilter filter = createXPathFilter(patternText);
202
203 return filter.matches(this);
204 }
205
206 public XPath createXPath(String xpathExpression) {
207 return getDocumentFactory().createXPath(xpathExpression);
208 }
209
210 public NodeFilter createXPathFilter(String patternText) {
211 return getDocumentFactory().createXPathFilter(patternText);
212 }
213
214 public Pattern createPattern(String patternText) {
215 return getDocumentFactory().createPattern(patternText);
216 }
217
218 public Node asXPathResult(Element parent) {
219 if (supportsParent()) {
220 return this;
221 }
222
223 return createXPathResult(parent);
224 }
225
226 protected DocumentFactory getDocumentFactory() {
227 return DOCUMENT_FACTORY;
228 }
229
230 protected Node createXPathResult(Element parent) {
231 throw new RuntimeException("asXPathResult() not yet implemented fully "
232 + "for: " + this);
233 }
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271