View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  
16  package com.bea.xml.stream;
17  
18  import com.bea.xml.stream.util.ElementTypeNames;
19  import java.util.Iterator;
20  
21  import javax.xml.namespace.QName;
22  
23  import javax.xml.stream.XMLEventFactory;
24  
25  import javax.xml.stream.XMLStreamReader;
26  import javax.xml.stream.util.XMLEventAllocator;
27  import javax.xml.stream.util.XMLEventConsumer;
28  import javax.xml.stream.XMLStreamException;
29  
30  import javax.xml.stream.events.XMLEvent;
31  import javax.xml.stream.events.StartElement;
32  import javax.xml.stream.events.ProcessingInstruction;
33  import javax.xml.stream.events.Comment;
34  import javax.xml.stream.events.EndElement;
35  import javax.xml.stream.events.Namespace;
36  import javax.xml.stream.events.Characters;
37  import javax.xml.stream.events.StartDocument;
38  import javax.xml.stream.events.EndDocument;
39  import javax.xml.stream.events.Namespace;
40  import javax.xml.stream.events.Attribute;
41  import javax.xml.stream.events.EntityReference;
42  import javax.xml.stream.events.DTD;
43  
44  import com.bea.xml.stream.events.CharactersEvent;
45  import com.bea.xml.stream.events.CommentEvent;
46  import com.bea.xml.stream.events.EndDocumentEvent;
47  import com.bea.xml.stream.events.EndElementEvent;
48  import com.bea.xml.stream.events.EntityReferenceEvent;
49  import com.bea.xml.stream.events.NamedEvent;
50  import com.bea.xml.stream.events.ProcessingInstructionEvent;
51  import com.bea.xml.stream.events.StartDocumentEvent;
52  import com.bea.xml.stream.events.StartElementEvent;
53  import com.bea.xml.stream.events.DTDEvent;
54   
55  /***
56   * <p> Return a single event for each allocate call </p>
57   */
58  
59  public class StaticAllocator 
60    implements XMLEventAllocator 
61  {
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    public StaticAllocator() { }
75    public XMLEventAllocator newInstance() {
76      return new StaticAllocator();
77    }
78    
79    public StartElement allocateStartElement(XMLStreamReader reader) 
80      throws XMLStreamException 
81    {
82      startElement.reset();
83      String prefix = EventFactory.checkPrefix(reader.getPrefix());
84      startElement.setName(new QName(reader.getNamespaceURI(),
85                                     reader.getLocalName(),
86                                     prefix));
87      Iterator ai = XMLEventAllocatorBase.getAttributes(reader);
88      while (ai.hasNext())
89        startElement.addAttribute((Attribute)ai.next());
90      
91      Iterator ni = XMLEventAllocatorBase.getNamespaces(reader);
92      while (ni.hasNext())
93        startElement.addAttribute((Namespace)ni.next());
94      return startElement;
95    }
96  
97    public EndElement allocateEndElement(XMLStreamReader reader) 
98      throws XMLStreamException 
99    {
100     endElement.reset();
101     String prefix = EventFactory.checkPrefix(reader.getPrefix());
102     endElement.setName(new QName(reader.getNamespaceURI(),
103                                  reader.getLocalName(),
104                                  prefix
105                                  ));
106     Iterator ni = XMLEventAllocatorBase.getNamespaces(reader);
107     while (ni.hasNext())
108       endElement.addNamespace((Namespace) ni.next());
109     return endElement;
110   }
111 
112   public Characters allocateCharacters(XMLStreamReader reader) 
113     throws XMLStreamException
114   {
115     characters.setData(reader.getText()); 
116     return characters;
117   }
118 
119   public Characters allocateCData(XMLStreamReader reader) 
120     throws XMLStreamException
121   {
122     cData.setData(reader.getText()); 
123     return cData;
124   }
125 
126   public Characters allocateSpace(XMLStreamReader reader) 
127     throws XMLStreamException
128   {
129     space.setSpace(true);
130     space.setData(reader.getText()); 
131     return space;
132   }
133 
134 
135   public EntityReference allocateEntityReference(XMLStreamReader reader) 
136     throws XMLStreamException
137   {
138     entity.setName(reader.getLocalName());
139     entity.setReplacementText(reader.getText());
140     return entity;
141   }
142 
143   public ProcessingInstruction allocatePI(XMLStreamReader reader) 
144     throws XMLStreamException
145   {
146     pi.setTarget(reader.getPITarget());
147     pi.setData(reader.getPIData());
148     return pi;
149   }
150 
151   public Comment allocateComment(XMLStreamReader reader) 
152     throws XMLStreamException
153   {
154     comment.setData(reader.getText());
155     return comment;
156   }
157 
158   public StartDocument allocateStartDocument(XMLStreamReader reader)
159     throws XMLStreamException
160   {
161     allocateXMLDeclaration(reader);
162     return startDoc;
163   }
164 
165   public EndDocument allocateEndDocument(XMLStreamReader reader)
166     throws XMLStreamException
167   {
168     return endDoc;
169   }
170 
171   public DTD allocateDTD(XMLStreamReader reader)
172     throws XMLStreamException
173   {
174     dtd.setDTD(reader.getText());
175     return dtd;
176   }
177 
178   public StartDocument allocateXMLDeclaration(XMLStreamReader reader)
179     throws XMLStreamException
180   {
181     startDoc.clear();
182     String encoding = reader.getCharacterEncodingScheme();
183     String version = reader.getVersion();
184     boolean standalone = reader.isStandalone();
185     if (encoding != null && 
186         version != null &&
187         !standalone ) {
188       startDoc.setEncoding(encoding);
189       startDoc.setVersion(version);
190       startDoc.setStandalone(standalone);
191       return startDoc;
192     }
193     if (version != null && 
194         encoding != null) {
195       startDoc.setEncoding(encoding);
196       startDoc.setVersion(version);
197       return startDoc;
198     }
199 
200     if (encoding != null)
201       startDoc.setEncoding(encoding);
202     return startDoc;
203   }
204   
205   public XMLEvent allocate(XMLStreamReader reader) 
206     throws XMLStreamException
207   {
208     switch (reader.getEventType()) {
209     case XMLEvent.START_ELEMENT: return allocateStartElement(reader);
210     case XMLEvent.END_ELEMENT: return allocateEndElement(reader);
211     case XMLEvent.CHARACTERS: return allocateCharacters(reader);
212     case XMLEvent.SPACE: return allocateCharacters(reader);
213     case XMLEvent.CDATA: return allocateCData(reader);
214     case XMLEvent.ENTITY_REFERENCE: return allocateEntityReference(reader);
215     case XMLEvent.PROCESSING_INSTRUCTION: return allocatePI(reader);
216     case XMLEvent.COMMENT: return allocateComment(reader);
217       //case XMLEvent.XML_DECLARATION: return allocateXMLDeclaration(reader);
218     case XMLEvent.START_DOCUMENT: return allocateStartDocument(reader);
219     case XMLEvent.END_DOCUMENT: return allocateEndDocument(reader);
220     case XMLEvent.DTD: return allocateDTD(reader);
221     default:
222       throw new XMLStreamException("Unable to allocate event["+
223                                    ElementTypeNames.getEventTypeString(reader.getEventType())+"]");
224     }
225     //    return new com.bea.xml.stream.events.NullEvent();
226   }
227 
228   public void allocate(XMLStreamReader reader,
229                        XMLEventConsumer consumer) 
230     throws XMLStreamException
231   {
232     consumer.add(allocate(reader));
233   }
234 
235   public String toString() {
236     return "Static Allocator";
237   }
238 
239 }
240 
241 
242 
243 
244 
245