1 |
769 |
jeremybenn |
/* XMLEventAllocatorImpl.java --
|
2 |
|
|
Copyright (C) 2005,2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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.
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
package gnu.xml.stream;
|
39 |
|
|
|
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;
|
53 |
|
|
|
54 |
|
|
/**
|
55 |
|
|
* Allocator for creating XML events based on a reader state.
|
56 |
|
|
*
|
57 |
|
|
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
|
58 |
|
|
*/
|
59 |
|
|
public class XMLEventAllocatorImpl
|
60 |
|
|
implements XMLEventAllocator
|
61 |
|
|
{
|
62 |
|
|
|
63 |
|
|
protected Map entityDeclarations;
|
64 |
|
|
|
65 |
|
|
protected XMLEventAllocatorImpl()
|
66 |
|
|
{
|
67 |
|
|
entityDeclarations = new HashMap();
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
public XMLEvent allocate(XMLStreamReader reader)
|
71 |
|
|
throws XMLStreamException
|
72 |
|
|
{
|
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)
|
81 |
|
|
{
|
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 |
|
|
false));
|
115 |
|
|
return new EndElementImpl(location,
|
116 |
|
|
reader.getName(),
|
117 |
|
|
namespaces);
|
118 |
|
|
case XMLStreamConstants.ENTITY_REFERENCE:
|
119 |
|
|
String name = reader.getLocalName();
|
120 |
|
|
EntityDeclaration decl =
|
121 |
|
|
(EntityDeclaration) entityDeclarations.get(name);
|
122 |
|
|
return new EntityReferenceImpl(location, decl, name);
|
123 |
|
|
case XMLStreamConstants.PROCESSING_INSTRUCTION:
|
124 |
|
|
return new ProcessingInstructionImpl(location,
|
125 |
|
|
reader.getPITarget(),
|
126 |
|
|
reader.getPIData());
|
127 |
|
|
case XMLStreamConstants.SPACE:
|
128 |
|
|
text = reader.getText();
|
129 |
|
|
whitespace = true;
|
130 |
|
|
// TODO ignorableWhitespace
|
131 |
|
|
ignorableWhitespace = whitespace && false;
|
132 |
|
|
return new CharactersImpl(location, text,
|
133 |
|
|
whitespace, false, ignorableWhitespace);
|
134 |
|
|
case XMLStreamConstants.START_DOCUMENT:
|
135 |
|
|
String systemId = location.getSystemId();
|
136 |
|
|
String encoding = reader.getCharacterEncodingScheme();
|
137 |
|
|
boolean encodingDeclared = encoding != null;
|
138 |
|
|
if (encoding == null)
|
139 |
|
|
{
|
140 |
|
|
encoding = reader.getEncoding();
|
141 |
|
|
if (encoding == null)
|
142 |
|
|
encoding = "UTF-8";
|
143 |
|
|
}
|
144 |
|
|
String xmlVersion = reader.getVersion();
|
145 |
|
|
if (xmlVersion == null)
|
146 |
|
|
xmlVersion = "1.0";
|
147 |
|
|
boolean xmlStandalone = reader.isStandalone();
|
148 |
|
|
boolean standaloneDeclared = reader.standaloneSet();
|
149 |
|
|
return new StartDocumentImpl(location,
|
150 |
|
|
systemId,
|
151 |
|
|
encoding,
|
152 |
|
|
xmlVersion,
|
153 |
|
|
xmlStandalone,
|
154 |
|
|
standaloneDeclared,
|
155 |
|
|
encodingDeclared);
|
156 |
|
|
case XMLStreamConstants.START_ELEMENT:
|
157 |
|
|
len = reader.getNamespaceCount();
|
158 |
|
|
namespaces = new LinkedList();
|
159 |
|
|
for (int i = 0; i < len; i++)
|
160 |
|
|
namespaces.add(new NamespaceImpl(location,
|
161 |
|
|
reader.getNamespacePrefix(i),
|
162 |
|
|
reader.getNamespaceURI(i),
|
163 |
|
|
false));
|
164 |
|
|
len = reader.getAttributeCount();
|
165 |
|
|
List attributes = new LinkedList();
|
166 |
|
|
for (int i = 0; i < len; i++)
|
167 |
|
|
attributes.add(new AttributeImpl(location,
|
168 |
|
|
reader.getAttributeName(i),
|
169 |
|
|
reader.getAttributeValue(i),
|
170 |
|
|
reader.getAttributeType(i),
|
171 |
|
|
reader.isAttributeSpecified(i)));
|
172 |
|
|
return new StartElementImpl(location,
|
173 |
|
|
reader.getName(),
|
174 |
|
|
attributes, namespaces,
|
175 |
|
|
reader.getNamespaceContext());
|
176 |
|
|
default:
|
177 |
|
|
throw new XMLStreamException("Unknown event type: " + eventType);
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
public void allocate(XMLStreamReader reader, XMLEventConsumer consumer)
|
182 |
|
|
throws XMLStreamException
|
183 |
|
|
{
|
184 |
|
|
consumer.add(allocate(reader));
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
public XMLEventAllocator newInstance()
|
188 |
|
|
{
|
189 |
|
|
return new XMLEventAllocatorImpl();
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
protected boolean isWhitespace(String text)
|
193 |
|
|
{
|
194 |
|
|
int len = text.length();
|
195 |
|
|
for (int i = 0; i < len; i++)
|
196 |
|
|
{
|
197 |
|
|
char c = text.charAt(i);
|
198 |
|
|
if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d)
|
199 |
|
|
return false;
|
200 |
|
|
}
|
201 |
|
|
return true;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
}
|