Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2004, 2012, 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 | * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
aoqi@0 | 26 | */ |
aoqi@0 | 27 | |
aoqi@0 | 28 | package com.sun.xml.internal.fastinfoset.tools; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import java.io.IOException; |
aoqi@0 | 31 | import java.io.OutputStream; |
aoqi@0 | 32 | import java.io.OutputStreamWriter; |
aoqi@0 | 33 | import java.io.Writer; |
aoqi@0 | 34 | import java.util.ArrayList; |
aoqi@0 | 35 | import java.util.List; |
aoqi@0 | 36 | import java.util.Stack; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import org.xml.sax.Attributes; |
aoqi@0 | 39 | import org.xml.sax.SAXException; |
aoqi@0 | 40 | import org.xml.sax.ext.LexicalHandler; |
aoqi@0 | 41 | import org.xml.sax.helpers.DefaultHandler; |
aoqi@0 | 42 | import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
aoqi@0 | 43 | |
aoqi@0 | 44 | public class SAXEventSerializer extends DefaultHandler |
aoqi@0 | 45 | implements LexicalHandler { |
aoqi@0 | 46 | |
aoqi@0 | 47 | private Writer _writer; |
aoqi@0 | 48 | private boolean _charactersAreCDATA; |
aoqi@0 | 49 | private StringBuffer _characters; |
aoqi@0 | 50 | |
aoqi@0 | 51 | private Stack _namespaceStack = new Stack(); |
aoqi@0 | 52 | protected List _namespaceAttributes; |
aoqi@0 | 53 | |
aoqi@0 | 54 | public SAXEventSerializer(OutputStream s) throws IOException { |
aoqi@0 | 55 | _writer = new OutputStreamWriter(s); |
aoqi@0 | 56 | _charactersAreCDATA = false; |
aoqi@0 | 57 | } |
aoqi@0 | 58 | |
aoqi@0 | 59 | // -- ContentHandler interface --------------------------------------- |
aoqi@0 | 60 | |
aoqi@0 | 61 | public void startDocument() throws SAXException { |
aoqi@0 | 62 | try { |
aoqi@0 | 63 | _writer.write("<sax xmlns=\"http://www.sun.com/xml/sax-events\">\n"); |
aoqi@0 | 64 | _writer.write("<startDocument/>\n"); |
aoqi@0 | 65 | _writer.flush(); |
aoqi@0 | 66 | } |
aoqi@0 | 67 | catch (IOException e) { |
aoqi@0 | 68 | throw new SAXException(e); |
aoqi@0 | 69 | } |
aoqi@0 | 70 | } |
aoqi@0 | 71 | |
aoqi@0 | 72 | public void endDocument() throws SAXException { |
aoqi@0 | 73 | try { |
aoqi@0 | 74 | _writer.write("<endDocument/>\n"); |
aoqi@0 | 75 | _writer.write("</sax>"); |
aoqi@0 | 76 | _writer.flush(); |
aoqi@0 | 77 | _writer.close(); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | catch (IOException e) { |
aoqi@0 | 80 | throw new SAXException(e); |
aoqi@0 | 81 | } |
aoqi@0 | 82 | } |
aoqi@0 | 83 | |
aoqi@0 | 84 | |
aoqi@0 | 85 | public void startPrefixMapping(String prefix, String uri) |
aoqi@0 | 86 | throws SAXException |
aoqi@0 | 87 | { |
aoqi@0 | 88 | if (_namespaceAttributes == null) { |
aoqi@0 | 89 | _namespaceAttributes = new ArrayList(); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | String qName = (prefix.length() == 0) ? "xmlns" : "xmlns" + prefix; |
aoqi@0 | 93 | AttributeValueHolder attribute = new AttributeValueHolder( |
aoqi@0 | 94 | qName, |
aoqi@0 | 95 | prefix, |
aoqi@0 | 96 | uri, |
aoqi@0 | 97 | null, |
aoqi@0 | 98 | null); |
aoqi@0 | 99 | _namespaceAttributes.add(attribute); |
aoqi@0 | 100 | } |
aoqi@0 | 101 | |
aoqi@0 | 102 | public void endPrefixMapping(String prefix) |
aoqi@0 | 103 | throws SAXException |
aoqi@0 | 104 | { |
aoqi@0 | 105 | /* |
aoqi@0 | 106 | try { |
aoqi@0 | 107 | outputCharacters(); |
aoqi@0 | 108 | |
aoqi@0 | 109 | _writer.write("<endPrefixMapping prefix=\"" + |
aoqi@0 | 110 | prefix + "\"/>\n"); |
aoqi@0 | 111 | _writer.flush(); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | catch (IOException e) { |
aoqi@0 | 114 | throw new SAXException(e); |
aoqi@0 | 115 | } |
aoqi@0 | 116 | */ |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | public void startElement(String uri, String localName, |
aoqi@0 | 120 | String qName, Attributes attributes) |
aoqi@0 | 121 | throws SAXException |
aoqi@0 | 122 | { |
aoqi@0 | 123 | try { |
aoqi@0 | 124 | outputCharacters(); |
aoqi@0 | 125 | |
aoqi@0 | 126 | if (_namespaceAttributes != null) { |
aoqi@0 | 127 | |
aoqi@0 | 128 | AttributeValueHolder[] attrsHolder = new AttributeValueHolder[0]; |
aoqi@0 | 129 | attrsHolder = (AttributeValueHolder[])_namespaceAttributes.toArray(attrsHolder); |
aoqi@0 | 130 | |
aoqi@0 | 131 | // Sort attributes |
aoqi@0 | 132 | quicksort(attrsHolder, 0, attrsHolder.length - 1); |
aoqi@0 | 133 | |
aoqi@0 | 134 | for (int i = 0; i < attrsHolder.length; i++) { |
aoqi@0 | 135 | _writer.write("<startPrefixMapping prefix=\"" + |
aoqi@0 | 136 | attrsHolder[i].localName + "\" uri=\"" + attrsHolder[i].uri + "\"/>\n"); |
aoqi@0 | 137 | _writer.flush(); |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | _namespaceStack.push(attrsHolder); |
aoqi@0 | 141 | _namespaceAttributes = null; |
aoqi@0 | 142 | } else { |
aoqi@0 | 143 | _namespaceStack.push(null); |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | AttributeValueHolder[] attrsHolder = |
aoqi@0 | 147 | new AttributeValueHolder[attributes.getLength()]; |
aoqi@0 | 148 | for (int i = 0; i < attributes.getLength(); i++) { |
aoqi@0 | 149 | attrsHolder[i] = new AttributeValueHolder( |
aoqi@0 | 150 | attributes.getQName(i), |
aoqi@0 | 151 | attributes.getLocalName(i), |
aoqi@0 | 152 | attributes.getURI(i), |
aoqi@0 | 153 | attributes.getType(i), |
aoqi@0 | 154 | attributes.getValue(i)); |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | // Sort attributes |
aoqi@0 | 158 | quicksort(attrsHolder, 0, attrsHolder.length - 1); |
aoqi@0 | 159 | |
aoqi@0 | 160 | int attributeCount = 0; |
aoqi@0 | 161 | for (int i = 0; i < attrsHolder.length; i++) { |
aoqi@0 | 162 | if (attrsHolder[i].uri.equals("http://www.w3.org/2000/xmlns/")) { |
aoqi@0 | 163 | // Ignore XMLNS attributes |
aoqi@0 | 164 | continue; |
aoqi@0 | 165 | } |
aoqi@0 | 166 | attributeCount++; |
aoqi@0 | 167 | } |
aoqi@0 | 168 | |
aoqi@0 | 169 | if (attributeCount == 0) { |
aoqi@0 | 170 | _writer.write("<startElement uri=\"" + uri |
aoqi@0 | 171 | + "\" localName=\"" + localName + "\" qName=\"" |
aoqi@0 | 172 | + qName + "\"/>\n"); |
aoqi@0 | 173 | return; |
aoqi@0 | 174 | } |
aoqi@0 | 175 | |
aoqi@0 | 176 | _writer.write("<startElement uri=\"" + uri |
aoqi@0 | 177 | + "\" localName=\"" + localName + "\" qName=\"" |
aoqi@0 | 178 | + qName + "\">\n"); |
aoqi@0 | 179 | |
aoqi@0 | 180 | // Serialize attributes as children |
aoqi@0 | 181 | for (int i = 0; i < attrsHolder.length; i++) { |
aoqi@0 | 182 | if (attrsHolder[i].uri.equals("http://www.w3.org/2000/xmlns/")) { |
aoqi@0 | 183 | // Ignore XMLNS attributes |
aoqi@0 | 184 | continue; |
aoqi@0 | 185 | } |
aoqi@0 | 186 | _writer.write( |
aoqi@0 | 187 | " <attribute qName=\"" + attrsHolder[i].qName + |
aoqi@0 | 188 | "\" localName=\"" + attrsHolder[i].localName + |
aoqi@0 | 189 | "\" uri=\"" + attrsHolder[i].uri + |
aoqi@0 | 190 | // "\" type=\"" + attrsHolder[i].type + |
aoqi@0 | 191 | "\" value=\"" + attrsHolder[i].value + |
aoqi@0 | 192 | "\"/>\n"); |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | _writer.write("</startElement>\n"); |
aoqi@0 | 196 | _writer.flush(); |
aoqi@0 | 197 | } |
aoqi@0 | 198 | catch (IOException e) { |
aoqi@0 | 199 | throw new SAXException(e); |
aoqi@0 | 200 | } |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | public void endElement(String uri, String localName, String qName) |
aoqi@0 | 204 | throws SAXException |
aoqi@0 | 205 | { |
aoqi@0 | 206 | try { |
aoqi@0 | 207 | outputCharacters(); |
aoqi@0 | 208 | |
aoqi@0 | 209 | _writer.write("<endElement uri=\"" + uri |
aoqi@0 | 210 | + "\" localName=\"" + localName + "\" qName=\"" |
aoqi@0 | 211 | + qName + "\"/>\n"); |
aoqi@0 | 212 | _writer.flush(); |
aoqi@0 | 213 | |
aoqi@0 | 214 | // Write out the end prefix here rather than waiting |
aoqi@0 | 215 | // for the explicit events |
aoqi@0 | 216 | AttributeValueHolder[] attrsHolder = (AttributeValueHolder[])_namespaceStack.pop(); |
aoqi@0 | 217 | if (attrsHolder != null) { |
aoqi@0 | 218 | for (int i = 0; i < attrsHolder.length; i++) { |
aoqi@0 | 219 | _writer.write("<endPrefixMapping prefix=\"" + |
aoqi@0 | 220 | attrsHolder[i].localName + "\"/>\n"); |
aoqi@0 | 221 | _writer.flush(); |
aoqi@0 | 222 | } |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | } |
aoqi@0 | 226 | catch (IOException e) { |
aoqi@0 | 227 | throw new SAXException(e); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | } |
aoqi@0 | 230 | |
aoqi@0 | 231 | public void characters(char[] ch, int start, int length) |
aoqi@0 | 232 | throws SAXException |
aoqi@0 | 233 | { |
aoqi@0 | 234 | if (length == 0) { |
aoqi@0 | 235 | return; |
aoqi@0 | 236 | } |
aoqi@0 | 237 | |
aoqi@0 | 238 | if (_characters == null) { |
aoqi@0 | 239 | _characters = new StringBuffer(); |
aoqi@0 | 240 | } |
aoqi@0 | 241 | |
aoqi@0 | 242 | // Coalesce multiple character events |
aoqi@0 | 243 | _characters.append(ch, start, length); |
aoqi@0 | 244 | |
aoqi@0 | 245 | /* |
aoqi@0 | 246 | try { |
aoqi@0 | 247 | _writer.write("<characters>" + |
aoqi@0 | 248 | (_charactersAreCDATA ? "<![CDATA[" : "") + |
aoqi@0 | 249 | new String(ch, start, length) + |
aoqi@0 | 250 | (_charactersAreCDATA ? "]]>" : "") + |
aoqi@0 | 251 | "</characters>\n"); |
aoqi@0 | 252 | _writer.flush(); |
aoqi@0 | 253 | } |
aoqi@0 | 254 | catch (IOException e) { |
aoqi@0 | 255 | throw new SAXException(e); |
aoqi@0 | 256 | } |
aoqi@0 | 257 | */ |
aoqi@0 | 258 | } |
aoqi@0 | 259 | |
aoqi@0 | 260 | private void outputCharacters() throws SAXException { |
aoqi@0 | 261 | if (_characters == null) { |
aoqi@0 | 262 | return; |
aoqi@0 | 263 | } |
aoqi@0 | 264 | |
aoqi@0 | 265 | try { |
aoqi@0 | 266 | _writer.write("<characters>" + |
aoqi@0 | 267 | (_charactersAreCDATA ? "<![CDATA[" : "") + |
aoqi@0 | 268 | _characters + |
aoqi@0 | 269 | (_charactersAreCDATA ? "]]>" : "") + |
aoqi@0 | 270 | "</characters>\n"); |
aoqi@0 | 271 | _writer.flush(); |
aoqi@0 | 272 | |
aoqi@0 | 273 | _characters = null; |
aoqi@0 | 274 | } catch (IOException e) { |
aoqi@0 | 275 | throw new SAXException(e); |
aoqi@0 | 276 | } |
aoqi@0 | 277 | } |
aoqi@0 | 278 | |
aoqi@0 | 279 | public void ignorableWhitespace(char[] ch, int start, int length) |
aoqi@0 | 280 | throws SAXException |
aoqi@0 | 281 | { |
aoqi@0 | 282 | // Report ignorable ws as characters (assumes validation off) |
aoqi@0 | 283 | characters(ch, start, length); |
aoqi@0 | 284 | } |
aoqi@0 | 285 | |
aoqi@0 | 286 | public void processingInstruction(String target, String data) |
aoqi@0 | 287 | throws SAXException |
aoqi@0 | 288 | { |
aoqi@0 | 289 | try { |
aoqi@0 | 290 | outputCharacters(); |
aoqi@0 | 291 | |
aoqi@0 | 292 | _writer.write("<processingInstruction target=\"" + target |
aoqi@0 | 293 | + "\" data=\"" + data + "\"/>\n"); |
aoqi@0 | 294 | _writer.flush(); |
aoqi@0 | 295 | } |
aoqi@0 | 296 | catch (IOException e) { |
aoqi@0 | 297 | throw new SAXException(e); |
aoqi@0 | 298 | } |
aoqi@0 | 299 | } |
aoqi@0 | 300 | |
aoqi@0 | 301 | // -- LexicalHandler interface --------------------------------------- |
aoqi@0 | 302 | |
aoqi@0 | 303 | public void startDTD(String name, String publicId, String systemId) |
aoqi@0 | 304 | throws SAXException { |
aoqi@0 | 305 | // Not implemented |
aoqi@0 | 306 | } |
aoqi@0 | 307 | |
aoqi@0 | 308 | public void endDTD() |
aoqi@0 | 309 | throws SAXException { |
aoqi@0 | 310 | // Not implemented |
aoqi@0 | 311 | } |
aoqi@0 | 312 | |
aoqi@0 | 313 | public void startEntity(String name) |
aoqi@0 | 314 | throws SAXException { |
aoqi@0 | 315 | // Not implemented |
aoqi@0 | 316 | } |
aoqi@0 | 317 | |
aoqi@0 | 318 | public void endEntity(String name) |
aoqi@0 | 319 | throws SAXException { |
aoqi@0 | 320 | // Not implemented |
aoqi@0 | 321 | } |
aoqi@0 | 322 | |
aoqi@0 | 323 | public void startCDATA() |
aoqi@0 | 324 | throws SAXException { |
aoqi@0 | 325 | _charactersAreCDATA = true; |
aoqi@0 | 326 | } |
aoqi@0 | 327 | |
aoqi@0 | 328 | public void endCDATA() |
aoqi@0 | 329 | throws SAXException { |
aoqi@0 | 330 | _charactersAreCDATA = false; |
aoqi@0 | 331 | } |
aoqi@0 | 332 | |
aoqi@0 | 333 | public void comment(char[] ch, int start, int length) |
aoqi@0 | 334 | throws SAXException |
aoqi@0 | 335 | { |
aoqi@0 | 336 | try { |
aoqi@0 | 337 | outputCharacters(); |
aoqi@0 | 338 | |
aoqi@0 | 339 | _writer.write("<comment>" + |
aoqi@0 | 340 | new String(ch, start, length) + |
aoqi@0 | 341 | "</comment>\n"); |
aoqi@0 | 342 | _writer.flush(); |
aoqi@0 | 343 | } |
aoqi@0 | 344 | catch (IOException e) { |
aoqi@0 | 345 | throw new SAXException(e); |
aoqi@0 | 346 | } |
aoqi@0 | 347 | } |
aoqi@0 | 348 | |
aoqi@0 | 349 | // -- Utility methods ------------------------------------------------ |
aoqi@0 | 350 | |
aoqi@0 | 351 | private void quicksort(AttributeValueHolder[] attrs, int p, int r) { |
aoqi@0 | 352 | while (p < r) { |
aoqi@0 | 353 | final int q = partition(attrs, p, r); |
aoqi@0 | 354 | quicksort(attrs, p, q); |
aoqi@0 | 355 | p = q + 1; |
aoqi@0 | 356 | } |
aoqi@0 | 357 | } |
aoqi@0 | 358 | |
aoqi@0 | 359 | private int partition(AttributeValueHolder[] attrs, int p, int r) { |
aoqi@0 | 360 | AttributeValueHolder x = attrs[(p + r) >>> 1]; |
aoqi@0 | 361 | int i = p - 1; |
aoqi@0 | 362 | int j = r + 1; |
aoqi@0 | 363 | while (true) { |
aoqi@0 | 364 | while (x.compareTo(attrs[--j]) < 0); |
aoqi@0 | 365 | while (x.compareTo(attrs[++i]) > 0); |
aoqi@0 | 366 | if (i < j) { |
aoqi@0 | 367 | final AttributeValueHolder t = attrs[i]; |
aoqi@0 | 368 | attrs[i] = attrs[j]; |
aoqi@0 | 369 | attrs[j] = t; |
aoqi@0 | 370 | } |
aoqi@0 | 371 | else { |
aoqi@0 | 372 | return j; |
aoqi@0 | 373 | } |
aoqi@0 | 374 | } |
aoqi@0 | 375 | } |
aoqi@0 | 376 | |
aoqi@0 | 377 | public static class AttributeValueHolder implements Comparable { |
aoqi@0 | 378 | public final String qName; |
aoqi@0 | 379 | public final String localName; |
aoqi@0 | 380 | public final String uri; |
aoqi@0 | 381 | public final String type; |
aoqi@0 | 382 | public final String value; |
aoqi@0 | 383 | |
aoqi@0 | 384 | public AttributeValueHolder(String qName, |
aoqi@0 | 385 | String localName, |
aoqi@0 | 386 | String uri, |
aoqi@0 | 387 | String type, |
aoqi@0 | 388 | String value) |
aoqi@0 | 389 | { |
aoqi@0 | 390 | this.qName = qName; |
aoqi@0 | 391 | this.localName = localName; |
aoqi@0 | 392 | this.uri = uri; |
aoqi@0 | 393 | this.type = type; |
aoqi@0 | 394 | this.value = value; |
aoqi@0 | 395 | } |
aoqi@0 | 396 | |
aoqi@0 | 397 | public int compareTo(Object o) { |
aoqi@0 | 398 | try { |
aoqi@0 | 399 | return qName.compareTo(((AttributeValueHolder) o).qName); |
aoqi@0 | 400 | } catch (Exception e) { |
aoqi@0 | 401 | throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.AttributeValueHolderExpected")); |
aoqi@0 | 402 | } |
aoqi@0 | 403 | } |
aoqi@0 | 404 | |
aoqi@0 | 405 | @Override |
aoqi@0 | 406 | public boolean equals(Object o) { |
aoqi@0 | 407 | try { |
aoqi@0 | 408 | return (o instanceof AttributeValueHolder) && |
aoqi@0 | 409 | qName.equals(((AttributeValueHolder) o).qName); |
aoqi@0 | 410 | } catch (Exception e) { |
aoqi@0 | 411 | throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.AttributeValueHolderExpected")); |
aoqi@0 | 412 | } |
aoqi@0 | 413 | } |
aoqi@0 | 414 | |
aoqi@0 | 415 | @Override |
aoqi@0 | 416 | public int hashCode() { |
aoqi@0 | 417 | int hash = 7; |
aoqi@0 | 418 | hash = 97 * hash + (this.qName != null ? this.qName.hashCode() : 0); |
aoqi@0 | 419 | return hash; |
aoqi@0 | 420 | } |
aoqi@0 | 421 | } |
aoqi@0 | 422 | |
aoqi@0 | 423 | } |