XMLEventAllocatorImpl.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* XMLEventAllocatorImpl.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.HashMap;
  33. import java.util.LinkedList;
  34. import java.util.List;
  35. import java.util.Map;
  36. import javax.xml.namespace.QName;
  37. import javax.xml.stream.Location;
  38. import javax.xml.stream.XMLStreamConstants;
  39. import javax.xml.stream.XMLStreamException;
  40. import javax.xml.stream.XMLStreamReader;
  41. import javax.xml.stream.events.EntityDeclaration;
  42. import javax.xml.stream.events.XMLEvent;
  43. import javax.xml.stream.util.XMLEventAllocator;
  44. import javax.xml.stream.util.XMLEventConsumer;
  45. /**
  46. * Allocator for creating XML events based on a reader state.
  47. *
  48. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  49. */
  50. public class XMLEventAllocatorImpl
  51. implements XMLEventAllocator
  52. {
  53. protected Map entityDeclarations;
  54. protected XMLEventAllocatorImpl()
  55. {
  56. entityDeclarations = new HashMap();
  57. }
  58. public XMLEvent allocate(XMLStreamReader reader)
  59. throws XMLStreamException
  60. {
  61. String text;
  62. boolean whitespace;
  63. boolean ignorableWhitespace;
  64. int len;
  65. List namespaces;
  66. int eventType = reader.getEventType();
  67. Location location = reader.getLocation();
  68. switch (eventType)
  69. {
  70. case XMLStreamConstants.CDATA:
  71. text = reader.getText();
  72. whitespace = isWhitespace(text);
  73. // TODO ignorableWhitespace
  74. ignorableWhitespace = whitespace && false;
  75. return new CharactersImpl(location, text,
  76. whitespace, true, ignorableWhitespace);
  77. case XMLStreamConstants.CHARACTERS:
  78. text = reader.getText();
  79. whitespace = false;
  80. // TODO ignorableWhitespace
  81. ignorableWhitespace = whitespace && false;
  82. return new CharactersImpl(location, text,
  83. whitespace, false, ignorableWhitespace);
  84. case XMLStreamConstants.COMMENT:
  85. text = reader.getText();
  86. return new CommentImpl(location, text);
  87. case XMLStreamConstants.DTD:
  88. text = reader.getText();
  89. List notations = new LinkedList();
  90. List entities = new LinkedList();
  91. // TODO readDTDBody(notations, entities);
  92. return new DTDImpl(location, text, null, notations, entities);
  93. case XMLStreamConstants.END_DOCUMENT:
  94. return new EndDocumentImpl(location);
  95. case XMLStreamConstants.END_ELEMENT:
  96. len = reader.getNamespaceCount();
  97. namespaces = new LinkedList();
  98. for (int i = 0; i < len; i++)
  99. namespaces.add(new NamespaceImpl(location,
  100. reader.getNamespacePrefix(i),
  101. reader.getNamespaceURI(i),
  102. false));
  103. return new EndElementImpl(location,
  104. reader.getName(),
  105. namespaces);
  106. case XMLStreamConstants.ENTITY_REFERENCE:
  107. String name = reader.getLocalName();
  108. EntityDeclaration decl =
  109. (EntityDeclaration) entityDeclarations.get(name);
  110. return new EntityReferenceImpl(location, decl, name);
  111. case XMLStreamConstants.PROCESSING_INSTRUCTION:
  112. return new ProcessingInstructionImpl(location,
  113. reader.getPITarget(),
  114. reader.getPIData());
  115. case XMLStreamConstants.SPACE:
  116. text = reader.getText();
  117. whitespace = true;
  118. // TODO ignorableWhitespace
  119. ignorableWhitespace = whitespace && false;
  120. return new CharactersImpl(location, text,
  121. whitespace, false, ignorableWhitespace);
  122. case XMLStreamConstants.START_DOCUMENT:
  123. String systemId = location.getSystemId();
  124. String encoding = reader.getCharacterEncodingScheme();
  125. boolean encodingDeclared = encoding != null;
  126. if (encoding == null)
  127. {
  128. encoding = reader.getEncoding();
  129. if (encoding == null)
  130. encoding = "UTF-8";
  131. }
  132. String xmlVersion = reader.getVersion();
  133. if (xmlVersion == null)
  134. xmlVersion = "1.0";
  135. boolean xmlStandalone = reader.isStandalone();
  136. boolean standaloneDeclared = reader.standaloneSet();
  137. return new StartDocumentImpl(location,
  138. systemId,
  139. encoding,
  140. xmlVersion,
  141. xmlStandalone,
  142. standaloneDeclared,
  143. encodingDeclared);
  144. case XMLStreamConstants.START_ELEMENT:
  145. len = reader.getNamespaceCount();
  146. namespaces = new LinkedList();
  147. for (int i = 0; i < len; i++)
  148. namespaces.add(new NamespaceImpl(location,
  149. reader.getNamespacePrefix(i),
  150. reader.getNamespaceURI(i),
  151. false));
  152. len = reader.getAttributeCount();
  153. List attributes = new LinkedList();
  154. for (int i = 0; i < len; i++)
  155. attributes.add(new AttributeImpl(location,
  156. reader.getAttributeName(i),
  157. reader.getAttributeValue(i),
  158. reader.getAttributeType(i),
  159. reader.isAttributeSpecified(i)));
  160. return new StartElementImpl(location,
  161. reader.getName(),
  162. attributes, namespaces,
  163. reader.getNamespaceContext());
  164. default:
  165. throw new XMLStreamException("Unknown event type: " + eventType);
  166. }
  167. }
  168. public void allocate(XMLStreamReader reader, XMLEventConsumer consumer)
  169. throws XMLStreamException
  170. {
  171. consumer.add(allocate(reader));
  172. }
  173. public XMLEventAllocator newInstance()
  174. {
  175. return new XMLEventAllocatorImpl();
  176. }
  177. protected boolean isWhitespace(String text)
  178. {
  179. int len = text.length();
  180. for (int i = 0; i < len; i++)
  181. {
  182. char c = text.charAt(i);
  183. if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d)
  184. return false;
  185. }
  186. return true;
  187. }
  188. }