XMLEventFactoryImpl.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* XMLEventFactoryImpl.java --
  2. Copyright (C) 2005,2006 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 java.util.Collections;
  33. import java.util.Iterator;
  34. import java.util.LinkedList;
  35. import javax.xml.XMLConstants;
  36. import javax.xml.namespace.NamespaceContext;
  37. import javax.xml.namespace.QName;
  38. import javax.xml.stream.Location;
  39. import javax.xml.stream.XMLEventFactory;
  40. import javax.xml.stream.events.Attribute;
  41. import javax.xml.stream.events.Characters;
  42. import javax.xml.stream.events.Comment;
  43. import javax.xml.stream.events.DTD;
  44. import javax.xml.stream.events.EndDocument;
  45. import javax.xml.stream.events.EndElement;
  46. import javax.xml.stream.events.EntityDeclaration;
  47. import javax.xml.stream.events.EntityReference;
  48. import javax.xml.stream.events.Namespace;
  49. import javax.xml.stream.events.ProcessingInstruction;
  50. import javax.xml.stream.events.StartDocument;
  51. import javax.xml.stream.events.StartElement;
  52. /**
  53. * Factory for XML events.
  54. *
  55. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  56. */
  57. public class XMLEventFactoryImpl
  58. extends XMLEventFactory
  59. {
  60. protected Location location;
  61. public void setLocation(Location location)
  62. {
  63. this.location = location;
  64. }
  65. public Attribute createAttribute(String prefix, String namespaceURI,
  66. String localName, String value)
  67. {
  68. return new AttributeImpl(location,
  69. new QName(namespaceURI, localName, prefix),
  70. value, "CDATA", true);
  71. }
  72. public Attribute createAttribute(String localName, String value)
  73. {
  74. return new AttributeImpl(location,
  75. new QName(localName),
  76. value, "CDATA", true);
  77. }
  78. public Attribute createAttribute(QName name, String value)
  79. {
  80. return new AttributeImpl(location, name, value,
  81. "CDATA", true);
  82. }
  83. public Namespace createNamespace(String namespaceURI)
  84. {
  85. return new NamespaceImpl(location,
  86. XMLConstants.DEFAULT_NS_PREFIX,
  87. namespaceURI,
  88. true);
  89. }
  90. public Namespace createNamespace(String prefix, String namespaceUri)
  91. {
  92. return new NamespaceImpl(location, prefix, namespaceUri, true);
  93. }
  94. public StartElement createStartElement(QName name,
  95. Iterator attributes,
  96. Iterator namespaces)
  97. {
  98. return new StartElementImpl(location, name,
  99. createLinkedList(attributes),
  100. createLinkedList(namespaces),
  101. null);
  102. }
  103. public StartElement createStartElement(String prefix,
  104. String namespaceUri,
  105. String localName)
  106. {
  107. return new StartElementImpl(location,
  108. new QName(namespaceUri, localName, prefix),
  109. Collections.EMPTY_LIST,
  110. Collections.EMPTY_LIST,
  111. null);
  112. }
  113. public StartElement createStartElement(String prefix,
  114. String namespaceUri,
  115. String localName,
  116. Iterator attributes,
  117. Iterator namespaces)
  118. {
  119. return new StartElementImpl(location,
  120. new QName(namespaceUri, localName, prefix),
  121. createLinkedList(attributes),
  122. createLinkedList(namespaces),
  123. null);
  124. }
  125. public StartElement createStartElement(String prefix,
  126. String namespaceUri,
  127. String localName,
  128. Iterator attributes,
  129. Iterator namespaces,
  130. NamespaceContext context)
  131. {
  132. return new StartElementImpl(location,
  133. new QName(namespaceUri, localName, prefix),
  134. createLinkedList(attributes),
  135. createLinkedList(namespaces),
  136. context);
  137. }
  138. public EndElement createEndElement(QName name,
  139. Iterator namespaces)
  140. {
  141. return new EndElementImpl(location, name,
  142. createLinkedList(namespaces));
  143. }
  144. public EndElement createEndElement(String prefix,
  145. String namespaceUri,
  146. String localName)
  147. {
  148. return new EndElementImpl(location,
  149. new QName(namespaceUri, localName, prefix),
  150. Collections.EMPTY_LIST);
  151. }
  152. public EndElement createEndElement(String prefix,
  153. String namespaceUri,
  154. String localName,
  155. Iterator namespaces)
  156. {
  157. return new EndElementImpl(location,
  158. new QName(namespaceUri, localName, prefix),
  159. createLinkedList(namespaces));
  160. }
  161. public Characters createCharacters(String content)
  162. {
  163. return new CharactersImpl(location, content, false, false, false);
  164. }
  165. public Characters createCData(String content)
  166. {
  167. return new CharactersImpl(location, content, false, true, false);
  168. }
  169. public Characters createSpace(String content)
  170. {
  171. return new CharactersImpl(location, content, true, false, false);
  172. }
  173. public Characters createIgnorableSpace(String content)
  174. {
  175. return new CharactersImpl(location, content, true, false, true);
  176. }
  177. public StartDocument createStartDocument()
  178. {
  179. return new StartDocumentImpl(location, null, "UTF-8", "1.0",
  180. false, false, false);
  181. }
  182. public StartDocument createStartDocument(String encoding,
  183. String version,
  184. boolean standalone)
  185. {
  186. return new StartDocumentImpl(location, null, encoding, version,
  187. standalone, true, true);
  188. }
  189. public StartDocument createStartDocument(String encoding,
  190. String version)
  191. {
  192. return new StartDocumentImpl(location, null, encoding, version,
  193. false, false, true);
  194. }
  195. public StartDocument createStartDocument(String encoding)
  196. {
  197. return new StartDocumentImpl(location, null, encoding, "1.0",
  198. false, false, true);
  199. }
  200. public EndDocument createEndDocument()
  201. {
  202. return new EndDocumentImpl(location);
  203. }
  204. public EntityReference createEntityReference(String name,
  205. EntityDeclaration declaration)
  206. {
  207. return new EntityReferenceImpl(location, declaration, name);
  208. }
  209. public Comment createComment(String text)
  210. {
  211. return new CommentImpl(location, text);
  212. }
  213. public ProcessingInstruction createProcessingInstruction(String target,
  214. String data)
  215. {
  216. return new ProcessingInstructionImpl(location, target, data);
  217. }
  218. public DTD createDTD(String dtd)
  219. {
  220. return new DTDImpl(location, dtd, null,
  221. Collections.EMPTY_LIST,
  222. Collections.EMPTY_LIST);
  223. }
  224. LinkedList createLinkedList(Iterator i)
  225. {
  226. LinkedList ret = new LinkedList();
  227. while (i.hasNext())
  228. ret.add(i.next());
  229. return ret;
  230. }
  231. }