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

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 650
121e938cb9c3
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aefimov@650 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
aoqi@0 27
aoqi@0 28 import com.sun.xml.internal.bind.Util;
aoqi@0 29 import javax.xml.bind.JAXBException;
aoqi@0 30 import javax.xml.bind.UnmarshallerHandler;
aoqi@0 31
aoqi@0 32 import com.sun.xml.internal.bind.WhiteSpaceProcessor;
aoqi@0 33 import com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl;
aoqi@0 34 import java.util.logging.Level;
aoqi@0 35 import java.util.logging.Logger;
aoqi@0 36
aoqi@0 37 import org.xml.sax.Attributes;
aoqi@0 38 import org.xml.sax.Locator;
aoqi@0 39 import org.xml.sax.SAXException;
aoqi@0 40
aoqi@0 41 /**
aoqi@0 42 * Receives SAX events and convert them to our internal events.
aoqi@0 43 *
aoqi@0 44 * @author Kohsuke Kawaguchi
aoqi@0 45 */
aoqi@0 46 public final class SAXConnector implements UnmarshallerHandler {
aoqi@0 47
aoqi@0 48 private LocatorEx loc;
aoqi@0 49
aoqi@0 50 private static final Logger logger = Util.getClassLogger();
aoqi@0 51
aoqi@0 52 /**
aoqi@0 53 * SAX may fire consecutive characters event, but we don't allow it.
aoqi@0 54 * so use this buffer to perform buffering.
aoqi@0 55 */
aoqi@0 56 private final StringBuilder buffer = new StringBuilder();
aoqi@0 57
aoqi@0 58 private final XmlVisitor next;
aoqi@0 59 private final UnmarshallingContext context;
aoqi@0 60 private final XmlVisitor.TextPredictor predictor;
aoqi@0 61
aoqi@0 62 private static final class TagNameImpl extends TagName {
aoqi@0 63 String qname;
aoqi@0 64 @Override
aoqi@0 65 public String getQname() {
aoqi@0 66 return qname;
aoqi@0 67 }
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 private final TagNameImpl tagName = new TagNameImpl();
aoqi@0 71
aoqi@0 72 /**
aoqi@0 73 * @param externalLocator
aoqi@0 74 * If the caller is producing SAX events from sources other than Unicode and angle brackets,
aoqi@0 75 * the caller can override the default SAX {@link Locator} object by this object
aoqi@0 76 * to provide better location information.
aoqi@0 77 */
aoqi@0 78 public SAXConnector(XmlVisitor next, LocatorEx externalLocator ) {
aoqi@0 79 this.next = next;
aoqi@0 80 this.context = next.getContext();
aoqi@0 81 this.predictor = next.getPredictor();
aoqi@0 82 this.loc = externalLocator;
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 @Override
aoqi@0 86 public Object getResult() throws JAXBException, IllegalStateException {
aoqi@0 87 return context.getResult();
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 public UnmarshallingContext getContext() {
aoqi@0 91 return context;
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 @Override
aoqi@0 95 public void setDocumentLocator(final Locator locator) {
aoqi@0 96 if(loc!=null)
aoqi@0 97 return; // we already have an external locator. ignore.
aoqi@0 98
aoqi@0 99 this.loc = new LocatorExWrapper(locator);
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 @Override
aoqi@0 103 public void startDocument() throws SAXException {
aoqi@0 104 if (logger.isLoggable(Level.FINER)) {
aoqi@0 105 logger.log(Level.FINER, "SAXConnector.startDocument");
aoqi@0 106 }
aoqi@0 107 next.startDocument(loc,null);
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 @Override
aoqi@0 111 public void endDocument() throws SAXException {
aoqi@0 112 if (logger.isLoggable(Level.FINER)) {
aoqi@0 113 logger.log(Level.FINER, "SAXConnector.endDocument");
aoqi@0 114 }
aoqi@0 115 next.endDocument();
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 @Override
aoqi@0 119 public void startPrefixMapping(String prefix, String uri) throws SAXException {
aoqi@0 120 if (logger.isLoggable(Level.FINER)) {
aoqi@0 121 logger.log(Level.FINER, "SAXConnector.startPrefixMapping: {0}:{1}", new Object[]{prefix, uri});
aoqi@0 122 }
aoqi@0 123 next.startPrefixMapping(prefix,uri);
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 @Override
aoqi@0 127 public void endPrefixMapping(String prefix) throws SAXException {
aoqi@0 128 if (logger.isLoggable(Level.FINER)) {
aoqi@0 129 logger.log(Level.FINER, "SAXConnector.endPrefixMapping: {0}", new Object[]{prefix});
aoqi@0 130 }
aoqi@0 131 next.endPrefixMapping(prefix);
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 @Override
aoqi@0 135 public void startElement(String uri, String local, String qname, Attributes atts) throws SAXException {
aoqi@0 136 if (logger.isLoggable(Level.FINER)) {
aoqi@0 137 logger.log(Level.FINER, "SAXConnector.startElement: {0}:{1}:{2}, attrs: {3}", new Object[]{uri, local, qname, atts});
aoqi@0 138 }
aoqi@0 139 // work gracefully with misconfigured parsers that don't support namespaces
aoqi@0 140 if( uri==null || uri.length()==0 )
aoqi@0 141 uri="";
aoqi@0 142 if( local==null || local.length()==0 )
aoqi@0 143 local=qname;
aoqi@0 144 if( qname==null || qname.length()==0 )
aoqi@0 145 qname=local;
aoqi@0 146
aefimov@650 147 processText(!context.getCurrentState().isMixed());
aoqi@0 148
aoqi@0 149 tagName.uri = uri;
aoqi@0 150 tagName.local = local;
aoqi@0 151 tagName.qname = qname;
aoqi@0 152 tagName.atts = atts;
aoqi@0 153 next.startElement(tagName);
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 @Override
aoqi@0 157 public void endElement(String uri, String localName, String qName) throws SAXException {
aoqi@0 158 if (logger.isLoggable(Level.FINER)) {
aoqi@0 159 logger.log(Level.FINER, "SAXConnector.startElement: {0}:{1}:{2}", new Object[]{uri, localName, qName});
aoqi@0 160 }
aoqi@0 161 processText(false);
aoqi@0 162 tagName.uri = uri;
aoqi@0 163 tagName.local = localName;
aoqi@0 164 tagName.qname = qName;
aoqi@0 165 next.endElement(tagName);
aoqi@0 166 }
aoqi@0 167
aoqi@0 168
aoqi@0 169 @Override
aoqi@0 170 public final void characters( char[] buf, int start, int len ) {
aoqi@0 171 if (logger.isLoggable(Level.FINEST)) {
aoqi@0 172 logger.log(Level.FINEST, "SAXConnector.characters: {0}", buf);
aoqi@0 173 }
aoqi@0 174 if( predictor.expectText() )
aoqi@0 175 buffer.append(buf,start,len);
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 @Override
aoqi@0 179 public final void ignorableWhitespace( char[] buf, int start, int len ) {
aoqi@0 180 if (logger.isLoggable(Level.FINEST)) {
aoqi@0 181 logger.log(Level.FINEST, "SAXConnector.characters{0}", buf);
aoqi@0 182 }
aoqi@0 183 characters(buf,start,len);
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 @Override
aoqi@0 187 public void processingInstruction(String target, String data) {
aoqi@0 188 // nop
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 @Override
aoqi@0 192 public void skippedEntity(String name) {
aoqi@0 193 // nop
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 private void processText( boolean ignorable ) throws SAXException {
aefimov@650 197 if (predictor.expectText() && (!ignorable || !WhiteSpaceProcessor.isWhiteSpace(buffer)))
aoqi@0 198 next.text(buffer);
aoqi@0 199 buffer.setLength(0);
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 }

mercurial