author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
* |
|
25 |
* THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
|
26 |
*/ |
|
27 |
||
28 |
package com.sun.xml.internal.fastinfoset.stax.events; |
|
29 |
||
30 |
import javax.xml.stream.XMLEventFactory; |
|
31 |
import javax.xml.stream.XMLStreamException; |
|
32 |
import javax.xml.stream.XMLStreamReader; |
|
33 |
import javax.xml.stream.events.Namespace; |
|
34 |
import javax.xml.stream.events.XMLEvent; |
|
35 |
import javax.xml.stream.util.XMLEventAllocator; |
|
36 |
import javax.xml.stream.util.XMLEventConsumer; |
|
37 |
||
38 |
import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
|
39 |
||
40 |
/** |
|
41 |
* allows a user to register a way to allocate events given an XMLStreamReader. |
|
42 |
* The XMLEventAllocator can be set on an XMLInputFactory |
|
43 |
* using the property "javax.xml.stream.allocator" |
|
44 |
* |
|
45 |
* This base class uses EventFactory to create events as recommended in the JavaDoc of XMLEventAllocator. |
|
46 |
* However, creating new object per each event reduces performance. The implementation of |
|
47 |
* EventReader therefore will set the Allocator to StAXEventAllocator which implements the |
|
48 |
* Allocate methods without creating new objects. |
|
49 |
* |
|
50 |
* The spec for the first Allocate method states that it must NOT modify the state of the Reader |
|
51 |
* while the second MAY. For consistency, both Allocate methods in this implementation will |
|
52 |
* NOT modify the state. |
|
53 |
* |
|
54 |
*/ |
|
55 |
public class StAXEventAllocatorBase implements XMLEventAllocator { |
|
56 |
XMLEventFactory factory; |
|
57 |
||
58 |
/** Creates a new instance of XMLEventAllocator */ |
|
59 |
public StAXEventAllocatorBase() { |
|
60 |
if (System.getProperty("javax.xml.stream.XMLEventFactory")==null) { |
|
61 |
System.setProperty("javax.xml.stream.XMLEventFactory", |
|
62 |
"com.sun.xml.internal.fastinfoset.stax.factory.StAXEventFactory"); |
|
63 |
} |
|
64 |
factory = XMLEventFactory.newInstance(); |
|
65 |
} |
|
66 |
||
67 |
// ---------------------methods defined by XMLEventAllocator-----------------// |
|
68 |
||
69 |
/** |
|
70 |
* This method creates an instance of the XMLEventAllocator. This |
|
71 |
* allows the XMLInputFactory to allocate a new instance per reader. |
|
72 |
*/ |
|
73 |
public XMLEventAllocator newInstance() { |
|
74 |
return new StAXEventAllocatorBase(); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* This method allocates an event given the current state of the XMLStreamReader. |
|
79 |
* If this XMLEventAllocator does not have a one-to-one mapping between reader state |
|
80 |
* and events this method will return null. |
|
81 |
* @param streamReader The XMLStreamReader to allocate from |
|
82 |
* @return the event corresponding to the current reader state |
|
83 |
*/ |
|
84 |
public XMLEvent allocate(XMLStreamReader streamReader) throws XMLStreamException { |
|
85 |
if(streamReader == null ) |
|
86 |
throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.nullReader")); |
|
87 |
return getXMLEvent(streamReader); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* This method allocates an event or set of events given the current state of |
|
92 |
* the XMLStreamReader and adds the event or set of events to the consumer that |
|
93 |
* was passed in. |
|
94 |
* @param streamReader The XMLStreamReader to allocate from |
|
95 |
* @param consumer The XMLEventConsumer to add to. |
|
96 |
*/ |
|
97 |
public void allocate(XMLStreamReader streamReader, XMLEventConsumer consumer) throws XMLStreamException { |
|
98 |
consumer.add(getXMLEvent(streamReader)); |
|
99 |
||
100 |
} |
|
101 |
// ---------------------end of methods defined by XMLEventAllocator-----------------// |
|
102 |
||
103 |
||
104 |
XMLEvent getXMLEvent(XMLStreamReader reader){ |
|
105 |
XMLEvent event = null; |
|
106 |
//returns the current event |
|
107 |
int eventType = reader.getEventType(); |
|
108 |
//this needs to be set before creating events |
|
109 |
factory.setLocation(reader.getLocation()); |
|
110 |
switch(eventType){ |
|
111 |
||
112 |
case XMLEvent.START_ELEMENT: |
|
113 |
{ |
|
114 |
StartElementEvent startElement = (StartElementEvent)factory.createStartElement(reader.getPrefix(), |
|
115 |
reader.getNamespaceURI(), reader.getLocalName()); |
|
116 |
||
117 |
addAttributes(startElement,reader); |
|
118 |
addNamespaces(startElement, reader); |
|
119 |
//need to fix it along with the Reader |
|
120 |
//setNamespaceContext(startElement,reader); |
|
121 |
event = startElement; |
|
122 |
break; |
|
123 |
} |
|
124 |
case XMLEvent.END_ELEMENT: |
|
125 |
{ |
|
126 |
EndElementEvent endElement = (EndElementEvent)factory.createEndElement( |
|
127 |
reader.getPrefix(), reader.getNamespaceURI(), reader.getLocalName()); |
|
128 |
addNamespaces(endElement,reader); |
|
129 |
event = endElement ; |
|
130 |
break; |
|
131 |
} |
|
132 |
case XMLEvent.PROCESSING_INSTRUCTION: |
|
133 |
{ |
|
134 |
event = factory.createProcessingInstruction(reader.getPITarget(),reader.getPIData()); |
|
135 |
break; |
|
136 |
} |
|
137 |
case XMLEvent.CHARACTERS: |
|
138 |
{ |
|
139 |
if (reader.isWhiteSpace()) |
|
140 |
event = factory.createSpace(reader.getText()); |
|
141 |
else |
|
142 |
event = factory.createCharacters(reader.getText()); |
|
143 |
||
144 |
break; |
|
145 |
} |
|
146 |
case XMLEvent.COMMENT: |
|
147 |
{ |
|
148 |
event = factory.createComment(reader.getText()); |
|
149 |
break; |
|
150 |
} |
|
151 |
case XMLEvent.START_DOCUMENT: |
|
152 |
{ |
|
153 |
StartDocumentEvent docEvent = (StartDocumentEvent)factory.createStartDocument( |
|
154 |
reader.getVersion(), reader.getEncoding(), reader.isStandalone()); |
|
155 |
if(reader.getCharacterEncodingScheme() != null){ |
|
156 |
docEvent.setDeclaredEncoding(true); |
|
157 |
}else{ |
|
158 |
docEvent.setDeclaredEncoding(false); |
|
159 |
} |
|
160 |
event = docEvent ; |
|
161 |
break; |
|
162 |
} |
|
163 |
case XMLEvent.END_DOCUMENT:{ |
|
164 |
EndDocumentEvent endDocumentEvent = new EndDocumentEvent() ; |
|
165 |
event = endDocumentEvent ; |
|
166 |
break; |
|
167 |
} |
|
168 |
case XMLEvent.ENTITY_REFERENCE:{ |
|
169 |
event = factory.createEntityReference(reader.getLocalName(), |
|
170 |
new EntityDeclarationImpl(reader.getLocalName(),reader.getText())); |
|
171 |
break; |
|
172 |
||
173 |
} |
|
174 |
case XMLEvent.ATTRIBUTE:{ |
|
175 |
event = null ; |
|
176 |
break; |
|
177 |
} |
|
178 |
case XMLEvent.DTD:{ |
|
179 |
event = factory.createDTD(reader.getText()); |
|
180 |
break; |
|
181 |
} |
|
182 |
case XMLEvent.CDATA:{ |
|
183 |
event = factory.createCData(reader.getText()); |
|
184 |
break; |
|
185 |
} |
|
186 |
case XMLEvent.SPACE:{ |
|
187 |
event = factory.createSpace(reader.getText()); |
|
188 |
break; |
|
189 |
} |
|
190 |
} |
|
191 |
return event ; |
|
192 |
} |
|
193 |
||
194 |
//use event.addAttribute instead of addAttributes to avoid creating another list |
|
195 |
protected void addAttributes(StartElementEvent event,XMLStreamReader streamReader){ |
|
196 |
AttributeBase attr = null; |
|
197 |
for(int i=0; i<streamReader.getAttributeCount() ;i++){ |
|
198 |
attr = (AttributeBase)factory.createAttribute(streamReader.getAttributeName(i), |
|
199 |
streamReader.getAttributeValue(i)); |
|
200 |
attr.setAttributeType(streamReader.getAttributeType(i)); |
|
201 |
attr.setSpecified(streamReader.isAttributeSpecified(i)); |
|
202 |
event.addAttribute(attr); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
//add namespaces to StartElement/EndElement |
|
207 |
protected void addNamespaces(StartElementEvent event,XMLStreamReader streamReader){ |
|
208 |
Namespace namespace = null; |
|
209 |
for(int i=0; i<streamReader.getNamespaceCount(); i++){ |
|
210 |
namespace = factory.createNamespace(streamReader.getNamespacePrefix(i), |
|
211 |
streamReader.getNamespaceURI(i)); |
|
212 |
event.addNamespace(namespace); |
|
213 |
} |
|
214 |
} |
|
215 |
protected void addNamespaces(EndElementEvent event,XMLStreamReader streamReader){ |
|
216 |
Namespace namespace = null; |
|
217 |
for(int i=0; i<streamReader.getNamespaceCount(); i++){ |
|
218 |
namespace = factory.createNamespace(streamReader.getNamespacePrefix(i), |
|
219 |
streamReader.getNamespaceURI(i)); |
|
220 |
event.addNamespace(namespace); |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
||
225 |
} |