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 super(out);
38 pos = 0;
39 }
40
41 @Override
42 public void write(int c) throws IOException
43 {
44 super.write(c);
45 c1 = c2;
46 c2 = (char) c;
47 pos ++;
48 if (pos > 2)
49 {
50 if (c1 == ']' && c2 == ']' && c == '>')
51 {
52
53 out.write("<![CDATA[]]]]><![CDATA[>");
54 }
55 }
56 }
57
58 @Override
59 public void write(char[] cbuf, int off, int len) throws IOException
60 {
61 int index = off;
62 for (int i = 0; i < len; i++)
63 {
64 char c = cbuf[off+i];
65 if (c1 == ']' && c2 == ']' && c == '>')
66 {
67 super.write(cbuf, index, i+1 - ( index - off ) );
68 index = off+i+1;
69 out.write("<![CDATA[]]]]><![CDATA[>");
70 }
71 c1 = c2;
72 c2 = c;
73 pos++;
74 }
75 if (index < off+len)
76 {
77 super.write(cbuf, index, off+len-index);
78 }
79 }
80
81 @Override
82 public void write(String str, int off, int len) throws IOException
83 {
84 int index = off;
85 for (int i = 0; i < len; i++)
86 {
87 char c = str.charAt(off+i);
88 if (c1 == ']' && c2 == ']' && c == '>')
89 {
90 super.write(str, index, i+1 - ( index - off ) );
91 index = off+i+1;
92 out.write("<![CDATA[]]]]><![CDATA[>");
93 }
94 c1 = c2;
95 c2 = c;
96 pos++;
97 }
98 if (index < off+len)
99 {
100 super.write(str, index, off+len-index);
101 }
102 }
103 }