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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial