1 package org.apache.velocity.anakia;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 /**
23 * This class is for escaping CDATA sections. The code was
24 * "borrowed" from the JDOM code. Also included is escaping
25 * the " -> " character and the conversion of newlines
26 * to the platform line separator.
27 *
28 * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
29 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
30 * @version $Id: Escape.java 463298 2006-10-12 16:10:32Z henning $
31 */
32 public class Escape
33 {
34 /**
35 *
36 */
37 public static final String LINE_SEPARATOR = System.getProperty("line.separator");
38
39 /**
40 * Empty constructor
41 */
42 public Escape()
43 {
44 // left blank on purpose
45 }
46
47 /**
48 * Do the escaping.
49 * @param st
50 * @return The escaped text.
51 */
52 public static final String getText(String st)
53 {
54 StringBuffer buff = new StringBuffer();
55 char[] block = st.toCharArray();
56 String stEntity = null;
57 int i, last;
58
59 for (i=0, last=0; i < block.length; i++)
60 {
61 switch(block[i])
62 {
63 case '<' :
64 stEntity = "<";
65 break;
66 case '>' :
67 stEntity = ">";
68 break;
69 case '&' :
70 stEntity = "&";
71 break;
72 case '"' :
73 stEntity = """;
74 break;
75 case '\n' :
76 stEntity = LINE_SEPARATOR;
77 break;
78 default :
79 /* no-op */
80 break;
81 }
82 if (stEntity != null)
83 {
84 buff.append(block, last, i - last);
85 buff.append(stEntity);
86 stEntity = null;
87 last = i + 1;
88 }
89 }
90 if(last < block.length)
91 {
92 buff.append(block, last, i - last);
93 }
94 return buff.toString();
95 }
96 }