src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/SAXOutput.java

changeset 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/bind/v2/runtime/output/SAXOutput.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,173 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 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 +
    1.29 +package com.sun.xml.internal.bind.v2.runtime.output;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +
    1.33 +import javax.xml.stream.XMLStreamException;
    1.34 +
    1.35 +import com.sun.xml.internal.bind.util.AttributesImpl;
    1.36 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    1.37 +
    1.38 +import org.xml.sax.ContentHandler;
    1.39 +import org.xml.sax.SAXException;
    1.40 +import org.xml.sax.helpers.LocatorImpl;
    1.41 +
    1.42 +/**
    1.43 + * {@link XmlOutput} implementation that writes to SAX {@link ContentHandler}.
    1.44 + *
    1.45 + * @author Kohsuke Kawaguchi
    1.46 + */
    1.47 +public class SAXOutput extends XmlOutputAbstractImpl {
    1.48 +    protected final ContentHandler out;
    1.49 +
    1.50 +    public SAXOutput(ContentHandler out) {
    1.51 +        this.out = out;
    1.52 +        out.setDocumentLocator(new LocatorImpl());
    1.53 +    }
    1.54 +
    1.55 +    private String elementNsUri,elementLocalName,elementQName;
    1.56 +
    1.57 +    private char[] buf = new char[256];
    1.58 +
    1.59 +    private final AttributesImpl atts = new AttributesImpl();
    1.60 +
    1.61 +
    1.62 +    // not called if we are generating fragments
    1.63 +    @Override
    1.64 +    public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws SAXException, IOException, XMLStreamException {
    1.65 +        super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext);
    1.66 +        if(!fragment)
    1.67 +            out.startDocument();
    1.68 +    }
    1.69 +
    1.70 +    public void endDocument(boolean fragment) throws SAXException, IOException, XMLStreamException {
    1.71 +        if(!fragment)
    1.72 +            out.endDocument();
    1.73 +        super.endDocument(fragment);
    1.74 +    }
    1.75 +
    1.76 +    public void beginStartTag(int prefix, String localName) {
    1.77 +        elementNsUri = nsContext.getNamespaceURI(prefix);
    1.78 +        elementLocalName = localName;
    1.79 +        elementQName = getQName(prefix,localName);
    1.80 +        atts.clear();
    1.81 +    }
    1.82 +
    1.83 +    public void attribute(int prefix, String localName, String value) {
    1.84 +        String qname;
    1.85 +        String nsUri;
    1.86 +        if(prefix==-1) {
    1.87 +            nsUri = "";
    1.88 +            qname = localName;
    1.89 +        } else {
    1.90 +            nsUri = nsContext.getNamespaceURI(prefix);
    1.91 +            String p = nsContext.getPrefix(prefix);
    1.92 +            if(p.length()==0)
    1.93 +                // this is more likely a bug in the application code (NamespacePrefixMapper implementation)
    1.94 +                // this only happens when it tries to assign "" prefix to a non-"" URI,
    1.95 +                // which is by itself violation of namespace rec. But let's just be safe.
    1.96 +                // See http://forums.java.net/jive/thread.jspa?messageID=212598#212598
    1.97 +                qname = localName;
    1.98 +            else
    1.99 +                qname = p +':'+localName;
   1.100 +        }
   1.101 +        atts.addAttribute( nsUri, localName, qname, "CDATA", value );
   1.102 +    }
   1.103 +
   1.104 +    public void endStartTag() throws SAXException {
   1.105 +        NamespaceContextImpl.Element ns = nsContext.getCurrent();
   1.106 +        if(ns!=null) {
   1.107 +            int sz = ns.count();
   1.108 +            for( int i=0; i<sz; i++ ) {
   1.109 +                String p = ns.getPrefix(i);
   1.110 +                String uri = ns.getNsUri(i);
   1.111 +                if(uri.length()==0 && ns.getBase()==1)
   1.112 +                    continue;   // no point in defining xmlns='' on the root
   1.113 +                out.startPrefixMapping(p,uri);
   1.114 +            }
   1.115 +        }
   1.116 +        out.startElement(elementNsUri,elementLocalName,elementQName,atts);
   1.117 +    }
   1.118 +
   1.119 +    public void endTag(int prefix, String localName) throws SAXException {
   1.120 +        out.endElement(
   1.121 +            nsContext.getNamespaceURI(prefix),
   1.122 +            localName,
   1.123 +            getQName(prefix, localName)
   1.124 +        );
   1.125 +
   1.126 +        NamespaceContextImpl.Element ns = nsContext.getCurrent();
   1.127 +        if(ns!=null) {
   1.128 +            int sz = ns.count();
   1.129 +            for( int i=sz-1; i>=0; i-- ) {
   1.130 +                String p = ns.getPrefix(i);
   1.131 +                String uri = ns.getNsUri(i);
   1.132 +                if(uri.length()==0 && ns.getBase()==1)
   1.133 +                    continue;   // no point in definint xmlns='' on the root
   1.134 +                out.endPrefixMapping(p);
   1.135 +            }
   1.136 +        }
   1.137 +    }
   1.138 +
   1.139 +    private String getQName(int prefix, String localName) {
   1.140 +        String qname;
   1.141 +        String p = nsContext.getPrefix(prefix);
   1.142 +        if(p.length()==0)
   1.143 +            qname = localName;
   1.144 +        else
   1.145 +            qname = p+':'+localName;
   1.146 +        return qname;
   1.147 +    }
   1.148 +
   1.149 +    public void text(String value, boolean needsSP) throws IOException, SAXException, XMLStreamException {
   1.150 +        int vlen = value.length();
   1.151 +        if(buf.length<=vlen) {
   1.152 +            buf = new char[Math.max(buf.length*2,vlen+1)];
   1.153 +        }
   1.154 +        if(needsSP) {
   1.155 +            value.getChars(0,vlen,buf,1);
   1.156 +            buf[0] = ' ';
   1.157 +        } else {
   1.158 +            value.getChars(0,vlen,buf,0);
   1.159 +        }
   1.160 +        out.characters(buf,0,vlen+(needsSP?1:0));
   1.161 +    }
   1.162 +
   1.163 +    public void text(Pcdata value, boolean needsSP) throws IOException, SAXException, XMLStreamException {
   1.164 +        int vlen = value.length();
   1.165 +        if(buf.length<=vlen) {
   1.166 +            buf = new char[Math.max(buf.length*2,vlen+1)];
   1.167 +        }
   1.168 +        if(needsSP) {
   1.169 +            value.writeTo(buf,1);
   1.170 +            buf[0] = ' ';
   1.171 +        } else {
   1.172 +            value.writeTo(buf,0);
   1.173 +        }
   1.174 +        out.characters(buf,0,vlen+(needsSP?1:0));
   1.175 +    }
   1.176 +}

mercurial