SAXSerializer.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* SAXSerializer.java --
  2. Copyright (C) 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.transform;
  32. import java.util.HashMap;
  33. import java.util.Iterator;
  34. import java.util.LinkedList;
  35. import org.w3c.dom.Attr;
  36. import org.w3c.dom.DocumentType;
  37. import org.w3c.dom.NamedNodeMap;
  38. import org.w3c.dom.Node;
  39. import org.xml.sax.Attributes;
  40. import org.xml.sax.ContentHandler;
  41. import org.xml.sax.SAXException;
  42. import org.xml.sax.ext.LexicalHandler;
  43. /**
  44. * Serializes a DOM node to a sequence of SAX events.
  45. *
  46. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  47. */
  48. class SAXSerializer
  49. implements Attributes
  50. {
  51. transient NamedNodeMap attrs;
  52. transient LinkedList namespaces = new LinkedList();
  53. boolean isDefined(String prefix, String uri)
  54. {
  55. for (Iterator i = namespaces.iterator(); i.hasNext(); )
  56. {
  57. HashMap ctx = (HashMap) i.next();
  58. if (uri.equals(ctx.get(prefix)))
  59. {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. void define(String prefix, String uri)
  66. {
  67. for (Iterator i = namespaces.iterator(); i.hasNext(); )
  68. {
  69. HashMap ctx = (HashMap) i.next();
  70. if (ctx.containsKey(prefix))
  71. {
  72. HashMap newCtx = new HashMap();
  73. newCtx.put(prefix, uri);
  74. namespaces.addFirst(newCtx);
  75. return;
  76. }
  77. }
  78. HashMap ctx;
  79. if (namespaces.isEmpty())
  80. {
  81. ctx = new HashMap();
  82. namespaces.add(ctx);
  83. }
  84. else
  85. {
  86. ctx = (HashMap) namespaces.getFirst();
  87. }
  88. ctx.put(prefix, uri);
  89. }
  90. void undefine(String prefix, String uri)
  91. {
  92. for (Iterator i = namespaces.iterator(); i.hasNext(); )
  93. {
  94. HashMap ctx = (HashMap) i.next();
  95. if (uri.equals(ctx.get(prefix)))
  96. {
  97. ctx.remove(prefix);
  98. if (ctx.isEmpty())
  99. {
  100. namespaces.remove(ctx);
  101. }
  102. return;
  103. }
  104. }
  105. }
  106. public int getLength()
  107. {
  108. return attrs.getLength();
  109. }
  110. public String getURI(int index)
  111. {
  112. return attrs.item(index).getNamespaceURI();
  113. }
  114. public String getLocalName(int index)
  115. {
  116. return attrs.item(index).getLocalName();
  117. }
  118. public String getQName(int index)
  119. {
  120. return attrs.item(index).getNodeName();
  121. }
  122. public String getType(int index)
  123. {
  124. Attr attr = (Attr) attrs.item(index);
  125. return attr.isId() ? "ID" : "CDATA";
  126. }
  127. public String getValue(int index)
  128. {
  129. return attrs.item(index).getNodeValue();
  130. }
  131. public int getIndex(String uri, String localName)
  132. {
  133. int len = attrs.getLength();
  134. for (int i = 0; i < len; i++)
  135. {
  136. Node attr = attrs.item(i);
  137. String a_uri = attr.getNamespaceURI();
  138. String a_localName = attr.getLocalName();
  139. if (((a_uri == null && uri == null) ||
  140. (a_uri != null && a_uri.equals(uri))) &&
  141. a_localName.equals(localName))
  142. {
  143. return i;
  144. }
  145. }
  146. return -1;
  147. }
  148. public int getIndex(String qName)
  149. {
  150. int len = attrs.getLength();
  151. for (int i = 0; i < len; i++)
  152. {
  153. Node attr = attrs.item(i);
  154. String a_name = attr.getNodeName();
  155. if (a_name.equals(qName))
  156. {
  157. return i;
  158. }
  159. }
  160. return -1;
  161. }
  162. public String getType(String uri, String localName)
  163. {
  164. Attr attr = (Attr) attrs.getNamedItemNS(uri, localName);
  165. return attr.isId() ? "ID" : "CDATA";
  166. }
  167. public String getType(String qName)
  168. {
  169. Attr attr = (Attr) attrs.getNamedItem(qName);
  170. return attr.isId() ? "ID" : "CDATA";
  171. }
  172. public String getValue(String uri, String localName)
  173. {
  174. return attrs.getNamedItemNS(uri, localName).getNodeValue();
  175. }
  176. public String getValue(String qName)
  177. {
  178. Attr attr = (Attr) attrs.getNamedItem(qName);
  179. return (attr == null) ? null : attr.getNodeValue();
  180. }
  181. void serialize(Node node, ContentHandler ch, LexicalHandler lh)
  182. throws SAXException
  183. {
  184. attrs = node.getAttributes();
  185. Node children;
  186. Node next = node.getNextSibling();
  187. switch (node.getNodeType())
  188. {
  189. case Node.ELEMENT_NODE:
  190. String uri = node.getNamespaceURI();
  191. String prefix = node.getPrefix();
  192. boolean defined = isDefined(prefix, uri);
  193. if (!defined)
  194. {
  195. define(prefix, uri);
  196. ch.startPrefixMapping(prefix, uri);
  197. }
  198. String localName = node.getLocalName();
  199. String qName = node.getNodeName();
  200. ch.startElement(uri, localName, qName, this);
  201. children = node.getFirstChild();
  202. if (children != null)
  203. {
  204. serialize(children, ch, lh);
  205. }
  206. ch.endElement(uri, localName, qName);
  207. if (!defined)
  208. {
  209. ch.endPrefixMapping(prefix);
  210. undefine(prefix, uri);
  211. }
  212. break;
  213. case Node.TEXT_NODE:
  214. char[] chars = node.getNodeValue().toCharArray();
  215. ch.characters(chars, 0, chars.length);
  216. break;
  217. case Node.CDATA_SECTION_NODE:
  218. char[] cdata = node.getNodeValue().toCharArray();
  219. if (lh != null)
  220. {
  221. lh.startCDATA();
  222. ch.characters(cdata, 0, cdata.length);
  223. lh.endCDATA();
  224. }
  225. else
  226. {
  227. ch.characters(cdata, 0, cdata.length);
  228. }
  229. break;
  230. case Node.COMMENT_NODE:
  231. if (lh != null)
  232. {
  233. char[] comment = node.getNodeValue().toCharArray();
  234. lh.comment(comment, 0, comment.length);
  235. }
  236. break;
  237. case Node.DOCUMENT_NODE:
  238. case Node.DOCUMENT_FRAGMENT_NODE:
  239. ch.startDocument();
  240. children = node.getFirstChild();
  241. if (children != null)
  242. {
  243. serialize(children, ch, lh);
  244. }
  245. ch.endDocument();
  246. break;
  247. case Node.DOCUMENT_TYPE_NODE:
  248. if (lh != null)
  249. {
  250. DocumentType doctype = (DocumentType) node;
  251. String publicId = doctype.getPublicId();
  252. String systemId = doctype.getSystemId();
  253. lh.startDTD(node.getNodeName(), publicId, systemId);
  254. NamedNodeMap entities = doctype.getEntities();
  255. int len = entities.getLength();
  256. for (int i = 0; i < len; i++)
  257. {
  258. Node entity = entities.item(i);
  259. String entityName = entity.getNodeName();
  260. lh.startEntity(entityName);
  261. lh.endEntity(entityName);
  262. }
  263. lh.endDTD();
  264. }
  265. break;
  266. case Node.PROCESSING_INSTRUCTION_NODE:
  267. ch.processingInstruction(node.getNodeName(), node.getNodeValue());
  268. break;
  269. case Node.ENTITY_REFERENCE_NODE:
  270. ch.skippedEntity(node.getNodeName());
  271. break;
  272. }
  273. attrs = null;
  274. if (next != null)
  275. {
  276. serialize(next, ch, lh);
  277. }
  278. }
  279. }