src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/tools/SAXEventSerializer.java

Fri, 23 Aug 2013 09:57:21 +0100

author
mkos
date
Fri, 23 Aug 2013 09:57:21 +0100
changeset 397
b99d7e355d4b
parent 384
8f2986ff0235
child 637
9c07ef4934dd
permissions
-rw-r--r--

8022885: Update JAX-WS RI integration to 2.2.9-b14140
8013016: Rebase 8009009 against the latest jdk8/jaxws
Reviewed-by: alanb, chegar

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

mercurial