01: package org.apache.anakia;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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 524478 2007-03-31 20:51:49Z wglass $
31: */
32: public class Escape {
33: /**
34: *
35: */
36: public static final String LINE_SEPARATOR = System
37: .getProperty("line.separator");
38:
39: /**
40: * Empty constructor
41: */
42: public Escape() {
43: // left blank on purpose
44: }
45:
46: /**
47: * Do the escaping.
48: * @param st
49: * @return The escaped text.
50: */
51: public static final String getText(String st) {
52: StringBuffer buff = new StringBuffer();
53: char[] block = st.toCharArray();
54: String stEntity = null;
55: int i, last;
56:
57: for (i = 0, last = 0; i < block.length; i++) {
58: switch (block[i]) {
59: case '<':
60: stEntity = "<";
61: break;
62: case '>':
63: stEntity = ">";
64: break;
65: case '&':
66: stEntity = "&";
67: break;
68: case '"':
69: stEntity = """;
70: break;
71: case '\n':
72: stEntity = LINE_SEPARATOR;
73: break;
74: default:
75: /* no-op */
76: break;
77: }
78: if (stEntity != null) {
79: buff.append(block, last, i - last);
80: buff.append(stEntity);
81: stEntity = null;
82: last = i + 1;
83: }
84: }
85: if (last < block.length) {
86: buff.append(block, last, i - last);
87: }
88: return buff.toString();
89: }
90: }
|