DomImpl.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* DomImpl.java --
  2. Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.dom;
  32. import org.w3c.dom.Document;
  33. import org.w3c.dom.DocumentType;
  34. import org.w3c.dom.DOMException;
  35. import org.w3c.dom.DOMImplementation;
  36. import org.w3c.dom.Element;
  37. import org.w3c.dom.ls.DOMImplementationLS;
  38. import org.w3c.dom.ls.LSInput;
  39. import org.w3c.dom.ls.LSOutput;
  40. import org.w3c.dom.ls.LSParser;
  41. import org.w3c.dom.ls.LSSerializer;
  42. import gnu.xml.dom.html2.DomHTMLImpl;
  43. import gnu.xml.dom.ls.DomLSInput;
  44. import gnu.xml.dom.ls.DomLSOutput;
  45. import gnu.xml.dom.ls.DomLSParser;
  46. import gnu.xml.dom.ls.DomLSSerializer;
  47. /**
  48. * <p> "DOMImplementation" implementation. </p>
  49. *
  50. * <p> At this writing, the following features are supported:
  51. * "XML" (L1, L2, L3),
  52. * "Events" (L2), "MutationEvents" (L2), "USER-Events" (a conformant extension),
  53. * "HTMLEvents" (L2), "UIEvents" (L2), "Traversal" (L2), "XPath" (L3),
  54. * "LS" (L3) "LS-Async" (L3).
  55. * It is possible to compile the package so it doesn't support some of these
  56. * features (notably, Traversal).
  57. *
  58. * @author David Brownell
  59. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  60. */
  61. public class DomImpl
  62. implements DOMImplementation, DOMImplementationLS
  63. {
  64. /**
  65. * Constructs a DOMImplementation object which supports
  66. * "XML" and other DOM Level 2 features.
  67. */
  68. public DomImpl()
  69. {
  70. }
  71. /**
  72. * <b>DOM L1</b>
  73. * Returns true if the specified feature and version are
  74. * supported. Note that the case of the feature name is ignored.
  75. */
  76. public boolean hasFeature(String name, String version)
  77. {
  78. if (name.length() == 0)
  79. {
  80. return false;
  81. }
  82. name = name.toLowerCase();
  83. if (name.charAt(0) == '+')
  84. {
  85. name = name.substring(1);
  86. }
  87. if ("xml".equals(name) || "core".equals(name))
  88. {
  89. return (version == null ||
  90. "".equals(version) ||
  91. "1.0".equals(version) ||
  92. "2.0".equals(version) ||
  93. "3.0".equals(version));
  94. }
  95. else if ("ls".equals(name) || "ls-async".equals(name))
  96. {
  97. return (version == null ||
  98. "".equals(version) ||
  99. "3.0".equals(version));
  100. }
  101. else if ("events".equals(name)
  102. || "mutationevents".equals(name)
  103. || "uievents".equals(name)
  104. // || "mouseevents".equals(name)
  105. || "htmlevents".equals(name))
  106. {
  107. return (version == null ||
  108. "".equals(version) ||
  109. "2.0".equals(version));
  110. // Extension: "USER-" prefix event types can
  111. // be created and passed through the DOM.
  112. }
  113. else if ("user-events".equals(name))
  114. {
  115. return (version == null ||
  116. "".equals(version) ||
  117. "0.1".equals(version));
  118. // NOTE: "hasFeature" for events is here interpreted to
  119. // mean the DOM can manufacture those sorts of events,
  120. // since actually choosing to report the events is more
  121. // often part of the environment or application. It's
  122. // only really an issue for mutation events.
  123. }
  124. else if (DomNode.reportMutations
  125. && "traversal".equals(name))
  126. {
  127. return (version == null ||
  128. "".equals(version) ||
  129. "2.0".equals(version));
  130. }
  131. else if ("xpath".equals(name))
  132. {
  133. return (version == null ||
  134. "".equals(version) ||
  135. "3.0".equals(version));
  136. }
  137. else if ("html".equals(name) || "xhtml".equals(name))
  138. {
  139. return (version == null ||
  140. "".equals(version) ||
  141. "2.0".equals(version));
  142. }
  143. // views
  144. // stylesheets
  145. // css, css2
  146. // range
  147. return false;
  148. }
  149. /**
  150. * <b>DOM L2</b>
  151. * Creates and returns a DocumentType, associated with this
  152. * implementation. This DocumentType can have no associated
  153. * objects(notations, entities) until the DocumentType is
  154. * first associated with a document.
  155. *
  156. * <p> Note that there is no implication that this DTD will
  157. * be parsed by the DOM, or ever have contents. Moreover, the
  158. * DocumentType created here can only be added to a document by
  159. * the createDocument method(below). <em>That means that the only
  160. * portable way to create a Document object is to start parsing,
  161. * queue comment and processing instruction (PI) nodes, and then only
  162. * create a DOM Document after <b>(a)</b> it's known if a DocumentType
  163. * object is needed, and <b>(b) the name and namespace of the root
  164. * element is known. Queued comment and PI nodes would then be
  165. * inserted appropriately in the document prologue, both before and
  166. * after the DTD node, and additional attributes assigned to the
  167. * root element.</em>
  168. *(One hopes that the final DOM REC fixes this serious botch.)
  169. */
  170. public DocumentType createDocumentType(String rootName,
  171. String publicId,
  172. String systemId)
  173. // CR2 deleted internal subset, ensuring DocumentType
  174. // is 100% useless instead of just 90% so.
  175. {
  176. DomDocument.checkNCName(rootName, false);
  177. return new DomDoctype(this, rootName, publicId, systemId, null);
  178. }
  179. /**
  180. * <b>DOM L2</b>
  181. * Creates and returns a Document, populated only with a root element and
  182. * optionally a document type(if that was provided).
  183. */
  184. public Document createDocument(String namespaceURI,
  185. String rootName,
  186. DocumentType doctype)
  187. {
  188. Document doc = createDocument();
  189. Element root = null;
  190. if (rootName != null)
  191. {
  192. root = doc.createElementNS(namespaceURI, rootName);
  193. if (rootName.startsWith("xmlns:"))
  194. {
  195. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  196. "xmlns is reserved", null, 0);
  197. }
  198. }
  199. // Bleech -- L2 seemingly _requires_ omission of xmlns attributes.
  200. if (doctype != null)
  201. {
  202. doc.appendChild(doctype); // handles WRONG_DOCUMENT error
  203. }
  204. if (root != null)
  205. {
  206. doc.appendChild(root);
  207. }
  208. return doc;
  209. }
  210. protected Document createDocument()
  211. {
  212. return new DomDocument(this);
  213. }
  214. // DOM Level 3
  215. public Object getFeature(String feature, String version)
  216. {
  217. if (hasFeature(feature, version))
  218. {
  219. if ("html".equalsIgnoreCase(feature) ||
  220. "xhtml".equalsIgnoreCase(feature))
  221. {
  222. return new DomHTMLImpl();
  223. }
  224. return this;
  225. }
  226. return null;
  227. }
  228. // -- DOMImplementationLS --
  229. public LSParser createLSParser(short mode, String schemaType)
  230. throws DOMException
  231. {
  232. return new DomLSParser(mode, schemaType);
  233. }
  234. public LSSerializer createLSSerializer()
  235. {
  236. return new DomLSSerializer();
  237. }
  238. public LSInput createLSInput()
  239. {
  240. return new DomLSInput();
  241. }
  242. public LSOutput createLSOutput()
  243. {
  244. return new DomLSOutput();
  245. }
  246. }