Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / gnu / xml / stream / XMLEventAllocatorImpl.java
blobfb1e4c28c5839a4f7b3e92cb553b9f5bc4f04c9a
1 /* XMLEventAllocatorImpl.java --
2 Copyright (C) 2005,2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.stream;
40 import java.util.HashMap;
41 import java.util.LinkedList;
42 import java.util.List;
43 import java.util.Map;
44 import javax.xml.namespace.QName;
45 import javax.xml.stream.Location;
46 import javax.xml.stream.XMLStreamConstants;
47 import javax.xml.stream.XMLStreamException;
48 import javax.xml.stream.XMLStreamReader;
49 import javax.xml.stream.events.EntityDeclaration;
50 import javax.xml.stream.events.XMLEvent;
51 import javax.xml.stream.util.XMLEventAllocator;
52 import javax.xml.stream.util.XMLEventConsumer;
54 /**
55 * Allocator for creating XML events based on a reader state.
57 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
59 public class XMLEventAllocatorImpl
60 implements XMLEventAllocator
63 protected Map entityDeclarations;
65 protected XMLEventAllocatorImpl()
67 entityDeclarations = new HashMap();
70 public XMLEvent allocate(XMLStreamReader reader)
71 throws XMLStreamException
73 String text;
74 boolean whitespace;
75 boolean ignorableWhitespace;
76 int len;
77 List namespaces;
78 int eventType = reader.getEventType();
79 Location location = reader.getLocation();
80 switch (eventType)
82 case XMLStreamConstants.CDATA:
83 text = reader.getText();
84 whitespace = isWhitespace(text);
85 // TODO ignorableWhitespace
86 ignorableWhitespace = whitespace && false;
87 return new CharactersImpl(location, text,
88 whitespace, true, ignorableWhitespace);
89 case XMLStreamConstants.CHARACTERS:
90 text = reader.getText();
91 whitespace = false;
92 // TODO ignorableWhitespace
93 ignorableWhitespace = whitespace && false;
94 return new CharactersImpl(location, text,
95 whitespace, false, ignorableWhitespace);
96 case XMLStreamConstants.COMMENT:
97 text = reader.getText();
98 return new CommentImpl(location, text);
99 case XMLStreamConstants.DTD:
100 text = reader.getText();
101 List notations = new LinkedList();
102 List entities = new LinkedList();
103 // TODO readDTDBody(notations, entities);
104 return new DTDImpl(location, text, null, notations, entities);
105 case XMLStreamConstants.END_DOCUMENT:
106 return new EndDocumentImpl(location);
107 case XMLStreamConstants.END_ELEMENT:
108 len = reader.getNamespaceCount();
109 namespaces = new LinkedList();
110 for (int i = 0; i < len; i++)
111 namespaces.add(new NamespaceImpl(location,
112 reader.getNamespacePrefix(i),
113 reader.getNamespaceURI(i)));
114 return new EndElementImpl(location,
115 reader.getName(),
116 namespaces);
117 case XMLStreamConstants.ENTITY_REFERENCE:
118 String name = reader.getLocalName();
119 EntityDeclaration decl =
120 (EntityDeclaration) entityDeclarations.get(name);
121 return new EntityReferenceImpl(location, decl, name);
122 case XMLStreamConstants.PROCESSING_INSTRUCTION:
123 return new ProcessingInstructionImpl(location,
124 reader.getPITarget(),
125 reader.getPIData());
126 case XMLStreamConstants.SPACE:
127 text = reader.getText();
128 whitespace = true;
129 // TODO ignorableWhitespace
130 ignorableWhitespace = whitespace && false;
131 return new CharactersImpl(location, text,
132 whitespace, false, ignorableWhitespace);
133 case XMLStreamConstants.START_DOCUMENT:
134 String systemId = location.getSystemId();
135 String encoding = reader.getCharacterEncodingScheme();
136 boolean encodingDeclared = encoding != null;
137 if (encoding == null)
139 encoding = reader.getEncoding();
140 if (encoding == null)
141 encoding = "UTF-8";
143 String xmlVersion = reader.getVersion();
144 if (xmlVersion == null)
145 xmlVersion = "1.0";
146 boolean xmlStandalone = reader.isStandalone();
147 boolean standaloneDeclared = reader.standaloneSet();
148 return new StartDocumentImpl(location,
149 systemId,
150 encoding,
151 xmlVersion,
152 xmlStandalone,
153 standaloneDeclared,
154 encodingDeclared);
155 case XMLStreamConstants.START_ELEMENT:
156 len = reader.getNamespaceCount();
157 namespaces = new LinkedList();
158 for (int i = 0; i < len; i++)
159 namespaces.add(new NamespaceImpl(location,
160 reader.getNamespacePrefix(i),
161 reader.getNamespaceURI(i)));
162 len = reader.getAttributeCount();
163 List attributes = new LinkedList();
164 for (int i = 0; i < len; i++)
165 attributes.add(new AttributeImpl(location,
166 reader.getAttributeName(i),
167 reader.getAttributeValue(i),
168 reader.getAttributeType(i),
169 reader.isAttributeSpecified(i)));
170 return new StartElementImpl(location,
171 reader.getName(),
172 attributes, namespaces,
173 reader.getNamespaceContext());
174 default:
175 throw new XMLStreamException("Unknown event type: " + eventType);
179 public void allocate(XMLStreamReader reader, XMLEventConsumer consumer)
180 throws XMLStreamException
182 consumer.add(allocate(reader));
185 public XMLEventAllocator newInstance()
187 return new XMLEventAllocatorImpl();
190 protected boolean isWhitespace(String text)
192 int len = text.length();
193 for (int i = 0; i < len; i++)
195 char c = text.charAt(i);
196 if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d)
197 return false;
199 return true;