KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > anakia > Escape


1 package org.apache.velocity.anakia;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 /**
20  * This class is for escaping CDATA sections. The code was
21  * "borrowed" from the JDOM code. I also added in escaping
22  * of the " -> " character.
23  *
24  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
25  * @version $Id: Escape.java,v 1.4.14.1 2004/03/03 23:22:04 geirm Exp $
26  */

27 public class Escape
28 {
29     /**
30      * Empty constructor
31      */

32     public Escape()
33     {
34         // left blank on purpose
35
}
36     
37     /**
38      * Do the escaping.
39      */

40     public static final String JavaDoc getText(String JavaDoc st)
41     {
42         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
43         char[] block = st.toCharArray();
44         String JavaDoc stEntity = null;
45         int i, last;
46
47         for (i=0, last=0; i < block.length; i++)
48         {
49             switch(block[i])
50             {
51                 case '<' :
52                     stEntity = "&lt;";
53                     break;
54                 case '>' :
55                     stEntity = "&gt;";
56                     break;
57                 case '&' :
58                     stEntity = "&amp;";
59                     break;
60                 case '"' :
61                     stEntity = "&quot;";
62                     break;
63                 default :
64                     /* no-op */ ;
65             }
66             if (stEntity != null)
67             {
68                 buff.append(block, last, i - last);
69                 buff.append(stEntity);
70                 stEntity = null;
71                 last = i + 1;
72             }
73         }
74         if(last < block.length)
75         {
76             buff.append(block, last, i - last);
77         }
78         return buff.toString();
79     }
80 }
81
Popular Tags