Pages

2014년 2월 21일 금요일

[JAVA] JAVA 문자열 스크립트 필터링

1
2
3
4
5
6
7
8
9
10
11
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    /**
     * <PRE>
     * 스크립트 필터링
     * ClassName    : ValidUtil
     * MethodName   : scriptFilterEnc
     * 처리내용       :
     * - Biz Logic (간략)
     *   스크립트 필터링
     * </PRE>
     * @return String
     * @param value         필터링 할 값
     */
    public static String scriptFilterEnc(String value) {
        if (value == null) {
            return null;
        }        
        
        StringBuffer result = new StringBuffer(value.length());
        
        for(int i = 0; i < value.length(); i++) {
            switch (value.charAt(i)) {
            case '<':
                result.append("&lt;");
                break;
            case '>': 
                result.append("&gt;");
                break;
            case '"': 
                result.append("&quot;");
                break;
            case '\'': 
                result.append("&#39;");
                break;
            case '%': 
                result.append("&#37;");
                break;
            case ';': 
                result.append("&#59;");
                break;
            case '(': 
                result.append("&#40;");
                break;
            case ')': 
                result.append("&#41;");
                break;
            case '&': 
                result.append("&amp;");
                break;
            case '+':
                result.append("&#43;");
                break;
            case '#':
                result.append("&#35;");
                break;
            case '/':
                result.append("&#47;");
                break;
            case '.':
                result.append("&#46;");
                break;       
            default:
                result.append(value.charAt(i));
                break;
            }        
        }
        return result.toString();
    }

댓글 없음:

댓글 쓰기