Tue, 09 Apr 2013 14:51:13 +0100
8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com
ohair@286 | 1 | /* |
alanb@368 | 2 | * Copyright (c) 2005, 2012, 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.stream.buffer.stax; |
ohair@286 | 27 | |
ohair@286 | 28 | import com.sun.xml.internal.stream.buffer.AbstractProcessor; |
ohair@286 | 29 | import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; |
ohair@286 | 30 | |
alanb@368 | 31 | import java.io.IOException; |
ohair@286 | 32 | import java.util.Collections; |
ohair@286 | 33 | import java.util.HashSet; |
ohair@286 | 34 | import java.util.Map; |
ohair@286 | 35 | import java.util.Set; |
ohair@286 | 36 | |
alanb@368 | 37 | import com.sun.xml.internal.org.jvnet.staxex.Base64Data; |
ohair@286 | 38 | import com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx; |
ohair@286 | 39 | |
ohair@286 | 40 | import javax.xml.stream.XMLStreamException; |
ohair@286 | 41 | import javax.xml.stream.XMLStreamWriter; |
ohair@286 | 42 | |
ohair@286 | 43 | |
ohair@286 | 44 | /** |
ohair@286 | 45 | * A processor of a {@link XMLStreamBuffer} that writes the XML infoset to a |
ohair@286 | 46 | * {@link XMLStreamWriter}. |
ohair@286 | 47 | * |
ohair@286 | 48 | * @author Paul.Sandoz@Sun.Com |
ohair@286 | 49 | * @author K.Venugopal@sun.com |
ohair@286 | 50 | */ |
ohair@286 | 51 | public class StreamWriterBufferProcessor extends AbstractProcessor { |
ohair@286 | 52 | |
ohair@286 | 53 | |
ohair@286 | 54 | public StreamWriterBufferProcessor() { |
ohair@286 | 55 | } |
ohair@286 | 56 | |
ohair@286 | 57 | /** |
ohair@286 | 58 | * @deprecated |
ohair@286 | 59 | * Use {@link #StreamWriterBufferProcessor(XMLStreamBuffer, boolean)} |
ohair@286 | 60 | */ |
ohair@286 | 61 | public StreamWriterBufferProcessor(XMLStreamBuffer buffer) { |
ohair@286 | 62 | setXMLStreamBuffer(buffer,buffer.isFragment()); |
ohair@286 | 63 | } |
ohair@286 | 64 | |
ohair@286 | 65 | /** |
ohair@286 | 66 | * @param produceFragmentEvent |
ohair@286 | 67 | * True to generate fragment SAX events without start/endDocument. |
ohair@286 | 68 | * False to generate a full document SAX events. |
ohair@286 | 69 | */ |
ohair@286 | 70 | public StreamWriterBufferProcessor(XMLStreamBuffer buffer,boolean produceFragmentEvent) { |
ohair@286 | 71 | setXMLStreamBuffer(buffer,produceFragmentEvent); |
ohair@286 | 72 | } |
ohair@286 | 73 | |
ohair@286 | 74 | public final void process(XMLStreamBuffer buffer, XMLStreamWriter writer) throws XMLStreamException { |
ohair@286 | 75 | setXMLStreamBuffer(buffer,buffer.isFragment()); |
ohair@286 | 76 | process(writer); |
ohair@286 | 77 | } |
ohair@286 | 78 | |
ohair@286 | 79 | public void process(XMLStreamWriter writer) throws XMLStreamException { |
ohair@286 | 80 | if(_fragmentMode){ |
ohair@286 | 81 | writeFragment(writer); |
ohair@286 | 82 | }else{ |
ohair@286 | 83 | write(writer); |
ohair@286 | 84 | } |
ohair@286 | 85 | } |
ohair@286 | 86 | |
ohair@286 | 87 | /** |
ohair@286 | 88 | * @deprecated |
ohair@286 | 89 | * Use {@link #setXMLStreamBuffer(XMLStreamBuffer, boolean)} |
ohair@286 | 90 | */ |
ohair@286 | 91 | public void setXMLStreamBuffer(XMLStreamBuffer buffer) { |
ohair@286 | 92 | setBuffer(buffer); |
ohair@286 | 93 | } |
ohair@286 | 94 | |
ohair@286 | 95 | /** |
ohair@286 | 96 | * @param produceFragmentEvent |
ohair@286 | 97 | * True to generate fragment SAX events without start/endDocument. |
ohair@286 | 98 | * False to generate a full document SAX events. |
ohair@286 | 99 | */ |
ohair@286 | 100 | public void setXMLStreamBuffer(XMLStreamBuffer buffer, boolean produceFragmentEvent) { |
ohair@286 | 101 | setBuffer(buffer,produceFragmentEvent); |
ohair@286 | 102 | } |
ohair@286 | 103 | |
ohair@286 | 104 | /** |
ohair@286 | 105 | * Writes a full XML infoset event to the given writer, |
ohair@286 | 106 | * including start/end document. |
ohair@286 | 107 | * Any inscope namespaces present will be written as namespace |
ohair@286 | 108 | * delcarations on each top-level element. |
ohair@286 | 109 | */ |
ohair@286 | 110 | public void write(XMLStreamWriter writer) throws XMLStreamException{ |
ohair@286 | 111 | |
ohair@286 | 112 | if(!_fragmentMode) { |
ohair@286 | 113 | if(_treeCount>1) |
ohair@286 | 114 | throw new IllegalStateException("forest cannot be written as a full infoset"); |
ohair@286 | 115 | writer.writeStartDocument(); |
ohair@286 | 116 | } |
ohair@286 | 117 | |
ohair@286 | 118 | while(true) { |
ohair@286 | 119 | int item = getEIIState(peekStructure()); |
ohair@286 | 120 | writer.flush(); |
ohair@286 | 121 | |
ohair@286 | 122 | switch(item) { |
ohair@286 | 123 | case STATE_DOCUMENT: |
ohair@286 | 124 | readStructure(); //skip |
ohair@286 | 125 | break; |
ohair@286 | 126 | case STATE_ELEMENT_U_LN_QN: |
ohair@286 | 127 | case STATE_ELEMENT_P_U_LN: |
ohair@286 | 128 | case STATE_ELEMENT_U_LN: |
ohair@286 | 129 | case STATE_ELEMENT_LN: |
ohair@286 | 130 | writeFragment(writer); |
ohair@286 | 131 | break; |
ohair@286 | 132 | case STATE_COMMENT_AS_CHAR_ARRAY_SMALL: { |
ohair@286 | 133 | readStructure(); |
ohair@286 | 134 | final int length = readStructure(); |
ohair@286 | 135 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 136 | final String comment = new String(_contentCharactersBuffer, start, length); |
ohair@286 | 137 | writer.writeComment(comment); |
ohair@286 | 138 | break; |
ohair@286 | 139 | } |
ohair@286 | 140 | case STATE_COMMENT_AS_CHAR_ARRAY_MEDIUM: { |
ohair@286 | 141 | readStructure(); |
ohair@286 | 142 | final int length = readStructure16(); |
ohair@286 | 143 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 144 | final String comment = new String(_contentCharactersBuffer, start, length); |
ohair@286 | 145 | writer.writeComment(comment); |
ohair@286 | 146 | break; |
ohair@286 | 147 | } |
ohair@286 | 148 | case STATE_COMMENT_AS_CHAR_ARRAY_COPY: { |
ohair@286 | 149 | readStructure(); |
ohair@286 | 150 | final char[] ch = readContentCharactersCopy(); |
ohair@286 | 151 | writer.writeComment(new String(ch)); |
ohair@286 | 152 | break; |
ohair@286 | 153 | } |
ohair@286 | 154 | case STATE_PROCESSING_INSTRUCTION: |
ohair@286 | 155 | readStructure(); |
ohair@286 | 156 | writer.writeProcessingInstruction(readStructureString(), readStructureString()); |
ohair@286 | 157 | break; |
ohair@286 | 158 | case STATE_END: // done |
ohair@286 | 159 | readStructure(); |
ohair@286 | 160 | writer.writeEndDocument(); |
ohair@286 | 161 | return; |
ohair@286 | 162 | default: |
ohair@286 | 163 | throw new XMLStreamException("Invalid State "+item); |
ohair@286 | 164 | } |
ohair@286 | 165 | } |
ohair@286 | 166 | |
ohair@286 | 167 | } |
ohair@286 | 168 | |
ohair@286 | 169 | /** |
ohair@286 | 170 | * Writes the buffer as a fragment, meaning |
ohair@286 | 171 | * the writer will not receive start/endDocument events. |
ohair@286 | 172 | * Any inscope namespaces present will be written as namespace |
ohair@286 | 173 | * delcarations on each top-level element. |
ohair@286 | 174 | * <p> |
ohair@286 | 175 | * If {@link XMLStreamBuffer} has a forest, this method will write all the forests. |
ohair@286 | 176 | */ |
ohair@286 | 177 | public void writeFragment(XMLStreamWriter writer) throws XMLStreamException { |
ohair@286 | 178 | if (writer instanceof XMLStreamWriterEx) { |
ohair@286 | 179 | writeFragmentEx((XMLStreamWriterEx)writer); |
ohair@286 | 180 | } else { |
ohair@286 | 181 | writeFragmentNoEx(writer); |
ohair@286 | 182 | } |
ohair@286 | 183 | } |
ohair@286 | 184 | |
ohair@286 | 185 | public void writeFragmentEx(XMLStreamWriterEx writer) throws XMLStreamException { |
ohair@286 | 186 | int depth = 0; // used to determine when we are done with a tree. |
ohair@286 | 187 | |
ohair@286 | 188 | int item = getEIIState(peekStructure()); |
ohair@286 | 189 | if(item==STATE_DOCUMENT) |
ohair@286 | 190 | readStructure(); // skip STATE_DOCUMENT |
ohair@286 | 191 | |
ohair@286 | 192 | do { |
ohair@286 | 193 | |
ohair@286 | 194 | item = readEiiState(); |
ohair@286 | 195 | |
ohair@286 | 196 | switch(item) { |
ohair@286 | 197 | case STATE_DOCUMENT: |
ohair@286 | 198 | throw new AssertionError(); |
ohair@286 | 199 | case STATE_ELEMENT_U_LN_QN: { |
ohair@286 | 200 | depth ++; |
ohair@286 | 201 | final String uri = readStructureString(); |
ohair@286 | 202 | final String localName = readStructureString(); |
ohair@286 | 203 | final String prefix = getPrefixFromQName(readStructureString()); |
ohair@286 | 204 | writer.writeStartElement(prefix,localName,uri); |
ohair@286 | 205 | writeAttributes(writer, isInscope(depth)); |
ohair@286 | 206 | break; |
ohair@286 | 207 | } |
ohair@286 | 208 | case STATE_ELEMENT_P_U_LN: { |
ohair@286 | 209 | depth ++; |
ohair@286 | 210 | final String prefix = readStructureString(); |
ohair@286 | 211 | final String uri = readStructureString(); |
ohair@286 | 212 | final String localName = readStructureString(); |
ohair@286 | 213 | writer.writeStartElement(prefix,localName,uri); |
ohair@286 | 214 | writeAttributes(writer, isInscope(depth)); |
ohair@286 | 215 | break; |
ohair@286 | 216 | } |
ohair@286 | 217 | case STATE_ELEMENT_U_LN: { |
ohair@286 | 218 | depth ++; |
ohair@286 | 219 | final String uri = readStructureString(); |
ohair@286 | 220 | final String localName = readStructureString(); |
ohair@286 | 221 | writer.writeStartElement("",localName,uri); |
ohair@286 | 222 | writeAttributes(writer, isInscope(depth)); |
ohair@286 | 223 | break; |
ohair@286 | 224 | } |
ohair@286 | 225 | case STATE_ELEMENT_LN: { |
ohair@286 | 226 | depth ++; |
ohair@286 | 227 | final String localName = readStructureString(); |
ohair@286 | 228 | writer.writeStartElement(localName); |
ohair@286 | 229 | writeAttributes(writer, isInscope(depth)); |
ohair@286 | 230 | break; |
ohair@286 | 231 | } |
ohair@286 | 232 | case STATE_TEXT_AS_CHAR_ARRAY_SMALL: { |
ohair@286 | 233 | final int length = readStructure(); |
ohair@286 | 234 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 235 | writer.writeCharacters(_contentCharactersBuffer,start,length); |
ohair@286 | 236 | break; |
ohair@286 | 237 | } |
ohair@286 | 238 | case STATE_TEXT_AS_CHAR_ARRAY_MEDIUM: { |
ohair@286 | 239 | final int length = readStructure16(); |
ohair@286 | 240 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 241 | writer.writeCharacters(_contentCharactersBuffer,start,length); |
ohair@286 | 242 | break; |
ohair@286 | 243 | } |
ohair@286 | 244 | case STATE_TEXT_AS_CHAR_ARRAY_COPY: { |
ohair@286 | 245 | char[] c = readContentCharactersCopy(); |
ohair@286 | 246 | writer.writeCharacters(c,0,c.length); |
ohair@286 | 247 | break; |
ohair@286 | 248 | } |
ohair@286 | 249 | case STATE_TEXT_AS_STRING: { |
ohair@286 | 250 | final String s = readContentString(); |
ohair@286 | 251 | writer.writeCharacters(s); |
ohair@286 | 252 | break; |
ohair@286 | 253 | } |
ohair@286 | 254 | case STATE_TEXT_AS_OBJECT: { |
ohair@286 | 255 | final CharSequence c = (CharSequence)readContentObject(); |
ohair@286 | 256 | writer.writePCDATA(c); |
ohair@286 | 257 | break; |
ohair@286 | 258 | } |
ohair@286 | 259 | case STATE_COMMENT_AS_CHAR_ARRAY_SMALL: { |
ohair@286 | 260 | final int length = readStructure(); |
ohair@286 | 261 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 262 | final String comment = new String(_contentCharactersBuffer, start, length); |
ohair@286 | 263 | writer.writeComment(comment); |
ohair@286 | 264 | break; |
ohair@286 | 265 | } |
ohair@286 | 266 | case STATE_COMMENT_AS_CHAR_ARRAY_MEDIUM: { |
ohair@286 | 267 | final int length = readStructure16(); |
ohair@286 | 268 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 269 | final String comment = new String(_contentCharactersBuffer, start, length); |
ohair@286 | 270 | writer.writeComment(comment); |
ohair@286 | 271 | break; |
ohair@286 | 272 | } |
ohair@286 | 273 | case STATE_COMMENT_AS_CHAR_ARRAY_COPY: { |
ohair@286 | 274 | final char[] ch = readContentCharactersCopy(); |
ohair@286 | 275 | writer.writeComment(new String(ch)); |
ohair@286 | 276 | break; |
ohair@286 | 277 | } |
ohair@286 | 278 | case STATE_PROCESSING_INSTRUCTION: |
ohair@286 | 279 | writer.writeProcessingInstruction(readStructureString(), readStructureString()); |
ohair@286 | 280 | break; |
ohair@286 | 281 | case STATE_END: |
ohair@286 | 282 | writer.writeEndElement(); |
ohair@286 | 283 | depth --; |
ohair@286 | 284 | if(depth==0) |
ohair@286 | 285 | _treeCount--; |
ohair@286 | 286 | break; |
ohair@286 | 287 | default: |
ohair@286 | 288 | throw new XMLStreamException("Invalid State "+item); |
ohair@286 | 289 | } |
ohair@286 | 290 | } while(depth>0 || _treeCount>0); |
ohair@286 | 291 | |
ohair@286 | 292 | } |
ohair@286 | 293 | |
ohair@286 | 294 | public void writeFragmentNoEx(XMLStreamWriter writer) throws XMLStreamException { |
ohair@286 | 295 | int depth = 0; |
ohair@286 | 296 | |
ohair@286 | 297 | int item = getEIIState(peekStructure()); |
ohair@286 | 298 | if(item==STATE_DOCUMENT) |
ohair@286 | 299 | readStructure(); // skip STATE_DOCUMENT |
ohair@286 | 300 | |
ohair@286 | 301 | do { |
ohair@286 | 302 | item = readEiiState(); |
ohair@286 | 303 | |
ohair@286 | 304 | switch(item) { |
ohair@286 | 305 | case STATE_DOCUMENT: |
ohair@286 | 306 | throw new AssertionError(); |
ohair@286 | 307 | case STATE_ELEMENT_U_LN_QN: { |
ohair@286 | 308 | depth ++; |
ohair@286 | 309 | final String uri = readStructureString(); |
ohair@286 | 310 | final String localName = readStructureString(); |
ohair@286 | 311 | final String prefix = getPrefixFromQName(readStructureString()); |
ohair@286 | 312 | writer.writeStartElement(prefix,localName,uri); |
ohair@286 | 313 | writeAttributes(writer, isInscope(depth)); |
ohair@286 | 314 | break; |
ohair@286 | 315 | } |
ohair@286 | 316 | case STATE_ELEMENT_P_U_LN: { |
ohair@286 | 317 | depth ++; |
ohair@286 | 318 | final String prefix = readStructureString(); |
ohair@286 | 319 | final String uri = readStructureString(); |
ohair@286 | 320 | final String localName = readStructureString(); |
ohair@286 | 321 | writer.writeStartElement(prefix,localName,uri); |
ohair@286 | 322 | writeAttributes(writer, isInscope(depth)); |
ohair@286 | 323 | break; |
ohair@286 | 324 | } |
ohair@286 | 325 | case STATE_ELEMENT_U_LN: { |
ohair@286 | 326 | depth ++; |
ohair@286 | 327 | final String uri = readStructureString(); |
ohair@286 | 328 | final String localName = readStructureString(); |
ohair@286 | 329 | writer.writeStartElement("",localName,uri); |
ohair@286 | 330 | writeAttributes(writer, isInscope(depth)); |
ohair@286 | 331 | break; |
ohair@286 | 332 | } |
ohair@286 | 333 | case STATE_ELEMENT_LN: { |
ohair@286 | 334 | depth ++; |
ohair@286 | 335 | final String localName = readStructureString(); |
ohair@286 | 336 | writer.writeStartElement(localName); |
ohair@286 | 337 | writeAttributes(writer, isInscope(depth)); |
ohair@286 | 338 | break; |
ohair@286 | 339 | } |
ohair@286 | 340 | case STATE_TEXT_AS_CHAR_ARRAY_SMALL: { |
ohair@286 | 341 | final int length = readStructure(); |
ohair@286 | 342 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 343 | writer.writeCharacters(_contentCharactersBuffer,start,length); |
ohair@286 | 344 | break; |
ohair@286 | 345 | } |
ohair@286 | 346 | case STATE_TEXT_AS_CHAR_ARRAY_MEDIUM: { |
ohair@286 | 347 | final int length = readStructure16(); |
ohair@286 | 348 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 349 | writer.writeCharacters(_contentCharactersBuffer,start,length); |
ohair@286 | 350 | break; |
ohair@286 | 351 | } |
ohair@286 | 352 | case STATE_TEXT_AS_CHAR_ARRAY_COPY: { |
ohair@286 | 353 | char[] c = readContentCharactersCopy(); |
ohair@286 | 354 | writer.writeCharacters(c,0,c.length); |
ohair@286 | 355 | break; |
ohair@286 | 356 | } |
ohair@286 | 357 | case STATE_TEXT_AS_STRING: { |
ohair@286 | 358 | final String s = readContentString(); |
ohair@286 | 359 | writer.writeCharacters(s); |
ohair@286 | 360 | break; |
ohair@286 | 361 | } |
ohair@286 | 362 | case STATE_TEXT_AS_OBJECT: { |
ohair@286 | 363 | final CharSequence c = (CharSequence)readContentObject(); |
alanb@368 | 364 | if (c instanceof Base64Data) { |
alanb@368 | 365 | try { |
alanb@368 | 366 | Base64Data bd = (Base64Data)c; |
alanb@368 | 367 | bd.writeTo(writer); |
alanb@368 | 368 | } catch (IOException e) { |
alanb@368 | 369 | throw new XMLStreamException(e); |
alanb@368 | 370 | } |
alanb@368 | 371 | } else { |
alanb@368 | 372 | writer.writeCharacters(c.toString()); |
alanb@368 | 373 | } |
ohair@286 | 374 | break; |
ohair@286 | 375 | } |
ohair@286 | 376 | case STATE_COMMENT_AS_CHAR_ARRAY_SMALL: { |
ohair@286 | 377 | final int length = readStructure(); |
ohair@286 | 378 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 379 | final String comment = new String(_contentCharactersBuffer, start, length); |
ohair@286 | 380 | writer.writeComment(comment); |
ohair@286 | 381 | break; |
ohair@286 | 382 | } |
ohair@286 | 383 | case STATE_COMMENT_AS_CHAR_ARRAY_MEDIUM: { |
ohair@286 | 384 | final int length = readStructure16(); |
ohair@286 | 385 | final int start = readContentCharactersBuffer(length); |
ohair@286 | 386 | final String comment = new String(_contentCharactersBuffer, start, length); |
ohair@286 | 387 | writer.writeComment(comment); |
ohair@286 | 388 | break; |
ohair@286 | 389 | } |
ohair@286 | 390 | case STATE_COMMENT_AS_CHAR_ARRAY_COPY: { |
ohair@286 | 391 | final char[] ch = readContentCharactersCopy(); |
ohair@286 | 392 | writer.writeComment(new String(ch)); |
ohair@286 | 393 | break; |
ohair@286 | 394 | } |
ohair@286 | 395 | case STATE_PROCESSING_INSTRUCTION: |
ohair@286 | 396 | writer.writeProcessingInstruction(readStructureString(), readStructureString()); |
ohair@286 | 397 | break; |
ohair@286 | 398 | case STATE_END: |
ohair@286 | 399 | writer.writeEndElement(); |
ohair@286 | 400 | depth --; |
ohair@286 | 401 | if(depth==0) |
ohair@286 | 402 | _treeCount--; |
ohair@286 | 403 | break; |
ohair@286 | 404 | default: |
ohair@286 | 405 | throw new XMLStreamException("Invalid State "+item); |
ohair@286 | 406 | } |
ohair@286 | 407 | } while(depth > 0 || _treeCount>0); |
ohair@286 | 408 | |
ohair@286 | 409 | } |
ohair@286 | 410 | |
ohair@286 | 411 | private boolean isInscope(int depth) { |
ohair@286 | 412 | return _buffer.getInscopeNamespaces().size() > 0 && depth ==1; |
ohair@286 | 413 | } |
ohair@286 | 414 | |
ohair@286 | 415 | /* |
ohair@286 | 416 | * @param inscope: true means write inscope namespaces |
ohair@286 | 417 | */ |
ohair@286 | 418 | private void writeAttributes(XMLStreamWriter writer, boolean inscope) throws XMLStreamException { |
ohair@286 | 419 | // prefixSet to collect prefixes that are written before writing inscope namespaces |
ohair@286 | 420 | Set<String> prefixSet = inscope ? new HashSet<String>() : Collections.<String>emptySet(); |
ohair@286 | 421 | int item = peekStructure(); |
ohair@286 | 422 | if ((item & TYPE_MASK) == T_NAMESPACE_ATTRIBUTE) { |
ohair@286 | 423 | // Skip the namespace declarations on the element |
ohair@286 | 424 | // they will have been added already |
ohair@286 | 425 | item = writeNamespaceAttributes(item, writer, inscope, prefixSet); |
ohair@286 | 426 | } |
ohair@286 | 427 | if (inscope) { |
ohair@286 | 428 | writeInscopeNamespaces(writer, prefixSet); |
ohair@286 | 429 | } |
ohair@286 | 430 | if ((item & TYPE_MASK) == T_ATTRIBUTE) { |
ohair@286 | 431 | writeAttributes(item, writer); |
ohair@286 | 432 | } |
ohair@286 | 433 | } |
ohair@286 | 434 | |
ohair@286 | 435 | private static String fixNull(String s) { |
ohair@286 | 436 | if (s == null) return ""; |
ohair@286 | 437 | else return s; |
ohair@286 | 438 | } |
ohair@286 | 439 | |
ohair@286 | 440 | /* |
ohair@286 | 441 | * @param prefixSet: already written prefixes |
ohair@286 | 442 | */ |
ohair@286 | 443 | private void writeInscopeNamespaces(XMLStreamWriter writer, Set<String> prefixSet) throws XMLStreamException { |
ohair@286 | 444 | for (Map.Entry<String, String> e : _buffer.getInscopeNamespaces().entrySet()) { |
ohair@286 | 445 | String key = fixNull(e.getKey()); |
ohair@286 | 446 | // If the prefix is already written, do not write the prefix |
ohair@286 | 447 | if (!prefixSet.contains(key)) { |
ohair@286 | 448 | writer.writeNamespace(key, e.getValue()); |
ohair@286 | 449 | } |
ohair@286 | 450 | } |
ohair@286 | 451 | } |
ohair@286 | 452 | |
ohair@286 | 453 | private int writeNamespaceAttributes(int item, XMLStreamWriter writer, boolean collectPrefixes, Set<String> prefixSet) throws XMLStreamException { |
ohair@286 | 454 | do { |
ohair@286 | 455 | switch(getNIIState(item)){ |
ohair@286 | 456 | case STATE_NAMESPACE_ATTRIBUTE: |
ohair@286 | 457 | // Undeclaration of default namespace |
ohair@286 | 458 | writer.writeDefaultNamespace(""); |
ohair@286 | 459 | if (collectPrefixes) { |
ohair@286 | 460 | prefixSet.add(""); |
ohair@286 | 461 | } |
ohair@286 | 462 | break; |
ohair@286 | 463 | case STATE_NAMESPACE_ATTRIBUTE_P: |
ohair@286 | 464 | // Undeclaration of namespace |
ohair@286 | 465 | // Declaration with prefix |
ohair@286 | 466 | String prefix = readStructureString(); |
ohair@286 | 467 | writer.writeNamespace(prefix, ""); |
ohair@286 | 468 | if (collectPrefixes) { |
ohair@286 | 469 | prefixSet.add(prefix); |
ohair@286 | 470 | } |
ohair@286 | 471 | break; |
ohair@286 | 472 | case STATE_NAMESPACE_ATTRIBUTE_P_U: |
ohair@286 | 473 | // Declaration with prefix |
ohair@286 | 474 | prefix = readStructureString(); |
ohair@286 | 475 | writer.writeNamespace(prefix, readStructureString()); |
ohair@286 | 476 | if (collectPrefixes) { |
ohair@286 | 477 | prefixSet.add(prefix); |
ohair@286 | 478 | } |
ohair@286 | 479 | break; |
ohair@286 | 480 | case STATE_NAMESPACE_ATTRIBUTE_U: |
ohair@286 | 481 | // Default declaration |
ohair@286 | 482 | writer.writeDefaultNamespace(readStructureString()); |
ohair@286 | 483 | if (collectPrefixes) { |
ohair@286 | 484 | prefixSet.add(""); |
ohair@286 | 485 | } |
ohair@286 | 486 | break; |
ohair@286 | 487 | } |
ohair@286 | 488 | readStructure(); |
ohair@286 | 489 | |
ohair@286 | 490 | item = peekStructure(); |
ohair@286 | 491 | } while((item & TYPE_MASK) == T_NAMESPACE_ATTRIBUTE); |
ohair@286 | 492 | |
ohair@286 | 493 | return item; |
ohair@286 | 494 | } |
ohair@286 | 495 | |
ohair@286 | 496 | private void writeAttributes(int item, XMLStreamWriter writer) throws XMLStreamException { |
ohair@286 | 497 | do { |
ohair@286 | 498 | switch(getAIIState(item)) { |
ohair@286 | 499 | case STATE_ATTRIBUTE_U_LN_QN: { |
ohair@286 | 500 | final String uri = readStructureString(); |
ohair@286 | 501 | final String localName = readStructureString(); |
ohair@286 | 502 | final String prefix = getPrefixFromQName(readStructureString()); |
ohair@286 | 503 | writer.writeAttribute(prefix,uri,localName,readContentString()); |
ohair@286 | 504 | break; |
ohair@286 | 505 | } |
ohair@286 | 506 | case STATE_ATTRIBUTE_P_U_LN: |
ohair@286 | 507 | writer.writeAttribute(readStructureString(), readStructureString(), |
ohair@286 | 508 | readStructureString(), readContentString()); |
ohair@286 | 509 | break; |
ohair@286 | 510 | case STATE_ATTRIBUTE_U_LN: |
ohair@286 | 511 | writer.writeAttribute(readStructureString(), readStructureString(), readContentString()); |
ohair@286 | 512 | break; |
ohair@286 | 513 | case STATE_ATTRIBUTE_LN: |
ohair@286 | 514 | writer.writeAttribute(readStructureString(), readContentString()); |
ohair@286 | 515 | break; |
ohair@286 | 516 | } |
ohair@286 | 517 | // Ignore the attribute type |
ohair@286 | 518 | readStructureString(); |
ohair@286 | 519 | |
ohair@286 | 520 | readStructure(); |
ohair@286 | 521 | |
ohair@286 | 522 | item = peekStructure(); |
ohair@286 | 523 | } while((item & TYPE_MASK) == T_ATTRIBUTE); |
ohair@286 | 524 | } |
ohair@286 | 525 | } |