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