1 20 package org.enhydra.barracuda.plankton.xml; 21 22 23 28 public class XMLUtil { 29 30 private static String sep = System.getProperty("line.separator"); 31 32 40 public static String fromXMLUnicodeString(String s) { 41 StringBuffer sb = new StringBuffer (s.length()); 42 43 char c[] = s.toCharArray(); 44 int cpos = 0; 45 int spos = -1; 46 int epos = -1; 47 int mpos = s.length(); 48 while (cpos<mpos) { 49 spos = s.indexOf("&#",cpos); 50 if (spos>-1) epos = s.indexOf(";", spos); 51 52 if (spos>-1 && epos>-1) { 53 sb.append(s.substring(cpos,spos)); 54 String unicode = s.substring(spos+2,epos); 55 String newChar = null; 56 try { 57 int newi = Integer.decode("0"+unicode).intValue(); 58 char newch = (char) newi; 59 sb.append(newch); 60 cpos = epos+1; 61 } catch (Exception e) { 62 sb.append(s.substring(spos,spos+2)); 63 cpos = spos + 2; 64 } 65 66 } else { 67 sb.append(s.substring(cpos,mpos)); 68 cpos = mpos; 69 } 70 } 71 return sb.toString(); 72 } 73 74 87 public static String toXMLUnicodeString(String s) { 88 StringBuffer sb = new StringBuffer (s.length()); 89 char c[] = s.toCharArray(); 90 int max = c.length; 91 int code = -1; 92 int epos = max-1; 93 int convCntr = 0; 94 int convThreshold = max/15; 95 96 for (int i=0; i<max; i++) { 97 code = c[i]; 98 99 if (code==10 || code==13) { 101 sb.append(sep); 102 int nextCode = (i<epos ? c[i+1] : -1); 103 if ((code==10 && nextCode==13) || (code==13 && nextCode==10)) i++; 104 continue; 105 } 106 107 if (((code>=' ') && (code<='~') && (code!='&') && (code!='<') && (code!='>') && (code!='\'') && (code!='"')) || 111 (code=='\t') || (code=='\n') || (code=='\r')) { 112 sb.append(c[i]); 113 } else { 114 String uc = null; 115 String hex = Integer.toHexString(c[i]); 116 117 uc = "&#x"+hex+";"; 119 120 sb.append(uc); 121 convCntr++; 122 } 123 124 } 131 return sb.toString(); 132 } 133 134 135 139 public static void main (String args[]) { 140 String target = null; 141 String dest = null; 142 String result = null; 143 144 System.out.println (""); 146 System.out.println ("From XML to Java..."); 147 148 target = "blah blah blah"; 150 dest = target; 151 result = fromXMLUnicodeString(target); 152 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 153 154 target = "blah < blah > blah"; 156 dest = target; 157 result = fromXMLUnicodeString(target); 158 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 159 160 target = "Test ©1 and Test©2 and ©"; 162 dest = "Test \u00a91 and Test\u00a92 and \u00a9"; 163 result = fromXMLUnicodeString(target); 164 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 165 166 target = "©1 and Test©2 and © sdf"; 168 dest = "\u00a91 and Test\u00a92 and \u00a9 sdf"; 169 result = fromXMLUnicodeString(target); 170 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 171 172 target = "©"; 174 dest = "\u00a9"; 175 result = fromXMLUnicodeString(target); 176 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 177 178 179 180 System.out.println (""); 182 System.out.println ("From Java to XML..."); 183 184 target = "blah blah blah"; 186 dest = target; 187 result = toXMLUnicodeString(target); 188 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 189 190 target = "blah < blah > blah"; 192 dest = "blah < blah > blah"; 193 result = toXMLUnicodeString(target); 194 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 195 196 target = "Test \u00a91 and Test\u00a92 and \u00a9"; 198 dest = "Test ©1 and Test©2 and ©"; 199 result = toXMLUnicodeString(target); 200 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 201 202 target = "\u00a91 and Test\u00a92 and \u00a9 sdf"; 204 dest = "©1 and Test©2 and © sdf"; 205 result = toXMLUnicodeString(target); 206 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 207 208 target = "\u00a9"; 210 dest = "©"; 211 result = toXMLUnicodeString(target); 212 System.out.println ("S/B:["+dest+"] Result:["+result+"]..."+(dest.equals(result) ? "ok" : "failed")); 213 214 } 215 216 } 217 | Popular Tags |