adding all of botlist, initial add
[botlist.git] / botlistprojects / botbert / src / KeywordProcessor.java
blob4e4af905496cb9aaa3b5abcb96a006dd4218133a
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLDecoder;
5 import java.net.URLEncoder;
7 /**
8 * This is class is used by botverse.
9 * @author Berlin Brown
11 public class KeywordProcessor {
13 public final static String [][] FILTER_SET = {
14 { "'", "'" } ,
15 { """, "'" },
16 { "&", "&" },
19 /**
20 * When saving a keyword, process the input to
21 * filter invalid characters and downcase the string.
23 * @return
25 public final static String createKeywords(String value) {
26 if (value == null) {
27 return value;
29 // Filter out non alphanumeric chars
30 String output = value.replaceAll("[^\\s0-9a-zA-Z]", "");
31 return output.trim().toLowerCase();
34 public final static String urlEncode(String val) throws UnsupportedEncodingException {
35 return URLEncoder.encode(val, "UTF-8");
37 public final static String urlEncodeEnc(String val, String enc) throws UnsupportedEncodingException {
38 return URLEncoder.encode(val, enc);
41 public final static String urlDecode(String val) throws UnsupportedEncodingException {
42 return URLDecoder.decode(val, "UTF-8");
44 public final static String urlDecodeEnc(String val, String enc) throws UnsupportedEncodingException {
45 return URLDecoder.decode(val, enc);
48 /**
49 * Utility for filtering alpha [a-Z], etc text using regular expressions, used with queries
50 * to prevent sql injection hacks.
52 public final static String filterAlphaText(final String value) {
53 if (value == null) {
54 return value;
56 // Filter out non alphanumeric chars
57 String output = value.replaceAll("[^\\sa-zA-Z]", "");
58 return output.trim();
61 public final static String filterAlphaNumeric(String value) {
62 if (value == null) {
63 return value;
65 // Filter out non alphanumeric chars
66 String output = value.replaceAll("[^\\s0-9a-zA-Z]", "");
67 return output.trim();
69 public final static boolean validateFilterAlphaNumeric(String value) {
70 if (value == null) {
71 return false;
73 // Filter out non alphanumeric chars
74 String output = filterAlphaNumeric(value);
75 output = output.replaceAll(" ", "");
76 return (output.trim().length() == value.length());
79 public final static String filterNonAscii(String value) {
80 if (value == null) {
81 return value;
83 // Filter out non alphanumeric chars
84 String output = value.replaceAll("\\P{ASCII}+", "");
85 for (int i = 0; i < FILTER_SET.length; i++)
86 output = output.replaceAll(FILTER_SET[i][0], FILTER_SET[i][1]);
88 return output.trim();
91 /**
92 * Utility to create a filename from an input form title.
94 * @param value
95 * @return
97 public final static String createFilenameTitle(String value) {
98 if (value == null) {
99 return value;
101 // Filter out non alphanumeric chars
102 String output = value.replaceAll(" ", "_");
103 output = output.replaceAll("[^\\s_0-9a-zA-Z]", "");
104 return output.trim().toLowerCase();
108 * View keywords.
110 * @return
112 public final static String tagViewKeywords(
113 final String value, final String styleClass, final String urlPrefix, final String suffix) {
114 String input = createKeywords(value);
115 StringBuffer output = new StringBuffer();
116 if (input != null) {
117 String [] res = input.split("\\s+");
118 //String [] res = input.split("[^\\s+]");
119 for (int i = 0; i < res.length; i++) {
120 output.append("<a class=\"" + styleClass + "\" href=\"" + urlPrefix + res[i] + "\">");
121 output.append(res[i]);
122 output.append("</a>\n");
123 output.append(suffix);
126 return output.toString();