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