1 /* GnomeDocument.java -
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu
.xml
.libxmlj
.dom
;
40 import gnu
.java
.lang
.CPStringBuilder
;
41 import gnu
.xml
.dom
.DomNodeIterator
;
43 import java
.util
.Iterator
;
45 import org
.w3c
.dom
.Attr
;
46 import org
.w3c
.dom
.CDATASection
;
47 import org
.w3c
.dom
.Comment
;
48 import org
.w3c
.dom
.Document
;
49 import org
.w3c
.dom
.DocumentFragment
;
50 import org
.w3c
.dom
.DocumentType
;
51 import org
.w3c
.dom
.DOMConfiguration
;
52 import org
.w3c
.dom
.DOMErrorHandler
;
53 import org
.w3c
.dom
.DOMException
;
54 import org
.w3c
.dom
.DOMImplementation
;
55 import org
.w3c
.dom
.DOMStringList
;
56 import org
.w3c
.dom
.Element
;
57 import org
.w3c
.dom
.EntityReference
;
58 import org
.w3c
.dom
.Node
;
59 import org
.w3c
.dom
.NodeList
;
60 import org
.w3c
.dom
.ProcessingInstruction
;
61 import org
.w3c
.dom
.Text
;
62 import org
.w3c
.dom
.UserDataHandler
;
63 import org
.w3c
.dom
.traversal
.DocumentTraversal
;
64 import org
.w3c
.dom
.traversal
.NodeFilter
;
65 import org
.w3c
.dom
.traversal
.NodeIterator
;
66 import org
.w3c
.dom
.traversal
.TreeWalker
;
67 import org
.w3c
.dom
.xpath
.XPathEvaluator
;
68 import org
.w3c
.dom
.xpath
.XPathException
;
69 import org
.w3c
.dom
.xpath
.XPathExpression
;
70 import org
.w3c
.dom
.xpath
.XPathNSResolver
;
73 * A DOM document node implemented in libxml2.
75 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
77 public class GnomeDocument
79 implements Document
, DOMConfiguration
, XPathEvaluator
, DocumentTraversal
82 DOMImplementation dom
;
87 boolean strictErrorChecking
;
89 /* DOMConfiguration */
90 boolean canonicalForm
= false;
91 boolean cdataSections
= true;
92 boolean checkCharacterNormalization
= false;
93 boolean comments
= true;
94 boolean datatypeNormalization
= false;
95 boolean elementContentWhitespace
= true;
96 boolean entities
= true;
97 DOMErrorHandler errorHandler
;
98 boolean namespaces
= true;
99 boolean namespaceDeclarations
= true;
100 boolean normalizeCharacters
= false;
101 boolean splitCdataSections
= true;
102 boolean validate
= false;
103 boolean validateIfSchema
= false;
104 boolean wellFormed
= true;
106 GnomeDocument(Object id
)
109 strictErrorChecking
= true;
112 protected void finalize()
117 private native void free(Object id
);
119 public native DocumentType
getDoctype();
121 public DOMImplementation
getImplementation()
126 public native Element
getDocumentElement();
128 public Element
createElement(String tagName
)
131 return createElementNS(null, tagName
);
134 public native DocumentType
createDocumentType(String name
, String publicId
,
137 public native DocumentFragment
createDocumentFragment();
139 public native Text
createTextNode(String data
);
141 public native Comment
createComment(String data
);
143 public native CDATASection
createCDATASection(String data
)
146 public native ProcessingInstruction
createProcessingInstruction(String target
,
150 public Attr
createAttribute(String name
)
153 return createAttributeNS(null, name
);
156 public native EntityReference
createEntityReference(String name
)
159 public native NodeList
getElementsByTagName(String tagName
);
161 public Node
importNode(Node importedNode
, boolean deep
)
164 Node ret
= xmljImportNode(importedNode
, deep
);
165 if (importedNode
instanceof GnomeNode
)
167 ((GnomeNode
) importedNode
)
168 .notifyUserDataHandlers(UserDataHandler
.NODE_IMPORTED
,
174 private native Node
xmljImportNode(Node importedNode
, boolean deep
)
177 public native Element
createElementNS(String namespaceURI
, String
181 public native Attr
createAttributeNS(String namespaceURI
, String
185 public native NodeList
getElementsByTagNameNS(String namespaceURI
,
188 public Element
getElementById(String elementId
)
190 Element element
= xmljGetElementById(elementId
);
193 TreeWalker walker
= createTreeWalker(this, NodeFilter
.SHOW_ELEMENT
,
195 for (Node node
= walker
.nextNode(); node
!= null;
196 node
= walker
.nextNode())
198 GnomeElement e
= (GnomeElement
) node
;
199 if (e
.userIdAttrs
!= null)
201 for (Iterator i
= e
.userIdAttrs
.iterator(); i
.hasNext(); )
203 Attr attr
= (Attr
) i
.next();
204 if (attr
.getNodeValue().equals(elementId
))
215 private native Element
xmljGetElementById(String elementId
);
217 // DOM Level 3 methods
219 public native String
getInputEncoding();
221 public native String
getXmlEncoding();
223 public native boolean getXmlStandalone();
225 public native void setXmlStandalone(boolean xmlStandalone
);
227 public native String
getXmlVersion();
229 public native void setXmlVersion(String xmlVersion
);
231 public boolean getStrictErrorChecking()
233 return strictErrorChecking
;
236 public void setStrictErrorChecking(boolean strictErrorChecking
)
238 this.strictErrorChecking
= strictErrorChecking
;
241 public native String
getDocumentURI();
243 public native void setDocumentURI(String documentURI
);
245 public Node
adoptNode(Node source
)
248 if (source
== null || !(source
instanceof GnomeNode
))
252 Node ret
= xmljAdoptNode(source
);
253 if (source
instanceof GnomeNode
)
255 ((GnomeNode
) source
).
256 notifyUserDataHandlers(UserDataHandler
.NODE_ADOPTED
,
262 private native Node
xmljAdoptNode(Node source
)
265 public DOMConfiguration
getDomConfig()
270 public void normalizeDocument()
275 public native Node
renameNode(Node n
, String namespaceURI
,
276 String qualifiedName
);
278 // -- DOMConfiguration methods --
280 public void setParameter(String name
, Object value
)
283 name
= name
.toLowerCase();
284 if ("canonical-form".equals(name
))
287 canonicalForm = getBooleanValue(value);*/
289 else if ("cdata-sections".equals(name
))
291 cdataSections
= getBooleanValue(value
);
293 else if ("check-character-normalization".equals(name
))
296 checkCharacterNormalization = getBooleanValue(value);*/
298 else if ("comments".equals(name
))
300 comments
= getBooleanValue(value
);
302 else if ("datatype-normalization".equals(name
))
305 datatypeNormalization = getBooleanValue(value);*/
307 else if ("element-content-whitespace".equals(name
))
310 elementContentWhitespace = getBooleanValue(value);*/
312 else if ("entities".equals(name
))
314 entities
= getBooleanValue(value
);
316 else if ("error-handler".equals(name
))
318 errorHandler
= (DOMErrorHandler
) value
;
320 else if ("infoset".equals(name
))
322 if (getBooleanValue(value
))
324 validateIfSchema
= false;
326 datatypeNormalization
= false;
327 cdataSections
= false;
328 namespaceDeclarations
= true;
330 elementContentWhitespace
= true;
335 else if ("namespaces".equals(name
))
338 namespaces = getBooleanValue(value);*/
340 else if ("namespace-declarations".equals(name
))
342 namespaceDeclarations
= getBooleanValue(value
);
344 else if ("normalize-characters".equals(name
))
347 normalizeCharacters = getBooleanValue(value);*/
349 else if ("split-cdata-sections".equals(name
))
351 splitCdataSections
= getBooleanValue(value
);
353 else if ("validate".equals(name
))
356 validate = getBooleanValue(value);*/
358 else if ("validate-if-schema".equals(name
))
361 validateIfSchema = getBooleanValue(value);*/
363 else if ("well-formed".equals(name
))
366 wellFormed = getBooleanValue(value);*/
370 throw new GnomeDOMException(DOMException
.NOT_FOUND_ERR
, name
);
374 public Object
getParameter(String name
)
377 name
= name
.toLowerCase();
378 if ("canonical-form".equals(name
))
380 return Boolean
.valueOf(canonicalForm
);
382 else if ("cdata-sections".equals(name
))
384 return Boolean
.valueOf(cdataSections
);
386 else if ("check-character-normalization".equals(name
))
388 return Boolean
.valueOf(checkCharacterNormalization
);
390 else if ("comments".equals(name
))
392 return Boolean
.valueOf(comments
);
394 else if ("datatype-normalization".equals(name
))
396 return Boolean
.valueOf(datatypeNormalization
);
398 else if ("element-content-whitespace".equals(name
))
400 return Boolean
.valueOf(elementContentWhitespace
);
402 else if ("entities".equals(name
))
404 return Boolean
.valueOf(entities
);
406 else if ("error-handler".equals(name
))
410 else if ("infoset".equals(name
))
412 return Boolean
.valueOf(!validateIfSchema
&&
414 !datatypeNormalization
&&
416 namespaceDeclarations
&&
418 elementContentWhitespace
&&
422 else if ("namespaces".equals(name
))
424 return Boolean
.valueOf(namespaces
);
426 else if ("namespace-declarations".equals(name
))
428 return Boolean
.valueOf(namespaceDeclarations
);
430 else if ("normalize-characters".equals(name
))
432 return Boolean
.valueOf(normalizeCharacters
);
434 else if ("split-cdata-sections".equals(name
))
436 return Boolean
.valueOf(splitCdataSections
);
438 else if ("validate".equals(name
))
440 return Boolean
.valueOf(validate
);
442 else if ("validate-if-schema".equals(name
))
444 return Boolean
.valueOf(validateIfSchema
);
446 else if ("well-formed".equals(name
))
448 return Boolean
.valueOf(wellFormed
);
452 throw new GnomeDOMException(DOMException
.NOT_FOUND_ERR
, name
);
456 public boolean canSetParameter(String name
, Object value
)
458 name
= name
.toLowerCase();
459 if ("error-handler".equals(name
))
461 return (value
== null || value
instanceof DOMErrorHandler
);
463 return ("cdata-sections".equals(name
) ||
464 "comments".equals(name
) ||
465 "entities".equals(name
) ||
466 "namespace-declarations".equals(name
) ||
467 "split-cdata-sections".equals(name
));
470 public DOMStringList
getParameterNames()
472 String
[] names
= new String
[] {
475 "check-character-normalization",
477 "datatype-normalization",
478 "element-content-whitespace",
483 "namespace-declarations",
484 "normalize-characters",
485 "split-cdata-sections",
487 "validate-if-schema",
490 return new GnomeDOMStringList(names
);
493 private boolean getBooleanValue(Object value
)
495 if (value
instanceof Boolean
)
497 return ((Boolean
) value
).booleanValue();
499 else if (value
instanceof String
)
501 return Boolean
.valueOf ((String
) value
).booleanValue();
506 // -- XPathEvaluator methods --
508 public XPathExpression
createExpression(String expression
,
509 XPathNSResolver resolver
)
510 throws XPathException
, DOMException
512 return new GnomeXPathExpression(this, expression
, resolver
);
515 public XPathNSResolver
createNSResolver(Node nodeResolver
)
517 return new GnomeXPathNSResolver(nodeResolver
);
520 public native Object
evaluate(String expression
,
522 XPathNSResolver resolver
,
525 throws XPathException
, DOMException
;
527 // -- DocumentTraversal methods --
529 public NodeIterator
createNodeIterator(Node root
,
532 boolean entityReferenceExpansion
)
535 return new DomNodeIterator(root
, whatToShow
, filter
,
536 entityReferenceExpansion
, false);
539 public TreeWalker
createTreeWalker(Node root
,
542 boolean entityReferenceExpansion
)
545 return new DomNodeIterator(root
, whatToShow
, filter
,
546 entityReferenceExpansion
, true);
551 public String
toString()
553 CPStringBuilder buffer
= new CPStringBuilder(getClass().getName());
554 buffer
.append("[version=");
555 buffer
.append(getXmlVersion());
556 buffer
.append(",standalone=");
557 buffer
.append(getXmlStandalone());
559 return buffer
.toString();