KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > events > StAXEventAllocatorBase


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39 package com.sun.xml.fastinfoset.stax.events;
40
41 import javax.xml.stream.XMLStreamConstants;
42 import javax.xml.stream.XMLEventFactory;
43 import javax.xml.stream.XMLStreamReader;
44 import javax.xml.stream.XMLStreamException;
45 import javax.xml.stream.events.*;
46 import javax.xml.stream.util.XMLEventAllocator;
47 import javax.xml.stream.util.XMLEventConsumer;
48 import javax.xml.XMLConstants JavaDoc;
49 import javax.xml.namespace.QName JavaDoc;
50 import com.sun.xml.fastinfoset.CommonResourceBundle;
51
52 /**
53  * allows a user to register a way to allocate events given an XMLStreamReader.
54  * The XMLEventAllocator can be set on an XMLInputFactory
55  * using the property "javax.xml.stream.allocator"
56  *
57  * This base class uses EventFactory to create events as recommended in the JavaDoc of XMLEventAllocator.
58  * However, creating new object per each event reduces performance. The implementation of
59  * EventReader therefore will set the Allocator to StAXEventAllocator which implements the
60  * Allocate methods without creating new objects.
61  *
62  * The spec for the first Allocate method states that it must NOT modify the state of the Reader
63  * while the second MAY. For consistency, both Allocate methods in this implementation will
64  * NOT modify the state.
65  *
66  */

67 public class StAXEventAllocatorBase implements XMLEventAllocator {
68     XMLEventFactory factory;
69     
70     /** Creates a new instance of XMLEventAllocator */
71     public StAXEventAllocatorBase() {
72         if (System.getProperty("javax.xml.stream.XMLEventFactory")==null) {
73             System.setProperty("javax.xml.stream.XMLEventFactory",
74                        "com.sun.xml.fastinfoset.stax.StAXEventFactory");
75         }
76         factory = XMLEventFactory.newInstance();
77     }
78
79     // ---------------------methods defined by XMLEventAllocator-----------------//
80

81   /**
82    * This method creates an instance of the XMLEventAllocator. This
83    * allows the XMLInputFactory to allocate a new instance per reader.
84    */

85     public XMLEventAllocator newInstance() {
86         return new StAXEventAllocatorBase();
87     }
88
89   /**
90    * This method allocates an event given the current state of the XMLStreamReader.
91    * If this XMLEventAllocator does not have a one-to-one mapping between reader state
92    * and events this method will return null.
93    * @param reader The XMLStreamReader to allocate from
94    * @return the event corresponding to the current reader state
95    */

96     public XMLEvent allocate(XMLStreamReader streamReader) throws XMLStreamException {
97         if(streamReader == null )
98             throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.nullReader"));
99         return getXMLEvent(streamReader);
100     }
101     
102   /**
103    * This method allocates an event or set of events given the current state of
104    * the XMLStreamReader and adds the event or set of events to the consumer that
105    * was passed in.
106    * @param reader The XMLStreamReader to allocate from
107    * @param consumer The XMLEventConsumer to add to.
108    */

109     public void allocate(XMLStreamReader streamReader, XMLEventConsumer consumer) throws XMLStreamException {
110         consumer.add(getXMLEvent(streamReader));
111
112     }
113     // ---------------------end of methods defined by XMLEventAllocator-----------------//
114

115     
116     XMLEvent getXMLEvent(XMLStreamReader reader){
117         XMLEvent event = null;
118         //returns the current event
119
int eventType = reader.getEventType();
120         //this needs to be set before creating events
121
factory.setLocation(reader.getLocation());
122         switch(eventType){
123             
124             case XMLEvent.START_ELEMENT:
125             {
126                 StartElementEvent startElement = (StartElementEvent)factory.createStartElement(reader.getPrefix(),
127                                     reader.getNamespaceURI(), reader.getLocalName());
128
129                 addAttributes(startElement,reader);
130                 addNamespaces(startElement, reader);
131                 //need to fix it along with the Reader
132
//setNamespaceContext(startElement,reader);
133
event = startElement;
134                 break;
135             }
136             case XMLEvent.END_ELEMENT:
137             {
138                 EndElementEvent endElement = (EndElementEvent)factory.createEndElement(
139                         reader.getPrefix(), reader.getNamespaceURI(), reader.getLocalName());
140                 addNamespaces(endElement,reader);
141                 event = endElement ;
142                 break;
143             }
144             case XMLEvent.PROCESSING_INSTRUCTION:
145             {
146                 event = factory.createProcessingInstruction(reader.getPITarget(),reader.getPIData());
147                 break;
148             }
149             case XMLEvent.CHARACTERS:
150             {
151                 if (reader.isWhiteSpace())
152                   event = factory.createSpace(reader.getText());
153                 else
154                   event = factory.createCharacters(reader.getText());
155                 
156                 break;
157             }
158             case XMLEvent.COMMENT:
159             {
160                 event = factory.createComment(reader.getText());
161                 break;
162             }
163             case XMLEvent.START_DOCUMENT:
164             {
165                 StartDocumentEvent docEvent = (StartDocumentEvent)factory.createStartDocument(
166                         reader.getVersion(), reader.getEncoding(), reader.isStandalone());
167                 if(reader.getCharacterEncodingScheme() != null){
168                     docEvent.setDeclaredEncoding(true);
169                 }else{
170                     docEvent.setDeclaredEncoding(false);
171                 }
172                 event = docEvent ;
173                 break;
174             }
175             case XMLEvent.END_DOCUMENT:{
176                 EndDocumentEvent endDocumentEvent = new EndDocumentEvent() ;
177                 event = endDocumentEvent ;
178                 break;
179             }
180             case XMLEvent.ENTITY_REFERENCE:{
181                 event = factory.createEntityReference(reader.getLocalName(),
182                         new EntityDeclarationImpl(reader.getLocalName(),reader.getText()));
183                 break;
184                 
185             }
186             case XMLEvent.ATTRIBUTE:{
187                 event = null ;
188                 break;
189             }
190             case XMLEvent.DTD:{
191                 event = factory.createDTD(reader.getText());
192                 break;
193             }
194             case XMLEvent.CDATA:{
195                 event = factory.createCData(reader.getText());
196                 break;
197             }
198             case XMLEvent.SPACE:{
199                 event = factory.createSpace(reader.getText());
200                 break;
201             }
202         }
203         return event ;
204     }
205     
206     //use event.addAttribute instead of addAttributes to avoid creating another list
207
protected void addAttributes(StartElementEvent event,XMLStreamReader streamReader){
208         AttributeBase attr = null;
209         for(int i=0; i<streamReader.getAttributeCount() ;i++){
210             attr = (AttributeBase)factory.createAttribute(streamReader.getAttributeName(i),
211                                     streamReader.getAttributeValue(i));
212             attr.setAttributeType(streamReader.getAttributeType(i));
213             attr.setSpecified(streamReader.isAttributeSpecified(i));
214             event.addAttribute(attr);
215         }
216     }
217     
218     //add namespaces to StartElement/EndElement
219
protected void addNamespaces(StartElementEvent event,XMLStreamReader streamReader){
220         Namespace namespace = null;
221         for(int i=0; i<streamReader.getNamespaceCount(); i++){
222             namespace = factory.createNamespace(streamReader.getNamespacePrefix(i),
223                                 streamReader.getNamespaceURI(i));
224             event.addNamespace(namespace);
225         }
226     }
227     protected void addNamespaces(EndElementEvent event,XMLStreamReader streamReader){
228         Namespace namespace = null;
229         for(int i=0; i<streamReader.getNamespaceCount(); i++){
230             namespace = factory.createNamespace(streamReader.getNamespacePrefix(i),
231                                 streamReader.getNamespaceURI(i));
232             event.addNamespace(namespace);
233         }
234     }
235     
236     
237 }
238
Popular Tags