1 23 24 package org.apache.webdav.lib.util; 25 26 import java.io.OutputStreamWriter ; 27 import java.io.PrintWriter ; 28 import java.io.UnsupportedEncodingException ; 29 import java.io.Writer ; 30 31 import org.w3c.dom.Attr ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.NamedNodeMap ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 37 public class DOMWriter { 38 39 43 44 private static String 45 PRINTWRITER_ENCODING = "UTF8"; 46 47 private static String MIME2JAVA_ENCODINGS[] = 48 { "Default", "UTF-8", "US-ASCII", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", 49 "ISO-8859-5", "ISO-8859-6", "ISO-8859-7", "ISO-8859-8", "ISO-8859-9", "ISO-2022-JP", 50 "SHIFT_JIS", "EUC-JP","GB2312", "BIG5", "EUC-KR", "ISO-2022-KR", "KOI8-R", "EBCDIC-CP-US", 51 "EBCDIC-CP-CA", "EBCDIC-CP-NL", "EBCDIC-CP-DK", "EBCDIC-CP-NO", "EBCDIC-CP-FI", "EBCDIC-CP-SE", 52 "EBCDIC-CP-IT", "EBCDIC-CP-ES", "EBCDIC-CP-GB", "EBCDIC-CP-FR", "EBCDIC-CP-AR1", 53 "EBCDIC-CP-HE", "EBCDIC-CP-CH", "EBCDIC-CP-ROECE","EBCDIC-CP-YU", 54 "EBCDIC-CP-IS", "EBCDIC-CP-AR2", "UTF-16" 55 }; 56 57 58 59 protected PrintWriter out; 60 61 62 protected boolean canonical; 63 64 65 public DOMWriter(String encoding, boolean canonical) 66 throws UnsupportedEncodingException { 67 out = new PrintWriter (new OutputStreamWriter (System.out, encoding)); 68 this.canonical = canonical; 69 } 71 75 76 public DOMWriter(boolean canonical) throws UnsupportedEncodingException { 77 this( getWriterEncoding(), canonical); 78 } 79 80 public DOMWriter(Writer writer, boolean canonical) { 81 out = new PrintWriter (writer); 82 this.canonical = canonical; 83 } 84 85 public static String getWriterEncoding( ) { 86 return (PRINTWRITER_ENCODING); 87 } 89 public static void setWriterEncoding( String encoding ) { 90 if( encoding.equalsIgnoreCase( "DEFAULT" ) ) 91 PRINTWRITER_ENCODING = "UTF8"; 92 else if( encoding.equalsIgnoreCase( "UTF-16" ) ) 93 PRINTWRITER_ENCODING = "Unicode"; 94 else 95 PRINTWRITER_ENCODING = MIME2Java.convert( encoding ); 96 } 98 99 public static boolean isValidJavaEncoding( String encoding ) { 100 for ( int i = 0; i < MIME2JAVA_ENCODINGS.length; i++ ) 101 if ( encoding.equals( MIME2JAVA_ENCODINGS[i] ) ) 102 return (true); 103 104 return (false); 105 } 107 108 109 public void print(Node node) { 110 111 if ( node == null ) { 113 return; 114 } 115 116 int type = node.getNodeType(); 117 switch ( type ) { 118 case Node.DOCUMENT_NODE: { 120 if ( !canonical ) { 121 String Encoding = getWriterEncoding(); 122 if( Encoding.equalsIgnoreCase( "DEFAULT" ) ) 123 Encoding = "UTF-8"; 124 else if( Encoding.equalsIgnoreCase( "Unicode" ) ) 125 Encoding = "UTF-16"; 126 else 127 Encoding = MIME2Java.reverse( Encoding ); 128 129 out.println("<?xml version=\"1.0\" encoding=\""+ 130 Encoding + "\"?>"); 131 } 132 print(((Document )node).getDocumentElement()); 133 out.flush(); 134 break; 135 } 136 137 case Node.ELEMENT_NODE: { 139 out.print('<'); 140 out.print(node.getNodeName()); 141 Attr attrs[] = sortAttributes(node.getAttributes()); 142 for ( int i = 0; i < attrs.length; i++ ) { 143 Attr attr = attrs[i]; 144 out.print(' '); 145 out.print(attr.getNodeName()); 146 out.print("=\""); 147 out.print(normalize(attr.getNodeValue())); 148 out.print('"'); 149 } 150 out.print('>'); 151 NodeList children = node.getChildNodes(); 152 if ( children != null ) { 153 int len = children.getLength(); 154 for ( int i = 0; i < len; i++ ) { 155 print(children.item(i)); 156 } 157 } 158 break; 159 } 160 161 case Node.ENTITY_REFERENCE_NODE: { 163 if ( canonical ) { 164 NodeList children = node.getChildNodes(); 165 if ( children != null ) { 166 int len = children.getLength(); 167 for ( int i = 0; i < len; i++ ) { 168 print(children.item(i)); 169 } 170 } 171 } else { 172 out.print('&'); 173 out.print(node.getNodeName()); 174 out.print(';'); 175 } 176 break; 177 } 178 179 case Node.CDATA_SECTION_NODE: { 181 if ( canonical ) { 182 out.print(normalize(node.getNodeValue())); 183 } else { 184 out.print("<![CDATA["); 185 out.print(node.getNodeValue()); 186 out.print("]]>"); 187 } 188 break; 189 } 190 191 case Node.TEXT_NODE: { 193 out.print(normalize(node.getNodeValue())); 194 break; 195 } 196 197 case Node.PROCESSING_INSTRUCTION_NODE: { 199 out.print("<?"); 200 out.print(node.getNodeName()); 201 String data = node.getNodeValue(); 202 if ( data != null && data.length() > 0 ) { 203 out.print(' '); 204 out.print(data); 205 } 206 out.print("?>"); 207 break; 208 } 209 } 210 211 if ( type == Node.ELEMENT_NODE ) { 212 out.print("</"); 213 out.print(node.getNodeName()); 214 out.print('>'); 215 } 216 217 out.flush(); 218 219 } 221 222 protected Attr [] sortAttributes(NamedNodeMap attrs) { 223 224 int len = (attrs != null) ? attrs.getLength() : 0; 225 Attr array[] = new Attr [len]; 226 for ( int i = 0; i < len; i++ ) { 227 array[i] = (Attr )attrs.item(i); 228 } 229 for ( int i = 0; i < len - 1; i++ ) { 230 String name = array[i].getNodeName(); 231 int index = i; 232 for ( int j = i + 1; j < len; j++ ) { 233 String curName = array[j].getNodeName(); 234 if ( curName.compareTo(name) < 0 ) { 235 name = curName; 236 index = j; 237 } 238 } 239 if ( index != i ) { 240 Attr temp = array[i]; 241 array[i] = array[index]; 242 array[index] = temp; 243 } 244 } 245 246 return (array); 247 248 } 250 251 252 protected String normalize(String s) { 253 StringBuffer str = new StringBuffer (); 254 255 int len = (s != null) ? s.length() : 0; 256 for ( int i = 0; i < len; i++ ) { 257 char ch = s.charAt(i); 258 switch ( ch ) { 259 case '<': { 260 str.append("<"); 261 break; 262 } 263 case '>': { 264 str.append(">"); 265 break; 266 } 267 case '&': { 268 str.append("&"); 269 break; 270 } 271 case '"': { 272 str.append("""); 273 break; 274 } 275 case '\r': 276 case '\n': { 277 if ( canonical ) { 278 str.append("&#"); 279 str.append(Integer.toString(ch)); 280 str.append(';'); 281 break; 282 } 283 } 285 default: { 286 str.append(ch); 287 } 288 } 289 } 290 291 return (str.toString()); 292 293 } 295 } 296 | Popular Tags |