001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Adam Megacz
028: */
029:
030: package com.caucho.xml.stream;
031:
032: import com.caucho.xml.stream.events.*;
033:
034: import javax.xml.XMLConstants;
035: import javax.xml.namespace.QName;
036: import static javax.xml.stream.XMLStreamConstants.*;
037: import javax.xml.stream.XMLEventFactory;
038: import javax.xml.stream.XMLEventReader;
039: import javax.xml.stream.XMLStreamException;
040: import javax.xml.stream.XMLStreamReader;
041: import javax.xml.stream.events.*;
042: import javax.xml.stream.util.XMLEventAllocator;
043: import javax.xml.stream.util.XMLEventConsumer;
044:
045: import java.util.HashMap;
046:
047: public class XMLEventAllocatorImpl implements XMLEventAllocator {
048: private static final XMLEventFactory EVENT_FACTORY = XMLEventFactory
049: .newInstance();
050:
051: public XMLEvent allocate(XMLStreamReader reader)
052: throws XMLStreamException {
053: switch (reader.getEventType()) {
054: case ATTRIBUTE:
055: // won't happen: our stream reader does not return attributes
056: // independent of start elements/empty elements
057: break;
058:
059: case CDATA:
060: return EVENT_FACTORY.createCData(reader.getText());
061:
062: case CHARACTERS:
063: return EVENT_FACTORY.createCharacters(reader.getText());
064:
065: case COMMENT:
066: return EVENT_FACTORY.createComment(reader.getText());
067:
068: case DTD:
069: // XXX
070: break;
071:
072: case END_DOCUMENT:
073: return EVENT_FACTORY.createEndDocument();
074:
075: case END_ELEMENT:
076: // XXX namespaces?
077: return EVENT_FACTORY.createEndElement(reader.getName(),
078: null);
079:
080: case ENTITY_DECLARATION:
081: // XXX
082: break;
083:
084: case ENTITY_REFERENCE:
085: // XXX
086: break;
087:
088: case NAMESPACE:
089: // won't happen: our stream reader does not return attributes
090: // independent of start elements/empty elements
091: break;
092:
093: case NOTATION_DECLARATION:
094: // XXX
095: break;
096:
097: case PROCESSING_INSTRUCTION:
098: return EVENT_FACTORY.createProcessingInstruction(reader
099: .getPITarget(), reader.getPIData());
100:
101: case SPACE:
102: NamespaceContextImpl context = (NamespaceContextImpl) reader
103: .getNamespaceContext();
104:
105: if (context.getDepth() == 0)
106: return EVENT_FACTORY.createIgnorableSpace(reader
107: .getText());
108:
109: return EVENT_FACTORY.createSpace(reader.getText());
110:
111: case START_DOCUMENT:
112: boolean encodingSet = true;
113: String encoding = reader.getCharacterEncodingScheme();
114:
115: if (encoding == null) {
116: encoding = "utf-8"; // XXX
117: encodingSet = false;
118: }
119:
120: return new StartDocumentImpl(encodingSet, encoding,
121: null /* XXX: system id */, reader.getVersion(),
122: reader.isStandalone(), reader.standaloneSet());
123:
124: case START_ELEMENT:
125: HashMap<QName, Attribute> attributes = new HashMap<QName, Attribute>();
126:
127: for (int i = 0; i < reader.getAttributeCount(); i++) {
128: Attribute attribute = new AttributeImpl(reader
129: .getAttributeName(i), reader
130: .getAttributeValue(i));
131: attributes.put(reader.getAttributeName(i), attribute);
132: }
133:
134: HashMap<String, Namespace> namespaces = new HashMap<String, Namespace>();
135:
136: for (int i = 0; i < reader.getNamespaceCount(); i++) {
137: String prefix = reader.getNamespacePrefix(i);
138:
139: if (prefix == null)
140: prefix = XMLConstants.DEFAULT_NS_PREFIX;
141:
142: Namespace namespace = new NamespaceImpl(reader
143: .getNamespaceURI(i), prefix);
144:
145: namespaces.put(prefix, namespace);
146: }
147:
148: // bypass factory
149: return new StartElementImpl(reader.getName(), attributes,
150: namespaces, reader.getNamespaceContext());
151: }
152:
153: throw new XMLStreamException("Event type = "
154: + reader.getEventType());
155: }
156:
157: public void allocate(XMLStreamReader reader,
158: XMLEventConsumer consumer) throws XMLStreamException {
159: consumer.add(allocate(reader));
160: }
161:
162: public XMLEventAllocator newInstance() {
163: return new XMLEventAllocatorImpl();
164: }
165: }
|