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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     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/unmarshaller/SAXConnector.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,212 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, 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.unmarshaller;
    1.30 +
    1.31 +import com.sun.xml.internal.bind.Util;
    1.32 +import javax.xml.bind.JAXBException;
    1.33 +import javax.xml.bind.UnmarshallerHandler;
    1.34 +
    1.35 +import com.sun.xml.internal.bind.WhiteSpaceProcessor;
    1.36 +import com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl;
    1.37 +import java.util.logging.Level;
    1.38 +import java.util.logging.Logger;
    1.39 +
    1.40 +import org.xml.sax.Attributes;
    1.41 +import org.xml.sax.Locator;
    1.42 +import org.xml.sax.SAXException;
    1.43 +
    1.44 +/**
    1.45 + * Receives SAX events and convert them to our internal events.
    1.46 + *
    1.47 + * @author Kohsuke Kawaguchi
    1.48 + */
    1.49 +public final class SAXConnector implements UnmarshallerHandler {
    1.50 +
    1.51 +    private LocatorEx loc;
    1.52 +
    1.53 +    private static final Logger logger = Util.getClassLogger();
    1.54 +
    1.55 +    /**
    1.56 +     * SAX may fire consecutive characters event, but we don't allow it.
    1.57 +     * so use this buffer to perform buffering.
    1.58 +     */
    1.59 +    private final StringBuilder buffer = new StringBuilder();
    1.60 +
    1.61 +    private final XmlVisitor next;
    1.62 +    private final UnmarshallingContext context;
    1.63 +    private final XmlVisitor.TextPredictor predictor;
    1.64 +
    1.65 +    private static final class TagNameImpl extends TagName {
    1.66 +        String qname;
    1.67 +        @Override
    1.68 +        public String getQname() {
    1.69 +            return qname;
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    private final TagNameImpl tagName = new TagNameImpl();
    1.74 +
    1.75 +    /**
    1.76 +     * @param externalLocator
    1.77 +     *      If the caller is producing SAX events from sources other than Unicode and angle brackets,
    1.78 +     *      the caller can override the default SAX {@link Locator} object by this object
    1.79 +     *      to provide better location information.
    1.80 +     */
    1.81 +    public SAXConnector(XmlVisitor next, LocatorEx externalLocator ) {
    1.82 +        this.next = next;
    1.83 +        this.context = next.getContext();
    1.84 +        this.predictor = next.getPredictor();
    1.85 +        this.loc = externalLocator;
    1.86 +    }
    1.87 +
    1.88 +    @Override
    1.89 +    public Object getResult() throws JAXBException, IllegalStateException {
    1.90 +        return context.getResult();
    1.91 +    }
    1.92 +
    1.93 +    public UnmarshallingContext getContext() {
    1.94 +        return context;
    1.95 +    }
    1.96 +
    1.97 +    @Override
    1.98 +    public void setDocumentLocator(final Locator locator) {
    1.99 +        if(loc!=null)
   1.100 +            return; // we already have an external locator. ignore.
   1.101 +
   1.102 +        this.loc = new LocatorExWrapper(locator);
   1.103 +    }
   1.104 +
   1.105 +    @Override
   1.106 +    public void startDocument() throws SAXException {
   1.107 +        if (logger.isLoggable(Level.FINER)) {
   1.108 +            logger.log(Level.FINER, "SAXConnector.startDocument");
   1.109 +        }
   1.110 +        next.startDocument(loc,null);
   1.111 +    }
   1.112 +
   1.113 +    @Override
   1.114 +    public void endDocument() throws SAXException {
   1.115 +        if (logger.isLoggable(Level.FINER)) {
   1.116 +            logger.log(Level.FINER, "SAXConnector.endDocument");
   1.117 +        }
   1.118 +        next.endDocument();
   1.119 +    }
   1.120 +
   1.121 +    @Override
   1.122 +    public void startPrefixMapping(String prefix, String uri) throws SAXException {
   1.123 +        if (logger.isLoggable(Level.FINER)) {
   1.124 +            logger.log(Level.FINER, "SAXConnector.startPrefixMapping: {0}:{1}", new Object[]{prefix, uri});
   1.125 +        }
   1.126 +        next.startPrefixMapping(prefix,uri);
   1.127 +    }
   1.128 +
   1.129 +    @Override
   1.130 +    public void endPrefixMapping(String prefix) throws SAXException {
   1.131 +        if (logger.isLoggable(Level.FINER)) {
   1.132 +            logger.log(Level.FINER, "SAXConnector.endPrefixMapping: {0}", new Object[]{prefix});
   1.133 +        }
   1.134 +        next.endPrefixMapping(prefix);
   1.135 +    }
   1.136 +
   1.137 +    @Override
   1.138 +    public void startElement(String uri, String local, String qname, Attributes atts) throws SAXException {
   1.139 +        if (logger.isLoggable(Level.FINER)) {
   1.140 +            logger.log(Level.FINER, "SAXConnector.startElement: {0}:{1}:{2}, attrs: {3}", new Object[]{uri, local, qname, atts});
   1.141 +        }
   1.142 +        // work gracefully with misconfigured parsers that don't support namespaces
   1.143 +        if( uri==null || uri.length()==0 )
   1.144 +            uri="";
   1.145 +        if( local==null || local.length()==0 )
   1.146 +            local=qname;
   1.147 +        if( qname==null || qname.length()==0 )
   1.148 +            qname=local;
   1.149 +
   1.150 +
   1.151 +        boolean ignorable = true;
   1.152 +        StructureLoader sl;
   1.153 +
   1.154 +        // not null only if element content is processed (StructureLoader is used)
   1.155 +        // ugly
   1.156 +        if((sl = this.context.getStructureLoader()) != null) {
   1.157 +            ignorable = ((ClassBeanInfoImpl)sl.getBeanInfo()).hasElementOnlyContentModel();
   1.158 +        }
   1.159 +
   1.160 +        processText(ignorable);
   1.161 +
   1.162 +        tagName.uri = uri;
   1.163 +        tagName.local = local;
   1.164 +        tagName.qname = qname;
   1.165 +        tagName.atts = atts;
   1.166 +        next.startElement(tagName);
   1.167 +    }
   1.168 +
   1.169 +    @Override
   1.170 +    public void endElement(String uri, String localName, String qName) throws SAXException {
   1.171 +        if (logger.isLoggable(Level.FINER)) {
   1.172 +            logger.log(Level.FINER, "SAXConnector.startElement: {0}:{1}:{2}", new Object[]{uri, localName, qName});
   1.173 +        }
   1.174 +        processText(false);
   1.175 +        tagName.uri = uri;
   1.176 +        tagName.local = localName;
   1.177 +        tagName.qname = qName;
   1.178 +        next.endElement(tagName);
   1.179 +    }
   1.180 +
   1.181 +
   1.182 +    @Override
   1.183 +    public final void characters( char[] buf, int start, int len ) {
   1.184 +        if (logger.isLoggable(Level.FINEST)) {
   1.185 +            logger.log(Level.FINEST, "SAXConnector.characters: {0}", buf);
   1.186 +        }
   1.187 +        if( predictor.expectText() )
   1.188 +            buffer.append(buf,start,len);
   1.189 +    }
   1.190 +
   1.191 +    @Override
   1.192 +    public final void ignorableWhitespace( char[] buf, int start, int len ) {
   1.193 +        if (logger.isLoggable(Level.FINEST)) {
   1.194 +            logger.log(Level.FINEST, "SAXConnector.characters{0}", buf);
   1.195 +        }
   1.196 +        characters(buf,start,len);
   1.197 +    }
   1.198 +
   1.199 +    @Override
   1.200 +    public void processingInstruction(String target, String data) {
   1.201 +        // nop
   1.202 +    }
   1.203 +
   1.204 +    @Override
   1.205 +    public void skippedEntity(String name) {
   1.206 +        // nop
   1.207 +    }
   1.208 +
   1.209 +    private void processText( boolean ignorable ) throws SAXException {
   1.210 +        if( predictor.expectText() && (!ignorable || !WhiteSpaceProcessor.isWhiteSpace(buffer)))
   1.211 +            next.text(buffer);
   1.212 +        buffer.setLength(0);
   1.213 +    }
   1.214 +
   1.215 +}

mercurial