KickJava   Java API By Example, From Geeks To Geeks.

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


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
40 package com.sun.xml.fastinfoset.stax.events;
41
42 import javax.xml.stream.XMLStreamConstants;
43 import javax.xml.stream.XMLEventFactory;
44 import javax.xml.stream.XMLStreamReader;
45 import javax.xml.stream.XMLStreamException;
46 import javax.xml.stream.events.*;
47 import javax.xml.stream.util.XMLEventAllocator;
48 import javax.xml.stream.util.XMLEventConsumer;
49 import javax.xml.XMLConstants JavaDoc;
50 import javax.xml.namespace.QName JavaDoc;
51 import com.sun.xml.fastinfoset.CommonResourceBundle;
52
53 /**
54  * This class provides the same functionality as StAXEventAllocatorBase, but without
55  * using EventFactory and creating a new object for each call.
56  *
57  * It seems to be good idea using static components. Unfortunately, EventReader's peek
58  * and next methods require that multiple instances being created.
59  *
60  */

61 public class StAXEventAllocator implements XMLEventAllocator {
62     StartElementEvent startElement = new StartElementEvent();
63     EndElementEvent endElement = new EndElementEvent();
64     CharactersEvent characters = new CharactersEvent();
65     CharactersEvent cData = new CharactersEvent("",true);
66     CharactersEvent space = new CharactersEvent();
67     CommentEvent comment = new CommentEvent();
68     EntityReferenceEvent entity = new EntityReferenceEvent();
69     ProcessingInstructionEvent pi = new ProcessingInstructionEvent();
70     StartDocumentEvent startDoc = new StartDocumentEvent();
71     EndDocumentEvent endDoc = new EndDocumentEvent();
72     DTDEvent dtd = new DTDEvent();
73     
74     /** Creates a new instance of StAXEventAllocator */
75     public StAXEventAllocator() {
76     }
77     public XMLEventAllocator newInstance() {
78         return new StAXEventAllocator();
79     }
80
81   /**
82    * This method allocates an event given the current state of the XMLStreamReader.
83    * If this XMLEventAllocator does not have a one-to-one mapping between reader state
84    * and events this method will return null.
85    * @param reader The XMLStreamReader to allocate from
86    * @return the event corresponding to the current reader state
87    */

88     public XMLEvent allocate(XMLStreamReader streamReader) throws XMLStreamException {
89         if(streamReader == null )
90             throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.nullReader"));
91         return getXMLEvent(streamReader);
92     }
93     
94   /**
95    * This method allocates an event or set of events given the current state of
96    * the XMLStreamReader and adds the event or set of events to the consumer that
97    * was passed in.
98    * @param reader The XMLStreamReader to allocate from
99    * @param consumer The XMLEventConsumer to add to.
100    */

101     public void allocate(XMLStreamReader streamReader, XMLEventConsumer consumer) throws XMLStreamException {
102         consumer.add(getXMLEvent(streamReader));
103
104     }
105     // ---------------------end of methods defined by XMLEventAllocator-----------------//
106

107     
108     XMLEvent getXMLEvent(XMLStreamReader reader){
109         EventBase event = null;
110         int eventType = reader.getEventType();
111         
112         switch(eventType){
113             
114             case XMLEvent.START_ELEMENT:
115             {
116                 startElement.reset();
117                 startElement.setName(new QName JavaDoc(reader.getNamespaceURI(),
118                                    reader.getLocalName(), reader.getPrefix()));
119
120                 addAttributes(startElement,reader);
121                 addNamespaces(startElement, reader);
122                 //need to fix it along with the Reader
123
//setNamespaceContext(startElement,reader);
124
event = startElement;
125                 break;
126             }
127             case XMLEvent.END_ELEMENT:
128             {
129                 endElement.reset();
130                 endElement.setName(new QName JavaDoc(reader.getNamespaceURI(),
131                                  reader.getLocalName(),reader.getPrefix()));
132                 addNamespaces(endElement,reader);
133                 event = endElement ;
134                 break;
135             }
136             case XMLEvent.PROCESSING_INSTRUCTION:
137             {
138                 pi.setTarget(reader.getPITarget());
139                 pi.setData(reader.getPIData());
140                 event = pi;
141                 break;
142             }
143             case XMLEvent.CHARACTERS:
144             {
145                 characters.setData(reader.getText());
146                 event = characters;
147                 /**
148                 if (reader.isWhiteSpace()) {
149                     space.setData(reader.getText());
150                     space.setSpace(true);
151                     event = space;
152                 }
153                 else {
154                     characters.setData(reader.getText());
155                     event = characters;
156                 }
157                  */

158                 break;
159             }
160             case XMLEvent.COMMENT:
161             {
162                 comment.setText(reader.getText());
163                 event = comment;
164                 break;
165             }
166             case XMLEvent.START_DOCUMENT:
167             {
168                 startDoc.reset();
169                 String JavaDoc encoding = reader.getEncoding();
170                 String JavaDoc version = reader.getVersion();
171                 if (encoding != null)
172                     startDoc.setEncoding(encoding);
173                 if (version != null)
174                     startDoc.setVersion(version);
175                 startDoc.setStandalone(reader.isStandalone());
176                 if(reader.getCharacterEncodingScheme() != null){
177                     startDoc.setDeclaredEncoding(true);
178                 }else{
179                     startDoc.setDeclaredEncoding(false);
180                 }
181                 event = startDoc ;
182                 break;
183             }
184             case XMLEvent.END_DOCUMENT:{
185                 event = endDoc;
186                 break;
187             }
188             case XMLEvent.ENTITY_REFERENCE:{
189                 entity.setName(reader.getLocalName());
190                 entity.setDeclaration(new EntityDeclarationImpl(reader.getLocalName(),reader.getText()));
191                 event = entity;
192                 break;
193                 
194             }
195             case XMLEvent.ATTRIBUTE:{
196                 event = null ;
197                 break;
198             }
199             case XMLEvent.DTD:{
200                 dtd.setDTD(reader.getText());
201                 event = dtd;
202                 break;
203             }
204             case XMLEvent.CDATA:{
205                 cData.setData(reader.getText());
206                 event = cData;
207                 break;
208             }
209             case XMLEvent.SPACE:{
210                 space.setData(reader.getText());
211                 space.setSpace(true);
212                 event = space;
213                 break;
214             }
215         }
216         event.setLocation(reader.getLocation());
217         return event ;
218     }
219     
220     //use event.addAttribute instead of addAttributes to avoid creating another list
221
protected void addAttributes(StartElementEvent event,XMLStreamReader reader){
222         AttributeBase attr = null;
223         for(int i=0; i<reader.getAttributeCount() ;i++){
224             attr = new AttributeBase(reader.getAttributeName(i), reader.getAttributeValue(i));
225             attr.setAttributeType(reader.getAttributeType(i));
226             attr.setSpecified(reader.isAttributeSpecified(i));
227             event.addAttribute(attr);
228         }
229     }
230     
231     //add namespaces to StartElement/EndElement
232
protected void addNamespaces(StartElementEvent event,XMLStreamReader reader){
233         Namespace namespace = null;
234         for(int i=0; i<reader.getNamespaceCount(); i++){
235             namespace = new NamespaceBase(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
236             event.addNamespace(namespace);
237         }
238     }
239     
240     protected void addNamespaces(EndElementEvent event,XMLStreamReader reader){
241         Namespace namespace = null;
242         for(int i=0; i<reader.getNamespaceCount(); i++){
243             namespace = new NamespaceBase(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
244             event.addNamespace(namespace);
245         }
246     }
247         
248 }
249
Popular Tags