Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | * |
aoqi@0 | 25 | * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
aoqi@0 | 26 | */ |
aoqi@0 | 27 | |
aoqi@0 | 28 | package com.sun.xml.internal.fastinfoset.tools; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import com.sun.xml.internal.fastinfoset.QualifiedName; |
aoqi@0 | 31 | import java.util.ArrayList; |
aoqi@0 | 32 | import java.util.logging.Level; |
aoqi@0 | 33 | import java.util.logging.Logger; |
aoqi@0 | 34 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 35 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 36 | import org.xml.sax.Attributes; |
aoqi@0 | 37 | import org.xml.sax.Locator; |
aoqi@0 | 38 | import org.xml.sax.SAXException; |
aoqi@0 | 39 | import org.xml.sax.ext.LexicalHandler; |
aoqi@0 | 40 | import org.xml.sax.helpers.DefaultHandler; |
aoqi@0 | 41 | |
aoqi@0 | 42 | public class SAX2StAXWriter extends DefaultHandler implements LexicalHandler { |
aoqi@0 | 43 | private static final Logger logger = Logger.getLogger(SAX2StAXWriter.class.getName()); |
aoqi@0 | 44 | |
aoqi@0 | 45 | /** |
aoqi@0 | 46 | * XML stream writer where events are pushed. |
aoqi@0 | 47 | */ |
aoqi@0 | 48 | XMLStreamWriter _writer; |
aoqi@0 | 49 | |
aoqi@0 | 50 | /** |
aoqi@0 | 51 | * List of namespace decl for upcoming element. |
aoqi@0 | 52 | */ |
aoqi@0 | 53 | ArrayList _namespaces = new ArrayList(); |
aoqi@0 | 54 | |
aoqi@0 | 55 | public SAX2StAXWriter(XMLStreamWriter writer) { |
aoqi@0 | 56 | _writer = writer; |
aoqi@0 | 57 | } |
aoqi@0 | 58 | |
aoqi@0 | 59 | public XMLStreamWriter getWriter() { |
aoqi@0 | 60 | return _writer; |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | public void startDocument() throws SAXException { |
aoqi@0 | 64 | try { |
aoqi@0 | 65 | _writer.writeStartDocument(); |
aoqi@0 | 66 | } |
aoqi@0 | 67 | catch (XMLStreamException e) { |
aoqi@0 | 68 | throw new SAXException(e); |
aoqi@0 | 69 | } |
aoqi@0 | 70 | } |
aoqi@0 | 71 | |
aoqi@0 | 72 | public void endDocument() throws SAXException { |
aoqi@0 | 73 | try { |
aoqi@0 | 74 | _writer.writeEndDocument(); |
aoqi@0 | 75 | _writer.flush(); |
aoqi@0 | 76 | } |
aoqi@0 | 77 | catch (XMLStreamException e) { |
aoqi@0 | 78 | throw new SAXException(e); |
aoqi@0 | 79 | } |
aoqi@0 | 80 | } |
aoqi@0 | 81 | |
aoqi@0 | 82 | public void characters(char[] ch, int start, int length) |
aoqi@0 | 83 | throws SAXException |
aoqi@0 | 84 | { |
aoqi@0 | 85 | try { |
aoqi@0 | 86 | _writer.writeCharacters(ch, start, length); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | catch (XMLStreamException e) { |
aoqi@0 | 89 | throw new SAXException(e); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | public void startElement(String namespaceURI, String localName, |
aoqi@0 | 94 | String qName, Attributes atts) throws SAXException |
aoqi@0 | 95 | { |
aoqi@0 | 96 | try { |
aoqi@0 | 97 | int k = qName.indexOf(':'); |
aoqi@0 | 98 | String prefix = (k > 0) ? qName.substring(0, k) : ""; |
aoqi@0 | 99 | _writer.writeStartElement(prefix, localName, namespaceURI); |
aoqi@0 | 100 | |
aoqi@0 | 101 | int length = _namespaces.size(); |
aoqi@0 | 102 | for (int i = 0; i < length; i++) { |
aoqi@0 | 103 | QualifiedName nsh = (QualifiedName) _namespaces.get(i); |
aoqi@0 | 104 | _writer.writeNamespace(nsh.prefix, nsh.namespaceName); |
aoqi@0 | 105 | } |
aoqi@0 | 106 | _namespaces.clear(); |
aoqi@0 | 107 | |
aoqi@0 | 108 | length = atts.getLength(); |
aoqi@0 | 109 | for (int i = 0; i < length; i++) { |
aoqi@0 | 110 | _writer.writeAttribute(atts.getURI(i), |
aoqi@0 | 111 | atts.getLocalName(i), |
aoqi@0 | 112 | atts.getValue(i)); |
aoqi@0 | 113 | } |
aoqi@0 | 114 | } |
aoqi@0 | 115 | catch (XMLStreamException e) { |
aoqi@0 | 116 | throw new SAXException(e); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | public void endElement(String namespaceURI, String localName, |
aoqi@0 | 121 | String qName) throws SAXException |
aoqi@0 | 122 | { |
aoqi@0 | 123 | try { |
aoqi@0 | 124 | _writer.writeEndElement(); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | catch (XMLStreamException e) { |
aoqi@0 | 127 | logger.log(Level.FINE, "Exception on endElement", e); |
aoqi@0 | 128 | throw new SAXException(e); |
aoqi@0 | 129 | } |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | public void startPrefixMapping(String prefix, String uri) |
aoqi@0 | 133 | throws SAXException |
aoqi@0 | 134 | { |
aoqi@0 | 135 | // Commented as in StAX NS are declared for current element? |
aoqi@0 | 136 | // now _writer.setPrefix() is invoked in _writer.writeNamespace() |
aoqi@0 | 137 | // try { |
aoqi@0 | 138 | // _writer.setPrefix(prefix, uri); |
aoqi@0 | 139 | _namespaces.add(new QualifiedName(prefix, uri)); |
aoqi@0 | 140 | // } |
aoqi@0 | 141 | // catch (XMLStreamException e) { |
aoqi@0 | 142 | // throw new SAXException(e); |
aoqi@0 | 143 | // } |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | public void endPrefixMapping(String prefix) throws SAXException { |
aoqi@0 | 147 | } |
aoqi@0 | 148 | |
aoqi@0 | 149 | public void ignorableWhitespace(char[] ch, int start, int length) |
aoqi@0 | 150 | throws SAXException |
aoqi@0 | 151 | { |
aoqi@0 | 152 | characters(ch, start, length); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | public void processingInstruction(String target, String data) |
aoqi@0 | 156 | throws SAXException |
aoqi@0 | 157 | { |
aoqi@0 | 158 | try { |
aoqi@0 | 159 | _writer.writeProcessingInstruction(target, data); |
aoqi@0 | 160 | } |
aoqi@0 | 161 | catch (XMLStreamException e) { |
aoqi@0 | 162 | throw new SAXException(e); |
aoqi@0 | 163 | } |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | public void setDocumentLocator(Locator locator) { |
aoqi@0 | 167 | } |
aoqi@0 | 168 | |
aoqi@0 | 169 | public void skippedEntity(String name) throws SAXException { |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | public void comment(char[] ch, int start, int length) |
aoqi@0 | 173 | throws SAXException |
aoqi@0 | 174 | { |
aoqi@0 | 175 | try { |
aoqi@0 | 176 | _writer.writeComment(new String(ch, start, length)); |
aoqi@0 | 177 | } |
aoqi@0 | 178 | catch (XMLStreamException e) { |
aoqi@0 | 179 | throw new SAXException(e); |
aoqi@0 | 180 | } |
aoqi@0 | 181 | } |
aoqi@0 | 182 | |
aoqi@0 | 183 | public void endCDATA() throws SAXException { |
aoqi@0 | 184 | } |
aoqi@0 | 185 | |
aoqi@0 | 186 | public void endDTD() throws SAXException { |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | public void endEntity(String name) throws SAXException { |
aoqi@0 | 190 | } |
aoqi@0 | 191 | |
aoqi@0 | 192 | public void startCDATA() throws SAXException { |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | public void startDTD(String name, String publicId, String systemId) throws SAXException { |
aoqi@0 | 196 | } |
aoqi@0 | 197 | |
aoqi@0 | 198 | public void startEntity(String name) throws SAXException { |
aoqi@0 | 199 | } |
aoqi@0 | 200 | |
aoqi@0 | 201 | } |