aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.fastinfoset.stax.events; aoqi@0: aoqi@0: import javax.xml.stream.XMLEventFactory; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: import javax.xml.stream.XMLStreamReader; aoqi@0: import javax.xml.stream.events.Namespace; aoqi@0: import javax.xml.stream.events.XMLEvent; aoqi@0: import javax.xml.stream.util.XMLEventAllocator; aoqi@0: import javax.xml.stream.util.XMLEventConsumer; aoqi@0: aoqi@0: import com.sun.xml.internal.fastinfoset.CommonResourceBundle; aoqi@0: aoqi@0: /** aoqi@0: * allows a user to register a way to allocate events given an XMLStreamReader. aoqi@0: * The XMLEventAllocator can be set on an XMLInputFactory aoqi@0: * using the property "javax.xml.stream.allocator" aoqi@0: * aoqi@0: * This base class uses EventFactory to create events as recommended in the JavaDoc of XMLEventAllocator. aoqi@0: * However, creating new object per each event reduces performance. The implementation of aoqi@0: * EventReader therefore will set the Allocator to StAXEventAllocator which implements the aoqi@0: * Allocate methods without creating new objects. aoqi@0: * aoqi@0: * The spec for the first Allocate method states that it must NOT modify the state of the Reader aoqi@0: * while the second MAY. For consistency, both Allocate methods in this implementation will aoqi@0: * NOT modify the state. aoqi@0: * aoqi@0: */ aoqi@0: public class StAXEventAllocatorBase implements XMLEventAllocator { aoqi@0: XMLEventFactory factory; aoqi@0: aoqi@0: /** Creates a new instance of XMLEventAllocator */ aoqi@0: public StAXEventAllocatorBase() { aoqi@0: if (System.getProperty("javax.xml.stream.XMLEventFactory")==null) { aoqi@0: System.setProperty("javax.xml.stream.XMLEventFactory", aoqi@0: "com.sun.xml.internal.fastinfoset.stax.factory.StAXEventFactory"); aoqi@0: } aoqi@0: factory = XMLEventFactory.newInstance(); aoqi@0: } aoqi@0: aoqi@0: // ---------------------methods defined by XMLEventAllocator-----------------// aoqi@0: aoqi@0: /** aoqi@0: * This method creates an instance of the XMLEventAllocator. This aoqi@0: * allows the XMLInputFactory to allocate a new instance per reader. aoqi@0: */ aoqi@0: public XMLEventAllocator newInstance() { aoqi@0: return new StAXEventAllocatorBase(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This method allocates an event given the current state of the XMLStreamReader. aoqi@0: * If this XMLEventAllocator does not have a one-to-one mapping between reader state aoqi@0: * and events this method will return null. aoqi@0: * @param streamReader The XMLStreamReader to allocate from aoqi@0: * @return the event corresponding to the current reader state aoqi@0: */ aoqi@0: public XMLEvent allocate(XMLStreamReader streamReader) throws XMLStreamException { aoqi@0: if(streamReader == null ) aoqi@0: throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.nullReader")); aoqi@0: return getXMLEvent(streamReader); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This method allocates an event or set of events given the current state of aoqi@0: * the XMLStreamReader and adds the event or set of events to the consumer that aoqi@0: * was passed in. aoqi@0: * @param streamReader The XMLStreamReader to allocate from aoqi@0: * @param consumer The XMLEventConsumer to add to. aoqi@0: */ aoqi@0: public void allocate(XMLStreamReader streamReader, XMLEventConsumer consumer) throws XMLStreamException { aoqi@0: consumer.add(getXMLEvent(streamReader)); aoqi@0: aoqi@0: } aoqi@0: // ---------------------end of methods defined by XMLEventAllocator-----------------// aoqi@0: aoqi@0: aoqi@0: XMLEvent getXMLEvent(XMLStreamReader reader){ aoqi@0: XMLEvent event = null; aoqi@0: //returns the current event aoqi@0: int eventType = reader.getEventType(); aoqi@0: //this needs to be set before creating events aoqi@0: factory.setLocation(reader.getLocation()); aoqi@0: switch(eventType){ aoqi@0: aoqi@0: case XMLEvent.START_ELEMENT: aoqi@0: { aoqi@0: StartElementEvent startElement = (StartElementEvent)factory.createStartElement(reader.getPrefix(), aoqi@0: reader.getNamespaceURI(), reader.getLocalName()); aoqi@0: aoqi@0: addAttributes(startElement,reader); aoqi@0: addNamespaces(startElement, reader); aoqi@0: //need to fix it along with the Reader aoqi@0: //setNamespaceContext(startElement,reader); aoqi@0: event = startElement; aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.END_ELEMENT: aoqi@0: { aoqi@0: EndElementEvent endElement = (EndElementEvent)factory.createEndElement( aoqi@0: reader.getPrefix(), reader.getNamespaceURI(), reader.getLocalName()); aoqi@0: addNamespaces(endElement,reader); aoqi@0: event = endElement ; aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.PROCESSING_INSTRUCTION: aoqi@0: { aoqi@0: event = factory.createProcessingInstruction(reader.getPITarget(),reader.getPIData()); aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.CHARACTERS: aoqi@0: { aoqi@0: if (reader.isWhiteSpace()) aoqi@0: event = factory.createSpace(reader.getText()); aoqi@0: else aoqi@0: event = factory.createCharacters(reader.getText()); aoqi@0: aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.COMMENT: aoqi@0: { aoqi@0: event = factory.createComment(reader.getText()); aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.START_DOCUMENT: aoqi@0: { aoqi@0: StartDocumentEvent docEvent = (StartDocumentEvent)factory.createStartDocument( aoqi@0: reader.getVersion(), reader.getEncoding(), reader.isStandalone()); aoqi@0: if(reader.getCharacterEncodingScheme() != null){ aoqi@0: docEvent.setDeclaredEncoding(true); aoqi@0: }else{ aoqi@0: docEvent.setDeclaredEncoding(false); aoqi@0: } aoqi@0: event = docEvent ; aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.END_DOCUMENT:{ aoqi@0: EndDocumentEvent endDocumentEvent = new EndDocumentEvent() ; aoqi@0: event = endDocumentEvent ; aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.ENTITY_REFERENCE:{ aoqi@0: event = factory.createEntityReference(reader.getLocalName(), aoqi@0: new EntityDeclarationImpl(reader.getLocalName(),reader.getText())); aoqi@0: break; aoqi@0: aoqi@0: } aoqi@0: case XMLEvent.ATTRIBUTE:{ aoqi@0: event = null ; aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.DTD:{ aoqi@0: event = factory.createDTD(reader.getText()); aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.CDATA:{ aoqi@0: event = factory.createCData(reader.getText()); aoqi@0: break; aoqi@0: } aoqi@0: case XMLEvent.SPACE:{ aoqi@0: event = factory.createSpace(reader.getText()); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: return event ; aoqi@0: } aoqi@0: aoqi@0: //use event.addAttribute instead of addAttributes to avoid creating another list aoqi@0: protected void addAttributes(StartElementEvent event,XMLStreamReader streamReader){ aoqi@0: AttributeBase attr = null; aoqi@0: for(int i=0; i