Consumer.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* Consumer.java --
  2. Copyright (C) 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.DocumentType;
  33. import org.w3c.dom.Node;
  34. import org.w3c.dom.Text;
  35. import org.xml.sax.Attributes;
  36. import org.xml.sax.SAXException;
  37. import org.xml.sax.ext.Attributes2;
  38. import gnu.xml.pipeline.DomConsumer;
  39. import gnu.xml.pipeline.EventConsumer;
  40. /**
  41. * Event consumer which constructs DOM documents using the implementation
  42. * in this package, using SAX2 events. This packages various backdoors
  43. * into this DOM implementation, as needed to address DOM requirements
  44. * that can't be met by strictly conforming implementations of DOM.
  45. *
  46. * <p> These requirements all relate to {@link DocumentType} nodes and
  47. * features of that node type. These features are normally not used,
  48. * because that interface only exposes a subset of the information found
  49. * in DTDs. More, that subset does not include the most important typing
  50. * information. For example, it excludes element content models and
  51. * attribute typing. It does expose some entity management issues,
  52. * although entity management doesn't relate to document typing.
  53. *
  54. * <p> Note that SAX2 does not expose the literal text of the DTD's
  55. * internal subset, so it will not be present in DOM trees constructed
  56. * using this API. (Though with a good SAX2 implementation, it could
  57. * be partially recreated...)
  58. *
  59. * @author David Brownell
  60. */
  61. public class Consumer extends DomConsumer
  62. {
  63. /**
  64. * Constructs an unconfigured event consumer,
  65. * as a terminus in a SAX event pipeline.
  66. */
  67. // used by PipelineFactory [terminus]
  68. public Consumer ()
  69. throws SAXException
  70. {
  71. super (DomDocument.class);
  72. setHandler (new Backdoor (this));
  73. }
  74. /**
  75. * Constructs an unconfigured event consumer,
  76. * as a stage in a SAX event pipeline.
  77. */
  78. // used by PipelineFactory [filter]
  79. public Consumer (EventConsumer next)
  80. throws SAXException
  81. {
  82. super (DomDocument.class, next);
  83. setHandler (new Backdoor (this));
  84. }
  85. /**
  86. * Implements the backdoors needed by DOM.
  87. * All methods in this class use implementation-specific APIs that are
  88. * implied by the DOM specification (needed to implement testable
  89. * behavior) but which are excluded from the DOM specification.
  90. */
  91. public static class Backdoor extends DomConsumer.Handler
  92. {
  93. /**
  94. * Constructor.
  95. * @param consumer must have been initialized to use the
  96. * {@link DomDocument} class (or a subclass) for
  97. * constructing DOM trees
  98. */
  99. protected Backdoor (DomConsumer consumer)
  100. throws SAXException
  101. { super (consumer); }
  102. // helper routine
  103. private DomDoctype getDoctype ()
  104. throws SAXException
  105. {
  106. DomDocument doc = (DomDocument) getDocument ();
  107. DocumentType dt = doc.getDoctype ();
  108. if (dt == null)
  109. throw new SAXException ("doctype missing!");
  110. return (DomDoctype) dt;
  111. }
  112. // SAX2 "lexical" event
  113. public void startDTD (String name, String publicId, String systemId)
  114. throws SAXException
  115. {
  116. DomDocument doc = (DomDocument) getDocument ();
  117. super.startDTD (name, publicId, systemId);
  118. // DOM L2 doctype creation model is bizarre
  119. DomDoctype dt = new DomDoctype (doc, name, publicId, systemId);
  120. doc.appendChild (dt);
  121. }
  122. // SAX2 "lexical" event
  123. public void endDTD ()
  124. throws SAXException
  125. {
  126. super.endDTD ();
  127. // DOM L2 has no way to make things readonly
  128. getDoctype ().makeReadonly ();
  129. }
  130. // SAX1 DTD event
  131. public void notationDecl (
  132. String name,
  133. String publicId, String systemId
  134. ) throws SAXException
  135. {
  136. // DOM L2 can't create/save notation nodes
  137. getDoctype ().declareNotation (name, publicId, systemId);
  138. }
  139. // SAX1 DTD event
  140. public void unparsedEntityDecl (
  141. String name,
  142. String publicId, String systemId,
  143. String notationName
  144. ) throws SAXException
  145. {
  146. // DOM L2 can't create/save entity nodes
  147. getDoctype ().declareEntity (name, publicId, systemId,
  148. notationName);
  149. }
  150. // SAX2 declaration event
  151. public void internalEntityDecl (String name, String value)
  152. throws SAXException
  153. {
  154. // DOM L2 can't create/save entity nodes
  155. // NOTE: this doesn't save the value as a child of this
  156. // node, though it could realistically do so.
  157. getDoctype ().declareEntity (name, null, null, null);
  158. }
  159. // SAX2 declaration event
  160. public void externalEntityDecl (
  161. String name,
  162. String publicId,
  163. String systemId
  164. ) throws SAXException
  165. {
  166. // DOM L2 can't create/save entity nodes
  167. // NOTE: DOM allows for these to have children, if
  168. // they don't have unbound namespace references.
  169. getDoctype ().declareEntity (name, publicId, systemId, null);
  170. }
  171. // SAX2 element
  172. public void startElement (
  173. String uri,
  174. String localName,
  175. String qName,
  176. Attributes atts
  177. ) throws SAXException
  178. {
  179. Node top;
  180. super.startElement (uri, localName, qName, atts);
  181. // might there be more work?
  182. top = getTop ();
  183. if (!top.hasAttributes () || !(atts instanceof Attributes2))
  184. return;
  185. // remember any attributes that got defaulted
  186. DomNamedNodeMap map = (DomNamedNodeMap) top.getAttributes ();
  187. Attributes2 attrs = (Attributes2) atts;
  188. int length = atts.getLength ();
  189. //map.compact ();
  190. for (int i = 0; i < length; i++) {
  191. if (attrs.isSpecified (i))
  192. continue;
  193. // value was defaulted.
  194. String temp = attrs.getQName (i);
  195. DomAttr attr;
  196. if ("".equals (temp))
  197. attr = (DomAttr) map.getNamedItemNS (attrs.getURI (i),
  198. atts.getLocalName (i));
  199. else
  200. attr = (DomAttr) map.getNamedItem (temp);
  201. // DOM L2 can't write this flag, only read it
  202. attr.setSpecified (false);
  203. }
  204. }
  205. public void endElement (
  206. String uri,
  207. String localName,
  208. String qName
  209. ) throws SAXException
  210. {
  211. DomNode top = (DomNode) getTop ();
  212. top.compact ();
  213. super.endElement (uri, localName, qName);
  214. }
  215. protected Text createText (
  216. boolean isCDATA,
  217. char buf [],
  218. int off,
  219. int len
  220. ) {
  221. DomDocument doc = (DomDocument) getDocument ();
  222. if (isCDATA)
  223. return doc.createCDATASection (buf, off, len);
  224. else
  225. return doc.createTextNode (buf, off, len);
  226. }
  227. public void elementDecl(String name, String model)
  228. throws SAXException
  229. {
  230. getDoctype().elementDecl(name, model);
  231. }
  232. public void attributeDecl (
  233. String ename,
  234. String aname,
  235. String type,
  236. String mode,
  237. String value
  238. ) throws SAXException
  239. {
  240. getDoctype().attributeDecl(ename, aname, type, mode, value);
  241. /*
  242. if (value == null && !"ID".equals (type))
  243. return;
  244. DomDoctype.ElementInfo info;
  245. info = getDoctype ().getElementInfo (ename);
  246. if (value != null)
  247. info.setAttrDefault (aname, value);
  248. if ("ID".equals (type))
  249. info.setIdAttr (aname);
  250. */
  251. }
  252. // force duplicate name checking off while we're
  253. // using parser output (don't duplicate the work)
  254. public void startDocument () throws SAXException
  255. {
  256. super.startDocument ();
  257. DomDocument doc = (DomDocument) getDocument ();
  258. doc.setStrictErrorChecking(false);
  259. doc.setBuilding(true);
  260. }
  261. public void endDocument ()
  262. throws SAXException
  263. {
  264. DomDocument doc = (DomDocument) getDocument ();
  265. doc.setStrictErrorChecking(true);
  266. doc.setBuilding(false);
  267. doc.compact ();
  268. DomDoctype doctype = (DomDoctype) doc.getDoctype();
  269. if (doctype != null)
  270. {
  271. doctype.makeReadonly();
  272. }
  273. super.endDocument ();
  274. }
  275. // these three methods collaborate to populate entity
  276. // refs, marking contents readonly on end-of-entity
  277. public boolean canPopulateEntityRefs ()
  278. { return true; }
  279. public void startEntity (String name)
  280. throws SAXException
  281. {
  282. if (name.charAt (0) == '%' || "[dtd]".equals (name))
  283. return;
  284. super.startEntity (name);
  285. DomNode top = (DomNode) getTop ();
  286. if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE)
  287. top.readonly = false;
  288. }
  289. public void endEntity (String name)
  290. throws SAXException
  291. {
  292. if (name.charAt (0) == '%' || "[dtd]".equals (name))
  293. return;
  294. DomNode top = (DomNode) getTop ();
  295. if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE) {
  296. top.compact ();
  297. top.makeReadonly ();
  298. }
  299. super.endEntity (name);
  300. }
  301. }
  302. }