1 |
14 |
jlechner |
/* XMLEventAllocatorImpl.java --
|
2 |
|
|
Copyright (C) 2005 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 |
|
|
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 |
|
|
return new EntityReferenceImpl(location, name, null, null, null, null);
|
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.getLocationURI();
|
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 |
|
|
len = reader.getAttributeCount();
|
164 |
|
|
List attributes = new LinkedList();
|
165 |
|
|
for (int i = 0; i < len; i++)
|
166 |
|
|
attributes.add(new AttributeImpl(location,
|
167 |
|
|
reader.getAttributeQName(i),
|
168 |
|
|
reader.getAttributeValue(i),
|
169 |
|
|
QName.valueOf(reader.getAttributeType(i)),
|
170 |
|
|
reader.isAttributeSpecified(i)));
|
171 |
|
|
return new StartElementImpl(location,
|
172 |
|
|
reader.getName(),
|
173 |
|
|
attributes, namespaces,
|
174 |
|
|
reader.getNamespaceContext());
|
175 |
|
|
default:
|
176 |
|
|
throw new XMLStreamException("Unknown event type: " + eventType);
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
public void allocate(XMLStreamReader reader, XMLEventConsumer consumer)
|
181 |
|
|
throws XMLStreamException
|
182 |
|
|
{
|
183 |
|
|
consumer.add(allocate(reader));
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
public XMLEventAllocator newInstance()
|
187 |
|
|
{
|
188 |
|
|
return new XMLEventAllocatorImpl();
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
protected boolean isWhitespace(String text)
|
192 |
|
|
{
|
193 |
|
|
int len = text.length();
|
194 |
|
|
for (int i = 0; i < len; i++)
|
195 |
|
|
{
|
196 |
|
|
char c = text.charAt(i);
|
197 |
|
|
if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d)
|
198 |
|
|
return false;
|
199 |
|
|
}
|
200 |
|
|
return true;
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
}
|
204 |
|
|
|