XMLEventWriterImpl.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* XMLEventWriterImpl.java --
  2. Copyright (C) 2005 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.stream;
  32. import javax.xml.namespace.NamespaceContext;
  33. import javax.xml.namespace.QName;
  34. import javax.xml.stream.XMLEventReader;
  35. import javax.xml.stream.XMLEventWriter;
  36. import javax.xml.stream.XMLStreamConstants;
  37. import javax.xml.stream.XMLStreamException;
  38. import javax.xml.stream.XMLStreamWriter;
  39. import javax.xml.stream.events.Attribute;
  40. import javax.xml.stream.events.Characters;
  41. import javax.xml.stream.events.Comment;
  42. import javax.xml.stream.events.DTD;
  43. import javax.xml.stream.events.Namespace;
  44. import javax.xml.stream.events.ProcessingInstruction;
  45. import javax.xml.stream.events.StartDocument;
  46. import javax.xml.stream.events.StartElement;
  47. import javax.xml.stream.events.XMLEvent;
  48. /**
  49. * Writer to write events to an underlying XML stream writer.
  50. *
  51. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  52. */
  53. public class XMLEventWriterImpl
  54. implements XMLEventWriter
  55. {
  56. protected final XMLStreamWriter writer;
  57. protected XMLEventWriterImpl(XMLStreamWriter writer)
  58. {
  59. this.writer = writer;
  60. }
  61. public void flush()
  62. throws XMLStreamException
  63. {
  64. writer.flush();
  65. }
  66. public void close()
  67. throws XMLStreamException
  68. {
  69. writer.close();
  70. }
  71. public void add(XMLEvent event)
  72. throws XMLStreamException
  73. {
  74. QName name;
  75. String uri;
  76. switch (event.getEventType())
  77. {
  78. case XMLStreamConstants.START_ELEMENT:
  79. StartElement startElement = event.asStartElement();
  80. name = startElement.getName();
  81. uri = name.getNamespaceURI();
  82. if (uri != null && !"".equals(uri))
  83. writer.writeStartElement(name.getPrefix(), name.getLocalPart(), uri);
  84. else
  85. writer.writeStartElement(name.getLocalPart());
  86. break;
  87. case XMLStreamConstants.END_ELEMENT:
  88. writer.writeEndElement();
  89. break;
  90. case XMLStreamConstants.ATTRIBUTE:
  91. Attribute attribute = (Attribute) event;
  92. name = attribute.getName();
  93. uri = name.getNamespaceURI();
  94. if (uri != null && !"".equals(uri))
  95. writer.writeAttribute(name.getPrefix(), uri, name.getLocalPart(),
  96. attribute.getValue());
  97. else
  98. writer.writeAttribute(name.getLocalPart(), attribute.getValue());
  99. break;
  100. case XMLStreamConstants.NAMESPACE:
  101. Namespace namespace = (Namespace) event;
  102. uri = namespace.getNamespaceURI();
  103. writer.writeNamespace(namespace.getPrefix(), uri);
  104. break;
  105. case XMLStreamConstants.PROCESSING_INSTRUCTION:
  106. ProcessingInstruction pi = (ProcessingInstruction) event;
  107. String data = pi.getData();
  108. if (data == null)
  109. writer.writeProcessingInstruction(pi.getTarget());
  110. else
  111. writer.writeProcessingInstruction(pi.getTarget(), data);
  112. break;
  113. case XMLStreamConstants.COMMENT:
  114. Comment comment = (Comment) event;
  115. writer.writeComment(comment.getText());
  116. break;
  117. case XMLStreamConstants.START_DOCUMENT:
  118. StartDocument startDocument = (StartDocument) event;
  119. writer.writeStartDocument(startDocument.getVersion());
  120. break;
  121. case XMLStreamConstants.END_DOCUMENT:
  122. writer.writeEndDocument();
  123. break;
  124. case XMLStreamConstants.DTD:
  125. DTD dtd = (DTD) event;
  126. writer.writeDTD(dtd.getDocumentTypeDeclaration());
  127. break;
  128. case XMLStreamConstants.CHARACTERS:
  129. case XMLStreamConstants.SPACE:
  130. Characters characters = event.asCharacters();
  131. writer.writeCharacters(characters.getData());
  132. break;
  133. case XMLStreamConstants.CDATA:
  134. Characters cdata = event.asCharacters();
  135. writer.writeCData(cdata.getData());
  136. break;
  137. }
  138. }
  139. public void add(XMLEventReader reader)
  140. throws XMLStreamException
  141. {
  142. while (reader.hasNext())
  143. add(reader.nextEvent());
  144. }
  145. public String getPrefix(String uri)
  146. throws XMLStreamException
  147. {
  148. return writer.getPrefix(uri);
  149. }
  150. public void setPrefix(String prefix, String uri)
  151. throws XMLStreamException
  152. {
  153. writer.setPrefix(prefix, uri);
  154. }
  155. public void setDefaultNamespace(String uri)
  156. throws XMLStreamException
  157. {
  158. writer.setDefaultNamespace(uri);
  159. }
  160. public void setNamespaceContext(NamespaceContext context)
  161. throws XMLStreamException
  162. {
  163. writer.setNamespaceContext(context);
  164. }
  165. public NamespaceContext getNamespaceContext()
  166. {
  167. return writer.getNamespaceContext();
  168. }
  169. }