aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.fastinfoset.tools; aoqi@0: aoqi@0: import com.sun.xml.internal.fastinfoset.QualifiedName; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: import org.xml.sax.Attributes; aoqi@0: import org.xml.sax.Locator; aoqi@0: import org.xml.sax.SAXException; aoqi@0: import org.xml.sax.ext.LexicalHandler; aoqi@0: import org.xml.sax.helpers.DefaultHandler; aoqi@0: aoqi@0: public class SAX2StAXWriter extends DefaultHandler implements LexicalHandler { aoqi@0: private static final Logger logger = Logger.getLogger(SAX2StAXWriter.class.getName()); aoqi@0: aoqi@0: /** aoqi@0: * XML stream writer where events are pushed. aoqi@0: */ aoqi@0: XMLStreamWriter _writer; aoqi@0: aoqi@0: /** aoqi@0: * List of namespace decl for upcoming element. aoqi@0: */ aoqi@0: ArrayList _namespaces = new ArrayList(); aoqi@0: aoqi@0: public SAX2StAXWriter(XMLStreamWriter writer) { aoqi@0: _writer = writer; aoqi@0: } aoqi@0: aoqi@0: public XMLStreamWriter getWriter() { aoqi@0: return _writer; aoqi@0: } aoqi@0: aoqi@0: public void startDocument() throws SAXException { aoqi@0: try { aoqi@0: _writer.writeStartDocument(); aoqi@0: } aoqi@0: catch (XMLStreamException e) { aoqi@0: throw new SAXException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void endDocument() throws SAXException { aoqi@0: try { aoqi@0: _writer.writeEndDocument(); aoqi@0: _writer.flush(); aoqi@0: } aoqi@0: catch (XMLStreamException e) { aoqi@0: throw new SAXException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void characters(char[] ch, int start, int length) aoqi@0: throws SAXException aoqi@0: { aoqi@0: try { aoqi@0: _writer.writeCharacters(ch, start, length); aoqi@0: } aoqi@0: catch (XMLStreamException e) { aoqi@0: throw new SAXException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void startElement(String namespaceURI, String localName, aoqi@0: String qName, Attributes atts) throws SAXException aoqi@0: { aoqi@0: try { aoqi@0: int k = qName.indexOf(':'); aoqi@0: String prefix = (k > 0) ? qName.substring(0, k) : ""; aoqi@0: _writer.writeStartElement(prefix, localName, namespaceURI); aoqi@0: aoqi@0: int length = _namespaces.size(); aoqi@0: for (int i = 0; i < length; i++) { aoqi@0: QualifiedName nsh = (QualifiedName) _namespaces.get(i); aoqi@0: _writer.writeNamespace(nsh.prefix, nsh.namespaceName); aoqi@0: } aoqi@0: _namespaces.clear(); aoqi@0: aoqi@0: length = atts.getLength(); aoqi@0: for (int i = 0; i < length; i++) { aoqi@0: _writer.writeAttribute(atts.getURI(i), aoqi@0: atts.getLocalName(i), aoqi@0: atts.getValue(i)); aoqi@0: } aoqi@0: } aoqi@0: catch (XMLStreamException e) { aoqi@0: throw new SAXException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void endElement(String namespaceURI, String localName, aoqi@0: String qName) throws SAXException aoqi@0: { aoqi@0: try { aoqi@0: _writer.writeEndElement(); aoqi@0: } aoqi@0: catch (XMLStreamException e) { aoqi@0: logger.log(Level.FINE, "Exception on endElement", e); aoqi@0: throw new SAXException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void startPrefixMapping(String prefix, String uri) aoqi@0: throws SAXException aoqi@0: { aoqi@0: // Commented as in StAX NS are declared for current element? aoqi@0: // now _writer.setPrefix() is invoked in _writer.writeNamespace() aoqi@0: // try { aoqi@0: // _writer.setPrefix(prefix, uri); aoqi@0: _namespaces.add(new QualifiedName(prefix, uri)); aoqi@0: // } aoqi@0: // catch (XMLStreamException e) { aoqi@0: // throw new SAXException(e); aoqi@0: // } aoqi@0: } aoqi@0: aoqi@0: public void endPrefixMapping(String prefix) throws SAXException { aoqi@0: } aoqi@0: aoqi@0: public void ignorableWhitespace(char[] ch, int start, int length) aoqi@0: throws SAXException aoqi@0: { aoqi@0: characters(ch, start, length); aoqi@0: } aoqi@0: aoqi@0: public void processingInstruction(String target, String data) aoqi@0: throws SAXException aoqi@0: { aoqi@0: try { aoqi@0: _writer.writeProcessingInstruction(target, data); aoqi@0: } aoqi@0: catch (XMLStreamException e) { aoqi@0: throw new SAXException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void setDocumentLocator(Locator locator) { aoqi@0: } aoqi@0: aoqi@0: public void skippedEntity(String name) throws SAXException { aoqi@0: } aoqi@0: aoqi@0: public void comment(char[] ch, int start, int length) aoqi@0: throws SAXException aoqi@0: { aoqi@0: try { aoqi@0: _writer.writeComment(new String(ch, start, length)); aoqi@0: } aoqi@0: catch (XMLStreamException e) { aoqi@0: throw new SAXException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void endCDATA() throws SAXException { aoqi@0: } aoqi@0: aoqi@0: public void endDTD() throws SAXException { aoqi@0: } aoqi@0: aoqi@0: public void endEntity(String name) throws SAXException { aoqi@0: } aoqi@0: aoqi@0: public void startCDATA() throws SAXException { aoqi@0: } aoqi@0: aoqi@0: public void startDTD(String name, String publicId, String systemId) throws SAXException { aoqi@0: } aoqi@0: aoqi@0: public void startEntity(String name) throws SAXException { aoqi@0: } aoqi@0: aoqi@0: }