1 20 21 package fr.dyade.aaa.util; 22 23 import java.io.*; 24 25 public class XMLWriter { 26 27 36 public static void print(PrintWriter out, String str) throws Exception { 37 if (str == null) { 38 out.print("\"\""); 39 return; 40 } 41 42 out.print('"'); 43 int max = str.length(); 44 for (int i = 0; i < max; i ++) { 45 int b = (int) str.charAt(i); 47 if ((b >= 32) && (b <= 126)) { 48 switch (b) { 51 case (int) '"': 52 out.write("""); 53 break; 54 case (int) '\'': 55 out.write("'"); 56 break; 57 case (int) '<': 58 out.write("<"); 59 break; 60 case (int) '>': 61 out.write(">"); 62 break; 63 case (int) '&': 64 out.write("&"); 65 break; 66 case (int) '%': 67 out.write("%"); 68 break; 69 default: 70 out.write(b); 71 break; 72 } 73 } else { 74 if (b == 0) { 75 out.write("�"); 76 } else { 77 int[] xvalue = new int[4]; 78 int len = 0; 79 while (b > 0) { 80 xvalue[len] = b & 0xf; 81 if (xvalue[len] < 10) 82 xvalue[len] += (int) '0'; 83 else 84 xvalue[len] += ((int) 'a') - 10; 85 b >>= 4; 86 len ++; 87 } 88 out.write("&#x"); 89 while (len-- > 0) 90 out.write(xvalue[len]); 91 out.write(';'); 92 } 93 } 94 } 95 out.print('"'); 96 } 97 } 98 | Popular Tags |