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  import javax.xml.stream.XMLStreamReader;
25  import javax.xml.stream.util.XMLEventAllocator;
26  import javax.xml.stream.util.XMLEventConsumer;
27  import javax.xml.stream.XMLStreamException;
28  
29  import javax.xml.stream.events.XMLEvent;
30  import javax.xml.stream.events.StartElement;
31  import javax.xml.stream.events.ProcessingInstruction;
32  import javax.xml.stream.events.Comment;
33  import javax.xml.stream.events.EndElement;
34  import javax.xml.stream.events.Namespace;
35  import javax.xml.stream.events.Characters;
36  import javax.xml.stream.events.StartDocument;
37  import javax.xml.stream.events.EndDocument;
38  import javax.xml.stream.events.Namespace;
39  import javax.xml.stream.events.Attribute;
40  import javax.xml.stream.events.EntityReference;
41  import javax.xml.stream.events.DTD;
42  
43  import com.bea.xml.stream.util.EmptyIterator;
44  import java.util.ArrayList;
45  
46  /***
47   * <p> An allocator that creates an event per method call. </p>
48   */
49  
50  public class XMLEventAllocatorBase 
51    implements XMLEventAllocator 
52  {
53    XMLEventFactory factory;
54  
55    public XMLEventAllocatorBase() {
56      factory = XMLEventFactory.newInstance();
57    }
58  
59    public XMLEventAllocator newInstance() {
60      return new XMLEventAllocatorBase();
61    }
62  
63    public static Iterator getAttributes(XMLStreamReader reader) {
64      if (reader.getAttributeCount()==0) return EmptyIterator.emptyIterator;
65      int attributeCount = reader.getAttributeCount();
66      ArrayList atts = new ArrayList();
67      for (int i = 0; i < attributeCount; i++){
68        atts.add(new AttributeBase(reader.getAttributePrefix(i),
69                                   reader.getAttributeNamespace(i),
70                                   reader.getAttributeLocalName(i),
71                                   reader.getAttributeValue(i),
72                                   reader.getAttributeType(i)));
73      }
74      return atts.iterator();
75    }
76    
77    public static Iterator getNamespaces(XMLStreamReader reader) {
78      if (reader.getNamespaceCount()==0) return EmptyIterator.emptyIterator;
79      ArrayList ns = new ArrayList();
80      for (int i = 0; i < reader.getNamespaceCount(); i++){  
81        String prefix = reader.getNamespacePrefix(i);
82        if(prefix == null ||
83           prefix.equals("")){
84          ns.add(new NamespaceBase(reader.getNamespaceURI(i)));
85        } else {
86          ns.add(new NamespaceBase(prefix,
87                                   reader.getNamespaceURI(i)));
88        }
89      }
90      return ns.iterator();
91    }
92  
93    public StartElement allocateStartElement(XMLStreamReader reader) 
94      throws XMLStreamException 
95    {
96      String prefix = reader.getPrefix();
97      String uri = reader.getNamespaceURI();
98      if (prefix == null) prefix = "";
99      if (uri == null) uri = "";
100     return factory.createStartElement(prefix,
101                                       uri,
102                                       reader.getLocalName(),
103                                       getAttributes(reader),
104                                       getNamespaces(reader));
105   }
106 
107   public EndElement allocateEndElement(XMLStreamReader reader) 
108     throws XMLStreamException 
109   {
110     String prefix = reader.getPrefix();
111     String uri = reader.getNamespaceURI();
112     if (prefix == null) prefix = "";
113     if (uri == null) uri = "";
114     return factory.createEndElement(prefix,
115                                     uri,
116                                     reader.getLocalName(),
117                                     getNamespaces(reader)
118                                     );
119   }
120 
121   public Characters allocateCharacters(XMLStreamReader reader) 
122     throws XMLStreamException
123   {
124     int start = reader.getTextStart();
125     int length = reader.getTextLength();
126     String result = new String(reader.getTextCharacters(),
127                                start,
128                                length);
129     if (reader.isWhiteSpace())
130       return factory.createSpace(result);
131     else
132       return factory.createCharacters(result);
133   }
134 
135   public Characters allocateCData(XMLStreamReader reader) 
136     throws XMLStreamException
137   {
138     return factory.createCData(reader.getText());
139   }
140 
141   public Characters allocateSpace(XMLStreamReader reader) 
142     throws XMLStreamException
143   {
144     return factory.createSpace(reader.getText());
145   }
146 
147   public EntityReference allocateEntityReference(XMLStreamReader reader) 
148     throws XMLStreamException
149   {
150     return factory.createEntityReference(reader.getLocalName(),
151                                          null);
152   }
153 
154   public ProcessingInstruction allocatePI(XMLStreamReader reader) 
155     throws XMLStreamException
156   {
157     return factory.createProcessingInstruction(reader.getPITarget(),
158                                                reader.getPIData());
159   }
160 
161   public Comment allocateComment(XMLStreamReader reader) 
162     throws XMLStreamException
163   {
164     return factory.createComment(reader.getText());
165   }
166 
167   public StartDocument allocateStartDocument(XMLStreamReader reader)
168     throws XMLStreamException
169   {
170     return allocateXMLDeclaration(reader);
171   }
172 
173   public EndDocument allocateEndDocument(XMLStreamReader reader)
174     throws XMLStreamException
175   {
176     return factory.createEndDocument();
177   }
178 
179   public DTD allocateDTD(XMLStreamReader reader)
180     throws XMLStreamException
181   {
182     return factory.createDTD(reader.getText());
183   }
184 
185   public StartDocument allocateXMLDeclaration(XMLStreamReader reader)
186     throws XMLStreamException
187   {
188     String encoding = reader.getCharacterEncodingScheme();
189     String version = reader.getVersion();
190     boolean standalone = reader.isStandalone();
191     if (encoding != null && 
192         version != null &&
193         !standalone ) {
194       return factory.createStartDocument(encoding,
195                                          version,
196                                          standalone);
197     }
198     if (version != null && 
199         encoding != null)
200       return factory.createStartDocument(encoding,
201                                          version);
202 
203     if (encoding != null)
204     return factory.createStartDocument(encoding);
205 
206     return factory.createStartDocument();
207   }
208   
209 
210   public XMLEvent allocate(XMLStreamReader reader) 
211     throws XMLStreamException
212   {
213     switch (reader.getEventType()) {
214     case XMLEvent.START_ELEMENT: return allocateStartElement(reader);
215     case XMLEvent.END_ELEMENT: return allocateEndElement(reader);
216     case XMLEvent.CHARACTERS: return allocateCharacters(reader);
217     case XMLEvent.SPACE: return allocateCharacters(reader);
218     case XMLEvent.CDATA: return allocateCData(reader);
219     case XMLEvent.ENTITY_REFERENCE: return allocateEntityReference(reader);
220     case XMLEvent.PROCESSING_INSTRUCTION: return allocatePI(reader);
221     case XMLEvent.COMMENT: return allocateComment(reader);
222       //    case XMLEvent.XML_DECLARATION: return allocateXMLDeclaration(reader);
223     case XMLEvent.START_DOCUMENT: return allocateStartDocument(reader);
224     case XMLEvent.END_DOCUMENT: return allocateEndDocument(reader);
225     case XMLEvent.DTD: return allocateDTD(reader);
226     default:
227       throw new XMLStreamException("Unable to allocate event["+
228                                    reader.getEventType()+" , "+
229                                    ElementTypeNames.getEventTypeString(reader.getEventType())+"]");
230     }
231     //    return new com.bea.xml.stream.events.NullEvent();
232   }
233 
234   public void allocate(XMLStreamReader reader,
235                        XMLEventConsumer consumer) 
236     throws XMLStreamException
237   {
238     consumer.add(allocate(reader));
239   }
240 
241   public String toString() {
242     return "NonStaticAllocator";
243   }
244 }
245 
246 
247 
248 
249