Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 2004, 2011, 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 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26 */
28 package com.sun.xml.internal.fastinfoset.tools;
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;
42 public class SAX2StAXWriter extends DefaultHandler implements LexicalHandler {
43 private static final Logger logger = Logger.getLogger(SAX2StAXWriter.class.getName());
45 /**
46 * XML stream writer where events are pushed.
47 */
48 XMLStreamWriter _writer;
50 /**
51 * List of namespace decl for upcoming element.
52 */
53 ArrayList _namespaces = new ArrayList();
55 public SAX2StAXWriter(XMLStreamWriter writer) {
56 _writer = writer;
57 }
59 public XMLStreamWriter getWriter() {
60 return _writer;
61 }
63 public void startDocument() throws SAXException {
64 try {
65 _writer.writeStartDocument();
66 }
67 catch (XMLStreamException e) {
68 throw new SAXException(e);
69 }
70 }
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 }
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 }
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);
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();
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 }
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 }
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 }
146 public void endPrefixMapping(String prefix) throws SAXException {
147 }
149 public void ignorableWhitespace(char[] ch, int start, int length)
150 throws SAXException
151 {
152 characters(ch, start, length);
153 }
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 }
166 public void setDocumentLocator(Locator locator) {
167 }
169 public void skippedEntity(String name) throws SAXException {
170 }
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 }
183 public void endCDATA() throws SAXException {
184 }
186 public void endDTD() throws SAXException {
187 }
189 public void endEntity(String name) throws SAXException {
190 }
192 public void startCDATA() throws SAXException {
193 }
195 public void startDTD(String name, String publicId, String systemId) throws SAXException {
196 }
198 public void startEntity(String name) throws SAXException {
199 }
201 }