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

changeset 0
373ffda63c9a
child 658
45676aaa9d47
     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/ContentHandlerAdaptor.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,158 @@
     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;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +
    1.33 +import javax.xml.stream.XMLStreamException;
    1.34 +
    1.35 +import com.sun.istack.internal.FinalArrayList;
    1.36 +import com.sun.istack.internal.SAXException2;
    1.37 +
    1.38 +import org.xml.sax.Attributes;
    1.39 +import org.xml.sax.SAXException;
    1.40 +import org.xml.sax.helpers.DefaultHandler;
    1.41 +
    1.42 +/**
    1.43 + * Receives SAX2 events and send the equivalent events to
    1.44 + * {@link XMLSerializer}
    1.45 + *
    1.46 + * @author
    1.47 + *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.48 + */
    1.49 +final class ContentHandlerAdaptor extends DefaultHandler {
    1.50 +
    1.51 +    /** Stores newly declared prefix-URI mapping. */
    1.52 +    private final FinalArrayList<String> prefixMap = new FinalArrayList<String>();
    1.53 +
    1.54 +    /** Events will be sent to this object. */
    1.55 +    private final XMLSerializer serializer;
    1.56 +
    1.57 +    private final StringBuffer text = new StringBuffer();
    1.58 +
    1.59 +
    1.60 +    ContentHandlerAdaptor( XMLSerializer _serializer ) {
    1.61 +        this.serializer = _serializer;
    1.62 +    }
    1.63 +
    1.64 +    public void startDocument() {
    1.65 +        prefixMap.clear();
    1.66 +    }
    1.67 +
    1.68 +    public void startPrefixMapping(String prefix, String uri) {
    1.69 +        prefixMap.add(prefix);
    1.70 +        prefixMap.add(uri);
    1.71 +    }
    1.72 +
    1.73 +    private boolean containsPrefixMapping(String prefix, String uri) {
    1.74 +        for( int i=0; i<prefixMap.size(); i+=2 ) {
    1.75 +            if(prefixMap.get(i).equals(prefix)
    1.76 +            && prefixMap.get(i+1).equals(uri))
    1.77 +                return true;
    1.78 +        }
    1.79 +        return false;
    1.80 +    }
    1.81 +
    1.82 +    public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
    1.83 +        throws SAXException {
    1.84 +        try {
    1.85 +            flushText();
    1.86 +
    1.87 +            int len = atts.getLength();
    1.88 +
    1.89 +            String p = getPrefix(qName);
    1.90 +
    1.91 +            // is this prefix going to be declared on this element?
    1.92 +            if(containsPrefixMapping(p,namespaceURI))
    1.93 +                serializer.startElementForce(namespaceURI,localName,p,null);
    1.94 +            else
    1.95 +                serializer.startElement(namespaceURI,localName, p,null);
    1.96 +
    1.97 +            // declare namespace events
    1.98 +            for( int i=0; i<prefixMap.size(); i+=2 ) {
    1.99 +                // forcibly set this binding, instead of using declareNsUri.
   1.100 +                // this guarantees that namespaces used in DOM will show up
   1.101 +                // as-is in the marshalled output (instead of reassigned to something else,
   1.102 +                // which may happen if you'd use declareNsUri.)
   1.103 +                serializer.getNamespaceContext().force(
   1.104 +                    prefixMap.get(i+1), prefixMap.get(i) );
   1.105 +            }
   1.106 +            // make sure namespaces needed by attributes are bound
   1.107 +            for( int i=0; i<len; i++ ) {
   1.108 +                String qname = atts.getQName(i);
   1.109 +                if(qname.startsWith("xmlns") || atts.getURI(i).length() == 0)
   1.110 +                    continue;
   1.111 +                String prefix = getPrefix(qname);
   1.112 +
   1.113 +                serializer.getNamespaceContext().declareNamespace(
   1.114 +                    atts.getURI(i), prefix, true );
   1.115 +            }
   1.116 +
   1.117 +            serializer.endNamespaceDecls(null);
   1.118 +            // fire attribute events
   1.119 +            for( int i=0; i<len; i++ ) {
   1.120 +                // be defensive.
   1.121 +                if(atts.getQName(i).startsWith("xmlns"))
   1.122 +                    continue;
   1.123 +                serializer.attribute( atts.getURI(i), atts.getLocalName(i), atts.getValue(i));
   1.124 +            }
   1.125 +            prefixMap.clear();
   1.126 +            serializer.endAttributes();
   1.127 +        } catch (IOException e) {
   1.128 +            throw new SAXException2(e);
   1.129 +        } catch (XMLStreamException e) {
   1.130 +            throw new SAXException2(e);
   1.131 +        }
   1.132 +    }
   1.133 +
   1.134 +    private String getPrefix(String qname) {
   1.135 +        int idx = qname.indexOf(':');
   1.136 +        String prefix = (idx==-1)?qname:qname.substring(0,idx);
   1.137 +        return prefix;
   1.138 +    }
   1.139 +
   1.140 +    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
   1.141 +        try {
   1.142 +            flushText();
   1.143 +            serializer.endElement();
   1.144 +        } catch (IOException e) {
   1.145 +            throw new SAXException2(e);
   1.146 +        } catch (XMLStreamException e) {
   1.147 +            throw new SAXException2(e);
   1.148 +        }
   1.149 +    }
   1.150 +
   1.151 +    private void flushText() throws SAXException, IOException, XMLStreamException {
   1.152 +        if( text.length()!=0 ) {
   1.153 +            serializer.text(text.toString(),null);
   1.154 +            text.setLength(0);
   1.155 +        }
   1.156 +    }
   1.157 +
   1.158 +    public void characters(char[] ch, int start, int length) {
   1.159 +        text.append(ch,start,length);
   1.160 +    }
   1.161 +}

mercurial