|
|||||||||||||||||||
| This license of Clover is provided to support the development of Flock only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover. | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| HTMLUtil.java | 77.3% | 78.8% | 100% | 78.9% |
|
||||||||||||||
| 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 | 4 |
public static String unescape(String source) {
|
| 58 | 4 |
if (source==null || source.indexOf('&')==-1) {
|
| 59 |
// no entities |
|
| 60 | 1 |
return source; |
| 61 |
} |
|
| 62 | 3 |
int len = source.length(); |
| 63 | 3 |
StringBuffer result = new StringBuffer(len); |
| 64 | 3 |
for (int i=0; i<len; i++) {
|
| 65 | 5 |
int startPos = source.indexOf('&', i);
|
| 66 |
|
|
| 67 | 5 |
if (startPos!=i) {
|
| 68 |
// we skipped some chars, append them to result |
|
| 69 | 4 |
result.append(source.substring(i, startPos==-1 ? len : startPos)); |
| 70 |
} |
|
| 71 |
|
|
| 72 | 5 |
if (startPos==-1) {
|
| 73 |
// no more entities |
|
| 74 | 1 |
break; |
| 75 |
} |
|
| 76 |
|
|
| 77 | 4 |
int endPos = source.indexOf(';', startPos);
|
| 78 | 4 |
if (endPos==-1) {
|
| 79 |
// broken entity |
|
| 80 | 1 |
result.append( source.substring(startPos) ); |
| 81 | 1 |
break; |
| 82 |
} |
|
| 83 |
|
|
| 84 | 3 |
String entity = source.substring(startPos, endPos+1); |
| 85 | 3 |
int p=0; |
| 86 | 3 |
for (; p<ENTITIES.length; p++) {
|
| 87 | 46 |
if (entity.equals(ENTITIES[p][0])) {
|
| 88 | 2 |
result.append( ENTITIES[p][1] ); |
| 89 | 2 |
break; |
| 90 |
} |
|
| 91 |
} |
|
| 92 | 3 |
if (p>=ENTITIES.length) {
|
| 93 |
// no entity replacement found, leave as-is |
|
| 94 | 1 |
result.append(entity); |
| 95 |
} |
|
| 96 |
|
|
| 97 |
// skip ahead a littlebit |
|
| 98 | 3 |
i=endPos; |
| 99 |
} |
|
| 100 | 3 |
return result.toString(); |
| 101 |
} |
|
| 102 |
|
|
| 103 | 1 |
public static String escape(String source) {
|
| 104 | 1 |
if (source==null) |
| 105 | 1 |
return null; |
| 106 |
|
|
| 107 | 0 |
StringBuffer result = new StringBuffer(source.length()); |
| 108 |
|
|
| 109 | 0 |
for (int i=0;i<source.length();i++) {
|
| 110 |
|
|
| 111 | 0 |
char ch = source.charAt(i); |
| 112 |
|
|
| 113 | 0 |
if ((int)ch>127) {
|
| 114 | 0 |
result.append("&#").append((int)ch).append(';');
|
| 115 |
} else {
|
|
| 116 | 0 |
result.append(ch); |
| 117 |
} |
|
| 118 |
} |
|
| 119 | 0 |
return result.toString(); |
| 120 |
} |
|
| 121 |
|
|
| 122 |
} |
|
| 123 |
|
|
||||||||||