src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/tools/SAX2StAXWriter.java

changeset 286
f50545b5e2f1
parent 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/tools/SAX2StAXWriter.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,201 @@
     1.4 +/*
     1.5 + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + *
    1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    1.29 + */
    1.30 +
    1.31 +package com.sun.xml.internal.fastinfoset.tools;
    1.32 +
    1.33 +import com.sun.xml.internal.fastinfoset.QualifiedName;
    1.34 +import java.util.ArrayList;
    1.35 +import java.util.logging.Level;
    1.36 +import java.util.logging.Logger;
    1.37 +import javax.xml.stream.XMLStreamException;
    1.38 +import javax.xml.stream.XMLStreamWriter;
    1.39 +import org.xml.sax.Attributes;
    1.40 +import org.xml.sax.Locator;
    1.41 +import org.xml.sax.SAXException;
    1.42 +import org.xml.sax.ext.LexicalHandler;
    1.43 +import org.xml.sax.helpers.DefaultHandler;
    1.44 +
    1.45 +public class SAX2StAXWriter extends DefaultHandler implements LexicalHandler {
    1.46 +    private static final Logger logger = Logger.getLogger(SAX2StAXWriter.class.getName());
    1.47 +
    1.48 +    /**
    1.49 +     * XML stream writer where events are pushed.
    1.50 +     */
    1.51 +    XMLStreamWriter _writer;
    1.52 +
    1.53 +    /**
    1.54 +     * List of namespace decl for upcoming element.
    1.55 +     */
    1.56 +    ArrayList _namespaces = new ArrayList();
    1.57 +
    1.58 +    public SAX2StAXWriter(XMLStreamWriter writer) {
    1.59 +        _writer = writer;
    1.60 +    }
    1.61 +
    1.62 +    public XMLStreamWriter getWriter() {
    1.63 +        return _writer;
    1.64 +    }
    1.65 +
    1.66 +    public void startDocument() throws SAXException {
    1.67 +        try {
    1.68 +            _writer.writeStartDocument();
    1.69 +        }
    1.70 +        catch (XMLStreamException e) {
    1.71 +            throw new SAXException(e);
    1.72 +        }
    1.73 +    }
    1.74 +
    1.75 +    public void endDocument() throws SAXException {
    1.76 +        try {
    1.77 +            _writer.writeEndDocument();
    1.78 +            _writer.flush();
    1.79 +        }
    1.80 +        catch (XMLStreamException e) {
    1.81 +            throw new SAXException(e);
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +    public void characters(char[] ch, int start, int length)
    1.86 +        throws SAXException
    1.87 +    {
    1.88 +        try {
    1.89 +            _writer.writeCharacters(ch, start, length);
    1.90 +        }
    1.91 +        catch (XMLStreamException e) {
    1.92 +            throw new SAXException(e);
    1.93 +        }
    1.94 +    }
    1.95 +
    1.96 +    public void startElement(String namespaceURI, String localName,
    1.97 +        String qName, Attributes atts) throws SAXException
    1.98 +    {
    1.99 +        try {
   1.100 +            int k = qName.indexOf(':');
   1.101 +            String prefix = (k > 0) ? qName.substring(0, k) : "";
   1.102 +            _writer.writeStartElement(prefix, localName, namespaceURI);
   1.103 +
   1.104 +            int length = _namespaces.size();
   1.105 +            for (int i = 0; i < length; i++) {
   1.106 +                QualifiedName nsh = (QualifiedName) _namespaces.get(i);
   1.107 +                _writer.writeNamespace(nsh.prefix, nsh.namespaceName);
   1.108 +            }
   1.109 +            _namespaces.clear();
   1.110 +
   1.111 +            length = atts.getLength();
   1.112 +            for (int i = 0; i < length; i++) {
   1.113 +                _writer.writeAttribute(atts.getURI(i),
   1.114 +                                       atts.getLocalName(i),
   1.115 +                                       atts.getValue(i));
   1.116 +            }
   1.117 +        }
   1.118 +        catch (XMLStreamException e) {
   1.119 +            throw new SAXException(e);
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    public void endElement(String namespaceURI, String localName,
   1.124 +        String qName) throws SAXException
   1.125 +    {
   1.126 +        try {
   1.127 +            _writer.writeEndElement();
   1.128 +        }
   1.129 +        catch (XMLStreamException e) {
   1.130 +            logger.log(Level.FINE, "Exception on endElement", e);
   1.131 +            throw new SAXException(e);
   1.132 +        }
   1.133 +    }
   1.134 +
   1.135 +    public void startPrefixMapping(String prefix, String uri)
   1.136 +        throws SAXException
   1.137 +    {
   1.138 +    // Commented as in StAX NS are declared for current element?
   1.139 +    // now _writer.setPrefix() is invoked in _writer.writeNamespace()
   1.140 +//        try {
   1.141 +//            _writer.setPrefix(prefix, uri);
   1.142 +            _namespaces.add(new QualifiedName(prefix, uri));
   1.143 +//        }
   1.144 +//        catch (XMLStreamException e) {
   1.145 +//            throw new SAXException(e);
   1.146 +//        }
   1.147 +    }
   1.148 +
   1.149 +    public void endPrefixMapping(String prefix) throws SAXException {
   1.150 +    }
   1.151 +
   1.152 +    public void ignorableWhitespace(char[] ch, int start, int length)
   1.153 +        throws SAXException
   1.154 +    {
   1.155 +        characters(ch, start, length);
   1.156 +    }
   1.157 +
   1.158 +    public void processingInstruction(String target, String data)
   1.159 +        throws SAXException
   1.160 +    {
   1.161 +        try {
   1.162 +            _writer.writeProcessingInstruction(target, data);
   1.163 +        }
   1.164 +        catch (XMLStreamException e) {
   1.165 +            throw new SAXException(e);
   1.166 +        }
   1.167 +    }
   1.168 +
   1.169 +    public void setDocumentLocator(Locator locator) {
   1.170 +    }
   1.171 +
   1.172 +    public void skippedEntity(String name) throws SAXException {
   1.173 +    }
   1.174 +
   1.175 +    public void comment(char[] ch, int start, int length)
   1.176 +        throws SAXException
   1.177 +    {
   1.178 +        try {
   1.179 +            _writer.writeComment(new String(ch, start, length));
   1.180 +        }
   1.181 +        catch (XMLStreamException e) {
   1.182 +            throw new SAXException(e);
   1.183 +        }
   1.184 +    }
   1.185 +
   1.186 +    public void endCDATA() throws SAXException {
   1.187 +    }
   1.188 +
   1.189 +    public void endDTD() throws SAXException {
   1.190 +    }
   1.191 +
   1.192 +    public void endEntity(String name) throws SAXException {
   1.193 +    }
   1.194 +
   1.195 +    public void startCDATA() throws SAXException {
   1.196 +    }
   1.197 +
   1.198 +    public void startDTD(String name, String publicId, String systemId) throws SAXException {
   1.199 +    }
   1.200 +
   1.201 +    public void startEntity(String name) throws SAXException {
   1.202 +    }
   1.203 +
   1.204 +}

mercurial