1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.util; |
20 | |
|
21 | |
import java.io.FilterWriter; |
22 | |
import java.io.IOException; |
23 | |
import java.io.Writer; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class CDataEndEscapeFilterWriter extends FilterWriter |
30 | |
{ |
31 | |
private char c1; |
32 | |
private char c2; |
33 | |
private int pos; |
34 | |
|
35 | |
public CDataEndEscapeFilterWriter(Writer out) |
36 | |
{ |
37 | 0 | super(out); |
38 | 0 | pos = 0; |
39 | 0 | } |
40 | |
|
41 | |
@Override |
42 | |
public void write(int c) throws IOException |
43 | |
{ |
44 | 0 | super.write(c); |
45 | 0 | c1 = c2; |
46 | 0 | c2 = (char) c; |
47 | 0 | pos ++; |
48 | 0 | if (pos > 2) |
49 | |
{ |
50 | 0 | if (c1 == ']' && c2 == ']' && c == '>') |
51 | |
{ |
52 | |
|
53 | 0 | out.write("<![CDATA[]]]]><![CDATA[>"); |
54 | |
} |
55 | |
} |
56 | 0 | } |
57 | |
|
58 | |
@Override |
59 | |
public void write(char[] cbuf, int off, int len) throws IOException |
60 | |
{ |
61 | 0 | int index = off; |
62 | 0 | for (int i = 0; i < len; i++) |
63 | |
{ |
64 | 0 | char c = cbuf[off+i]; |
65 | 0 | if (c1 == ']' && c2 == ']' && c == '>') |
66 | |
{ |
67 | 0 | super.write(cbuf, index, i+1 - ( index - off ) ); |
68 | 0 | index = off+i+1; |
69 | 0 | out.write("<![CDATA[]]]]><![CDATA[>"); |
70 | |
} |
71 | 0 | c1 = c2; |
72 | 0 | c2 = c; |
73 | 0 | pos++; |
74 | |
} |
75 | 0 | if (index < off+len) |
76 | |
{ |
77 | 0 | super.write(cbuf, index, off+len-index); |
78 | |
} |
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public void write(String str, int off, int len) throws IOException |
83 | |
{ |
84 | 0 | int index = off; |
85 | 0 | for (int i = 0; i < len; i++) |
86 | |
{ |
87 | 0 | char c = str.charAt(off+i); |
88 | 0 | if (c1 == ']' && c2 == ']' && c == '>') |
89 | |
{ |
90 | 0 | super.write(str, index, i+1 - ( index - off ) ); |
91 | 0 | index = off+i+1; |
92 | 0 | out.write("<![CDATA[]]]]><![CDATA[>"); |
93 | |
} |
94 | 0 | c1 = c2; |
95 | 0 | c2 = c; |
96 | 0 | pos++; |
97 | |
} |
98 | 0 | if (index < off+len) |
99 | |
{ |
100 | 0 | super.write(str, index, off+len-index); |
101 | |
} |
102 | 0 | } |
103 | |
} |