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 PropertyWriter { 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 PropertyWriter(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 PropertyWriter(boolean canonical) throws UnsupportedEncodingException { 77 this( getWriterEncoding(), canonical); 78 } 79 80 public PropertyWriter(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.getLocalName()); 141 out.print(' '); 142 out.print("xmlns=\""); 143 if (node.getNamespaceURI() != null) { 144 out.print(node.getNamespaceURI()); 145 } 146 out.print("\""); 147 Attr attrs[] = sortAttributes(node.getAttributes()); 148 for ( int i = 0; i < attrs.length; i++ ) { 149 Attr attr = attrs[i]; 150 if (!attr.getNodeName().equals("xmlns")) { 151 out.print(' '); 152 out.print(attr.getNodeName()); 153 out.print("=\""); 154 out.print(normalize(attr.getNodeValue())); 155 out.print('"'); 156 } 157 } 158 out.print('>'); 159 NodeList children = node.getChildNodes(); 160 if ( children != null ) { 161 int len = children.getLength(); 162 for ( int i = 0; i < len; i++ ) { 163 print(children.item(i)); 164 } 165 } 166 break; 167 } 168 169 case Node.ENTITY_REFERENCE_NODE: { 171 if ( canonical ) { 172 NodeList children = node.getChildNodes(); 173 if ( children != null ) { 174 int len = children.getLength(); 175 for ( int i = 0; i < len; i++ ) { 176 print(children.item(i)); 177 } 178 } 179 } else { 180 out.print('&'); 181 out.print(node.getNodeName()); 182 out.print(';'); 183 } 184 break; 185 } 186 187 case Node.CDATA_SECTION_NODE: { 189 if ( canonical ) { 190 out.print(normalize(node.getNodeValue())); 191 } else { 192 out.print("<![CDATA["); 193 out.print(node.getNodeValue()); 194 out.print("]]>"); 195 } 196 break; 197 } 198 199 case Node.TEXT_NODE: { 201 out.print(normalize(node.getNodeValue())); 202 break; 203 } 204 205 case Node.PROCESSING_INSTRUCTION_NODE: { 207 out.print("<?"); 208 out.print(node.getNodeName()); 209 String data = node.getNodeValue(); 210 if ( data != null && data.length() > 0 ) { 211 out.print(' '); 212 out.print(data); 213 } 214 out.print("?>"); 215 break; 216 } 217 } 218 219 if ( type == Node.ELEMENT_NODE ) { 220 out.print("</"); 221 out.print(node.getLocalName()); 222 out.print('>'); 223 } 224 225 out.flush(); 226 227 } 229 230 protected Attr [] sortAttributes(NamedNodeMap attrs) { 231 232 int len = (attrs != null) ? attrs.getLength() : 0; 233 Attr array[] = new Attr [len]; 234 for ( int i = 0; i < len; i++ ) { 235 array[i] = (Attr )attrs.item(i); 236 } 237 for ( int i = 0; i < len - 1; i++ ) { 238 String name = array[i].getNodeName(); 239 int index = i; 240 for ( int j = i + 1; j < len; j++ ) { 241 String curName = array[j].getNodeName(); 242 if ( curName.compareTo(name) < 0 ) { 243 name = curName; 244 index = j; 245 } 246 } 247 if ( index != i ) { 248 Attr temp = array[i]; 249 array[i] = array[index]; 250 array[index] = temp; 251 } 252 } 253 254 return (array); 255 256 } 258 259 260 protected String normalize(String s) { 261 StringBuffer str = new StringBuffer (); 262 263 int len = (s != null) ? s.length() : 0; 264 for ( int i = 0; i < len; i++ ) { 265 char ch = s.charAt(i); 266 switch ( ch ) { 267 case '<': { 268 str.append("<"); 269 break; 270 } 271 case '>': { 272 str.append(">"); 273 break; 274 } 275 case '&': { 276 str.append("&"); 277 break; 278 } 279 case '"': { 280 str.append("""); 281 break; 282 } 283 case '\r': 284 case '\n': { 285 if ( canonical ) { 286 str.append("&#"); 287 str.append(Integer.toString(ch)); 288 str.append(';'); 289 break; 290 } 291 } 293 default: { 294 str.append(ch); 295 } 296 } 297 } 298 299 return (str.toString()); 300 301 } 303 } 304 | Popular Tags |