001package org.hl7.fhir.utilities.xml; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import java.io.IOException; 035import java.io.OutputStream; 036import java.io.OutputStreamWriter; 037import java.io.UnsupportedEncodingException; 038 039import org.hl7.fhir.utilities.ElementDecoration; 040 041/** 042 * XML Writer class. 043 */ 044public class XMLWriter extends OutputStreamWriter implements IXMLWriter { 045 046 private boolean xmlHeader = true; 047 private String charset; 048 private boolean prettyBase; 049 private boolean prettyHeader; 050 private boolean pendingClose; 051 private boolean pendingOpen; 052 private String pendingComment; 053 private int lineType = LINE_UNIX; 054 private OutputStream stream; 055 private boolean started = false; 056 private String[] specialAttributeNames = new String[] {"id", "name" }; 057 private boolean sortAttributes; 058 private int attributeLineWrap; 059 private boolean xml1_1; 060 061 public final static int LINE_UNIX = 0; 062 public final static int LINE_WINDOWS = 1; 063 064 public XMLWriter(OutputStream stream, String charset, boolean xml1_1) throws UnsupportedEncodingException { 065 super(stream, charset); 066 this.stream = stream; 067 this.charset = charset; 068 this.xml1_1 = xml1_1; 069 } 070 public XMLWriter(OutputStream stream, String charset) throws UnsupportedEncodingException { 071 super(stream, charset); 072 this.stream = stream; 073 this.charset = charset; 074 this.xml1_1 = false; 075 } 076 077 protected boolean condition(boolean bTest, String message) throws IOException { 078 if (!bTest) 079 throw new IOException(message); 080 return bTest; 081 } 082 083 // -- writing context ------------------------------------------------ 084 085 086 087 /** 088 * Returns the encoding. 089 * 090 * @param charset 091 * @return encoding 092 * @throws IOException 093 */ 094 public static String getXMLCharsetName(String charset) throws IOException { 095 if (charset == null || charset.equals("")) 096 return "UTF-8"; 097 else if (charset.equals("US-ASCII")) 098 return "UTF-8"; 099 else if (XMLUtil.charSetImpliesAscii(charset)) 100 return "ISO-8859-1"; 101 else if (charset.equals("UTF-8")) 102 return "UTF-8"; 103 else if (charset.equals("UTF-16") || charset.equals("UTF-16BE") || charset.equals("UTF-16LE")) 104 return "UTF-16"; 105 else 106 throw new IOException("Unknown charset encoding "+charset); 107 } 108 109 /* (non-Javadoc) 110 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#start() 111 */ 112 @Override 113 public void start() throws IOException { 114 condition(!started, "attempt to start after starting"); 115 levels.clear(); 116 attributes = null; 117 try { 118 if (xmlHeader) { 119 write("<?xml version=\""+(xml1_1 ? "1.1" : "1.0")+"\" encoding=\""+getXMLCharsetName(charset)+"\"?>"); 120 if (prettyBase || prettyHeader) 121 write(lineType == LINE_UNIX ? "\n" : "\r\n"); 122 } 123 } catch (UnsupportedEncodingException e) { 124 // TODO Auto-generated catch block 125 throw new IOException(e.getMessage()); 126 } 127 started = true; 128 } 129 130 private void checkStarted () throws IOException { 131 condition(started, "not started"); 132 } 133 134 private void checkInElement() throws IOException { 135 condition(levels.size() > 0, "not in an element"); 136 } 137 138 // -- attributes ---------------------------------------------------- 139 140 private String[][] attributes; 141 142 private void addAttribute(String name, String value) throws IOException { 143 addAttribute(name, value, false); 144 } 145 146 private void addAttribute(String name, String value, boolean noLines) throws IOException { 147 if (!XMLUtil.isNMToken(name)) 148 throw new IOException("XML name "+name+" is not valid for value '"+value+"'"); 149 150 newLevelIfRequired(); 151 value = XMLUtil.escapeXML(value, charset, noLines); 152 153 if (attributes == null) 154 attributes = new String[][] {{name, value}}; 155 else { 156 String[][] newattr = new String[attributes.length+1][]; 157 for (int i = 0; i < attributes.length; i++) { 158 condition(!attributes[i][0].equals(name), "attempt to define attribute with name "+name+" more than once for value '"+value+"'"); 159 newattr[i] = attributes[i]; 160 } 161 attributes = newattr; 162 attributes[attributes.length-1] = new String[] {name, value}; 163 } 164 } 165 166 protected String getAttribute(String name) { 167 if (attributes != null) { 168 for (int i = 0; i < attributes.length; i++) { 169 if (attributes[i][0].equals(name)) { 170 return attributes[i][1]; 171 } 172 } 173 } 174 return null; 175 } 176 177 protected void setAttribute(String name, String value) throws IOException { 178 newLevelIfRequired(); 179 if (attributes == null) 180 addAttribute(name, value, false); 181 else { 182 for (int i = 0; i < attributes.length; i++) { 183 if (attributes[i][0].equals(name)) { 184 attributes[i][1] = XMLUtil.escapeXML(value, charset, false); 185 return; 186 } 187 } 188 addAttribute(name, value); 189 } 190 } 191 192 protected void commitAttributes() throws IOException { 193 194 } 195 196 197 private boolean nameIsSpecial(String name) { 198 for (int i = 0; i < specialAttributeNames.length; i++) { 199 String n = specialAttributeNames[i]; 200 if (n.equalsIgnoreCase(name)) 201 return true; 202 } 203 return false; 204 } 205 206 private void writeAttributes(int col) throws IOException { 207 commitAttributes(); 208 if (attributes != null && sortAttributes) 209 sortAttributes(); 210 int c = col; 211 c = writeAttributeSet(true, c, col); 212 writeAttributeSet(false, c, col); 213 attributes = null; 214 } 215 216 217 private void sortAttributes() { 218 // bubble sort - look, it's easy 219 for (int i = 0; i < attributes.length - 1; i++) { 220 for (int j = 0; j < attributes.length - 1; j++) { 221 if (String.CASE_INSENSITIVE_ORDER.compare(attributes[j][0], attributes[j+1][0]) < 0) { 222 String[] t = attributes[j]; 223 attributes[j] = attributes[j+1]; 224 attributes[j+1] = t; 225 } 226 } 227 } 228 229 } 230 231 232 private int writeAttributeSet(boolean special, int col, int wrap) throws IOException { 233 // first pass: name, id 234 if (attributes != null) { 235 for (int i=0; i < attributes.length; i++) { 236 String[] element = attributes[i]; 237 if (nameIsSpecial(element[0]) == special) { 238 col = col + element[0].length()+element[1].length() + 4; 239 if (isPretty() && attributeLineWrap > 0 && col > attributeLineWrap && col > wrap) { 240 write(lineType == LINE_UNIX ? "\n" : "\r\n"); 241 for (int j = 0; j < wrap; j++) 242 write(" "); 243 col = wrap; 244 } 245 write(' '); 246 write(element[0]); 247 write("=\""); 248 if (element[1] != null) 249 write(xmlEscape(element[1])); 250 write("\""); 251 } 252 } 253 } 254 return col; 255 } 256 257 protected String xmlEscape(String s) { 258 StringBuilder b = new StringBuilder(); 259 for (char c : s.toCharArray()) { 260 if (c < ' ' || c > '~') { 261 b.append("&#x"); 262 b.append(Integer.toHexString(c).toUpperCase()); 263 b.append(";"); 264 } else 265 b.append(c); 266 } 267 return b.toString(); 268 } 269 /* (non-Javadoc) 270 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String, java.lang.String, boolean) 271 */ 272 @Override 273 public void attribute(String namespace, String name, String value, boolean onlyIfNotEmpty) throws IOException { 274 if (!onlyIfNotEmpty || value != null && !value.equals("")) 275 attribute(namespace, name, value); 276 } 277 278 /* (non-Javadoc) 279 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String, java.lang.String) 280 */ 281 @Override 282 public void attribute(String namespace, String name, String value) throws IOException { 283 284 checkStarted(); 285 if (namespace == null || namespace.equals("")) 286 addAttribute(name, value); 287 else 288 addAttribute(getNSAbbreviation(namespace)+name, value); 289 } 290 291 /* (non-Javadoc) 292 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String, boolean) 293 */ 294 @Override 295 public void attribute(String name, String value, boolean onlyIfNotEmpty) throws IOException { 296 if (!onlyIfNotEmpty || value != null && !value.equals("")) 297 attribute(name, value); 298 } 299 300 /* (non-Javadoc) 301 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attribute(java.lang.String, java.lang.String) 302 */ 303 @Override 304 public void attribute(String name, String value) throws IOException { 305 checkStarted(); 306 addAttribute(name, value); 307 } 308 309 /* (non-Javadoc) 310 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#attributeNoLines(java.lang.String, java.lang.String) 311 */ 312 @Override 313 public void attributeNoLines(String name, String value) throws IOException { 314 checkStarted(); 315 addAttribute(name, value, true); 316 } 317 318 // -- levels ------------------------------------------------- 319 320 private XMLWriterStateStack levels = new XMLWriterStateStack(); 321 322 private void newLevelIfRequired() throws IOException { 323 if (!pendingOpen) { 324 if (!levels.empty()) 325 levels.current().seeChild(); 326 XMLWriterState level = new XMLWriterState(); 327 level.setPretty(isPretty()); 328 levels.push(level); 329 pendingOpen = true; 330 } 331 } 332 333 // -- namespaces --------------------------------------------- 334 335 336 private void defineNamespace(String namespace, String abbrev) throws IOException { 337 checkStarted(); 338 if (namespace != null && !namespace.equals("")) { 339 if ("".equals(abbrev)) 340 abbrev = null; 341 342 newLevelIfRequired(); 343 344 levels.current().addNamespaceDefn(namespace, abbrev); 345 if (abbrev == null) 346 addAttribute("xmlns", namespace); 347 else 348 addAttribute("xmlns:"+abbrev, namespace); 349 } 350 } 351 352 /* (non-Javadoc) 353 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#findByNamespace(java.lang.String) 354 */ 355 public XMLNamespace findByNamespace(String namespace) { 356 for (int i = levels.size() - 1; i >= 0; i--) { 357 XMLNamespace ns = levels.item(i).getDefnByNamespace(namespace); 358 if (ns != null) 359 return ns; 360 } 361 return null; 362 } 363 364 /* (non-Javadoc) 365 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#namespaceDefined(java.lang.String) 366 */ 367 @Override 368 public boolean namespaceDefined(String namespace) { 369 return namespace == null || namespace.equals("") || findByNamespace(namespace) != null; 370 } 371 372 /* (non-Javadoc) 373 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#findByAbbreviation(java.lang.String) 374 */ 375 public XMLNamespace findByAbbreviation(String abbreviation) { 376 for (int i = levels.size() - 1; i >= 0; i--) { 377 XMLNamespace ns = levels.item(i).getDefnByAbbreviation(abbreviation); 378 if (ns != null) 379 return ns; 380 } 381 return null; 382 } 383 384 /* (non-Javadoc) 385 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#abbreviationDefined(java.lang.String) 386 */ 387 @Override 388 public boolean abbreviationDefined(String abbreviation) { 389 return findByAbbreviation(abbreviation) != null; 390 } 391 392 protected XMLNamespace findDefaultNamespace() { 393 for (int i = levels.size() - 1; i >= 0; i--) { 394 XMLNamespace ns = levels.item(i).getDefaultNamespace(); 395 if (ns != null) 396 return ns; 397 } 398 return null; 399 } 400 401 /* (non-Javadoc) 402 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#getDefaultNamespace() 403 */ 404 @Override 405 public String getDefaultNamespace() { 406 XMLNamespace ns = findDefaultNamespace(); 407 if (ns == null) 408 return null; 409 else 410 return ns.getNamespace(); 411 } 412 413 /* (non-Javadoc) 414 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#namespace(java.lang.String) 415 */ 416 @Override 417 public void namespace(String namespace) throws IOException { 418 if (!namespaceDefined(namespace)) { 419 int index = 0; 420 while (abbreviationDefined("ns"+Integer.toString(index))) 421 index++; 422 defineNamespace(namespace, "ns"+Integer.toString(index)); 423 } 424 } 425 426 /* (non-Javadoc) 427 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#defaultNamespace(java.lang.String) 428 * 429 * Replace defaultNamespace() 430 */ 431 @Override 432 public void setDefaultNamespace(String namespace) throws IOException { 433 if ((namespace == null && getDefaultNamespace() != null) || 434 (namespace != null && !namespace.equals(getDefaultNamespace()))) 435 defineNamespace(namespace, ""); 436 } 437 438 /* (non-Javadoc) 439 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#namespace(java.lang.String, java.lang.String) 440 */ 441 @Override 442 public void namespace(String namespace, String abbreviation) throws IOException { 443 XMLNamespace ns = findByAbbreviation(abbreviation); 444 if (ns == null || !ns.getNamespace().equals(namespace)) 445 defineNamespace(namespace, abbreviation); 446 } 447 448 449 private String getNSAbbreviation(String namespace) throws IOException { 450 if ("http://www.w3.org/XML/1998/namespace".equals(namespace)) 451 return "xml:"; 452 453 if (namespace == null || "".equals(namespace)) 454 return ""; 455 456 XMLNamespace ns = findByNamespace(namespace); 457 if (ns == null) 458 throw new IOException("Namespace "+namespace+" is not defined"); 459 else if (ns.getAbbreviation() == null) 460 return ""; 461 else 462 return ns.getAbbreviation()+":"; 463 } 464 465 // -- public API ----------------------------------------------------------- 466 467 /* (non-Javadoc) 468 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#comment(java.lang.String, boolean) 469 */ 470 @Override 471 public void comment(String comment, boolean doPretty) throws IOException { 472 checkStarted(); 473 if (pendingClose) { 474 write('>'); 475 writePendingComment(); 476 pendingClose = false; 477 } 478 if (doPretty) { 479 writePretty(); 480 } 481 if (levels.inComment()) 482 write("<!-- "+comment+" -- >"); 483 else 484 write("<!-- "+comment+" -->"); 485 if (doPretty && !isPretty()) 486 writePretty(); 487 } 488 489 490 private void writePendingComment() throws IOException { 491 if (pendingComment != null) { 492 if (isPretty()) 493 write(" "); 494 if (levels.inComment()) 495 write("<!-- "+pendingComment+" -- >"); 496 else 497 write("<!-- "+pendingComment+" -->"); 498 } 499 } 500 501 /* (non-Javadoc) 502 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#open(java.lang.String, java.lang.String) 503 */ 504 @Override 505 public void enter(String namespace, String name) throws IOException { 506 enter(namespace, name, null); 507 } 508 509 510 /* (non-Javadoc) 511 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#open(java.lang.String, java.lang.String, java.lang.String) 512 */ 513 @Override 514 public void enter(String namespace, String name, String comment) throws IOException { 515 if (name == null) 516 throw new Error("name == null"); 517 if (!XMLUtil.isNMToken(name)) 518 throw new IOException("XML name "+name+" is not valid"); 519 checkStarted(); 520 if (pendingClose) { 521 write('>'); 522 writePendingComment(); 523 pendingClose = false; 524 } 525 526 if (name == null) { 527 throw new IOException("name is null"); 528 } 529 newLevelIfRequired(); 530 levels.current().setName(name); 531 levels.current().setNamespace(namespace); 532 int col = writePretty(); 533 write('<'); 534 if (namespace == null) { 535 write(name); 536 col = col + name.length()+1; 537 } else { 538 String n = getNSAbbreviation(namespace)+name; 539 write(n); 540 col = col + n.length()+1; 541 } 542 writeAttributes(col); 543 pendingOpen = false; 544 pendingClose = true; 545 pendingComment = comment; 546 } 547 548 549 /* (non-Javadoc) 550 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#close(java.lang.String) 551 */ 552 @Override 553 public void exit(String name) throws IOException { 554 checkStarted(); 555 if (levels.empty()) 556 throw new IOException("Unable to close null|"+name+", nothing to close"); 557 if (levels.current().getNamespace() != null || !levels.current().getName().equals(name)) 558 throw new IOException("Unable to close null|"+name+", found "+levels.current().getNamespace()+"|"+levels.current().getName()); 559 exit(); 560 } 561 562 /* (non-Javadoc) 563 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#close(java.lang.String, java.lang.String) 564 */ 565 @Override 566 public void exit(String namespace, String name) throws IOException { 567 checkStarted(); 568 if (levels.empty()) 569 throw new IOException("Unable to close "+namespace+"|"+name+", nothing to close"); 570 if (levels == null) 571 throw new Error("levels = null"); 572 if (levels.current() == null) 573 throw new Error("levels.current() = null"); 574 if (levels.current().getName() == null) 575 throw new Error("levels.current().getName() = null"); 576 if (levels.current().getNamespace() == null) 577 throw new Error("levels.current().getNamespace() = null"); 578 if (name == null) 579 throw new Error("name = null"); 580 if (namespace == null) 581 throw new Error("namespace = null"); 582 if (!levels.current().getNamespace().equals(namespace) || !levels.current().getName().equals(name)) 583 throw new IOException("Unable to close "+namespace+"|"+name+", found "+levels.current().getNamespace()+"|"+levels.current().getName()); 584 exit(); 585 } 586 587 /* (non-Javadoc) 588 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#closeToLevel(int) 589 */ 590 @Override 591 public void exitToLevel(int count) throws IOException { 592 checkStarted(); 593 while (levels.size() > count) 594 exit(); 595 } 596 597 598 /* (non-Javadoc) 599 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#close() 600 */ 601 @Override 602 public void close() throws IOException { 603 checkStarted(); 604 if (!levels.empty()) 605 throw new IOException("Called close before exiting all opened elements"); 606 super.close(); 607 } 608 609 @Override 610 public void end() throws IOException { 611 checkStarted(); 612 if (!levels.empty()) 613 throw new IOException("Called end() before exiting all opened elements"); 614 flush(); 615 } 616 @Override 617 public void exit() throws IOException { 618 checkStarted(); 619 if (levels.empty()) { 620 throw new IOException("Called exit one too many times"); 621 } else { 622 if (pendingClose) { 623 write("/>"); 624 writePendingComment(); 625 pendingClose = false; 626 } else { 627 if (levels.current().hasChildren()) 628 writePretty(); 629 write("</"); 630 if (levels.current().getNamespace() == null) 631 write(levels.current().getName()); 632 else 633 write(getNSAbbreviation(levels.current().getNamespace())+levels.current().getName()); 634 write('>'); 635 } 636 levels.pop(); 637 } 638 } 639 640 /* (non-Javadoc) 641 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#open(java.lang.String) 642 */ 643 @Override 644 public void enter(String name) throws IOException { 645 enter(null, name); 646 } 647 648 649 /* (non-Javadoc) 650 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, java.lang.String, boolean) 651 */ 652 @Override 653 public void element(String namespace, String name, String content, boolean onlyIfNotEmpty) throws IOException { 654 if (!onlyIfNotEmpty || content != null && !content.equals("")) 655 element(namespace, name, content); 656 } 657 658 /* (non-Javadoc) 659 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, java.lang.String, java.lang.String) 660 */ 661 @Override 662 public void element(String namespace, String name, String content, String comment) throws IOException { 663 if (!XMLUtil.isNMToken(name)) 664 throw new IOException("XML name "+name+" is not valid"); 665 enter(namespace, name, comment); 666 text(content); 667 exit(); 668 } 669 670 /* (non-Javadoc) 671 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, java.lang.String) 672 */ 673 @Override 674 public void element(String namespace, String name, String content) throws IOException { 675 if (!XMLUtil.isNMToken(name)) 676 throw new IOException("XML name "+name+" is not valid"); 677 enter(namespace, name); 678 text(content); 679 exit(); 680 } 681 682 /* (non-Javadoc) 683 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String, boolean) 684 */ 685 @Override 686 public void element(String name, String content, boolean onlyIfNotEmpty) throws IOException { 687 if (!onlyIfNotEmpty || content != null && !content.equals("")) 688 element(null, name, content); 689 } 690 691 /* (non-Javadoc) 692 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#element(java.lang.String, java.lang.String) 693 */ 694 @Override 695 public void element(String name, String content) throws IOException { 696 element(null, name, content); 697 } 698 699 @Override 700 public void element(String name) throws IOException { 701 element(null, name, null); 702 } 703 704 /* (non-Javadoc) 705 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#text(java.lang.String) 706 */ 707 @Override 708 public void text(String content) throws IOException { 709 text(content, false); 710 } 711 712 /* (non-Javadoc) 713 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#text(java.lang.String, boolean) 714 * 715 * Replace escapeText() 716 */ 717 @Override 718 public void text(String content, boolean dontEscape) throws IOException { 719 checkInElement(); 720 if (content != null) { 721 if (pendingClose) { 722 write(">"); 723 writePendingComment(); 724 pendingClose = false; 725 } 726 if (dontEscape) 727 write(content); 728 else 729 write(XMLUtil.escapeXML(content, charset, false)); 730 } 731 } 732 733 /* (non-Javadoc) 734 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#cData(java.lang.String) 735 */ 736 @Override 737 public void cData(String text) throws IOException { 738 text("<![CDATA["+text+"]]>"); 739 } 740 741 /* (non-Javadoc) 742 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#writeBytes(byte[]) 743 */ 744 @Override 745 public void writeBytes(byte[] bytes) throws IOException { 746 checkInElement(); 747 if (pendingClose) { 748 write(">"); 749 writePendingComment(); 750 pendingClose = false; 751 } 752 flush(); 753 stream.write(bytes); 754 } 755 756 757 /* (non-Javadoc) 758 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#isPretty() 759 */ 760 @Override 761 public boolean isPretty() throws IOException { 762 return (levels == null || levels.empty()) ? prettyBase : levels.current().isPretty(); 763 } 764 765 /* (non-Javadoc) 766 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#setPretty(boolean) 767 */ 768 @Override 769 public void setPretty(boolean pretty) throws IOException { 770 if (levels == null || levels.empty()) 771 this.prettyBase = pretty; 772 else 773 levels.current().setPretty(pretty); 774 } 775 776 /* (non-Javadoc) 777 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#startCommentBlock() 778 */ 779 @Override 780 public void startCommentBlock() throws IOException { 781 if (levels.inComment()) 782 throw new IOException("cannot nest comments"); 783 levels.current().setInComment(true); 784 if (isPretty()) 785 writePretty(); 786 write("<!--"); 787 if (isPretty()) 788 writePretty(); 789 } 790 791 /* (non-Javadoc) 792 * @see org.eclipse.ohf.utilities.xml.IXMLWriter#endCommentBlock() 793 */ 794 @Override 795 public void endCommentBlock() throws IOException { 796 if (!levels.inComment()) 797 throw new IOException("cannot close a comment block when it is open"); 798 if (!levels.current().isInComment()) 799 throw new IOException("cannot close a comment block when it is open"); 800 if (isPretty()) 801 writePretty(); 802 write("-->"); 803 if (isPretty()) 804 writePretty(); 805 levels.current().setInComment(false); 806 } 807 808 public boolean isSortAttributes() { 809 return sortAttributes; 810 } 811 812 public void setSortAttributes(boolean sortAttributes) { 813 this.sortAttributes = sortAttributes; 814 } 815 816 817 public boolean isPrettyHeader() { 818 return prettyHeader; 819 } 820 821 public void setPrettyHeader(boolean pretty) { 822 this.prettyHeader = pretty; 823 } 824 825 public int writePretty() throws IOException { 826 return writePretty(true); 827 } 828 829 public int writePretty(boolean eoln) throws IOException { 830 if (isPretty()) { 831 if (eoln) 832 write(lineType == LINE_UNIX ? "\n" : "\r\n"); 833 for (int i = 0; i < levels.size() - 1; i++) 834 write(" "); 835 return (levels.size() - 1) * 2; 836 } else 837 return 0; 838 } 839 840 public int getLineType() { 841 return lineType; 842 } 843 844 public void setLineType(int lineType) { 845 this.lineType = lineType; 846 } 847 848 public boolean isXmlHeader() { 849 return xmlHeader; 850 } 851 852 public void setXmlHeader(boolean xmlHeader) { 853 this.xmlHeader = xmlHeader; 854 } 855 856 public String[] getSpecialAttributeNames() { 857 return specialAttributeNames; 858 } 859 860 public void setSpecialAttributeNames(String[] specialAttributeNames) { 861 this.specialAttributeNames = specialAttributeNames; 862 } 863 864 public int getAttributeLineWrap() { 865 return attributeLineWrap; 866 } 867 868 public void setAttributeLineWrap(int attributeLineWrap) { 869 this.attributeLineWrap = attributeLineWrap; 870 } 871 872 @Override 873 public void escapedText(String content) throws IOException { 874 text(""); 875 int i = content.length(); 876 if (isPretty()) 877 while (i > 0 && (content.charAt(i-1) == '\r' || content.charAt(i-1) == '\n')) 878 i--; 879 write(content.substring(0, i)); 880 } 881 882 public void processingInstruction(String value) throws IOException { 883 write("<?"+value+"?>"); 884 if (isPrettyHeader()) 885 write("\r\n"); 886 } 887 888 @Override 889 public void link(String href) { 890 // ignore this 891 892 } 893 894 @Override 895 public void anchor(String name) { 896 // ignore this 897 } 898 899 @Override 900 public void decorate(ElementDecoration element) throws IOException { 901 // nothing... 902 } 903 904 905}