目的
テキストエリアから入力された文字列を、
適切な処理をして問題なくDBに格納をする。
実装のために考えたこと
- javascriptのスクリプトタグのエスケープ
- サロゲートペアの文字列の文字数カウントの変更
ソースコード
スクリプトタグのエスケープ処理
public static String removeTag(String inputedText) {
String text = inputedText;
if (text != null) {
text = text.replaceAll("<", "<");
text = text.replaceAll(">", ">");
}
return text;
}
サロゲートペアの文字列チェックの変更
サロゲートペアの文字列も1文字として扱いたいため。
public static void hasCorrectTextLength(String tweetText) {
BreakIterator bi = BreakIterator.getCharacterInstance();
bi.setText(tweetText);
int textCount = 0;
while (bi.next() != BreakIterator.DONE) {
textCount++;
}
if (textCount > 140) {
throw new ActionMessagesException("ツイートの長さが最大値(140)を超えています。", false);
}
}
改善点
BreakIterator
はほとんど見たことがないとの評価を受けた。
実装をする際には、一度どういったメソッドがはいっているのか調べておいた方がいいかもしれない。