1 5 7 package org.jahia.services.importexport; 8 9 import java.io.IOException ; 10 import java.io.OutputStreamWriter ; 11 import java.io.Writer ; 12 import java.util.Enumeration ; 13 import java.util.Hashtable ; 14 15 import org.xml.sax.Attributes ; 16 import org.xml.sax.SAXException ; 17 import org.xml.sax.XMLReader ; 18 import org.xml.sax.helpers.AttributesImpl ; 19 import org.xml.sax.helpers.NamespaceSupport ; 20 import org.xml.sax.helpers.XMLFilterImpl ; 21 22 23 241 public class XMLWriter extends XMLFilterImpl 242 { 243 244 245 246 250 251 256 public XMLWriter () 257 { 258 init(null); 259 } 260 261 262 270 public XMLWriter (Writer writer) 271 { 272 init(writer); 273 } 274 275 276 284 public XMLWriter (XMLReader xmlreader) 285 { 286 super(xmlreader); 287 init(null); 288 } 289 290 291 302 public XMLWriter (XMLReader xmlreader, Writer writer) 303 { 304 super(xmlreader); 305 init(writer); 306 } 307 308 309 317 private void init (Writer writer) 318 { 319 setOutput(writer); 320 nsSupport = new NamespaceSupport (); 321 prefixTable = new Hashtable (); 322 forcedDeclTable = new Hashtable (); 323 doneDeclTable = new Hashtable (); 324 } 325 326 327 328 329 333 334 353 public void reset () 354 { 355 elementLevel = 0; 356 prefixCounter = 0; 357 nsSupport.reset(); 358 } 359 360 361 375 public void flush () 376 throws IOException 377 { 378 output.flush(); 379 } 380 381 382 390 public void setOutput (Writer writer) 391 { 392 if (writer == null) { 393 output = new OutputStreamWriter (System.out); 394 } else { 395 output = writer; 396 } 397 } 398 399 400 414 public void setPrefix (String uri, String prefix) 415 { 416 prefixTable.put(uri, prefix); 417 } 418 419 420 427 public String getPrefix (String uri) 428 { 429 return (String )prefixTable.get(uri); 430 } 431 432 433 449 public void forceNSDecl (String uri) 450 { 451 forcedDeclTable.put(uri, Boolean.TRUE); 452 } 453 454 455 468 public void forceNSDecl (String uri, String prefix) 469 { 470 setPrefix(uri, prefix); 471 forceNSDecl(uri); 472 } 473 474 475 476 477 481 482 492 public void startDocument () 493 throws SAXException 494 { 495 reset(); 496 write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 497 super.startDocument(); 498 } 499 500 501 511 public void endDocument () 512 throws SAXException 513 { 514 write('\n'); 515 super.endDocument(); 516 try { 517 flush(); 518 } catch (IOException e) { 519 throw new SAXException (e); 520 } 521 } 522 523 524 543 public void startElement (String uri, String localName, 544 String qName, Attributes atts) 545 throws SAXException 546 { 547 elementLevel++; 548 nsSupport.pushContext(); 549 write('<'); 550 writeName(uri, localName, qName, true); 551 writeAttributes(atts); 552 if (elementLevel == 1) { 553 forceNSDecls(); 554 } 555 write('>'); 557 super.startElement(uri, localName, qName, atts); 558 } 559 560 561 579 public void endElement (String uri, String localName, String qName) 580 throws SAXException 581 { 582 write("</"); 583 writeName(uri, localName, qName, true); 584 write('>'); 585 if (elementLevel == 1) { 586 write('\n'); 587 } 588 super.endElement(uri, localName, qName); 589 nsSupport.popContext(); 590 elementLevel--; 591 } 592 593 594 607 public void characters (char ch[], int start, int len) 608 throws SAXException 609 { 610 writeEsc(ch, start, len, false); 611 super.characters(ch, start, len); 612 } 613 614 615 628 public void ignorableWhitespace (char ch[], int start, int length) 629 throws SAXException 630 { 631 writeEsc(ch, start, length, false); 632 super.ignorableWhitespace(ch, start, length); 633 } 634 635 636 637 649 public void processingInstruction (String target, String data) 650 throws SAXException 651 { 652 write("<?"); 653 write(target); 654 write(' '); 655 write(data); 656 write("?>"); 657 if (elementLevel < 1) { 658 write('\n'); 659 } 660 super.processingInstruction(target, data); 661 } 662 663 664 665 666 670 694 public void emptyElement (String uri, String localName, 695 String qName, Attributes atts) 696 throws SAXException 697 { 698 nsSupport.pushContext(); 699 write('<'); 700 writeName(uri, localName, qName, true); 701 writeAttributes(atts); 702 if (elementLevel == 1) { 703 forceNSDecls(); 704 } 705 writeNSDecls(); 706 write("/>"); 707 super.startElement(uri, localName, qName, atts); 708 super.endElement(uri, localName, qName); 709 } 710 711 712 713 714 718 719 720 736 public void startElement (String uri, String localName) 737 throws SAXException 738 { 739 startElement(uri, localName, "", EMPTY_ATTS); 740 } 741 742 743 758 public void startElement (String localName) 759 throws SAXException 760 { 761 startElement("", localName, "", EMPTY_ATTS); 762 } 763 764 765 779 public void endElement (String uri, String localName) 780 throws SAXException 781 { 782 endElement(uri, localName, ""); 783 } 784 785 786 800 public void endElement (String localName) 801 throws SAXException 802 { 803 endElement("", localName, ""); 804 } 805 806 807 822 public void emptyElement (String uri, String localName) 823 throws SAXException 824 { 825 emptyElement(uri, localName, "", EMPTY_ATTS); 826 } 827 828 829 844 public void emptyElement (String localName) 845 throws SAXException 846 { 847 emptyElement("", localName, "", EMPTY_ATTS); 848 } 849 850 851 876 public void dataElement (String uri, String localName, 877 String qName, Attributes atts, 878 String content) 879 throws SAXException 880 { 881 startElement(uri, localName, qName, atts); 882 characters(content); 883 endElement(uri, localName, qName); 884 } 885 886 887 911 public void dataElement (String uri, String localName, String content) 912 throws SAXException 913 { 914 dataElement(uri, localName, "", EMPTY_ATTS, content); 915 } 916 917 918 942 public void dataElement (String localName, String content) 943 throws SAXException 944 { 945 dataElement("", localName, "", EMPTY_ATTS, content); 946 } 947 948 949 962 public void characters (String data) 963 throws SAXException 964 { 965 char ch[] = data.toCharArray(); 966 characters(ch, 0, ch.length); 967 } 968 969 970 971 972 976 977 983 private void forceNSDecls () 984 { 985 Enumeration prefixes = forcedDeclTable.keys(); 986 while (prefixes.hasMoreElements()) { 987 String prefix = (String )prefixes.nextElement(); 988 doPrefix(prefix, null, true); 989 } 990 } 991 992 993 1006 private String doPrefix (String uri, String qName, boolean isElement) 1007 { 1008 String defaultNS = nsSupport.getURI(""); 1009 if ("".equals(uri)) { 1010 if (isElement && defaultNS != null) 1011 nsSupport.declarePrefix("", ""); 1012 return null; 1013 } 1014 String prefix; 1015 if (isElement && defaultNS != null && uri.equals(defaultNS)) { 1016 prefix = ""; 1017 } else { 1018 prefix = nsSupport.getPrefix(uri); 1019 } 1020 if (prefix != null) { 1021 return prefix; 1022 } 1023 prefix = (String ) doneDeclTable.get(uri); 1024 if (prefix != null && 1025 ((!isElement || defaultNS != null) && 1026 "".equals(prefix) || nsSupport.getURI(prefix) != null)) { 1027 prefix = null; 1028 } 1029 if (prefix == null) { 1030 prefix = (String ) prefixTable.get(uri); 1031 if (prefix != null && 1032 ((!isElement || defaultNS != null) && 1033 "".equals(prefix) || nsSupport.getURI(prefix) != null)) { 1034 prefix = null; 1035 } 1036 } 1037 if (prefix == null && qName != null && !"".equals(qName)) { 1038 int i = qName.indexOf(':'); 1039 if (i == -1) { 1040 if (isElement && defaultNS == null) { 1041 prefix = ""; 1042 } 1043 } else { 1044 prefix = qName.substring(0, i); 1045 } 1046 } 1047 for (; 1048 prefix == null || nsSupport.getURI(prefix) != null; 1049 prefix = "__NS" + ++prefixCounter) 1050 ; 1051 nsSupport.declarePrefix(prefix, uri); 1052 doneDeclTable.put(uri, prefix); 1053 return prefix; 1054 } 1055 1056 1057 1065 private void write (char c) 1066 throws SAXException 1067 { 1068 try { 1069 output.write(c); 1070 } catch (IOException e) { 1071 throw new SAXException (e); 1072 } 1073 } 1074 1075 1076 1084 private void write (String s) 1085 throws SAXException 1086 { 1087 try { 1088 output.write(s); 1089 } catch (IOException e) { 1090 throw new SAXException (e); 1091 } 1092 } 1093 1094 1095 1105 private void writeAttributes (Attributes atts) 1106 throws SAXException 1107 { 1108 int len = atts.getLength(); 1109 for (int i = 0; i < len; i++) { 1110 char ch[] = atts.getValue(i).toCharArray(); 1111 write(' '); 1112 writeName(atts.getURI(i), atts.getLocalName(i), 1113 atts.getQName(i), false); 1114 write("=\""); 1115 writeEsc(ch, 0, ch.length, true); 1116 write('"'); 1117 } 1118 } 1119 1120 1121 1132 private void writeEsc (char ch[], int start, 1133 int length, boolean isAttVal) 1134 throws SAXException 1135 { 1136 for (int i = start; i < start + length; i++) { 1137 switch (ch[i]) { 1138 case '&': 1139 write("&"); 1140 break; 1141 case '<': 1142 write("<"); 1143 break; 1144 case '>': 1145 write(">"); 1146 break; 1147 case '\"': 1148 if (isAttVal) { 1149 write("""); 1150 } else { 1151 write('\"'); 1152 } 1153 break; 1154 default: 1155 write(ch[i]); 1161 } 1163 } 1164 } 1165 1166 1167 1175 private void writeNSDecls () 1176 throws SAXException 1177 { 1178 Enumeration prefixes = nsSupport.getDeclaredPrefixes(); 1179 while (prefixes.hasMoreElements()) { 1180 String prefix = (String ) prefixes.nextElement(); 1181 String uri = nsSupport.getURI(prefix); 1182 if (uri == null) { 1183 uri = ""; 1184 } 1185 char ch[] = uri.toCharArray(); 1186 write(' '); 1187 if ("".equals(prefix)) { 1188 write("xmlns=\""); 1189 } else { 1190 write("xmlns:"); 1191 write(prefix); 1192 write("=\""); 1193 } 1194 writeEsc(ch, 0, ch.length, true); 1195 write('\"'); 1196 } 1197 } 1198 1199 1200 1212 private void writeName (String uri, String localName, 1213 String qName, boolean isElement) 1214 throws SAXException 1215 { 1216 String prefix = doPrefix(uri, qName, isElement); 1217 if (prefix != null && !"".equals(prefix)) { 1218 write(prefix); 1219 write(':'); 1220 } 1221 write(localName); 1222 } 1223 1224 1225 1226 1227 1231 private final Attributes EMPTY_ATTS = new AttributesImpl (); 1232 1233 1234 1235 1236 1240 private Hashtable prefixTable; 1241 private Hashtable forcedDeclTable; 1242 private Hashtable doneDeclTable; 1243 private int elementLevel = 0; 1244 private Writer output; 1245 private NamespaceSupport nsSupport; 1246 private int prefixCounter = 0; 1247} | Popular Tags |