001: /*
002: * $Id: EventAllocator.java,v 1.1 2004/07/05 23:10:00 cniles Exp $
003: *
004: * Copyright (c) 2004, Christian Niles, unit12.net
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * * Neither the name of Christian Niles, Unit12, nor the names of its
018: * contributors may be used to endorse or promote products derived from
019: * this software without specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: * POSSIBILITY OF SUCH DAMAGE.
032: *
033: */
034: package javanet.staxutils.events;
035:
036: import java.util.ArrayList;
037: import java.util.Collections;
038: import java.util.List;
039:
040: import javax.xml.namespace.NamespaceContext;
041: import javax.xml.namespace.QName;
042: import javax.xml.stream.Location;
043: import javax.xml.stream.XMLStreamException;
044: import javax.xml.stream.XMLStreamReader;
045: import javax.xml.stream.events.Attribute;
046: import javax.xml.stream.events.Characters;
047: import javax.xml.stream.events.Comment;
048: import javax.xml.stream.events.DTD;
049: import javax.xml.stream.events.EndDocument;
050: import javax.xml.stream.events.EndElement;
051: import javax.xml.stream.events.EntityDeclaration;
052: import javax.xml.stream.events.EntityReference;
053: import javax.xml.stream.events.Namespace;
054: import javax.xml.stream.events.ProcessingInstruction;
055: import javax.xml.stream.events.StartDocument;
056: import javax.xml.stream.events.StartElement;
057: import javax.xml.stream.util.XMLEventAllocator;
058:
059: /**
060: * Allocates events within this package.
061: *
062: * @author Christian Niles
063: * @version $Revision: 1.1 $
064: */
065: public class EventAllocator extends BaseXMLEventAllocator {
066:
067: public XMLEventAllocator newInstance() {
068:
069: return new EventAllocator();
070:
071: }
072:
073: public StartElement allocateStartElement(XMLStreamReader reader)
074: throws XMLStreamException {
075:
076: Location location = createStableLocation(reader);
077: QName name = reader.getName();
078: List attributes = allocateAttributes(location, reader);
079: List namespaces = allocateNamespaces(location, reader);
080: NamespaceContext nsCtx = createStableNamespaceContext(reader);
081: QName schemaType = determineSchemaType(reader);
082:
083: return new StartElementEvent(name, attributes.iterator(),
084: namespaces.iterator(), nsCtx, location, schemaType);
085:
086: }
087:
088: public EndElement allocateEndElement(XMLStreamReader reader)
089: throws XMLStreamException {
090:
091: Location location = createStableLocation(reader);
092: QName name = reader.getName();
093: List namespaces = allocateNamespaces(location, reader);
094: QName schemaType = determineSchemaType(reader);
095:
096: return new EndElementEvent(name, namespaces.iterator(),
097: location, schemaType);
098:
099: }
100:
101: public List allocateAttributes(Location location,
102: XMLStreamReader reader) throws XMLStreamException {
103:
104: List attributes = null;
105: for (int i = 0, s = reader.getAttributeCount(); i < s; i++) {
106:
107: QName name = reader.getAttributeName(i);
108: String value = reader.getAttributeValue(i);
109: String dtdType = reader.getAttributeType(i);
110: boolean specified = reader.isAttributeSpecified(i);
111: QName schemaType = determineAttributeSchemaType(reader, i);
112:
113: Attribute attr = new AttributeEvent(name, value, specified,
114: dtdType, location, schemaType);
115: if (attributes == null) {
116:
117: attributes = new ArrayList();
118:
119: }
120: attributes.add(attr);
121:
122: }
123:
124: return (attributes != null) ? attributes
125: : Collections.EMPTY_LIST;
126:
127: }
128:
129: public List allocateNamespaces(Location location,
130: XMLStreamReader reader) throws XMLStreamException {
131:
132: List namespaces = null;
133: for (int i = 0, s = reader.getNamespaceCount(); i < s; i++) {
134:
135: String prefix = reader.getNamespacePrefix(i);
136: String nsURI = reader.getNamespaceURI(i);
137:
138: Namespace ns = new NamespaceEvent(prefix, nsURI, location);
139: if (namespaces == null) {
140:
141: namespaces = new ArrayList();
142:
143: }
144: namespaces.add(ns);
145:
146: }
147:
148: return (namespaces != null) ? namespaces
149: : Collections.EMPTY_LIST;
150:
151: }
152:
153: public Characters allocateCData(XMLStreamReader reader)
154: throws XMLStreamException {
155:
156: Location location = createStableLocation(reader);
157: String text = reader.getText();
158: QName schemaType = determineSchemaType(reader);
159: return new CDataEvent(text, location, schemaType);
160:
161: }
162:
163: public Characters allocateCharacters(XMLStreamReader reader)
164: throws XMLStreamException {
165:
166: Location location = createStableLocation(reader);
167: String text = reader.getText();
168: QName schemaType = determineSchemaType(reader);
169: return new CharactersEvent(text, location, schemaType);
170:
171: }
172:
173: public Characters allocateIgnorableSpace(XMLStreamReader reader)
174: throws XMLStreamException {
175:
176: Location location = createStableLocation(reader);
177: String text = reader.getText();
178: QName schemaType = determineSchemaType(reader);
179: return new IgnorableSpaceEvent(text, location, schemaType);
180:
181: }
182:
183: public Comment allocateComment(XMLStreamReader reader)
184: throws XMLStreamException {
185:
186: Location location = createStableLocation(reader);
187: String text = reader.getText();
188: return new CommentEvent(text, location);
189:
190: }
191:
192: public DTD allocateDTD(XMLStreamReader reader)
193: throws XMLStreamException {
194:
195: Location location = createStableLocation(reader);
196: List entities = (List) reader
197: .getProperty("javax.xml.stream.entities");
198: List notations = (List) reader
199: .getProperty("javax.xml.stream.notations");
200: String text = reader.getText();
201:
202: return new DTDEvent(text, entities, notations, location);
203:
204: }
205:
206: public StartDocument allocateStartDocument(XMLStreamReader reader)
207: throws XMLStreamException {
208:
209: Location location = createStableLocation(reader);
210: String encoding = reader.getCharacterEncodingScheme();
211: String version = reader.getVersion();
212: Boolean standalone = reader.standaloneSet() ? Boolean
213: .valueOf(reader.isStandalone()) : null;
214: QName schemaType = determineSchemaType(reader);
215:
216: return new StartDocumentEvent(encoding, standalone, version,
217: location, schemaType);
218:
219: }
220:
221: public EndDocument allocateEndDocument(XMLStreamReader reader)
222: throws XMLStreamException {
223:
224: Location location = createStableLocation(reader);
225: QName schemaType = determineSchemaType(reader);
226: return new EndDocumentEvent(location, schemaType);
227:
228: }
229:
230: public EntityReference allocateEntityReference(
231: XMLStreamReader reader) throws XMLStreamException {
232:
233: Location location = createStableLocation(reader);
234: String name = reader.getLocalName();
235: EntityDeclaration decl = determineEntityDeclaration(name,
236: reader);
237:
238: return new EntityReferenceEvent(name, decl, location);
239:
240: }
241:
242: public ProcessingInstruction allocateProcessingInstruction(
243: XMLStreamReader reader) throws XMLStreamException {
244:
245: Location location = createStableLocation(reader);
246: String target = reader.getPITarget();
247: String data = reader.getPIData();
248:
249: return new ProcessingInstructionEvent(target, data, location);
250:
251: }
252:
253: public QName determineSchemaType(XMLStreamReader reader) {
254:
255: // TODO look for xsi:type?
256: return null;
257:
258: }
259:
260: public QName determineAttributeSchemaType(XMLStreamReader reader,
261: int index) {
262:
263: return null;
264:
265: }
266:
267: public EntityDeclaration determineEntityDeclaration(String name,
268: XMLStreamReader reader) {
269:
270: return new EntityDeclarationEvent(name, reader.getText(), null);
271:
272: }
273:
274: public Location createStableLocation(XMLStreamReader reader) {
275:
276: // FIXME assume location is already stable?
277: return reader.getLocation();
278:
279: }
280:
281: public NamespaceContext createStableNamespaceContext(
282: XMLStreamReader reader) {
283:
284: // FIXME assume context is already stable
285: return reader.getNamespaceContext();
286:
287: }
288:
289: }
|