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.tools; |
|
29 |
||
30 |
import com.sun.xml.internal.fastinfoset.QualifiedName; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.logging.Level; |
|
33 |
import java.util.logging.Logger; |
|
34 |
import javax.xml.stream.XMLStreamException; |
|
35 |
import javax.xml.stream.XMLStreamWriter; |
|
36 |
import org.xml.sax.Attributes; |
|
37 |
import org.xml.sax.Locator; |
|
38 |
import org.xml.sax.SAXException; |
|
39 |
import org.xml.sax.ext.LexicalHandler; |
|
40 |
import org.xml.sax.helpers.DefaultHandler; |
|
41 |
||
42 |
public class SAX2StAXWriter extends DefaultHandler implements LexicalHandler { |
|
43 |
private static final Logger logger = Logger.getLogger(SAX2StAXWriter.class.getName()); |
|
44 |
||
45 |
/** |
|
46 |
* XML stream writer where events are pushed. |
|
47 |
*/ |
|
48 |
XMLStreamWriter _writer; |
|
49 |
||
50 |
/** |
|
51 |
* List of namespace decl for upcoming element. |
|
52 |
*/ |
|
53 |
ArrayList _namespaces = new ArrayList(); |
|
54 |
||
55 |
public SAX2StAXWriter(XMLStreamWriter writer) { |
|
56 |
_writer = writer; |
|
57 |
} |
|
58 |
||
59 |
public XMLStreamWriter getWriter() { |
|
60 |
return _writer; |
|
61 |
} |
|
62 |
||
63 |
public void startDocument() throws SAXException { |
|
64 |
try { |
|
65 |
_writer.writeStartDocument(); |
|
66 |
} |
|
67 |
catch (XMLStreamException e) { |
|
68 |
throw new SAXException(e); |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
public void endDocument() throws SAXException { |
|
73 |
try { |
|
74 |
_writer.writeEndDocument(); |
|
75 |
_writer.flush(); |
|
76 |
} |
|
77 |
catch (XMLStreamException e) { |
|
78 |
throw new SAXException(e); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
public void characters(char[] ch, int start, int length) |
|
83 |
throws SAXException |
|
84 |
{ |
|
85 |
try { |
|
86 |
_writer.writeCharacters(ch, start, length); |
|
87 |
} |
|
88 |
catch (XMLStreamException e) { |
|
89 |
throw new SAXException(e); |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
public void startElement(String namespaceURI, String localName, |
|
94 |
String qName, Attributes atts) throws SAXException |
|
95 |
{ |
|
96 |
try { |
|
97 |
int k = qName.indexOf(':'); |
|
98 |
String prefix = (k > 0) ? qName.substring(0, k) : ""; |
|
99 |
_writer.writeStartElement(prefix, localName, namespaceURI); |
|
100 |
||
101 |
int length = _namespaces.size(); |
|
102 |
for (int i = 0; i < length; i++) { |
|
103 |
QualifiedName nsh = (QualifiedName) _namespaces.get(i); |
|
104 |
_writer.writeNamespace(nsh.prefix, nsh.namespaceName); |
|
105 |
} |
|
106 |
_namespaces.clear(); |
|
107 |
||
108 |
length = atts.getLength(); |
|
109 |
for (int i = 0; i < length; i++) { |
|
110 |
_writer.writeAttribute(atts.getURI(i), |
|
111 |
atts.getLocalName(i), |
|
112 |
atts.getValue(i)); |
|
113 |
} |
|
114 |
} |
|
115 |
catch (XMLStreamException e) { |
|
116 |
throw new SAXException(e); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
public void endElement(String namespaceURI, String localName, |
|
121 |
String qName) throws SAXException |
|
122 |
{ |
|
123 |
try { |
|
124 |
_writer.writeEndElement(); |
|
125 |
} |
|
126 |
catch (XMLStreamException e) { |
|
127 |
logger.log(Level.FINE, "Exception on endElement", e); |
|
128 |
throw new SAXException(e); |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
public void startPrefixMapping(String prefix, String uri) |
|
133 |
throws SAXException |
|
134 |
{ |
|
135 |
// Commented as in StAX NS are declared for current element? |
|
136 |
// now _writer.setPrefix() is invoked in _writer.writeNamespace() |
|
137 |
// try { |
|
138 |
// _writer.setPrefix(prefix, uri); |
|
139 |
_namespaces.add(new QualifiedName(prefix, uri)); |
|
140 |
// } |
|
141 |
// catch (XMLStreamException e) { |
|
142 |
// throw new SAXException(e); |
|
143 |
// } |
|
144 |
} |
|
145 |
||
146 |
public void endPrefixMapping(String prefix) throws SAXException { |
|
147 |
} |
|
148 |
||
149 |
public void ignorableWhitespace(char[] ch, int start, int length) |
|
150 |
throws SAXException |
|
151 |
{ |
|
152 |
characters(ch, start, length); |
|
153 |
} |
|
154 |
||
155 |
public void processingInstruction(String target, String data) |
|
156 |
throws SAXException |
|
157 |
{ |
|
158 |
try { |
|
159 |
_writer.writeProcessingInstruction(target, data); |
|
160 |
} |
|
161 |
catch (XMLStreamException e) { |
|
162 |
throw new SAXException(e); |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
public void setDocumentLocator(Locator locator) { |
|
167 |
} |
|
168 |
||
169 |
public void skippedEntity(String name) throws SAXException { |
|
170 |
} |
|
171 |
||
172 |
public void comment(char[] ch, int start, int length) |
|
173 |
throws SAXException |
|
174 |
{ |
|
175 |
try { |
|
176 |
_writer.writeComment(new String(ch, start, length)); |
|
177 |
} |
|
178 |
catch (XMLStreamException e) { |
|
179 |
throw new SAXException(e); |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
public void endCDATA() throws SAXException { |
|
184 |
} |
|
185 |
||
186 |
public void endDTD() throws SAXException { |
|
187 |
} |
|
188 |
||
189 |
public void endEntity(String name) throws SAXException { |
|
190 |
} |
|
191 |
||
192 |
public void startCDATA() throws SAXException { |
|
193 |
} |
|
194 |
||
195 |
public void startDTD(String name, String publicId, String systemId) throws SAXException { |
|
196 |
} |
|
197 |
||
198 |
public void startEntity(String name) throws SAXException { |
|
199 |
} |
|
200 |
||
201 |
} |