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 |
| |
26 |
| |
27 |
| |
28 |
| |
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 |
| |
39 |
| private static final DocumentFactory DOCUMENT_FACTORY = DocumentFactory.getInstance(); |
40 |
| |
41 |
| |
42 |
457236
| public AbstractNode() {
|
43 |
| } |
44 |
| |
45 |
0
| public short getNodeType() {
|
46 |
0
| return UNKNOWN_NODE;
|
47 |
| } |
48 |
| |
49 |
224
| public String getNodeTypeName() {
|
50 |
224
| int type = getNodeType();
|
51 |
224
| if ( type < 0 || type >= NODE_TYPE_NAMES.length ) {
|
52 |
0
| return "Unknown";
|
53 |
| } |
54 |
224
| return NODE_TYPE_NAMES[type];
|
55 |
| } |
56 |
| |
57 |
106
| public Document getDocument() {
|
58 |
106
| Element element = getParent();
|
59 |
106
| return ( element != null ) ? element.getDocument() : null;
|
60 |
| } |
61 |
| |
62 |
4220
| public void setDocument(Document document) {
|
63 |
| } |
64 |
| |
65 |
11860
| public Element getParent() {
|
66 |
11860
| return null;
|
67 |
| } |
68 |
| |
69 |
11888
| public void setParent(Element parent) {
|
70 |
| } |
71 |
| |
72 |
152
| public boolean supportsParent() {
|
73 |
152
| return false;
|
74 |
| } |
75 |
| |
76 |
12
| public boolean isReadOnly() {
|
77 |
12
| return true;
|
78 |
| } |
79 |
| |
80 |
0
| public boolean hasContent() {
|
81 |
0
| return false;
|
82 |
| } |
83 |
| |
84 |
36
| public String getPath() {
|
85 |
36
| return getPath(null);
|
86 |
| } |
87 |
| |
88 |
40
| public String getUniquePath() {
|
89 |
40
| return getUniquePath(null);
|
90 |
| } |
91 |
| |
92 |
| |
93 |
666
| public Object clone() {
|
94 |
666
| if ( isReadOnly() ) {
|
95 |
12
| return this;
|
96 |
| } |
97 |
| else { |
98 |
654
| try {
|
99 |
654
| Node answer = (Node) super.clone();
|
100 |
654
| answer.setParent( null );
|
101 |
654
| answer.setDocument( null );
|
102 |
654
| return answer;
|
103 |
| } |
104 |
| catch (CloneNotSupportedException e) { |
105 |
| |
106 |
0
| throw new RuntimeException( "This should never happen. Caught: " + e );
|
107 |
| } |
108 |
| } |
109 |
| } |
110 |
| |
111 |
3034
| public Node detach() {
|
112 |
3034
| Element parent = getParent();
|
113 |
3034
| if ( parent != null ) {
|
114 |
3030
| parent.remove( this );
|
115 |
| } |
116 |
| else { |
117 |
4
| Document document = getDocument();
|
118 |
4
| if ( document != null ) {
|
119 |
4
| document.remove( this );
|
120 |
| } |
121 |
| } |
122 |
3034
| setParent(null);
|
123 |
3034
| setDocument(null);
|
124 |
3034
| return this;
|
125 |
| } |
126 |
| |
127 |
0
| public String getName() {
|
128 |
0
| return null;
|
129 |
| } |
130 |
| |
131 |
0
| public void setName(String name) {
|
132 |
0
| throw new UnsupportedOperationException( "This node cannot be modified" );
|
133 |
| } |
134 |
| |
135 |
0
| public String getText() {
|
136 |
0
| return null;
|
137 |
| } |
138 |
| |
139 |
3026
| public String getStringValue() {
|
140 |
3026
| return getText();
|
141 |
| } |
142 |
| |
143 |
0
| public void setText(String text) {
|
144 |
0
| throw new UnsupportedOperationException( "This node cannot be modified" );
|
145 |
| } |
146 |
| |
147 |
| |
148 |
0
| public void write(Writer writer) throws IOException {
|
149 |
0
| writer.write( asXML() );
|
150 |
| } |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
4
| public Object selectObject(String xpathExpression) {
|
156 |
4
| XPath xpath = createXPath(xpathExpression);
|
157 |
0
| return xpath.selectObject(this);
|
158 |
| } |
159 |
| |
160 |
456
| public List selectNodes(String xpathExpression) {
|
161 |
456
| XPath xpath = createXPath(xpathExpression);
|
162 |
456
| return xpath.selectNodes(this);
|
163 |
| } |
164 |
| |
165 |
2
| public List selectNodes(
|
166 |
| String xpathExpression, |
167 |
| String comparisonXPathExpression |
168 |
| ) { |
169 |
2
| return selectNodes(
|
170 |
| xpathExpression, comparisonXPathExpression, false |
171 |
| ); |
172 |
| } |
173 |
| |
174 |
4
| public List selectNodes(
|
175 |
| String xpathExpression, |
176 |
| String comparisonXPathExpression, |
177 |
| boolean removeDuplicates |
178 |
| ) { |
179 |
4
| XPath xpath = createXPath(xpathExpression);
|
180 |
4
| XPath sortBy = createXPath(comparisonXPathExpression);
|
181 |
4
| return xpath.selectNodes(this, sortBy, removeDuplicates);
|
182 |
| } |
183 |
| |
184 |
128
| public Node selectSingleNode(String xpathExpression) {
|
185 |
128
| XPath xpath = createXPath(xpathExpression);
|
186 |
128
| return xpath.selectSingleNode(this);
|
187 |
| } |
188 |
| |
189 |
168
| public String valueOf(String xpathExpression) {
|
190 |
168
| XPath xpath = createXPath(xpathExpression);
|
191 |
168
| return xpath.valueOf(this);
|
192 |
| } |
193 |
| |
194 |
0
| public Number numberValueOf(String xpathExpression) {
|
195 |
0
| XPath xpath = createXPath(xpathExpression);
|
196 |
0
| return xpath.numberValueOf(this);
|
197 |
| } |
198 |
| |
199 |
18
| public boolean matches(String patternText) {
|
200 |
18
| NodeFilter filter = createXPathFilter(patternText);
|
201 |
18
| return filter.matches(this);
|
202 |
| } |
203 |
| |
204 |
1448
| public XPath createXPath(String xpathExpression) {
|
205 |
1448
| return getDocumentFactory().createXPath(xpathExpression);
|
206 |
| } |
207 |
| |
208 |
18
| public NodeFilter createXPathFilter(String patternText) {
|
209 |
18
| return getDocumentFactory().createXPathFilter(patternText);
|
210 |
| } |
211 |
| |
212 |
0
| public Pattern createPattern(String patternText) {
|
213 |
0
| return getDocumentFactory().createPattern(patternText);
|
214 |
| } |
215 |
| |
216 |
| |
217 |
152
| public Node asXPathResult(Element parent) {
|
218 |
152
| if (supportsParent()) {
|
219 |
0
| return this;
|
220 |
| } |
221 |
152
| return createXPathResult(parent);
|
222 |
| } |
223 |
| |
224 |
0
| protected DocumentFactory getDocumentFactory() {
|
225 |
0
| return DOCUMENT_FACTORY;
|
226 |
| } |
227 |
| |
228 |
0
| protected Node createXPathResult(Element parent) {
|
229 |
0
| 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 |
| |