Sun, 18 Jun 2017 23:18:45 +0100
8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea
ohair@286 | 1 | /* |
mkos@397 | 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; |
ohair@286 | 27 | |
ohair@286 | 28 | import com.sun.xml.internal.stream.buffer.sax.Properties; |
ohair@286 | 29 | import com.sun.xml.internal.stream.buffer.sax.SAXBufferCreator; |
ohair@286 | 30 | import com.sun.xml.internal.stream.buffer.stax.StreamReaderBufferCreator; |
ohair@286 | 31 | import com.sun.xml.internal.stream.buffer.stax.StreamWriterBufferCreator; |
ohair@286 | 32 | import org.xml.sax.ContentHandler; |
ohair@286 | 33 | import org.xml.sax.SAXException; |
ohair@286 | 34 | import org.xml.sax.XMLReader; |
ohair@286 | 35 | |
ohair@286 | 36 | import javax.xml.stream.XMLStreamException; |
ohair@286 | 37 | import javax.xml.stream.XMLStreamReader; |
ohair@286 | 38 | import javax.xml.stream.XMLStreamWriter; |
ohair@286 | 39 | import java.io.IOException; |
ohair@286 | 40 | import java.io.InputStream; |
ohair@286 | 41 | |
ohair@286 | 42 | /** |
ohair@286 | 43 | * |
ohair@286 | 44 | * A mutable stream-based buffer of an XML infoset. |
ohair@286 | 45 | * |
ohair@286 | 46 | * <p> |
ohair@286 | 47 | * A MutableXMLStreamBuffer is created using specific SAX and StAX-based |
ohair@286 | 48 | * creators. Utility methods on MutableXMLStreamBuffer are provided for |
ohair@286 | 49 | * such functionality that utilize SAX and StAX-based creators. |
ohair@286 | 50 | * |
ohair@286 | 51 | * <p> |
ohair@286 | 52 | * Once instantiated the same instance of a MutableXMLStreamBuffer may be reused for |
ohair@286 | 53 | * creation to reduce the amount of Objects instantiated and garbage |
ohair@286 | 54 | * collected that are required for internally representing an XML infoset. |
ohair@286 | 55 | * |
ohair@286 | 56 | * <p> |
ohair@286 | 57 | * A MutableXMLStreamBuffer is not designed to be created and processed |
ohair@286 | 58 | * concurrently. If done so unspecified behaviour may occur. |
ohair@286 | 59 | */ |
ohair@286 | 60 | public class MutableXMLStreamBuffer extends XMLStreamBuffer { |
ohair@286 | 61 | /** |
ohair@286 | 62 | * The default array size for the arrays used in internal representation |
ohair@286 | 63 | * of the XML infoset. |
ohair@286 | 64 | */ |
ohair@286 | 65 | public static final int DEFAULT_ARRAY_SIZE = 512; |
ohair@286 | 66 | |
ohair@286 | 67 | /** |
ohair@286 | 68 | * Create a new MutableXMLStreamBuffer using the |
ohair@286 | 69 | * {@link MutableXMLStreamBuffer#DEFAULT_ARRAY_SIZE}. |
ohair@286 | 70 | */ |
ohair@286 | 71 | public MutableXMLStreamBuffer() { |
ohair@286 | 72 | this(DEFAULT_ARRAY_SIZE); |
ohair@286 | 73 | } |
ohair@286 | 74 | |
ohair@286 | 75 | /** |
ohair@286 | 76 | * Set the system identifier for this buffer. |
ohair@286 | 77 | * @param systemId The system identifier. |
ohair@286 | 78 | */ |
ohair@286 | 79 | public void setSystemId(String systemId) { |
ohair@286 | 80 | this.systemId = systemId; |
ohair@286 | 81 | } |
ohair@286 | 82 | |
ohair@286 | 83 | /** |
ohair@286 | 84 | * Create a new MutableXMLStreamBuffer. |
ohair@286 | 85 | * |
ohair@286 | 86 | * @param size |
ohair@286 | 87 | * The size of the arrays used in the internal representation |
ohair@286 | 88 | * of the XML infoset. |
ohair@286 | 89 | * @throws NegativeArraySizeException |
ohair@286 | 90 | * If the <code>size</code> argument is less than <code>0</code>. |
ohair@286 | 91 | */ |
ohair@286 | 92 | public MutableXMLStreamBuffer(int size) { |
ohair@286 | 93 | _structure = new FragmentedArray<byte[]>(new byte[size]); |
ohair@286 | 94 | _structureStrings = new FragmentedArray<String[]>(new String[size]); |
ohair@286 | 95 | _contentCharactersBuffer = new FragmentedArray<char[]>(new char[4096]); |
ohair@286 | 96 | _contentObjects = new FragmentedArray<Object[]>(new Object[size]); |
ohair@286 | 97 | |
ohair@286 | 98 | // Set the first element of structure array to indicate an empty buffer |
ohair@286 | 99 | // that has not been created |
ohair@286 | 100 | _structure.getArray()[0] = (byte) AbstractCreatorProcessor.T_END; |
ohair@286 | 101 | } |
ohair@286 | 102 | |
ohair@286 | 103 | /** |
ohair@286 | 104 | * Create contents of a buffer from a XMLStreamReader. |
ohair@286 | 105 | * |
ohair@286 | 106 | * <p> |
ohair@286 | 107 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
ohair@286 | 108 | * |
ohair@286 | 109 | * <p> |
ohair@286 | 110 | * The MutableXMLStreamBuffer is created by consuming the events on the XMLStreamReader using |
ohair@286 | 111 | * an instance of {@link StreamReaderBufferCreator}. |
ohair@286 | 112 | * |
ohair@286 | 113 | * @param reader |
ohair@286 | 114 | * A XMLStreamReader to read from to create. |
ohair@286 | 115 | */ |
ohair@286 | 116 | public void createFromXMLStreamReader(XMLStreamReader reader) throws XMLStreamException { |
ohair@286 | 117 | reset(); |
ohair@286 | 118 | StreamReaderBufferCreator c = new StreamReaderBufferCreator(this); |
ohair@286 | 119 | c.create(reader); |
ohair@286 | 120 | } |
ohair@286 | 121 | |
ohair@286 | 122 | /** |
ohair@286 | 123 | * Create contents of a buffer from a XMLStreamWriter. |
ohair@286 | 124 | * |
ohair@286 | 125 | * <p> |
ohair@286 | 126 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
ohair@286 | 127 | * |
ohair@286 | 128 | * <p> |
ohair@286 | 129 | * The MutableXMLStreamBuffer is created by consuming events on a XMLStreamWriter using |
ohair@286 | 130 | * an instance of {@link StreamWriterBufferCreator}. |
ohair@286 | 131 | */ |
ohair@286 | 132 | public XMLStreamWriter createFromXMLStreamWriter() { |
ohair@286 | 133 | reset(); |
ohair@286 | 134 | return new StreamWriterBufferCreator(this); |
ohair@286 | 135 | } |
ohair@286 | 136 | |
ohair@286 | 137 | /** |
ohair@286 | 138 | * Create contents of a buffer from a {@link SAXBufferCreator}. |
ohair@286 | 139 | * |
ohair@286 | 140 | * <p> |
ohair@286 | 141 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
ohair@286 | 142 | * |
ohair@286 | 143 | * <p> |
ohair@286 | 144 | * The MutableXMLStreamBuffer is created by consuming events from a {@link ContentHandler} using |
ohair@286 | 145 | * an instance of {@link SAXBufferCreator}. |
ohair@286 | 146 | * |
ohair@286 | 147 | * @return The {@link SAXBufferCreator} to create from. |
ohair@286 | 148 | */ |
ohair@286 | 149 | public SAXBufferCreator createFromSAXBufferCreator() { |
ohair@286 | 150 | reset(); |
ohair@286 | 151 | SAXBufferCreator c = new SAXBufferCreator(); |
ohair@286 | 152 | c.setBuffer(this); |
ohair@286 | 153 | return c; |
ohair@286 | 154 | } |
ohair@286 | 155 | |
ohair@286 | 156 | /** |
ohair@286 | 157 | * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}. |
ohair@286 | 158 | * |
ohair@286 | 159 | * <p> |
ohair@286 | 160 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
ohair@286 | 161 | * |
ohair@286 | 162 | * <p> |
ohair@286 | 163 | * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator} |
ohair@286 | 164 | * and registering associated handlers on the {@link XMLReader}. |
ohair@286 | 165 | * |
ohair@286 | 166 | * @param reader |
ohair@286 | 167 | * The {@link XMLReader} to use for parsing. |
ohair@286 | 168 | * @param in |
ohair@286 | 169 | * The {@link InputStream} to be parsed. |
ohair@286 | 170 | */ |
ohair@286 | 171 | public void createFromXMLReader(XMLReader reader, InputStream in) throws SAXException, IOException { |
ohair@286 | 172 | createFromXMLReader(reader, in, null); |
ohair@286 | 173 | } |
ohair@286 | 174 | |
ohair@286 | 175 | /** |
ohair@286 | 176 | * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}. |
ohair@286 | 177 | * |
ohair@286 | 178 | * <p> |
ohair@286 | 179 | * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation. |
ohair@286 | 180 | * |
ohair@286 | 181 | * <p> |
ohair@286 | 182 | * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator} |
ohair@286 | 183 | * and registering associated handlers on the {@link XMLReader}. |
ohair@286 | 184 | * |
ohair@286 | 185 | * @param reader |
ohair@286 | 186 | * The {@link XMLReader} to use for parsing. |
ohair@286 | 187 | * @param in |
ohair@286 | 188 | * The {@link InputStream} to be parsed. |
ohair@286 | 189 | * @param systemId |
ohair@286 | 190 | * The system ID of the input stream. |
ohair@286 | 191 | */ |
ohair@286 | 192 | public void createFromXMLReader(XMLReader reader, InputStream in, String systemId) throws SAXException, IOException { |
ohair@286 | 193 | reset(); |
ohair@286 | 194 | SAXBufferCreator c = new SAXBufferCreator(this); |
ohair@286 | 195 | |
ohair@286 | 196 | reader.setContentHandler(c); |
ohair@286 | 197 | reader.setDTDHandler(c); |
ohair@286 | 198 | reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, c); |
ohair@286 | 199 | |
ohair@286 | 200 | c.create(reader, in, systemId); |
ohair@286 | 201 | } |
ohair@286 | 202 | |
ohair@286 | 203 | /** |
ohair@286 | 204 | * Reset the MutableXMLStreamBuffer. |
ohair@286 | 205 | * |
ohair@286 | 206 | * <p> |
ohair@286 | 207 | * This method will reset the MutableXMLStreamBuffer to a state of being "uncreated" |
ohair@286 | 208 | * similar to the state of a newly instantiated MutableXMLStreamBuffer. |
ohair@286 | 209 | * |
ohair@286 | 210 | * <p> |
ohair@286 | 211 | * As many Objects as possible will be retained for reuse in future creation. |
ohair@286 | 212 | */ |
ohair@286 | 213 | public void reset() { |
ohair@286 | 214 | // Reset the ptrs in arrays to 0 |
ohair@286 | 215 | _structurePtr = |
ohair@286 | 216 | _structureStringsPtr = |
ohair@286 | 217 | _contentCharactersBufferPtr = |
ohair@286 | 218 | _contentObjectsPtr = 0; |
ohair@286 | 219 | |
ohair@286 | 220 | // Set the first element of structure array to indicate an empty buffer |
ohair@286 | 221 | // that has not been created |
ohair@286 | 222 | _structure.getArray()[0] = (byte)AbstractCreatorProcessor.T_END; |
ohair@286 | 223 | |
ohair@286 | 224 | // Clean up content objects |
ohair@286 | 225 | _contentObjects.setNext(null); |
ohair@286 | 226 | final Object[] o = _contentObjects.getArray(); |
ohair@286 | 227 | for (int i = 0; i < o.length; i++) { |
ohair@286 | 228 | if (o[i] != null) { |
ohair@286 | 229 | o[i] = null; |
ohair@286 | 230 | } else { |
ohair@286 | 231 | break; |
ohair@286 | 232 | } |
ohair@286 | 233 | } |
ohair@286 | 234 | |
ohair@286 | 235 | treeCount = 0; |
ohair@286 | 236 | |
ohair@286 | 237 | /* |
ohair@286 | 238 | * TODO consider truncating the size of _structureStrings and |
ohair@286 | 239 | * _contentCharactersBuffer to limit the memory used by the buffer |
ohair@286 | 240 | */ |
ohair@286 | 241 | } |
ohair@286 | 242 | |
ohair@286 | 243 | |
ohair@286 | 244 | protected void setHasInternedStrings(boolean hasInternedStrings) { |
ohair@286 | 245 | _hasInternedStrings = hasInternedStrings; |
ohair@286 | 246 | } |
ohair@286 | 247 | } |