View Javadoc
1 package net.sf.flock.parser; 2 3 /*** 4 * Utility class for dealing with HTML entities. 5 * 6 * @version $Revision: 1.1 $ 7 * @author $Author: phraktle $ 8 */ 9 public class HTMLUtil { 10 11 private final static String[][] ENTITIES = 12 {{ "<" , "<" } , 13 { ">" , ">" } , 14 { "&" , "&" } , 15 { """ , "\"" } , 16 { "à" , "?" } , 17 { "À" , "?" } , 18 { "â" , "?" } , 19 { "ä" , "?" } , 20 { "Ä" , "?" } , 21 { "Â" , "?" } , 22 { "å" , "?" } , 23 { "Å" , "?" } , 24 { "æ" , "?" } , 25 { "Æ" , "?" } , 26 { "ç" , "?" } , 27 { "Ç" , "?" } , 28 { "é" , "?" } , 29 { "É" , "?" } , 30 { "è" , "?" } , 31 { "È" , "?" } , 32 { "ê" , "?" } , 33 { "Ê" , "?" } , 34 { "ë" , "?" } , 35 { "Ë" , "?" } , 36 { "ï" , "?" } , 37 { "Ï" , "?" } , 38 { "ô" , "?" } , 39 { "Ô" , "?" } , 40 { "ö" , "?" } , 41 { "Ö" , "?" } , 42 { "ø" , "?" } , 43 { "Ø" , "?" } , 44 { "ß" , "?" } , 45 { "ù" , "?" } , 46 { "Ù" , "?" } , 47 { "û" , "?" } , 48 { "Û" , "?" } , 49 { "ü" , "?" } , 50 { "Ü" , "?" } , 51 { " " , " " } , 52 { "®" , "\u00a9" } , 53 { "©" , "\u00ae" } , 54 { "€" , "\u20a0" } }; 55 56 57 public static String unescape(String source) { 58 if (source==null || source.indexOf('&')==-1) { 59 // no entities 60 return source; 61 } 62 int len = source.length(); 63 StringBuffer result = new StringBuffer(len); 64 for (int i=0; i<len; i++) { 65 int startPos = source.indexOf('&', i); 66 67 if (startPos!=i) { 68 // we skipped some chars, append them to result 69 result.append(source.substring(i, startPos==-1 ? len : startPos)); 70 } 71 72 if (startPos==-1) { 73 // no more entities 74 break; 75 } 76 77 int endPos = source.indexOf(';', startPos); 78 if (endPos==-1) { 79 // broken entity 80 result.append( source.substring(startPos) ); 81 break; 82 } 83 84 String entity = source.substring(startPos, endPos+1); 85 int p=0; 86 for (; p<ENTITIES.length; p++) { 87 if (entity.equals(ENTITIES[p][0])) { 88 result.append( ENTITIES[p][1] ); 89 break; 90 } 91 } 92 if (p>=ENTITIES.length) { 93 // no entity replacement found, leave as-is 94 result.append(entity); 95 } 96 97 // skip ahead a littlebit 98 i=endPos; 99 } 100 return result.toString(); 101 } 102 103 public static String escape(String source) { 104 if (source==null) 105 return null; 106 107 StringBuffer result = new StringBuffer(source.length()); 108 109 for (int i=0;i<source.length();i++) { 110 111 char ch = source.charAt(i); 112 113 if ((int)ch>127) { 114 result.append("&#").append((int)ch).append(';'); 115 } else { 116 result.append(ch); 117 } 118 } 119 return result.toString(); 120 } 121 122 }

This page was automatically generated by Maven