286N/A/*
286N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage com.sun.xml.internal.stream.events;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.PropertyManager;
286N/Aimport java.util.List;
286N/Aimport javax.xml.stream.util.XMLEventAllocator;
286N/Aimport javax.xml.stream.*;
286N/Aimport javax.xml.stream.events.*;
286N/Aimport javax.xml.XMLConstants;
286N/Aimport javax.xml.namespace.QName;
286N/Aimport com.sun.org.apache.xerces.internal.util.NamespaceContextWrapper;
286N/Aimport com.sun.org.apache.xerces.internal.util.NamespaceSupport;
286N/A
286N/A/**
286N/A * Implementation of XMLEvent Allocator.
286N/A * @author Neeraj.bajaj@sun.com, k.venugopal@sun.com
286N/A */
286N/Apublic class XMLEventAllocatorImpl implements XMLEventAllocator {
286N/A
286N/A /** Creates a new instance of XMLEventAllocator */
286N/A public XMLEventAllocatorImpl() {
286N/A }
286N/A
286N/A public javax.xml.stream.events.XMLEvent allocate(javax.xml.stream.XMLStreamReader xMLStreamReader) throws javax.xml.stream.XMLStreamException {
286N/A if(xMLStreamReader == null )
286N/A throw new XMLStreamException("Reader cannot be null");
286N/A // allocate is not supposed to change the state of the reader so we shouldn't be calling next.
286N/A // return getNextEvent(xMLStreamReader);
286N/A return getXMLEvent(xMLStreamReader);
286N/A }
286N/A
286N/A public void allocate(javax.xml.stream.XMLStreamReader xMLStreamReader, javax.xml.stream.util.XMLEventConsumer xMLEventConsumer) throws javax.xml.stream.XMLStreamException {
286N/A XMLEvent currentEvent = getXMLEvent(xMLStreamReader);
286N/A if(currentEvent != null )
286N/A xMLEventConsumer.add(currentEvent);
286N/A
286N/A return;
286N/A }
286N/A
286N/A public javax.xml.stream.util.XMLEventAllocator newInstance() {
286N/A return new XMLEventAllocatorImpl();
286N/A }
286N/A
286N/A //REVISIT: shouldn't we be using XMLEventFactory to create events.
286N/A XMLEvent getXMLEvent(XMLStreamReader streamReader){
286N/A XMLEvent event = null;
286N/A //returns the current event
286N/A int eventType = streamReader.getEventType();
286N/A switch(eventType){
286N/A
286N/A case XMLEvent.START_ELEMENT:{
286N/A StartElementEvent startElementEvent = new StartElementEvent(getQName(streamReader));
286N/A fillAttributes(startElementEvent,streamReader);
286N/A //we might have different XMLStreamReader so check every time for the namespace aware property
286N/A //we should be setting namespace related values only when isNamespaceAware is 'true'
286N/A if( ((Boolean)streamReader.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)).booleanValue() ){
286N/A fillNamespaceAttributes(startElementEvent, streamReader);
286N/A setNamespaceContext(startElementEvent,streamReader);
286N/A }
286N/A
286N/A startElementEvent.setLocation(streamReader.getLocation());
286N/A event = startElementEvent ;
286N/A break;
286N/A }
286N/A case XMLEvent.END_ELEMENT:{
286N/A EndElementEvent endElementEvent = new EndElementEvent(getQName(streamReader));
286N/A endElementEvent.setLocation(streamReader.getLocation());
286N/A
286N/A if( ((Boolean)streamReader.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)).booleanValue() ){
286N/A fillNamespaceAttributes(endElementEvent,streamReader);
286N/A }
286N/A event = endElementEvent ;
286N/A break;
286N/A }
286N/A case XMLEvent.PROCESSING_INSTRUCTION:{
286N/A ProcessingInstructionEvent piEvent = new ProcessingInstructionEvent(streamReader.getPITarget(),streamReader.getPIData());
286N/A piEvent.setLocation(streamReader.getLocation());
286N/A event = piEvent ;
286N/A break;
286N/A }
286N/A case XMLEvent.CHARACTERS:{
286N/A CharacterEvent cDataEvent = new CharacterEvent(streamReader.getText());
286N/A cDataEvent.setLocation(streamReader.getLocation());
286N/A event = cDataEvent ;
286N/A break;
286N/A }
286N/A case XMLEvent.COMMENT:{
286N/A CommentEvent commentEvent = new CommentEvent(streamReader.getText());
286N/A commentEvent.setLocation(streamReader.getLocation());
286N/A event = commentEvent ;
286N/A break;
286N/A }
286N/A case XMLEvent.START_DOCUMENT:{
286N/A StartDocumentEvent sdEvent = new StartDocumentEvent();
286N/A sdEvent.setVersion(streamReader.getVersion());
286N/A sdEvent.setEncoding(streamReader.getEncoding());
286N/A if(streamReader.getCharacterEncodingScheme() != null){
286N/A sdEvent.setDeclaredEncoding(true);
286N/A }else{
286N/A sdEvent.setDeclaredEncoding(false);
286N/A }
286N/A sdEvent.setStandalone(streamReader.isStandalone());
286N/A sdEvent.setLocation(streamReader.getLocation());
286N/A event = sdEvent ;
286N/A break;
286N/A }
286N/A case XMLEvent.END_DOCUMENT:{
286N/A EndDocumentEvent endDocumentEvent = new EndDocumentEvent() ;
286N/A endDocumentEvent.setLocation(streamReader.getLocation());
286N/A event = endDocumentEvent ;
286N/A break;
286N/A }
286N/A case XMLEvent.ENTITY_REFERENCE:{
286N/A EntityReferenceEvent entityEvent = new EntityReferenceEvent(streamReader.getLocalName(), new EntityDeclarationImpl(streamReader.getLocalName(),streamReader.getText()));
286N/A entityEvent.setLocation(streamReader.getLocation());
286N/A event = entityEvent;
286N/A break;
286N/A
286N/A }
286N/A case XMLEvent.ATTRIBUTE:{
286N/A event = null ;
286N/A break;
286N/A }
286N/A case XMLEvent.DTD:{
286N/A DTDEvent dtdEvent = new DTDEvent(streamReader.getText());
286N/A dtdEvent.setLocation(streamReader.getLocation());
286N/A List entities = (List)streamReader.getProperty(PropertyManager.STAX_ENTITIES);
286N/A if (entities != null && entities.size() != 0) dtdEvent.setEntities(entities);
286N/A List notations = (List)streamReader.getProperty(PropertyManager.STAX_NOTATIONS);
286N/A if (notations != null && notations.size() != 0) dtdEvent.setNotations(notations);
286N/A event = dtdEvent;
286N/A break;
286N/A }
286N/A case XMLEvent.CDATA:{
286N/A CharacterEvent cDataEvent = new CharacterEvent(streamReader.getText(),true);
286N/A cDataEvent.setLocation(streamReader.getLocation());
286N/A event = cDataEvent ;
286N/A break;
286N/A }
286N/A case XMLEvent.SPACE:{
286N/A CharacterEvent spaceEvent = new CharacterEvent(streamReader.getText(),false,true);
286N/A spaceEvent.setLocation(streamReader.getLocation());
286N/A event = spaceEvent ;
286N/A break;
286N/A }
286N/A }
286N/A return event ;
286N/A }
286N/A
286N/A //this function is not used..
286N/A protected XMLEvent getNextEvent(XMLStreamReader streamReader) throws XMLStreamException{
286N/A //advance the reader to next event.
286N/A streamReader.next();
286N/A return getXMLEvent(streamReader);
286N/A }
286N/A
286N/A protected void fillAttributes(StartElementEvent event,XMLStreamReader xmlr){
286N/A
286N/A int len = xmlr.getAttributeCount();
286N/A QName qname = null;
286N/A AttributeImpl attr = null;
286N/A NamespaceImpl nattr = null;
286N/A for(int i=0; i<len ;i++){
286N/A qname = xmlr.getAttributeName(i);
286N/A //this method doesn't include namespace declarations
286N/A //so we can be sure that there wont be any namespace declaration as part of this function call
286N/A //we can avoid this check - nb.
286N/A /**
286N/A * prefix = qname.getPrefix();
286N/A * localpart = qname.getLocalPart();
286N/A * if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE) ) {
286N/A * attr = new NamespaceImpl(localpart,xmlr.getAttributeValue(i));
286N/A * }else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)){
286N/A * attr = new NamespaceImpl(xmlr.getAttributeValue(i));
286N/A * }else{
286N/A * attr = new AttributeImpl();
286N/A * attr.setName(qname);
286N/A * }
286N/A **/
286N/A attr = new AttributeImpl();
286N/A attr.setName(qname);
286N/A attr.setAttributeType(xmlr.getAttributeType(i));
286N/A attr.setSpecified(xmlr.isAttributeSpecified(i));
286N/A attr.setValue(xmlr.getAttributeValue(i));
286N/A event.addAttribute(attr);
286N/A }
286N/A }
286N/A
286N/A protected void fillNamespaceAttributes(StartElementEvent event,XMLStreamReader xmlr){
286N/A int count = xmlr.getNamespaceCount();
286N/A String uri = null;
286N/A String prefix = null;
286N/A NamespaceImpl attr = null;
286N/A for(int i=0;i< count;i++){
286N/A uri = xmlr.getNamespaceURI(i);
286N/A prefix = xmlr.getNamespacePrefix(i);
286N/A if(prefix == null){
286N/A prefix = XMLConstants.DEFAULT_NS_PREFIX;
286N/A }
286N/A attr = new NamespaceImpl(prefix,uri);
286N/A event.addNamespaceAttribute(attr);
286N/A }
286N/A }
286N/A
286N/A protected void fillNamespaceAttributes(EndElementEvent event,XMLStreamReader xmlr){
286N/A int count = xmlr.getNamespaceCount();
286N/A String uri = null;
286N/A String prefix = null;
286N/A NamespaceImpl attr = null;
286N/A for(int i=0;i< count;i++){
286N/A uri = xmlr.getNamespaceURI(i);
286N/A prefix = xmlr.getNamespacePrefix(i);
286N/A if(prefix == null){
286N/A prefix = XMLConstants.DEFAULT_NS_PREFIX;
286N/A }
286N/A attr = new NamespaceImpl(prefix,uri);
286N/A event.addNamespace(attr);
286N/A }
286N/A }
286N/A
286N/A //Revisit : Creating a new Namespacecontext for now.
286N/A //see if we can do better job.
286N/A private void setNamespaceContext(StartElementEvent event , XMLStreamReader xmlr){
286N/A NamespaceContextWrapper contextWrapper =(NamespaceContextWrapper) xmlr.getNamespaceContext();
286N/A NamespaceSupport ns = new NamespaceSupport(contextWrapper.getNamespaceContext());
286N/A event.setNamespaceContext(new NamespaceContextWrapper(ns));
286N/A }
286N/A
286N/A private QName getQName(XMLStreamReader xmlr) {
286N/A return new QName(xmlr.getNamespaceURI(), xmlr.getLocalName(),
286N/A xmlr.getPrefix());
286N/A }
286N/A}