src/share/jaxws_classes/com/sun/xml/internal/ws/util/xml/XMLStreamReaderToXMLStreamWriter.java

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.util.xml;
ohair@286 27
alanb@368 28 import java.io.IOException;
alanb@368 29
alanb@368 30 import javax.xml.bind.attachment.AttachmentMarshaller;
ohair@286 31 import javax.xml.stream.XMLStreamConstants;
ohair@286 32 import javax.xml.stream.XMLStreamException;
ohair@286 33 import javax.xml.stream.XMLStreamReader;
ohair@286 34 import javax.xml.stream.XMLStreamWriter;
ohair@286 35 import javax.xml.XMLConstants;
ohair@286 36
alanb@368 37 import com.sun.xml.internal.ws.streaming.MtomStreamWriter;
alanb@368 38 import com.sun.xml.internal.org.jvnet.staxex.Base64Data;
alanb@368 39 import com.sun.xml.internal.org.jvnet.staxex.XMLStreamReaderEx;
alanb@368 40 import com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx;
alanb@368 41
ohair@286 42 /**
ohair@286 43 * Reads a sub-tree from {@link XMLStreamReader} and writes to {@link XMLStreamWriter}
ohair@286 44 * as-is.
ohair@286 45 *
ohair@286 46 * <p>
ohair@286 47 * This class can be sub-classed to implement a simple transformation logic.
ohair@286 48 *
ohair@286 49 * @author Kohsuke Kawaguchi
ohair@286 50 * @author Ryan Shoemaker
ohair@286 51 */
ohair@286 52 public class XMLStreamReaderToXMLStreamWriter {
ohair@286 53
ohair@286 54 private static final int BUF_SIZE = 4096;
ohair@286 55
ohair@286 56 protected XMLStreamReader in;
ohair@286 57 protected XMLStreamWriter out;
ohair@286 58
ohair@286 59 private char[] buf;
ohair@286 60
alanb@368 61 boolean optimizeBase64Data = false;
alanb@368 62
alanb@368 63 AttachmentMarshaller mtomAttachmentMarshaller;
alanb@368 64
ohair@286 65 /**
ohair@286 66 * Reads one subtree and writes it out.
ohair@286 67 *
ohair@286 68 * <p>
ohair@286 69 * The {@link XMLStreamWriter} never receives a start/end document event.
ohair@286 70 * Those need to be written separately by the caller.
ohair@286 71 */
ohair@286 72 public void bridge(XMLStreamReader in, XMLStreamWriter out) throws XMLStreamException {
ohair@286 73 assert in!=null && out!=null;
ohair@286 74 this.in = in;
ohair@286 75 this.out = out;
ohair@286 76
alanb@368 77 optimizeBase64Data = (in instanceof XMLStreamReaderEx);
alanb@368 78
alanb@368 79 if (out instanceof XMLStreamWriterEx && out instanceof MtomStreamWriter) {
alanb@368 80 mtomAttachmentMarshaller = ((MtomStreamWriter) out).getAttachmentMarshaller();
alanb@368 81 }
ohair@286 82 // remembers the nest level of elements to know when we are done.
ohair@286 83 int depth=0;
ohair@286 84
ohair@286 85 buf = new char[BUF_SIZE];
ohair@286 86
ohair@286 87 // if the parser is at the start tag, proceed to the first element
ohair@286 88 int event = in.getEventType();
ohair@286 89 if(event == XMLStreamConstants.START_DOCUMENT) {
ohair@286 90 // nextTag doesn't correctly handle DTDs
ohair@286 91 while( !in.isStartElement() ) {
ohair@286 92 event = in.next();
ohair@286 93 if (event == XMLStreamConstants.COMMENT)
ohair@286 94 handleComment();
ohair@286 95 }
ohair@286 96 }
ohair@286 97
ohair@286 98
ohair@286 99 if( event!=XMLStreamConstants.START_ELEMENT)
ohair@286 100 throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);
ohair@286 101
ohair@286 102 do {
ohair@286 103 // These are all of the events listed in the javadoc for
ohair@286 104 // XMLEvent.
ohair@286 105 // The spec only really describes 11 of them.
ohair@286 106 switch (event) {
ohair@286 107 case XMLStreamConstants.START_ELEMENT :
ohair@286 108 depth++;
ohair@286 109 handleStartElement();
ohair@286 110 break;
ohair@286 111 case XMLStreamConstants.END_ELEMENT :
ohair@286 112 handleEndElement();
ohair@286 113 depth--;
ohair@286 114 if(depth==0)
ohair@286 115 return;
ohair@286 116 break;
ohair@286 117 case XMLStreamConstants.CHARACTERS :
ohair@286 118 handleCharacters();
ohair@286 119 break;
ohair@286 120 case XMLStreamConstants.ENTITY_REFERENCE :
ohair@286 121 handleEntityReference();
ohair@286 122 break;
ohair@286 123 case XMLStreamConstants.PROCESSING_INSTRUCTION :
ohair@286 124 handlePI();
ohair@286 125 break;
ohair@286 126 case XMLStreamConstants.COMMENT :
ohair@286 127 handleComment();
ohair@286 128 break;
ohair@286 129 case XMLStreamConstants.DTD :
ohair@286 130 handleDTD();
ohair@286 131 break;
ohair@286 132 case XMLStreamConstants.CDATA :
ohair@286 133 handleCDATA();
ohair@286 134 break;
ohair@286 135 case XMLStreamConstants.SPACE :
ohair@286 136 handleSpace();
ohair@286 137 break;
ohair@286 138 case XMLStreamConstants.END_DOCUMENT:
ohair@286 139 throw new XMLStreamException("Malformed XML at depth="+depth+", Reached EOF. Event="+event);
ohair@286 140 default :
ohair@286 141 throw new XMLStreamException("Cannot process event: " + event);
ohair@286 142 }
ohair@286 143
ohair@286 144 event=in.next();
ohair@286 145 } while (depth!=0);
ohair@286 146 }
ohair@286 147
ohair@286 148 protected void handlePI() throws XMLStreamException {
ohair@286 149 out.writeProcessingInstruction(
ohair@286 150 in.getPITarget(),
ohair@286 151 in.getPIData());
ohair@286 152 }
ohair@286 153
ohair@286 154
ohair@286 155 protected void handleCharacters() throws XMLStreamException {
alanb@368 156
alanb@368 157 CharSequence c = null;
alanb@368 158
alanb@368 159 if (optimizeBase64Data) {
alanb@368 160 c = ((XMLStreamReaderEx)in).getPCDATA();
alanb@368 161 }
alanb@368 162
alanb@368 163 if ((c != null) && (c instanceof Base64Data)) {
alanb@368 164 if (mtomAttachmentMarshaller != null) {
alanb@368 165 Base64Data b64d = (Base64Data) c;
alanb@368 166 ((XMLStreamWriterEx)out).writeBinary(b64d.getDataHandler());
alanb@368 167 } else {
alanb@368 168 try {
alanb@368 169 ((Base64Data)c).writeTo(out);
alanb@368 170 } catch (IOException e) {
alanb@368 171 throw new XMLStreamException(e);
alanb@368 172 }
alanb@368 173 }
alanb@368 174 } else {
alanb@368 175 for (int start=0,read=buf.length; read == buf.length; start+=buf.length) {
alanb@368 176 read = in.getTextCharacters(start, buf, 0, buf.length);
alanb@368 177 out.writeCharacters(buf, 0, read);
alanb@368 178 }
ohair@286 179 }
ohair@286 180 }
ohair@286 181
ohair@286 182 protected void handleEndElement() throws XMLStreamException {
ohair@286 183 out.writeEndElement();
ohair@286 184 }
ohair@286 185
ohair@286 186 protected void handleStartElement() throws XMLStreamException {
ohair@286 187 String nsUri = in.getNamespaceURI();
ohair@286 188 if(nsUri==null)
ohair@286 189 out.writeStartElement(in.getLocalName());
ohair@286 190 else
ohair@286 191 out.writeStartElement(
ohair@286 192 fixNull(in.getPrefix()),
ohair@286 193 in.getLocalName(),
ohair@286 194 nsUri
ohair@286 195 );
ohair@286 196
ohair@286 197 // start namespace bindings
ohair@286 198 int nsCount = in.getNamespaceCount();
ohair@286 199 for (int i = 0; i < nsCount; i++) {
ohair@286 200 out.writeNamespace(
ohair@286 201 in.getNamespacePrefix(i),
ohair@286 202 fixNull(in.getNamespaceURI(i))); // zephyr doesn't like null, I don't know what is correct, so just fix null to "" for now
ohair@286 203 }
ohair@286 204
ohair@286 205 // write attributes
ohair@286 206 int attCount = in.getAttributeCount();
ohair@286 207 for (int i = 0; i < attCount; i++) {
ohair@286 208 handleAttribute(i);
ohair@286 209 }
ohair@286 210 }
ohair@286 211
ohair@286 212 /**
ohair@286 213 * Writes out the {@code i}-th attribute of the current element.
ohair@286 214 *
ohair@286 215 * <p>
ohair@286 216 * Used from {@link #handleStartElement()}.
ohair@286 217 */
ohair@286 218 protected void handleAttribute(int i) throws XMLStreamException {
ohair@286 219 String nsUri = in.getAttributeNamespace(i);
ohair@286 220 String prefix = in.getAttributePrefix(i);
ohair@286 221 if (fixNull(nsUri).equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
ohair@286 222 //Its a namespace decl, ignore as it is already written.
ohair@286 223 return;
ohair@286 224 }
ohair@286 225
ohair@286 226 if(nsUri==null || prefix == null || prefix.equals("")) {
ohair@286 227 out.writeAttribute(
ohair@286 228 in.getAttributeLocalName(i),
ohair@286 229 in.getAttributeValue(i)
ohair@286 230 );
ohair@286 231 } else {
ohair@286 232 out.writeAttribute(
ohair@286 233 prefix,
ohair@286 234 nsUri,
ohair@286 235 in.getAttributeLocalName(i),
ohair@286 236 in.getAttributeValue(i)
ohair@286 237 );
ohair@286 238 }
ohair@286 239 }
ohair@286 240
ohair@286 241 protected void handleDTD() throws XMLStreamException {
ohair@286 242 out.writeDTD(in.getText());
ohair@286 243 }
ohair@286 244
ohair@286 245 protected void handleComment() throws XMLStreamException {
ohair@286 246 out.writeComment(in.getText());
ohair@286 247 }
ohair@286 248
ohair@286 249 protected void handleEntityReference() throws XMLStreamException {
ohair@286 250 out.writeEntityRef(in.getText());
ohair@286 251 }
ohair@286 252
ohair@286 253 protected void handleSpace() throws XMLStreamException {
ohair@286 254 handleCharacters();
ohair@286 255 }
ohair@286 256
ohair@286 257 protected void handleCDATA() throws XMLStreamException {
ohair@286 258 out.writeCData(in.getText());
ohair@286 259 }
ohair@286 260
ohair@286 261 private static String fixNull(String s) {
ohair@286 262 if(s==null) return "";
ohair@286 263 else return s;
ohair@286 264 }
ohair@286 265 }

mercurial