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