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