Created
July 25, 2012 16:17
-
-
Save mdread/3177038 to your computer and use it in GitHub Desktop.
escape characters that are outside of the specified range in the form &#digits;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EscapeSpecialChars { | |
public static final char HEXA = 'H'; | |
public static final char DECIMAL = 'D'; | |
private int minRange; | |
private int maxRange; | |
private String prefixEscape; | |
private String suffixEscape; | |
private String exclusionCharList; | |
private char formatMethod; | |
//CONSTRUCTORS | |
public EscapeSpecialChars(int minRange, int maxRange, String prefixEscape, String suffixEscape, String exclusionCharList, char formatMethod){ | |
this.minRange = minRange; | |
this.maxRange = maxRange; | |
this.prefixEscape = prefixEscape; | |
this.suffixEscape = suffixEscape; | |
this.exclusionCharList = exclusionCharList; | |
this.formatMethod = formatMethod; | |
} | |
public EscapeSpecialChars(){ | |
this.minRange = 32; | |
this.maxRange = 255; | |
this.prefixEscape = "&#"; | |
this.suffixEscape = ";"; | |
this.exclusionCharList = "\n\t\r"; | |
this.formatMethod = EscapeHTML.DECIMAL; | |
} | |
//GETTERS | |
public String getExclusionCharList() { | |
return exclusionCharList; | |
} | |
public char getFormatMethod() { | |
return formatMethod; | |
} | |
public int getMaxRange() { | |
return maxRange; | |
} | |
public int getMinRange() { | |
return minRange; | |
} | |
public String getPrefixEscape() { | |
return prefixEscape; | |
} | |
public String getSuffixEscape() { | |
return suffixEscape; | |
} | |
//SETTERS | |
public void setExclusionCharList(String exclusionCharList) { | |
this.exclusionCharList = exclusionCharList; | |
} | |
public void setFormatMethod(char formatMethod) { | |
this.formatMethod = formatMethod; | |
} | |
public void setMaxRange(int maxRange) { | |
this.maxRange = maxRange; | |
} | |
public void setMinRange(int minRange) { | |
this.minRange = minRange; | |
} | |
public void setPrefixEscape(String prefixEscape) { | |
this.prefixEscape = prefixEscape; | |
} | |
public void setSuffixEscape(String suffixEscape) { | |
this.suffixEscape = suffixEscape; | |
} | |
//PUBLIC METHODS | |
public String format(String str){ | |
String formattedText = ""; | |
if(formatMethod == EscapeHTML.HEXA){ | |
formattedText = formatHexa(str); | |
} else if(formatMethod == EscapeHTML.DECIMAL){ | |
formattedText = formatDecimal(str); | |
}else{ | |
System.err.println("Invalid format method"); | |
} | |
return formattedText; | |
} | |
//PRIVATE METHODS | |
private String formatHexa(String str){ | |
String formattedText = ""; | |
for (int i = 0; i < str.length(); i++) { | |
int codePoint = (new Character(str.charAt(i)).hashCode()); | |
if(isInRange(codePoint) || isExcluded(codePoint)){ | |
formattedText += str.charAt(i); | |
}else{ | |
String hexValue = ""; | |
hexValue = Integer.toHexString(codePoint); | |
while(hexValue.length() < 4){ | |
hexValue = "0" + hexValue; | |
} | |
formattedText += prefixEscape + hexValue + suffixEscape; | |
} | |
} | |
return formattedText; | |
} | |
private String formatDecimal(String str){ | |
String formattedText = ""; | |
for (int i = 0; i < str.length(); i++) { | |
int codePoint = (new Character(str.charAt(i)).hashCode()); | |
if(isInRange(codePoint) || isExcluded(codePoint)){ | |
formattedText += str.charAt(i); | |
}else{ | |
formattedText += prefixEscape + codePoint + suffixEscape; | |
} | |
} | |
return formattedText; | |
} | |
private boolean isInRange(int ch){ | |
return ch >= minRange && ch <= maxRange; | |
} | |
private boolean isExcluded(int ch){ | |
return exclusionCharList.indexOf(ch) != -1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment