Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/com/sun/xml/internal/stream/writers/XMLEventWriterImpl.java
48527 views
1
/*
2
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
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
26
package com.sun.xml.internal.stream.writers;
27
28
import java.util.Iterator;
29
import javax.xml.namespace.QName;
30
import javax.xml.stream.XMLEventWriter;
31
import javax.xml.stream.XMLStreamException;
32
import javax.xml.stream.events.Attribute;
33
import javax.xml.stream.events.Characters;
34
import javax.xml.stream.events.Comment;
35
import javax.xml.stream.events.DTD;
36
import javax.xml.stream.events.EntityReference;
37
import javax.xml.stream.events.Namespace;
38
import javax.xml.stream.events.ProcessingInstruction;
39
import javax.xml.stream.events.StartDocument;
40
import javax.xml.stream.events.StartElement;
41
import javax.xml.stream.events.XMLEvent;
42
import javax.xml.stream.XMLStreamWriter;
43
44
/**
45
*
46
* @author Neeraj Bajaj, Sun Microsystems.
47
*
48
*/
49
public class XMLEventWriterImpl implements XMLEventWriter{
50
51
//delegate everything to XMLStreamWriter..
52
private XMLStreamWriter fStreamWriter ;
53
private static final boolean DEBUG = false;
54
/**
55
*
56
* @param streamWriter
57
*/
58
public XMLEventWriterImpl(XMLStreamWriter streamWriter){
59
fStreamWriter = streamWriter;
60
}
61
62
/**
63
*
64
* @param xMLEventReader
65
* @throws XMLStreamException
66
*/
67
public void add(javax.xml.stream.XMLEventReader xMLEventReader) throws javax.xml.stream.XMLStreamException {
68
if(xMLEventReader == null) throw new XMLStreamException("Event reader shouldn't be null");
69
while(xMLEventReader.hasNext()){
70
add(xMLEventReader.nextEvent());
71
}
72
}
73
74
/**
75
*
76
* @param xMLEvent
77
* @throws XMLStreamException
78
*/
79
public void add(javax.xml.stream.events.XMLEvent xMLEvent) throws javax.xml.stream.XMLStreamException {
80
int type = xMLEvent.getEventType();
81
switch(type){
82
case XMLEvent.DTD:{
83
DTD dtd = (DTD)xMLEvent ;
84
if (DEBUG)System.out.println("Adding DTD = " + dtd.toString());
85
fStreamWriter.writeDTD(dtd.getDocumentTypeDeclaration());
86
break;
87
}
88
case XMLEvent.START_DOCUMENT :{
89
StartDocument startDocument = (StartDocument)xMLEvent ;
90
if (DEBUG)System.out.println("Adding StartDocument = " + startDocument.toString());
91
try {
92
fStreamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion());
93
}catch(XMLStreamException e) {
94
fStreamWriter.writeStartDocument(startDocument.getVersion());
95
}
96
break;
97
}
98
case XMLEvent.START_ELEMENT :{
99
StartElement startElement = xMLEvent.asStartElement() ;
100
if (DEBUG)System.out.println("Adding startelement = " + startElement.toString());
101
QName qname = startElement.getName();
102
fStreamWriter.writeStartElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());
103
104
//getNamespaces() Returns an Iterator of namespaces declared on this element. This Iterator does not contain
105
//previously declared namespaces unless they appear on the current START_ELEMENT. Therefore
106
//this list may contain redeclared namespaces and duplicate namespace declarations. Use the
107
//getNamespaceContext() method to get the current context of namespace declarations.
108
109
//so we should be using getNamespaces() to write namespace declarations for this START_ELEMENT
110
Iterator iterator = startElement.getNamespaces();
111
while(iterator.hasNext()){
112
Namespace namespace = (Namespace)iterator.next();
113
fStreamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
114
}
115
//REVISIT: What about writing attributes ?
116
Iterator attributes = startElement.getAttributes();
117
while(attributes.hasNext()){
118
Attribute attribute = (Attribute)attributes.next();
119
QName aqname = attribute.getName();
120
fStreamWriter.writeAttribute(aqname.getPrefix(), aqname.getNamespaceURI(), aqname.getLocalPart(),attribute.getValue());
121
}
122
break;
123
}
124
case XMLEvent.NAMESPACE:{
125
Namespace namespace = (Namespace)xMLEvent;
126
if (DEBUG)System.out.println("Adding namespace = " + namespace.toString());
127
fStreamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
128
break ;
129
}
130
case XMLEvent.COMMENT: {
131
Comment comment = (Comment)xMLEvent ;
132
if (DEBUG)System.out.println("Adding comment = " + comment.toString());
133
fStreamWriter.writeComment(comment.getText());
134
break;
135
}
136
case XMLEvent.PROCESSING_INSTRUCTION:{
137
ProcessingInstruction processingInstruction = (ProcessingInstruction)xMLEvent ;
138
if (DEBUG)System.out.println("Adding processing instruction = " + processingInstruction.toString());
139
fStreamWriter.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData());
140
break;
141
}
142
case XMLEvent.CHARACTERS:{
143
Characters characters = xMLEvent.asCharacters();
144
if (DEBUG)System.out.println("Adding characters = " + characters.toString());
145
//check if the CHARACTERS are CDATA
146
if(characters.isCData()){
147
fStreamWriter.writeCData(characters.getData());
148
}
149
else{
150
fStreamWriter.writeCharacters(characters.getData());
151
}
152
break;
153
}
154
case XMLEvent.ENTITY_REFERENCE:{
155
EntityReference entityReference = (EntityReference)xMLEvent ;
156
if (DEBUG)System.out.println("Adding Entity Reference = "+ entityReference.toString());
157
fStreamWriter.writeEntityRef(entityReference.getName());
158
break;
159
}
160
case XMLEvent.ATTRIBUTE:{
161
Attribute attribute = (Attribute)xMLEvent;
162
if (DEBUG)System.out.println("Adding Attribute = " + attribute.toString());
163
QName qname = attribute.getName();
164
fStreamWriter.writeAttribute(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart(),attribute.getValue());
165
break;
166
}
167
case XMLEvent.CDATA:{
168
//there is no separate CDATA datatype but CDATA event can be reported
169
//by using vendor specific CDATA property.
170
Characters characters = (Characters)xMLEvent;
171
if (DEBUG)System.out.println("Adding characters = " + characters.toString());
172
if(characters.isCData()){
173
fStreamWriter.writeCData(characters.getData());
174
}
175
break;
176
}
177
//xxx: Why there isn't any event called Notation.
178
//case XMLEvent.NOTATION_DECLARATION:{
179
//}
180
181
case XMLEvent.END_ELEMENT:{
182
//we dont need to typecast it.. just call writeEndElement() and fStreamWriter will take care of it.
183
//EndElement endElement = (EndElement)xMLEvent;
184
fStreamWriter.writeEndElement();
185
break;
186
}
187
case XMLEvent.END_DOCUMENT:{
188
//we dont need to typecast just call writeEndDocument() and fStreamWriter will take care rest.
189
//EndDocument endDocument = (EndDocument)xMLEvent;
190
fStreamWriter.writeEndDocument();
191
break;
192
}
193
//throw new XMLStreamException("Unknown Event type = " + type);
194
};
195
196
}
197
198
/**
199
*
200
* @throws XMLStreamException
201
*/
202
public void close() throws javax.xml.stream.XMLStreamException {
203
fStreamWriter.close();
204
}
205
206
/**
207
*
208
* @throws XMLStreamException will inturn call flush on the stream to which data is being
209
* written.
210
*/
211
public void flush() throws javax.xml.stream.XMLStreamException {
212
fStreamWriter.flush();
213
}
214
215
/**
216
*
217
* @return
218
*/
219
public javax.xml.namespace.NamespaceContext getNamespaceContext() {
220
return fStreamWriter.getNamespaceContext();
221
}
222
223
/**
224
*
225
* @param namespaceURI Namespace URI
226
* @throws XMLStreamException
227
* @return prefix associated with the URI.
228
*/
229
public String getPrefix(String namespaceURI) throws javax.xml.stream.XMLStreamException {
230
return fStreamWriter.getPrefix(namespaceURI);
231
}
232
233
/**
234
*
235
* @param uri Namespace URI
236
* @throws XMLStreamException
237
*/
238
public void setDefaultNamespace(String uri) throws javax.xml.stream.XMLStreamException {
239
fStreamWriter.setDefaultNamespace(uri);
240
}
241
242
/**
243
*
244
* @param namespaceContext Namespace Context
245
* @throws XMLStreamException
246
*/
247
public void setNamespaceContext(javax.xml.namespace.NamespaceContext namespaceContext) throws javax.xml.stream.XMLStreamException {
248
fStreamWriter.setNamespaceContext(namespaceContext);
249
}
250
251
/**
252
*
253
* @param prefix namespace prefix associated with the uri.
254
* @param uri Namespace URI
255
* @throws XMLStreamException
256
*/
257
public void setPrefix(String prefix, String uri) throws javax.xml.stream.XMLStreamException {
258
fStreamWriter.setPrefix(prefix, uri);
259
}
260
261
}//XMLEventWriterImpl
262
263