1 11 package org.eclipse.jdt.internal.compiler.util; 12 13 import java.io.OutputStream ; 14 import java.io.PrintWriter ; 15 import java.io.Writer ; 16 import java.util.Arrays ; 17 import java.util.Comparator ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 public class GenericXMLWriter extends PrintWriter { 22 23 private static final String XML_VERSION= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; private static void appendEscapedChar(StringBuffer buffer, char c) { 25 String replacement= getReplacement(c); 26 if (replacement != null) { 27 buffer.append('&'); 28 buffer.append(replacement); 29 buffer.append(';'); 30 } else { 31 buffer.append(c); 32 } 33 } 34 private static String getEscaped(String s) { 35 StringBuffer result= new StringBuffer (s.length() + 10); 36 for (int i= 0; i < s.length(); ++i) 37 appendEscapedChar(result, s.charAt(i)); 38 return result.toString(); 39 } 40 private static String getReplacement(char c) { 41 switch (c) { 44 case '<' : 45 return "lt"; case '>' : 47 return "gt"; case '"' : 49 return "quot"; case '\'' : 51 return "apos"; case '&' : 53 return "amp"; } 55 return null; 56 } 57 private String lineSeparator; 58 private int tab; 59 public GenericXMLWriter(OutputStream stream, String lineSeparator, boolean printXmlVersion) { 60 this(new PrintWriter (stream), lineSeparator, printXmlVersion); 61 } 62 public GenericXMLWriter(Writer writer, String lineSeparator, boolean printXmlVersion) { 63 super(writer); 64 this.tab= 0; 65 this.lineSeparator = lineSeparator; 66 if (printXmlVersion) { 67 print(XML_VERSION); 68 print(this.lineSeparator); 69 } 70 } 71 public void endTag(String name, boolean insertTab, boolean insertNewLine) { 72 this.tab --; 73 printTag('/' + name, null, insertTab, insertNewLine, false); 74 } 75 78 public void printString(String string, boolean insertTab, boolean insertNewLine) { 79 if (insertTab) { 80 printTabulation(); 81 } 82 print(string); 83 if (insertNewLine) { 84 print(this.lineSeparator); 85 } 86 } 87 private void printTabulation() { 88 for (int i= 0; i < this.tab; i++) this.print('\t'); 89 } 90 public void printTag(String name, HashMap parameters, boolean insertTab, boolean insertNewLine, boolean closeTag) { 91 if (insertTab) { 92 this.printTabulation(); 93 } 94 this.print('<'); 95 this.print(name); 96 if (parameters != null) { 97 int length = parameters.size(); 98 Map.Entry [] entries = new Map.Entry [length]; 99 parameters.entrySet().toArray(entries); 100 Arrays.sort(entries, new Comparator () { 101 public int compare(Object o1, Object o2) { 102 Map.Entry entry1 = (Map.Entry ) o1; 103 Map.Entry entry2 = (Map.Entry ) o2; 104 return ((String ) entry1.getKey()).compareTo((String ) entry2.getKey()); 105 } 106 }); 107 for (int i = 0; i < length; i++) { 108 this.print(' '); 109 this.print(entries[i].getKey()); 110 this.print("=\""); this.print(getEscaped(String.valueOf(entries[i].getValue()))); 112 this.print('\"'); 113 } 114 } 115 if (closeTag) { 116 this.print("/>"); } else { 118 this.print(">"); } 120 if (insertNewLine) { 121 print(this.lineSeparator); 122 } 123 if (parameters != null && !closeTag) 124 this.tab++; 125 126 } 127 public void startTag(String name, boolean insertTab) { 128 printTag(name, null, insertTab, true, false); 129 this.tab++; 130 } 131 } 132 | Popular Tags |