1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.dom4j.dom; |
9 |
| |
10 |
| import java.util.ArrayList; |
11 |
| |
12 |
| import org.dom4j.DocumentFactory; |
13 |
| import org.dom4j.QName; |
14 |
| import org.dom4j.tree.DefaultDocument; |
15 |
| |
16 |
| import org.w3c.dom.Attr; |
17 |
| import org.w3c.dom.CDATASection; |
18 |
| import org.w3c.dom.DOMException; |
19 |
| import org.w3c.dom.Document; |
20 |
| import org.w3c.dom.EntityReference; |
21 |
| import org.w3c.dom.NamedNodeMap; |
22 |
| import org.w3c.dom.NodeList; |
23 |
| import org.w3c.dom.ProcessingInstruction; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class DOMDocument extends DefaultDocument implements Document { |
35 |
| |
36 |
| private static final DOMDocumentFactory DOCUMENT_FACTORY |
37 |
| = (DOMDocumentFactory) DOMDocumentFactory.getInstance(); |
38 |
| |
39 |
8
| public DOMDocument() {
|
40 |
8
| init();
|
41 |
| } |
42 |
| |
43 |
2
| public DOMDocument(String name) {
|
44 |
2
| super(name);
|
45 |
2
| init();
|
46 |
| } |
47 |
| |
48 |
0
| public DOMDocument(DOMElement rootElement) {
|
49 |
0
| super(rootElement);
|
50 |
0
| init();
|
51 |
| } |
52 |
| |
53 |
0
| public DOMDocument(DOMDocumentType docType) {
|
54 |
0
| super(docType);
|
55 |
0
| init();
|
56 |
| } |
57 |
| |
58 |
0
| public DOMDocument(DOMElement rootElement, DOMDocumentType docType) {
|
59 |
0
| super(rootElement, docType);
|
60 |
0
| init();
|
61 |
| } |
62 |
| |
63 |
0
| public DOMDocument(String name, DOMElement rootElement,
|
64 |
| DOMDocumentType docType) { |
65 |
0
| super(name, rootElement, docType);
|
66 |
0
| init();
|
67 |
| } |
68 |
| |
69 |
10
| private void init() {
|
70 |
10
| setDocumentFactory(DOCUMENT_FACTORY);
|
71 |
| } |
72 |
| |
73 |
| |
74 |
| |
75 |
0
| public boolean supports(String feature, String version) {
|
76 |
0
| return DOMNodeHelper.supports(this, feature, version);
|
77 |
| } |
78 |
| |
79 |
0
| public String getNamespaceURI() {
|
80 |
0
| return DOMNodeHelper.getNamespaceURI(this);
|
81 |
| } |
82 |
| |
83 |
0
| public String getPrefix() {
|
84 |
0
| return DOMNodeHelper.getPrefix(this);
|
85 |
| } |
86 |
| |
87 |
0
| public void setPrefix(String prefix) throws DOMException {
|
88 |
0
| DOMNodeHelper.setPrefix(this, prefix);
|
89 |
| } |
90 |
| |
91 |
0
| public String getLocalName() {
|
92 |
0
| return DOMNodeHelper.getLocalName(this);
|
93 |
| } |
94 |
| |
95 |
0
| public String getNodeName() {
|
96 |
0
| return "#document";
|
97 |
| } |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
0
| public String getNodeValue() throws DOMException {
|
103 |
0
| return null;
|
104 |
| } |
105 |
| |
106 |
0
| public void setNodeValue(String nodeValue) throws DOMException {
|
107 |
| } |
108 |
| |
109 |
0
| public org.w3c.dom.Node getParentNode() {
|
110 |
0
| return DOMNodeHelper.getParentNode(this);
|
111 |
| } |
112 |
| |
113 |
0
| public NodeList getChildNodes() {
|
114 |
0
| return DOMNodeHelper.createNodeList(content());
|
115 |
| } |
116 |
| |
117 |
0
| public org.w3c.dom.Node getFirstChild() {
|
118 |
0
| return DOMNodeHelper.asDOMNode(node(0));
|
119 |
| } |
120 |
| |
121 |
0
| public org.w3c.dom.Node getLastChild() {
|
122 |
0
| return DOMNodeHelper.asDOMNode(node(nodeCount() - 1));
|
123 |
| } |
124 |
| |
125 |
0
| public org.w3c.dom.Node getPreviousSibling() {
|
126 |
0
| return DOMNodeHelper.getPreviousSibling(this);
|
127 |
| } |
128 |
| |
129 |
0
| public org.w3c.dom.Node getNextSibling() {
|
130 |
0
| return DOMNodeHelper.getNextSibling(this);
|
131 |
| } |
132 |
| |
133 |
0
| public NamedNodeMap getAttributes() {
|
134 |
0
| return null;
|
135 |
| } |
136 |
| |
137 |
0
| public org.w3c.dom.Document getOwnerDocument() {
|
138 |
0
| return null;
|
139 |
| } |
140 |
| |
141 |
0
| public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild,
|
142 |
| org.w3c.dom.Node refChild) throws DOMException { |
143 |
0
| checkNewChildNode(newChild);
|
144 |
| |
145 |
0
| return DOMNodeHelper.insertBefore(this, newChild, refChild);
|
146 |
| } |
147 |
| |
148 |
0
| public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild,
|
149 |
| org.w3c.dom.Node oldChild) throws DOMException { |
150 |
0
| checkNewChildNode(newChild);
|
151 |
| |
152 |
0
| return DOMNodeHelper.replaceChild(this, newChild, oldChild);
|
153 |
| } |
154 |
| |
155 |
0
| public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild)
|
156 |
| throws DOMException { |
157 |
0
| return DOMNodeHelper.removeChild(this, oldChild);
|
158 |
| } |
159 |
| |
160 |
4
| public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild)
|
161 |
| throws DOMException { |
162 |
4
| checkNewChildNode(newChild);
|
163 |
| |
164 |
4
| return DOMNodeHelper.appendChild(this, newChild);
|
165 |
| } |
166 |
| |
167 |
4
| private void checkNewChildNode(org.w3c.dom.Node newChild)
|
168 |
| throws DOMException { |
169 |
4
| final int nodeType = newChild.getNodeType();
|
170 |
| |
171 |
4
| if (!((nodeType == org.w3c.dom.Node.ELEMENT_NODE)
|
172 |
| || (nodeType == org.w3c.dom.Node.COMMENT_NODE) |
173 |
| || (nodeType == org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE) |
174 |
| || (nodeType == org.w3c.dom.Node.DOCUMENT_TYPE_NODE))) { |
175 |
0
| throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
|
176 |
| "Given node cannot be a child of document"); |
177 |
| } |
178 |
| } |
179 |
| |
180 |
0
| public boolean hasChildNodes() {
|
181 |
0
| return nodeCount() > 0;
|
182 |
| } |
183 |
| |
184 |
1
| public org.w3c.dom.Node cloneNode(boolean deep) {
|
185 |
1
| return DOMNodeHelper.cloneNode(this, deep);
|
186 |
| } |
187 |
| |
188 |
0
| public boolean isSupported(String feature, String version) {
|
189 |
0
| return DOMNodeHelper.isSupported(this, feature, version);
|
190 |
| } |
191 |
| |
192 |
0
| public boolean hasAttributes() {
|
193 |
0
| return DOMNodeHelper.hasAttributes(this);
|
194 |
| } |
195 |
| |
196 |
| |
197 |
| |
198 |
0
| public NodeList getElementsByTagName(String name) {
|
199 |
0
| ArrayList list = new ArrayList();
|
200 |
0
| DOMNodeHelper.appendElementsByTagName(list, this, name);
|
201 |
| |
202 |
0
| return DOMNodeHelper.createNodeList(list);
|
203 |
| } |
204 |
| |
205 |
0
| public NodeList getElementsByTagNameNS(String namespace, String name) {
|
206 |
0
| ArrayList list = new ArrayList();
|
207 |
0
| DOMNodeHelper.appendElementsByTagNameNS(list, this, namespace, name);
|
208 |
| |
209 |
0
| return DOMNodeHelper.createNodeList(list);
|
210 |
| } |
211 |
| |
212 |
0
| public org.w3c.dom.DocumentType getDoctype() {
|
213 |
0
| return DOMNodeHelper.asDOMDocumentType(getDocType());
|
214 |
| } |
215 |
| |
216 |
0
| public org.w3c.dom.DOMImplementation getImplementation() {
|
217 |
0
| if (getDocumentFactory() instanceof org.w3c.dom.DOMImplementation) {
|
218 |
0
| return (org.w3c.dom.DOMImplementation) getDocumentFactory();
|
219 |
| } else { |
220 |
0
| return DOCUMENT_FACTORY;
|
221 |
| } |
222 |
| } |
223 |
| |
224 |
3
| public org.w3c.dom.Element getDocumentElement() {
|
225 |
3
| return DOMNodeHelper.asDOMElement(getRootElement());
|
226 |
| } |
227 |
| |
228 |
7
| public org.w3c.dom.Element createElement(String name) throws DOMException {
|
229 |
7
| return (org.w3c.dom.Element) getDocumentFactory().createElement(name);
|
230 |
| } |
231 |
| |
232 |
0
| public org.w3c.dom.DocumentFragment createDocumentFragment() {
|
233 |
0
| DOMNodeHelper.notSupported();
|
234 |
| |
235 |
0
| return null;
|
236 |
| } |
237 |
| |
238 |
15
| public org.w3c.dom.Text createTextNode(String data) {
|
239 |
15
| return (org.w3c.dom.Text) getDocumentFactory().createText(data);
|
240 |
| } |
241 |
| |
242 |
0
| public org.w3c.dom.Comment createComment(String data) {
|
243 |
0
| return (org.w3c.dom.Comment) getDocumentFactory().createComment(data);
|
244 |
| } |
245 |
| |
246 |
0
| public CDATASection createCDATASection(String data) throws DOMException {
|
247 |
0
| return (CDATASection) getDocumentFactory().createCDATA(data);
|
248 |
| } |
249 |
| |
250 |
0
| public ProcessingInstruction createProcessingInstruction(String target,
|
251 |
| String data) throws DOMException { |
252 |
0
| return (ProcessingInstruction) getDocumentFactory()
|
253 |
| .createProcessingInstruction(target, data); |
254 |
| } |
255 |
| |
256 |
0
| public Attr createAttribute(String name) throws DOMException {
|
257 |
0
| QName qname = getDocumentFactory().createQName(name);
|
258 |
| |
259 |
0
| return (Attr) getDocumentFactory().createAttribute(null, qname, "");
|
260 |
| } |
261 |
| |
262 |
0
| public EntityReference createEntityReference(String name)
|
263 |
| throws DOMException { |
264 |
0
| return (EntityReference) getDocumentFactory().createEntity(name, null);
|
265 |
| } |
266 |
| |
267 |
0
| public org.w3c.dom.Node importNode(org.w3c.dom.Node importedNode,
|
268 |
| boolean deep) throws DOMException { |
269 |
0
| DOMNodeHelper.notSupported();
|
270 |
| |
271 |
0
| return null;
|
272 |
| } |
273 |
| |
274 |
10
| public org.w3c.dom.Element createElementNS(String namespaceURI,
|
275 |
| String qualifiedName) throws DOMException { |
276 |
10
| QName qname = getDocumentFactory().createQName(qualifiedName,
|
277 |
| namespaceURI); |
278 |
| |
279 |
10
| return (org.w3c.dom.Element) getDocumentFactory().createElement(qname);
|
280 |
| } |
281 |
| |
282 |
0
| public org.w3c.dom.Attr createAttributeNS(String namespaceURI,
|
283 |
| String qualifiedName) throws DOMException { |
284 |
0
| QName qname = getDocumentFactory().createQName(qualifiedName,
|
285 |
| namespaceURI); |
286 |
| |
287 |
0
| return (org.w3c.dom.Attr) getDocumentFactory().createAttribute(null,
|
288 |
| qname, null); |
289 |
| } |
290 |
| |
291 |
0
| public org.w3c.dom.Element getElementById(String elementId) {
|
292 |
0
| return DOMNodeHelper.asDOMElement(elementByID(elementId));
|
293 |
| } |
294 |
| |
295 |
| |
296 |
| |
297 |
64
| protected DocumentFactory getDocumentFactory() {
|
298 |
64
| if (super.getDocumentFactory() == null) {
|
299 |
0
| return DOCUMENT_FACTORY;
|
300 |
| } else { |
301 |
64
| return super.getDocumentFactory();
|
302 |
| } |
303 |
| } |
304 |
| } |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| |