| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package org.xmlpull.renamed; |
| 18 |
|
| 19 |
import org.xmlpull.v1.XmlSerializer; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import java.io.OutputStream; |
| 23 |
import java.io.OutputStreamWriter; |
| 24 |
import java.io.Writer; |
| 25 |
|
| 26 |
/** |
| 27 |
* Implementation of XmlSerializer interface from XmlPull V1 API. This |
| 28 |
* implementation is optimized for performance and low memory footprint. |
| 29 |
* |
| 30 |
* <p> |
| 31 |
* Implemented features: |
| 32 |
* <ul> |
| 33 |
* <li>FEATURE_NAMES_INTERNED - when enabled all returned names (namespaces, |
| 34 |
* prefixes) will be interned and it is required that all names passed as |
| 35 |
* arguments MUST be interned |
| 36 |
* <li>FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE |
| 37 |
* </ul> |
| 38 |
* <p> |
| 39 |
* Implemented properties: |
| 40 |
* <ul> |
| 41 |
* <li>PROPERTY_SERIALIZER_INDENTATION |
| 42 |
* <li>PROPERTY_SERIALIZER_LINE_SEPARATOR |
| 43 |
* </ul> |
| 44 |
* |
| 45 |
*/ |
| 46 |
public class MXSerializer implements XmlSerializer { |
| 47 |
protected final static String XML_URI = "http://www.w3.org/XML/1998/namespace"; |
| 48 |
protected final static String XMLNS_URI = "http://www.w3.org/2000/xmlns/"; |
| 49 |
private static final boolean TRACE_SIZING = false; |
| 50 |
private static final boolean TRACE_ESCAPING = false; |
| 51 |
|
| 52 |
protected final String FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE = "http://xmlpull.org/v1/doc/features.html#serializer-attvalue-use-apostrophe"; |
| 53 |
protected final String FEATURE_NAMES_INTERNED = "http://xmlpull.org/v1/doc/features.html#names-interned"; |
| 54 |
protected final String PROPERTY_SERIALIZER_INDENTATION = "http://xmlpull.org/v1/doc/properties.html#serializer-indentation"; |
| 55 |
protected final String PROPERTY_SERIALIZER_LINE_SEPARATOR = "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator"; |
| 56 |
protected final static String PROPERTY_LOCATION = "http://xmlpull.org/v1/doc/properties.html#location"; |
| 57 |
|
| 58 |
// properties/features |
| 59 |
protected boolean namesInterned; |
| 60 |
protected boolean attributeUseApostrophe; |
| 61 |
protected String indentationString = null; // " "; |
| 62 |
protected String lineSeparator = "\n"; |
| 63 |
|
| 64 |
protected String location; |
| 65 |
protected Writer out; |
| 66 |
|
| 67 |
protected int autoDeclaredPrefixes; |
| 68 |
|
| 69 |
protected int depth = 0; |
| 70 |
|
| 71 |
// element stack |
| 72 |
protected String[] elNamespace = new String[2]; |
| 73 |
protected String[] elName = new String[elNamespace.length]; |
| 74 |
protected String[] elPrefix = new String[elNamespace.length]; |
| 75 |
protected int[] elNamespaceCount = new int[elNamespace.length]; |
| 76 |
|
| 77 |
// namespace stack |
| 78 |
protected int namespaceEnd = 0; |
| 79 |
protected String[] namespacePrefix = new String[8]; |
| 80 |
protected String[] namespaceUri = new String[namespacePrefix.length]; |
| 81 |
|
| 82 |
protected boolean finished; |
| 83 |
protected boolean pastRoot; |
| 84 |
protected boolean setPrefixCalled; |
| 85 |
protected boolean startTagIncomplete; |
| 86 |
|
| 87 |
protected boolean doIndent; |
| 88 |
protected boolean seenTag; |
| 89 |
|
| 90 |
protected boolean seenBracket; |
| 91 |
protected boolean seenBracketBracket; |
| 92 |
|
| 93 |
// buffer output if needed to write escaped String see text(String) |
| 94 |
private static final int BUF_LEN = Runtime.getRuntime().freeMemory() > 1000000L ? 8 * 1024 : 256; |
| 95 |
protected char[] buf = new char[BUF_LEN]; |
| 96 |
|
| 97 |
protected static final String[] precomputedPrefixes; |
| 98 |
|
| 99 |
static { |
| 100 |
precomputedPrefixes = new String[32]; // arbitrary number ... |
| 101 |
for (int i = 0; i < precomputedPrefixes.length; i++) { |
| 102 |
precomputedPrefixes[i] = ("n" + i).intern(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private final boolean checkNamesInterned = false; |
| 107 |
|
| 108 |
private void checkInterning(String name) { |
| 109 |
if (namesInterned && name != name.intern()) { |
| 110 |
throw new IllegalArgumentException("all names passed as arguments must be interned" |
| 111 |
+ "when NAMES INTERNED feature is enabled"); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
protected void reset() { |
| 116 |
location = null; |
| 117 |
out = null; |
| 118 |
autoDeclaredPrefixes = 0; |
| 119 |
depth = 0; |
| 120 |
|
| 121 |
// nullify references on all levels to allow it to be GCed |
| 122 |
for (int i = 0; i < elNamespaceCount.length; i++) { |
| 123 |
elName[i] = null; |
| 124 |
elPrefix[i] = null; |
| 125 |
elNamespace[i] = null; |
| 126 |
elNamespaceCount[i] = 2; |
| 127 |
} |
| 128 |
|
| 129 |
namespaceEnd = 0; |
| 130 |
|
| 131 |
// TODO: how to prevent from reporting this namespace? |
| 132 |
// this is special namespace declared for consistency with XML infoset |
| 133 |
namespacePrefix[namespaceEnd] = "xmlns"; |
| 134 |
namespaceUri[namespaceEnd] = XMLNS_URI; |
| 135 |
++namespaceEnd; |
| 136 |
|
| 137 |
namespacePrefix[namespaceEnd] = "xml"; |
| 138 |
namespaceUri[namespaceEnd] = XML_URI; |
| 139 |
++namespaceEnd; |
| 140 |
|
| 141 |
finished = false; |
| 142 |
pastRoot = false; |
| 143 |
setPrefixCalled = false; |
| 144 |
startTagIncomplete = false; |
| 145 |
seenTag = false; |
| 146 |
|
| 147 |
seenBracket = false; |
| 148 |
seenBracketBracket = false; |
| 149 |
} |
| 150 |
|
| 151 |
protected void ensureElementsCapacity() { |
| 152 |
final int elStackSize = elName.length; |
| 153 |
final int newSize = (depth >= 7 ? 2 * depth : 8) + 2; |
| 154 |
|
| 155 |
if (TRACE_SIZING) { |
| 156 |
System.err.println(getClass().getName() + " elStackSize " |
| 157 |
+ elStackSize + " ==> " + newSize); |
| 158 |
} |
| 159 |
final boolean needsCopying = elStackSize > 0; |
| 160 |
String[] arr; |
| 161 |
// reuse arr local variable slot |
| 162 |
arr = new String[newSize]; |
| 163 |
if (needsCopying) |
| 164 |
System.arraycopy(elName, 0, arr, 0, elStackSize); |
| 165 |
elName = arr; |
| 166 |
|
| 167 |
arr = new String[newSize]; |
| 168 |
if (needsCopying) |
| 169 |
System.arraycopy(elPrefix, 0, arr, 0, elStackSize); |
| 170 |
elPrefix = arr; |
| 171 |
|
| 172 |
arr = new String[newSize]; |
| 173 |
if (needsCopying) |
| 174 |
System.arraycopy(elNamespace, 0, arr, 0, elStackSize); |
| 175 |
elNamespace = arr; |
| 176 |
|
| 177 |
final int[] iarr = new int[newSize]; |
| 178 |
if (needsCopying) { |
| 179 |
System.arraycopy(elNamespaceCount, 0, iarr, 0, elStackSize); |
| 180 |
} else { |
| 181 |
// special initialization |
| 182 |
iarr[0] = 0; |
| 183 |
} |
| 184 |
elNamespaceCount = iarr; |
| 185 |
} |
| 186 |
|
| 187 |
protected void ensureNamespacesCapacity() { // int size) { |
| 188 |
final int newSize = namespaceEnd > 7 ? 2 * namespaceEnd : 8; |
| 189 |
if (TRACE_SIZING) { |
| 190 |
System.err.println(getClass().getName() + " namespaceSize " + namespacePrefix.length + " ==> " + newSize); |
| 191 |
} |
| 192 |
final String[] newNamespacePrefix = new String[newSize]; |
| 193 |
final String[] newNamespaceUri = new String[newSize]; |
| 194 |
if (namespacePrefix != null) { |
| 195 |
System.arraycopy(namespacePrefix, 0, newNamespacePrefix, 0, namespaceEnd); |
| 196 |
System.arraycopy(namespaceUri, 0, newNamespaceUri, 0, namespaceEnd); |
| 197 |
} |
| 198 |
namespacePrefix = newNamespacePrefix; |
| 199 |
namespaceUri = newNamespaceUri; |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public void setFeature(String name, boolean state) |
| 204 |
throws IllegalArgumentException, IllegalStateException { |
| 205 |
if (name == null) { |
| 206 |
throw new IllegalArgumentException("feature name can not be null"); |
| 207 |
} |
| 208 |
if (FEATURE_NAMES_INTERNED.equals(name)) { |
| 209 |
namesInterned = state; |
| 210 |
} else if (FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE.equals(name)) { |
| 211 |
attributeUseApostrophe = state; |
| 212 |
} else { |
| 213 |
throw new IllegalStateException("unsupported feature " + name); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public boolean getFeature(String name) throws IllegalArgumentException { |
| 219 |
if (name == null) { |
| 220 |
throw new IllegalArgumentException("feature name can not be null"); |
| 221 |
} |
| 222 |
if (FEATURE_NAMES_INTERNED.equals(name)) { |
| 223 |
return namesInterned; |
| 224 |
} else if (FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE.equals(name)) { |
| 225 |
return attributeUseApostrophe; |
| 226 |
} else { |
| 227 |
return false; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
// precomputed variables to simplify writing indentation |
| 232 |
protected int offsetNewLine; |
| 233 |
protected int indentationJump; |
| 234 |
protected char[] indentationBuf; |
| 235 |
protected int maxIndentLevel; |
| 236 |
protected boolean writeLineSepartor; // should end-of-line be written |
| 237 |
protected boolean writeIndentation; // is indentation used? |
| 238 |
|
| 239 |
/** |
| 240 |
* For maximum efficiency when writing indents the required output is |
| 241 |
* pre-computed This is internal function that recomputes buffer after user |
| 242 |
* requested chnages. |
| 243 |
*/ |
| 244 |
protected void rebuildIndentationBuf() { |
| 245 |
if (!doIndent) |
| 246 |
return; |
| 247 |
final int maxIndent = 65; // hardcoded maximum indentation size in characters |
| 248 |
int bufSize = 0; |
| 249 |
offsetNewLine = 0; |
| 250 |
if (writeLineSepartor) { |
| 251 |
offsetNewLine = lineSeparator.length(); |
| 252 |
bufSize += offsetNewLine; |
| 253 |
} |
| 254 |
maxIndentLevel = 0; |
| 255 |
if (writeIndentation) { |
| 256 |
indentationJump = indentationString.length(); |
| 257 |
maxIndentLevel = maxIndent / indentationJump; |
| 258 |
bufSize += maxIndentLevel * indentationJump; |
| 259 |
} |
| 260 |
if (indentationBuf == null || indentationBuf.length < bufSize) { |
| 261 |
indentationBuf = new char[bufSize + 8]; |
| 262 |
} |
| 263 |
int bufPos = 0; |
| 264 |
if (writeLineSepartor) { |
| 265 |
for (int i = 0; i < lineSeparator.length(); i++) { |
| 266 |
indentationBuf[bufPos++] = lineSeparator.charAt(i); |
| 267 |
} |
| 268 |
} |
| 269 |
if (writeIndentation) { |
| 270 |
for (int i = 0; i < maxIndentLevel; i++) { |
| 271 |
for (int j = 0; j < indentationString.length(); j++) { |
| 272 |
indentationBuf[bufPos++] = indentationString.charAt(j); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
protected void writeIndent() throws IOException { |
| 279 |
final int start = writeLineSepartor ? 0 : offsetNewLine; |
| 280 |
final int level = Math.min(depth, maxIndentLevel); |
| 281 |
|
| 282 |
out.write(indentationBuf, start, ((level - 1) * indentationJump) + offsetNewLine); |
| 283 |
} |
| 284 |
|
| 285 |
@Override |
| 286 |
public void setProperty(String name, Object value) |
| 287 |
throws IllegalArgumentException, IllegalStateException { |
| 288 |
if (name == null) { |
| 289 |
throw new IllegalArgumentException("property name can not be null"); |
| 290 |
} |
| 291 |
switch (name) { |
| 292 |
case PROPERTY_SERIALIZER_INDENTATION: |
| 293 |
indentationString = (String) value; |
| 294 |
break; |
| 295 |
case PROPERTY_SERIALIZER_LINE_SEPARATOR: |
| 296 |
lineSeparator = (String) value; |
| 297 |
break; |
| 298 |
case PROPERTY_LOCATION: |
| 299 |
location = (String) value; |
| 300 |
break; |
| 301 |
default: |
| 302 |
throw new IllegalStateException("unsupported property " + name); |
| 303 |
} |
| 304 |
writeLineSepartor = lineSeparator != null && lineSeparator.length() > 0; |
| 305 |
writeIndentation = indentationString != null |
| 306 |
&& indentationString.length() > 0; |
| 307 |
// optimize - do not write when nothing to write ... |
| 308 |
doIndent = indentationString != null |
| 309 |
&& (writeLineSepartor || writeIndentation); |
| 310 |
// NOTE: when indentationString == null there is no indentation |
| 311 |
// (even though writeLineSeparator may be true ...) |
| 312 |
rebuildIndentationBuf(); |
| 313 |
seenTag = false; // for consistency |
| 314 |
} |
| 315 |
|
| 316 |
@Override |
| 317 |
public Object getProperty(String name) throws IllegalArgumentException { |
| 318 |
if (name == null) { |
| 319 |
throw new IllegalArgumentException("property name can not be null"); |
| 320 |
} |
| 321 |
switch (name) { |
| 322 |
case PROPERTY_SERIALIZER_INDENTATION: |
| 323 |
return indentationString; |
| 324 |
case PROPERTY_SERIALIZER_LINE_SEPARATOR: |
| 325 |
return lineSeparator; |
| 326 |
case PROPERTY_LOCATION: |
| 327 |
return location; |
| 328 |
default: |
| 329 |
return null; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
private String getLocation() { |
| 334 |
return location != null ? " @" + location : ""; |
| 335 |
} |
| 336 |
|
| 337 |
// this is special method that can be accessed directly to retrieve Writer |
| 338 |
// serializer is using |
| 339 |
public Writer getWriter() { |
| 340 |
return out; |
| 341 |
} |
| 342 |
|
| 343 |
@Override |
| 344 |
public void setOutput(Writer writer) { |
| 345 |
reset(); |
| 346 |
out = writer; |
| 347 |
} |
| 348 |
|
| 349 |
@Override |
| 350 |
public void setOutput(OutputStream os, String encoding) throws IOException { |
| 351 |
if (os == null) |
| 352 |
throw new IllegalArgumentException("output stream can not be null"); |
| 353 |
reset(); |
| 354 |
if (encoding != null) { |
| 355 |
out = new OutputStreamWriter(os, encoding); |
| 356 |
} else { |
| 357 |
out = new OutputStreamWriter(os); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
@Override |
| 362 |
public void startDocument(String encoding, Boolean standalone) |
| 363 |
throws IOException { |
| 364 |
if (attributeUseApostrophe) { |
| 365 |
out.write("<?xml version='1.0'"); |
| 366 |
} else { |
| 367 |
out.write("<?xml version=\"1.0\""); |
| 368 |
} |
| 369 |
if (encoding != null) { |
| 370 |
out.write(" encoding="); |
| 371 |
out.write(attributeUseApostrophe ? '\'' : '"'); |
| 372 |
out.write(encoding); |
| 373 |
out.write(attributeUseApostrophe ? '\'' : '"'); |
| 374 |
} |
| 375 |
if (standalone != null) { |
| 376 |
out.write(" standalone="); |
| 377 |
out.write(attributeUseApostrophe ? '\'' : '"'); |
| 378 |
if (standalone) { |
| 379 |
out.write("yes"); |
| 380 |
} else { |
| 381 |
out.write("no"); |
| 382 |
} |
| 383 |
out.write(attributeUseApostrophe ? '\'' : '"'); |
| 384 |
} |
| 385 |
out.write("?>"); |
| 386 |
} |
| 387 |
|
| 388 |
@Override |
| 389 |
public void endDocument() throws IOException { |
| 390 |
// close all unclosed tag; |
| 391 |
while (depth > 0) { |
| 392 |
endTag(elNamespace[depth], elName[depth]); |
| 393 |
} |
| 394 |
finished = pastRoot = startTagIncomplete = true; |
| 395 |
out.flush(); |
| 396 |
} |
| 397 |
|
| 398 |
@Override |
| 399 |
public void setPrefix(String prefix, String namespace) throws IOException { |
| 400 |
if (startTagIncomplete) |
| 401 |
closeStartTag(); |
| 402 |
|
| 403 |
if (prefix == null) { |
| 404 |
prefix = ""; |
| 405 |
} |
| 406 |
if (!namesInterned) { |
| 407 |
prefix = prefix.intern(); // will throw NPE if prefix==null |
| 408 |
} else if (checkNamesInterned) { |
| 409 |
checkInterning(prefix); |
| 410 |
} else if (prefix == null) { |
| 411 |
throw new IllegalArgumentException("prefix must be not null" + getLocation()); |
| 412 |
} |
| 413 |
|
| 414 |
if (!namesInterned) { |
| 415 |
namespace = namespace.intern(); |
| 416 |
} else if (checkNamesInterned) { |
| 417 |
checkInterning(namespace); |
| 418 |
} else if (namespace == null) { |
| 419 |
throw new IllegalArgumentException("namespace must be not null" + getLocation()); |
| 420 |
} |
| 421 |
|
| 422 |
if (namespaceEnd >= namespacePrefix.length) { |
| 423 |
ensureNamespacesCapacity(); |
| 424 |
} |
| 425 |
namespacePrefix[namespaceEnd] = prefix; |
| 426 |
namespaceUri[namespaceEnd] = namespace; |
| 427 |
++namespaceEnd; |
| 428 |
setPrefixCalled = true; |
| 429 |
} |
| 430 |
|
| 431 |
protected String lookupOrDeclarePrefix(String namespace) { |
| 432 |
return getPrefix(namespace, true); |
| 433 |
} |
| 434 |
|
| 435 |
@Override |
| 436 |
public String getPrefix(String namespace, boolean generatePrefix) { |
| 437 |
return getPrefix(namespace, generatePrefix, false); |
| 438 |
} |
| 439 |
|
| 440 |
protected String getPrefix(String namespace, boolean generatePrefix, |
| 441 |
boolean nonEmpty) { |
| 442 |
if (!namesInterned) { |
| 443 |
// when String is interned we can do much faster namespace stack lookups ... |
| 444 |
namespace = namespace.intern(); |
| 445 |
} else if (checkNamesInterned) { |
| 446 |
checkInterning(namespace); |
| 447 |
} |
| 448 |
if (namespace == null) { |
| 449 |
throw new IllegalArgumentException("namespace must be not null" + getLocation()); |
| 450 |
} else if (namespace.length() == 0) { |
| 451 |
throw new IllegalArgumentException("default namespace cannot have prefix" + getLocation()); |
| 452 |
} |
| 453 |
|
| 454 |
// first check if namespace is already in scope |
| 455 |
for (int i = namespaceEnd - 1; i >= 0; --i) { |
| 456 |
if (namespace.equals(namespaceUri[i])) { |
| 457 |
final String prefix = namespacePrefix[i]; |
| 458 |
if (nonEmpty && prefix.length() == 0) { |
| 459 |
continue; |
| 460 |
} |
| 461 |
|
| 462 |
return prefix; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
// so not found it ... |
| 467 |
if (!generatePrefix) { |
| 468 |
return null; |
| 469 |
} |
| 470 |
return generatePrefix(namespace); |
| 471 |
} |
| 472 |
|
| 473 |
private String generatePrefix(String namespace) { |
| 474 |
++autoDeclaredPrefixes; |
| 475 |
// fast lookup uses table that was pre-initialized in static{} .... |
| 476 |
final String prefix = autoDeclaredPrefixes < precomputedPrefixes.length |
| 477 |
? precomputedPrefixes[autoDeclaredPrefixes] |
| 478 |
: ("n" + autoDeclaredPrefixes).intern(); |
| 479 |
|
| 480 |
// declare prefix |
| 481 |
if (namespaceEnd >= namespacePrefix.length) { |
| 482 |
ensureNamespacesCapacity(); |
| 483 |
} |
| 484 |
namespacePrefix[namespaceEnd] = prefix; |
| 485 |
namespaceUri[namespaceEnd] = namespace; |
| 486 |
++namespaceEnd; |
| 487 |
|
| 488 |
return prefix; |
| 489 |
} |
| 490 |
|
| 491 |
@Override |
| 492 |
public int getDepth() { |
| 493 |
return depth; |
| 494 |
} |
| 495 |
|
| 496 |
@Override |
| 497 |
public String getNamespace() { |
| 498 |
return elNamespace[depth]; |
| 499 |
} |
| 500 |
|
| 501 |
@Override |
| 502 |
public String getName() { |
| 503 |
return elName[depth]; |
| 504 |
} |
| 505 |
|
| 506 |
@Override |
| 507 |
public XmlSerializer startTag(String namespace, String name) |
| 508 |
throws IOException { |
| 509 |
if (startTagIncomplete) { |
| 510 |
closeStartTag(); |
| 511 |
} |
| 512 |
seenBracket = seenBracketBracket = false; |
| 513 |
++depth; |
| 514 |
if (doIndent && depth > 0 && seenTag) { |
| 515 |
writeIndent(); |
| 516 |
} |
| 517 |
seenTag = true; |
| 518 |
setPrefixCalled = false; |
| 519 |
startTagIncomplete = true; |
| 520 |
if ((depth + 1) >= elName.length) { |
| 521 |
ensureElementsCapacity(); |
| 522 |
} |
| 523 |
|
| 524 |
if (checkNamesInterned && namesInterned) |
| 525 |
checkInterning(namespace); |
| 526 |
|
| 527 |
elNamespace[depth] = (namesInterned || namespace == null) ? namespace : namespace.intern(); |
| 528 |
if (checkNamesInterned && namesInterned) |
| 529 |
checkInterning(name); |
| 530 |
|
| 531 |
elName[depth] = (namesInterned || name == null) ? name : name.intern(); |
| 532 |
if (out == null) { |
| 533 |
throw new IllegalStateException("setOutput() must called set before serialization can start"); |
| 534 |
} |
| 535 |
out.write('<'); |
| 536 |
if (namespace != null) { |
| 537 |
if (namespace.length() > 0) { |
| 538 |
// in future make this algo a feature on serializer |
| 539 |
String prefix = null; |
| 540 |
if (depth > 0 && (namespaceEnd - elNamespaceCount[depth - 1]) == 1) { |
| 541 |
// if only one prefix was declared un-declare it if the |
| 542 |
// prefix is already declared on parent el with the same URI |
| 543 |
String uri = namespaceUri[namespaceEnd - 1]; |
| 544 |
if (uri == namespace || uri.equals(namespace)) { |
| 545 |
String elPfx = namespacePrefix[namespaceEnd - 1]; |
| 546 |
for (int pos = elNamespaceCount[depth - 1] - 1; pos >= 2; --pos) { |
| 547 |
String pf = namespacePrefix[pos]; |
| 548 |
if (pf == elPfx || pf.equals(elPfx)) { |
| 549 |
String n = namespaceUri[pos]; |
| 550 |
if (n == uri || n.equals(uri)) { |
| 551 |
--namespaceEnd; // un-declare namespace: this is kludge! |
| 552 |
prefix = elPfx; |
| 553 |
} |
| 554 |
break; |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
if (prefix == null) { |
| 560 |
prefix = lookupOrDeclarePrefix(namespace); |
| 561 |
} |
| 562 |
// make sure that default ("") namespace to not print ":" |
| 563 |
if (prefix.length() > 0) { |
| 564 |
elPrefix[depth] = prefix; |
| 565 |
out.write(prefix); |
| 566 |
out.write(':'); |
| 567 |
} else { |
| 568 |
elPrefix[depth] = ""; |
| 569 |
} |
| 570 |
} else { |
| 571 |
// make sure that default namespace can be declared |
| 572 |
for (int i = namespaceEnd - 1; i >= 0; --i) { |
| 573 |
if (namespacePrefix[i] == "") { |
| 574 |
final String uri = namespaceUri[i]; |
| 575 |
if (uri == null) { |
| 576 |
setPrefix("", ""); |
| 577 |
} else if (uri.length() > 0) { |
| 578 |
throw new IllegalStateException("start tag can not be written in empty default namespace " |
| 579 |
+ "as default namespace is currently bound to '" |
| 580 |
+ uri + "'" + getLocation()); |
| 581 |
} |
| 582 |
break; |
| 583 |
} |
| 584 |
} |
| 585 |
elPrefix[depth] = ""; |
| 586 |
} |
| 587 |
} else { |
| 588 |
elPrefix[depth] = ""; |
| 589 |
} |
| 590 |
out.write(name); |
| 591 |
return this; |
| 592 |
} |
| 593 |
|
| 594 |
@Override |
| 595 |
public XmlSerializer attribute(String namespace, String name, String value) |
| 596 |
throws IOException { |
| 597 |
if (!startTagIncomplete) { |
| 598 |
throw new IllegalArgumentException("startTag() must be called before attribute()" + getLocation()); |
| 599 |
} |
| 600 |
out.write(' '); |
| 601 |
if (namespace != null && namespace.length() > 0) { |
| 602 |
if (!namesInterned) { |
| 603 |
namespace = namespace.intern(); |
| 604 |
} else if (checkNamesInterned) { |
| 605 |
checkInterning(namespace); |
| 606 |
} |
| 607 |
String prefix = getPrefix(namespace, false, true); |
| 608 |
if (prefix == null) { |
| 609 |
// needs to declare prefix to hold default namespace |
| 610 |
// NOTE: attributes such as a='b' are in NO namespace |
| 611 |
prefix = generatePrefix(namespace); |
| 612 |
} |
| 613 |
out.write(prefix); |
| 614 |
out.write(':'); |
| 615 |
} |
| 616 |
out.write(name); |
| 617 |
out.write('='); |
| 618 |
out.write(attributeUseApostrophe ? '\'' : '"'); |
| 619 |
writeAttributeValue(value, out); |
| 620 |
out.write(attributeUseApostrophe ? '\'' : '"'); |
| 621 |
return this; |
| 622 |
} |
| 623 |
|
| 624 |
protected void closeStartTag() throws IOException { |
| 625 |
if (finished) { |
| 626 |
throw new IllegalArgumentException("trying to write past already finished output" |
| 627 |
+ getLocation()); |
| 628 |
} |
| 629 |
if (seenBracket) { |
| 630 |
seenBracket = seenBracketBracket = false; |
| 631 |
} |
| 632 |
if (startTagIncomplete || setPrefixCalled) { |
| 633 |
if (setPrefixCalled) { |
| 634 |
throw new IllegalArgumentException("startTag() must be called immediately after setPrefix()" |
| 635 |
+ getLocation()); |
| 636 |
} |
| 637 |
if (!startTagIncomplete) { |
| 638 |
throw new IllegalArgumentException("trying to close start tag that is not opened" |
| 639 |
+ getLocation()); |
| 640 |
} |
| 641 |
|
| 642 |
// write all namespace declarations! |
| 643 |
writeNamespaceDeclarations(); |
| 644 |
out.write('>'); |
| 645 |
elNamespaceCount[depth] = namespaceEnd; |
| 646 |
startTagIncomplete = false; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
protected void writeNamespaceDeclarations() throws IOException { |
| 651 |
for (int i = elNamespaceCount[depth - 1]; i < namespaceEnd; i++) { |
| 652 |
if (doIndent && namespaceUri[i].length() > 40) { |
| 653 |
writeIndent(); |
| 654 |
out.write(" "); |
| 655 |
} |
| 656 |
if (namespacePrefix[i] != "") { |
| 657 |
out.write(" xmlns:"); |
| 658 |
out.write(namespacePrefix[i]); |
| 659 |
out.write('='); |
| 660 |
} else { |
| 661 |
out.write(" xmlns="); |
| 662 |
} |
| 663 |
out.write(attributeUseApostrophe ? '\'' : '"'); |
| 664 |
|
| 665 |
// NOTE: escaping of namespace value the same way as attributes!!!! |
| 666 |
writeAttributeValue(namespaceUri[i], out); |
| 667 |
out.write(attributeUseApostrophe ? '\'' : '"'); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
@Override |
| 672 |
public XmlSerializer endTag(String namespace, String name) |
| 673 |
throws IOException { |
| 674 |
seenBracket = seenBracketBracket = false; |
| 675 |
if (namespace != null) { |
| 676 |
if (!namesInterned) { |
| 677 |
namespace = namespace.intern(); |
| 678 |
} else if (checkNamesInterned) { |
| 679 |
checkInterning(namespace); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
if (namespace != elNamespace[depth]) { |
| 684 |
throw new IllegalArgumentException("expected namespace " + printable(elNamespace[depth]) + " and not " |
| 685 |
+ printable(namespace) + getLocation()); |
| 686 |
} |
| 687 |
if (name == null) { |
| 688 |
throw new IllegalArgumentException("end tag name can not be null" + getLocation()); |
| 689 |
} |
| 690 |
if (checkNamesInterned && namesInterned) { |
| 691 |
checkInterning(name); |
| 692 |
} |
| 693 |
String startTagName = elName[depth]; |
| 694 |
if ((!namesInterned && !name.equals(startTagName)) || (namesInterned && name != startTagName)) { |
| 695 |
throw new IllegalArgumentException("expected element name " |
| 696 |
+ printable(elName[depth]) + " and not " + printable(name) + getLocation()); |
| 697 |
} |
| 698 |
if (startTagIncomplete) { |
| 699 |
writeNamespaceDeclarations(); |
| 700 |
out.write(" />"); // space is added to make it easier to work in XHTML!!! |
| 701 |
} else { |
| 702 |
if (doIndent && seenTag) { |
| 703 |
writeIndent(); |
| 704 |
} |
| 705 |
out.write("</"); |
| 706 |
String startTagPrefix = elPrefix[depth]; |
| 707 |
if (startTagPrefix.length() > 0) { |
| 708 |
out.write(startTagPrefix); |
| 709 |
out.write(':'); |
| 710 |
} |
| 711 |
out.write(name); |
| 712 |
out.write('>'); |
| 713 |
} |
| 714 |
--depth; |
| 715 |
namespaceEnd = elNamespaceCount[depth]; |
| 716 |
startTagIncomplete = false; |
| 717 |
seenTag = true; |
| 718 |
return this; |
| 719 |
} |
| 720 |
|
| 721 |
@Override |
| 722 |
public XmlSerializer text(String text) throws IOException { |
| 723 |
if (startTagIncomplete || setPrefixCalled) |
| 724 |
closeStartTag(); |
| 725 |
if (doIndent && seenTag) |
| 726 |
seenTag = false; |
| 727 |
writeElementContent(text, out); |
| 728 |
return this; |
| 729 |
} |
| 730 |
|
| 731 |
@Override |
| 732 |
public XmlSerializer text(char[] buf, int start, int len) |
| 733 |
throws IOException { |
| 734 |
if (startTagIncomplete || setPrefixCalled) |
| 735 |
closeStartTag(); |
| 736 |
if (doIndent && seenTag) |
| 737 |
seenTag = false; |
| 738 |
writeElementContent(buf, start, len, out); |
| 739 |
return this; |
| 740 |
} |
| 741 |
|
| 742 |
@Override |
| 743 |
public void cdsect(String text) throws IOException { |
| 744 |
if (startTagIncomplete || setPrefixCalled || seenBracket) |
| 745 |
closeStartTag(); |
| 746 |
if (doIndent && seenTag) |
| 747 |
seenTag = false; |
| 748 |
out.write("<![CDATA["); |
| 749 |
out.write(text); // escape? |
| 750 |
out.write("]]>"); |
| 751 |
} |
| 752 |
|
| 753 |
@Override |
| 754 |
public void entityRef(String text) throws IOException { |
| 755 |
if (startTagIncomplete || setPrefixCalled || seenBracket) |
| 756 |
closeStartTag(); |
| 757 |
if (doIndent && seenTag) |
| 758 |
seenTag = false; |
| 759 |
out.write('&'); |
| 760 |
out.write(text); // escape? |
| 761 |
out.write(';'); |
| 762 |
} |
| 763 |
|
| 764 |
@Override |
| 765 |
public void processingInstruction(String text) throws IOException { |
| 766 |
if (startTagIncomplete || setPrefixCalled || seenBracket) |
| 767 |
closeStartTag(); |
| 768 |
if (doIndent && seenTag) |
| 769 |
seenTag = false; |
| 770 |
out.write("<?"); |
| 771 |
out.write(text); // escape? |
| 772 |
out.write("?>"); |
| 773 |
} |
| 774 |
|
| 775 |
@Override |
| 776 |
public void comment(String text) throws IOException { |
| 777 |
if (startTagIncomplete || setPrefixCalled || seenBracket) |
| 778 |
closeStartTag(); |
| 779 |
if (doIndent && seenTag) |
| 780 |
seenTag = false; |
| 781 |
out.write("<!--"); |
| 782 |
out.write(text); // escape? |
| 783 |
out.write("-->"); |
| 784 |
} |
| 785 |
|
| 786 |
@Override |
| 787 |
public void docdecl(String text) throws IOException { |
| 788 |
if (startTagIncomplete || setPrefixCalled || seenBracket) |
| 789 |
closeStartTag(); |
| 790 |
if (doIndent && seenTag) |
| 791 |
seenTag = false; |
| 792 |
out.write("<!DOCTYPE"); |
| 793 |
out.write(text); // escape? |
| 794 |
out.write(">"); |
| 795 |
} |
| 796 |
|
| 797 |
@Override |
| 798 |
public void ignorableWhitespace(String text) throws IOException { |
| 799 |
if (startTagIncomplete || setPrefixCalled || seenBracket) |
| 800 |
closeStartTag(); |
| 801 |
if (doIndent && seenTag) |
| 802 |
seenTag = false; |
| 803 |
if (text.length() == 0) { |
| 804 |
throw new IllegalArgumentException("empty string is not allowed for ignorable whitespace" + getLocation()); |
| 805 |
} |
| 806 |
out.write(text); // no escape? |
| 807 |
} |
| 808 |
|
| 809 |
@Override |
| 810 |
public void flush() throws IOException { |
| 811 |
if (!finished && startTagIncomplete) |
| 812 |
closeStartTag(); |
| 813 |
out.flush(); |
| 814 |
} |
| 815 |
|
| 816 |
// --- utility methods |
| 817 |
|
| 818 |
protected void writeAttributeValue(String value, Writer out) |
| 819 |
throws IOException { |
| 820 |
// .[apostrophe and <, & escaped], |
| 821 |
final char quot = attributeUseApostrophe ? '\'' : '"'; |
| 822 |
final String quotEntity = attributeUseApostrophe ? "'" : """; |
| 823 |
|
| 824 |
int pos = 0; |
| 825 |
for (int i = 0; i < value.length(); i++) { |
| 826 |
char ch = value.charAt(i); |
| 827 |
if (ch == '&') { |
| 828 |
if (i > pos) |
| 829 |
out.write(value.substring(pos, i)); |
| 830 |
out.write("&"); |
| 831 |
pos = i + 1; |
| 832 |
} |
| 833 |
if (ch == '<') { |
| 834 |
if (i > pos) |
| 835 |
out.write(value.substring(pos, i)); |
| 836 |
out.write("<"); |
| 837 |
pos = i + 1; |
| 838 |
} else if (ch == quot) { |
| 839 |
if (i > pos) |
| 840 |
out.write(value.substring(pos, i)); |
| 841 |
out.write(quotEntity); |
| 842 |
pos = i + 1; |
| 843 |
} else if (ch < 32) { |
| 844 |
// in XML 1.0 only legal character are #x9 | #xA | #xD |
| 845 |
// and they must be escaped otherwise in attribute value they |
| 846 |
// are normalized to spaces |
| 847 |
if (ch == 13 || ch == 10 || ch == 9) { |
| 848 |
if (i > pos) |
| 849 |
out.write(value.substring(pos, i)); |
| 850 |
out.write("&#"); |
| 851 |
out.write(Integer.toString(ch)); |
| 852 |
out.write(';'); |
| 853 |
pos = i + 1; |
| 854 |
} else { |
| 855 |
if (TRACE_ESCAPING) |
| 856 |
System.err.println(getClass().getName() + " DEBUG ATTR value.len=" + value.length() |
| 857 |
+ " " + printable(value)); |
| 858 |
|
| 859 |
throw new IllegalStateException( |
| 860 |
"character " + printable(ch) + " (" + Integer.toString(ch) + ") is not allowed in output" |
| 861 |
+ getLocation() + " (attr value=" |
| 862 |
+ printable(value) + ")"); |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
if (pos > 0) { |
| 867 |
out.write(value.substring(pos)); |
| 868 |
} else { |
| 869 |
out.write(value); // this is shortcut to the most common case |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
protected void writeElementContent(String text, Writer out) |
| 874 |
throws IOException { |
| 875 |
|
| 876 |
// For some reason, some non-empty, empty characters are surviving this far and getting filtered out |
| 877 |
// So we are left with null, which causes an NPE |
| 878 |
if (text == null) { |
| 879 |
return; |
| 880 |
} |
| 881 |
|
| 882 |
// escape '<', '&', ']]>', <32 if necessary |
| 883 |
int pos = 0; |
| 884 |
for (int i = 0; i < text.length(); i++) { |
| 885 |
// TODO: check if doing char[] text.getChars() would be faster than |
| 886 |
// getCharAt(i) ... |
| 887 |
char ch = text.charAt(i); |
| 888 |
if (ch == ']') { |
| 889 |
if (seenBracket) { |
| 890 |
seenBracketBracket = true; |
| 891 |
} else { |
| 892 |
seenBracket = true; |
| 893 |
} |
| 894 |
} else { |
| 895 |
if (ch == '&') { |
| 896 |
if (!(i < text.length() - 3 && text.charAt(i+1) == 'l' |
| 897 |
&& text.charAt(i+2) == 't' && text.charAt(i+3) == ';')) { |
| 898 |
if (i > pos) |
| 899 |
out.write(text.substring(pos, i)); |
| 900 |
out.write("&"); |
| 901 |
pos = i + 1; |
| 902 |
} |
| 903 |
} else if (ch == '<') { |
| 904 |
if (i > pos) |
| 905 |
out.write(text.substring(pos, i)); |
| 906 |
out.write("<"); |
| 907 |
pos = i + 1; |
| 908 |
} else if (seenBracketBracket && ch == '>') { |
| 909 |
if (i > pos) |
| 910 |
out.write(text.substring(pos, i)); |
| 911 |
out.write(">"); |
| 912 |
pos = i + 1; |
| 913 |
} else if (ch < 32) { |
| 914 |
// in XML 1.0 only legal character are #x9 | #xA | #xD |
| 915 |
if (ch == 9 || ch == 10 || ch == 13) { |
| 916 |
// pass through |
| 917 |
} else { |
| 918 |
if (TRACE_ESCAPING) |
| 919 |
System.err.println(getClass().getName() + " DEBUG TEXT value.len=" + text.length() |
| 920 |
+ " " + printable(text)); |
| 921 |
throw new IllegalStateException("character " + Integer.toString(ch) |
| 922 |
+ " is not allowed in output" + getLocation() |
| 923 |
+ " (text value=" + printable(text) + ")"); |
| 924 |
} |
| 925 |
} |
| 926 |
if (seenBracket) { |
| 927 |
seenBracketBracket = seenBracket = false; |
| 928 |
} |
| 929 |
|
| 930 |
} |
| 931 |
} |
| 932 |
if (pos > 0) { |
| 933 |
out.write(text.substring(pos)); |
| 934 |
} else { |
| 935 |
out.write(text); // this is shortcut to the most common case |
| 936 |
} |
| 937 |
|
| 938 |
} |
| 939 |
|
| 940 |
protected void writeElementContent(char[] buf, int off, int len, Writer out) |
| 941 |
throws IOException { |
| 942 |
// escape '<', '&', ']]>' |
| 943 |
final int end = off + len; |
| 944 |
int pos = off; |
| 945 |
for (int i = off; i < end; i++) { |
| 946 |
final char ch = buf[i]; |
| 947 |
if (ch == ']') { |
| 948 |
if (seenBracket) { |
| 949 |
seenBracketBracket = true; |
| 950 |
} else { |
| 951 |
seenBracket = true; |
| 952 |
} |
| 953 |
} else { |
| 954 |
if (ch == '&') { |
| 955 |
if (i > pos) { |
| 956 |
out.write(buf, pos, i - pos); |
| 957 |
} |
| 958 |
out.write("&"); |
| 959 |
pos = i + 1; |
| 960 |
} else if (ch == '<') { |
| 961 |
if (i > pos) { |
| 962 |
out.write(buf, pos, i - pos); |
| 963 |
} |
| 964 |
out.write("<"); |
| 965 |
pos = i + 1; |
| 966 |
|
| 967 |
} else if (seenBracketBracket && ch == '>') { |
| 968 |
if (i > pos) { |
| 969 |
out.write(buf, pos, i - pos); |
| 970 |
} |
| 971 |
out.write(">"); |
| 972 |
pos = i + 1; |
| 973 |
} else if (ch < 32) { |
| 974 |
// in XML 1.0 only legal character are #x9 | #xA | #xD |
| 975 |
if (ch == 9 || ch == 10 || ch == 13) { |
| 976 |
// pass through |
| 977 |
} else { |
| 978 |
if (TRACE_ESCAPING) |
| 979 |
System.err.println(getClass().getName() + " DEBUG TEXT value.len=" + len + " " |
| 980 |
+ printable(new String(buf, off, len))); |
| 981 |
throw new IllegalStateException("character " |
| 982 |
+ printable(ch) + " (" + Integer.toString(ch) |
| 983 |
+ ") is not allowed in output" + getLocation()); |
| 984 |
} |
| 985 |
} |
| 986 |
if (seenBracket) { |
| 987 |
seenBracketBracket = seenBracket = false; |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
if (end > pos) { |
| 992 |
out.write(buf, pos, end - pos); |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
protected static String printable(String s) { |
| 997 |
if (s == null) { |
| 998 |
return "null"; |
| 999 |
} |
| 1000 |
StringBuffer retval = new StringBuffer(s.length() + 16); |
| 1001 |
retval.append("'"); |
| 1002 |
for (int i = 0; i < s.length(); i++) { |
| 1003 |
addPrintable(retval, s.charAt(i)); |
| 1004 |
} |
| 1005 |
retval.append("'"); |
| 1006 |
return retval.toString(); |
| 1007 |
} |
| 1008 |
|
| 1009 |
protected static String printable(char ch) { |
| 1010 |
StringBuffer retval = new StringBuffer(); |
| 1011 |
addPrintable(retval, ch); |
| 1012 |
return retval.toString(); |
| 1013 |
} |
| 1014 |
|
| 1015 |
private static void addPrintable(StringBuffer retval, char ch) { |
| 1016 |
switch (ch) { |
| 1017 |
case '\b': |
| 1018 |
retval.append("\\b"); |
| 1019 |
break; |
| 1020 |
case '\t': |
| 1021 |
retval.append("\\t"); |
| 1022 |
break; |
| 1023 |
case '\n': |
| 1024 |
retval.append("\\n"); |
| 1025 |
break; |
| 1026 |
case '\f': |
| 1027 |
retval.append("\\f"); |
| 1028 |
break; |
| 1029 |
case '\r': |
| 1030 |
retval.append("\\r"); |
| 1031 |
break; |
| 1032 |
case '\"': |
| 1033 |
retval.append("\\\""); |
| 1034 |
break; |
| 1035 |
case '\'': |
| 1036 |
retval.append("\\'"); |
| 1037 |
break; |
| 1038 |
case '\\': |
| 1039 |
retval.append("\\\\"); |
| 1040 |
break; |
| 1041 |
default: |
| 1042 |
if (ch < 0x20 || ch > 0x7e) { |
| 1043 |
final String ss = "0000" + Integer.toString(ch, 16); |
| 1044 |
retval.append("\\u").append(ss.substring(ss.length() - 4)); |
| 1045 |
} else { |
| 1046 |
retval.append(ch); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |