aoqi@0: /* aefimov@650: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime.unmarshaller; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.Util; aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: import javax.xml.bind.UnmarshallerHandler; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.WhiteSpaceProcessor; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: import org.xml.sax.Attributes; aoqi@0: import org.xml.sax.Locator; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * Receives SAX events and convert them to our internal events. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class SAXConnector implements UnmarshallerHandler { aoqi@0: aoqi@0: private LocatorEx loc; aoqi@0: aoqi@0: private static final Logger logger = Util.getClassLogger(); aoqi@0: aoqi@0: /** aoqi@0: * SAX may fire consecutive characters event, but we don't allow it. aoqi@0: * so use this buffer to perform buffering. aoqi@0: */ aoqi@0: private final StringBuilder buffer = new StringBuilder(); aoqi@0: aoqi@0: private final XmlVisitor next; aoqi@0: private final UnmarshallingContext context; aoqi@0: private final XmlVisitor.TextPredictor predictor; aoqi@0: aoqi@0: private static final class TagNameImpl extends TagName { aoqi@0: String qname; aoqi@0: @Override aoqi@0: public String getQname() { aoqi@0: return qname; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private final TagNameImpl tagName = new TagNameImpl(); aoqi@0: aoqi@0: /** aoqi@0: * @param externalLocator aoqi@0: * If the caller is producing SAX events from sources other than Unicode and angle brackets, aoqi@0: * the caller can override the default SAX {@link Locator} object by this object aoqi@0: * to provide better location information. aoqi@0: */ aoqi@0: public SAXConnector(XmlVisitor next, LocatorEx externalLocator ) { aoqi@0: this.next = next; aoqi@0: this.context = next.getContext(); aoqi@0: this.predictor = next.getPredictor(); aoqi@0: this.loc = externalLocator; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Object getResult() throws JAXBException, IllegalStateException { aoqi@0: return context.getResult(); aoqi@0: } aoqi@0: aoqi@0: public UnmarshallingContext getContext() { aoqi@0: return context; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void setDocumentLocator(final Locator locator) { aoqi@0: if(loc!=null) aoqi@0: return; // we already have an external locator. ignore. aoqi@0: aoqi@0: this.loc = new LocatorExWrapper(locator); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void startDocument() throws SAXException { aoqi@0: if (logger.isLoggable(Level.FINER)) { aoqi@0: logger.log(Level.FINER, "SAXConnector.startDocument"); aoqi@0: } aoqi@0: next.startDocument(loc,null); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void endDocument() throws SAXException { aoqi@0: if (logger.isLoggable(Level.FINER)) { aoqi@0: logger.log(Level.FINER, "SAXConnector.endDocument"); aoqi@0: } aoqi@0: next.endDocument(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void startPrefixMapping(String prefix, String uri) throws SAXException { aoqi@0: if (logger.isLoggable(Level.FINER)) { aoqi@0: logger.log(Level.FINER, "SAXConnector.startPrefixMapping: {0}:{1}", new Object[]{prefix, uri}); aoqi@0: } aoqi@0: next.startPrefixMapping(prefix,uri); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void endPrefixMapping(String prefix) throws SAXException { aoqi@0: if (logger.isLoggable(Level.FINER)) { aoqi@0: logger.log(Level.FINER, "SAXConnector.endPrefixMapping: {0}", new Object[]{prefix}); aoqi@0: } aoqi@0: next.endPrefixMapping(prefix); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void startElement(String uri, String local, String qname, Attributes atts) throws SAXException { aoqi@0: if (logger.isLoggable(Level.FINER)) { aoqi@0: logger.log(Level.FINER, "SAXConnector.startElement: {0}:{1}:{2}, attrs: {3}", new Object[]{uri, local, qname, atts}); aoqi@0: } aoqi@0: // work gracefully with misconfigured parsers that don't support namespaces aoqi@0: if( uri==null || uri.length()==0 ) aoqi@0: uri=""; aoqi@0: if( local==null || local.length()==0 ) aoqi@0: local=qname; aoqi@0: if( qname==null || qname.length()==0 ) aoqi@0: qname=local; aoqi@0: aefimov@650: processText(!context.getCurrentState().isMixed()); aoqi@0: aoqi@0: tagName.uri = uri; aoqi@0: tagName.local = local; aoqi@0: tagName.qname = qname; aoqi@0: tagName.atts = atts; aoqi@0: next.startElement(tagName); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void endElement(String uri, String localName, String qName) throws SAXException { aoqi@0: if (logger.isLoggable(Level.FINER)) { aoqi@0: logger.log(Level.FINER, "SAXConnector.startElement: {0}:{1}:{2}", new Object[]{uri, localName, qName}); aoqi@0: } aoqi@0: processText(false); aoqi@0: tagName.uri = uri; aoqi@0: tagName.local = localName; aoqi@0: tagName.qname = qName; aoqi@0: next.endElement(tagName); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: @Override aoqi@0: public final void characters( char[] buf, int start, int len ) { aoqi@0: if (logger.isLoggable(Level.FINEST)) { aoqi@0: logger.log(Level.FINEST, "SAXConnector.characters: {0}", buf); aoqi@0: } aoqi@0: if( predictor.expectText() ) aoqi@0: buffer.append(buf,start,len); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public final void ignorableWhitespace( char[] buf, int start, int len ) { aoqi@0: if (logger.isLoggable(Level.FINEST)) { aoqi@0: logger.log(Level.FINEST, "SAXConnector.characters{0}", buf); aoqi@0: } aoqi@0: characters(buf,start,len); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void processingInstruction(String target, String data) { aoqi@0: // nop aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void skippedEntity(String name) { aoqi@0: // nop aoqi@0: } aoqi@0: aoqi@0: private void processText( boolean ignorable ) throws SAXException { aefimov@650: if (predictor.expectText() && (!ignorable || !WhiteSpaceProcessor.isWhiteSpace(buffer))) aoqi@0: next.text(buffer); aoqi@0: buffer.setLength(0); aoqi@0: } aoqi@0: aoqi@0: }