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