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/StAXEventConnector.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,286 @@ 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 java.util.Iterator; 1.32 + 1.33 +import javax.xml.namespace.QName; 1.34 +import javax.xml.stream.Location; 1.35 +import javax.xml.stream.XMLEventReader; 1.36 +import javax.xml.stream.XMLStreamConstants; 1.37 +import javax.xml.stream.XMLStreamException; 1.38 +import javax.xml.stream.events.Attribute; 1.39 +import javax.xml.stream.events.Characters; 1.40 +import javax.xml.stream.events.EndElement; 1.41 +import javax.xml.stream.events.Namespace; 1.42 +import javax.xml.stream.events.StartElement; 1.43 +import javax.xml.stream.events.XMLEvent; 1.44 + 1.45 +import org.xml.sax.Attributes; 1.46 +import org.xml.sax.SAXException; 1.47 +import org.xml.sax.helpers.AttributesImpl; 1.48 + 1.49 +/** 1.50 + * This is a simple utility class that adapts StAX events from an 1.51 + * {@link XMLEventReader} to unmarshaller events on a 1.52 + * {@link XmlVisitor}, bridging between the two 1.53 + * parser technologies. 1.54 + * 1.55 + * @author Ryan.Shoemaker@Sun.COM 1.56 + * @version 1.0 1.57 + */ 1.58 +final class StAXEventConnector extends StAXConnector { 1.59 + 1.60 + // StAX event source 1.61 + private final XMLEventReader staxEventReader; 1.62 + 1.63 + /** Current event. */ 1.64 + private XMLEvent event; 1.65 + 1.66 + /** 1.67 + * Shared and reused {@link Attributes}. 1.68 + */ 1.69 + private final AttributesImpl attrs = new AttributesImpl(); 1.70 + 1.71 + /** 1.72 + * SAX may fire consective characters event, but we don't allow it. 1.73 + * so use this buffer to perform buffering. 1.74 + */ 1.75 + private final StringBuilder buffer = new StringBuilder(); 1.76 + 1.77 + private boolean seenText; 1.78 + 1.79 + /** 1.80 + * Construct a new StAX to SAX adapter that will convert a StAX event 1.81 + * stream into a SAX event stream. 1.82 + * 1.83 + * @param staxCore 1.84 + * StAX event source 1.85 + * @param visitor 1.86 + * sink 1.87 + */ 1.88 + public StAXEventConnector(XMLEventReader staxCore, XmlVisitor visitor) { 1.89 + super(visitor); 1.90 + staxEventReader = staxCore; 1.91 + } 1.92 + 1.93 + public void bridge() throws XMLStreamException { 1.94 + 1.95 + try { 1.96 + // remembers the nest level of elements to know when we are done. 1.97 + int depth=0; 1.98 + 1.99 + event = staxEventReader.peek(); 1.100 + 1.101 + if( !event.isStartDocument() && !event.isStartElement() ) 1.102 + throw new IllegalStateException(); 1.103 + 1.104 + // if the parser is on START_DOCUMENT, skip ahead to the first element 1.105 + do { 1.106 + event = staxEventReader.nextEvent(); 1.107 + } while( !event.isStartElement() ); 1.108 + 1.109 + handleStartDocument(event.asStartElement().getNamespaceContext()); 1.110 + 1.111 + OUTER: 1.112 + while(true) { 1.113 + // These are all of the events listed in the javadoc for 1.114 + // XMLEvent. 1.115 + // The spec only really describes 11 of them. 1.116 + switch (event.getEventType()) { 1.117 + case XMLStreamConstants.START_ELEMENT : 1.118 + handleStartElement(event.asStartElement()); 1.119 + depth++; 1.120 + break; 1.121 + case XMLStreamConstants.END_ELEMENT : 1.122 + depth--; 1.123 + handleEndElement(event.asEndElement()); 1.124 + if(depth==0) break OUTER; 1.125 + break; 1.126 + case XMLStreamConstants.CHARACTERS : 1.127 + case XMLStreamConstants.CDATA : 1.128 + case XMLStreamConstants.SPACE : 1.129 + handleCharacters(event.asCharacters()); 1.130 + break; 1.131 + } 1.132 + 1.133 + 1.134 + event=staxEventReader.nextEvent(); 1.135 + } 1.136 + 1.137 + handleEndDocument(); 1.138 + event = null; // avoid keeping a stale reference 1.139 + } catch (SAXException e) { 1.140 + throw new XMLStreamException(e); 1.141 + } 1.142 + } 1.143 + 1.144 + protected Location getCurrentLocation() { 1.145 + return event.getLocation(); 1.146 + } 1.147 + 1.148 + protected String getCurrentQName() { 1.149 + QName qName; 1.150 + if(event.isEndElement()) 1.151 + qName = event.asEndElement().getName(); 1.152 + else 1.153 + qName = event.asStartElement().getName(); 1.154 + return getQName(qName.getPrefix(), qName.getLocalPart()); 1.155 + } 1.156 + 1.157 + 1.158 + private void handleCharacters(Characters event) throws SAXException, XMLStreamException { 1.159 + if(!predictor.expectText()) 1.160 + return; // text isn't expected. simply skip 1.161 + 1.162 + seenText = true; 1.163 + 1.164 + // check the next event 1.165 + XMLEvent next; 1.166 + while(true) { 1.167 + next = staxEventReader.peek(); 1.168 + if(!isIgnorable(next)) 1.169 + break; 1.170 + staxEventReader.nextEvent(); 1.171 + } 1.172 + 1.173 + if(isTag(next)) { 1.174 + // this is by far the common case --- you have <foo>abc</foo> or <foo>abc<bar/>...</foo> 1.175 + visitor.text(event.getData()); 1.176 + return; 1.177 + } 1.178 + 1.179 + // otherwise we have things like "abc<!-- test -->def". 1.180 + // concatenate all text 1.181 + buffer.append(event.getData()); 1.182 + 1.183 + while(true) { 1.184 + while(true) { 1.185 + next = staxEventReader.peek(); 1.186 + if(!isIgnorable(next)) 1.187 + break; 1.188 + staxEventReader.nextEvent(); 1.189 + } 1.190 + 1.191 + if(isTag(next)) { 1.192 + // found all adjacent text 1.193 + visitor.text(buffer); 1.194 + buffer.setLength(0); 1.195 + return; 1.196 + } 1.197 + 1.198 + buffer.append(next.asCharacters().getData()); 1.199 + staxEventReader.nextEvent(); // consume 1.200 + } 1.201 + } 1.202 + 1.203 + private boolean isTag(XMLEvent event) { 1.204 + int eventType = event.getEventType(); 1.205 + return eventType==XMLEvent.START_ELEMENT || eventType==XMLEvent.END_ELEMENT; 1.206 + } 1.207 + 1.208 + private boolean isIgnorable(XMLEvent event) { 1.209 + int eventType = event.getEventType(); 1.210 + return eventType==XMLEvent.COMMENT || eventType==XMLEvent.PROCESSING_INSTRUCTION; 1.211 + } 1.212 + 1.213 + private void handleEndElement(EndElement event) throws SAXException { 1.214 + if(!seenText && predictor.expectText()) { 1.215 + visitor.text(""); 1.216 + } 1.217 + 1.218 + // fire endElement 1.219 + QName qName = event.getName(); 1.220 + tagName.uri = fixNull(qName.getNamespaceURI()); 1.221 + tagName.local = qName.getLocalPart(); 1.222 + visitor.endElement(tagName); 1.223 + 1.224 + // end namespace bindings 1.225 + for( Iterator<Namespace> i = event.getNamespaces(); i.hasNext();) { 1.226 + String prefix = fixNull(i.next().getPrefix()); // be defensive 1.227 + visitor.endPrefixMapping(prefix); 1.228 + } 1.229 + 1.230 + seenText = false; 1.231 + } 1.232 + 1.233 + private void handleStartElement(StartElement event) throws SAXException { 1.234 + // start namespace bindings 1.235 + for (Iterator i = event.getNamespaces(); i.hasNext();) { 1.236 + Namespace ns = (Namespace)i.next(); 1.237 + visitor.startPrefixMapping( 1.238 + fixNull(ns.getPrefix()), 1.239 + fixNull(ns.getNamespaceURI())); 1.240 + } 1.241 + 1.242 + // fire startElement 1.243 + QName qName = event.getName(); 1.244 + tagName.uri = fixNull(qName.getNamespaceURI()); 1.245 + String localName = qName.getLocalPart(); 1.246 + tagName.uri = fixNull(qName.getNamespaceURI()); 1.247 + tagName.local = localName; 1.248 + tagName.atts = getAttributes(event); 1.249 + visitor.startElement(tagName); 1.250 + 1.251 + seenText = false; 1.252 + } 1.253 + 1.254 + 1.255 + 1.256 + /** 1.257 + * Get the attributes associated with the given START_ELEMENT StAXevent. 1.258 + * 1.259 + * @return the StAX attributes converted to an org.xml.sax.Attributes 1.260 + */ 1.261 + private Attributes getAttributes(StartElement event) { 1.262 + attrs.clear(); 1.263 + 1.264 + // in SAX, namespace declarations are not part of attributes by default. 1.265 + // (there's a property to control that, but as far as we are concerned 1.266 + // we don't use it.) So don't add xmlns:* to attributes. 1.267 + 1.268 + // gather non-namespace attrs 1.269 + for (Iterator i = event.getAttributes(); i.hasNext();) { 1.270 + Attribute staxAttr = (Attribute)i.next(); 1.271 + 1.272 + QName name = staxAttr.getName(); 1.273 + String uri = fixNull(name.getNamespaceURI()); 1.274 + String localName = name.getLocalPart(); 1.275 + String prefix = name.getPrefix(); 1.276 + String qName; 1.277 + if (prefix == null || prefix.length() == 0) 1.278 + qName = localName; 1.279 + else 1.280 + qName = prefix + ':' + localName; 1.281 + String type = staxAttr.getDTDType(); 1.282 + String value = staxAttr.getValue(); 1.283 + 1.284 + attrs.addAttribute(uri, localName, qName, type, value); 1.285 + } 1.286 + 1.287 + return attrs; 1.288 + } 1.289 +}