01: /*
02: * Copyright (c) 2003-2008, Franz-Josef Elmer, All rights reserved.
03: *
04: * Redistribution and use in source and binary forms, with or without
05: * modification, are permitted provided that the following conditions are met:
06: *
07: * - Redistributions of source code must retain the above copyright notice,
08: * this list of conditions and the following disclaimer.
09: * - Redistributions in binary form must reproduce the above copyright notice,
10: * this list of conditions and the following disclaimer in the documentation
11: * and/or other materials provided with the distribution.
12: *
13: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24: */
25: package classycle.util;
26:
27: import java.io.BufferedReader;
28: import java.io.File;
29: import java.io.FileReader;
30: import java.io.IOException;
31:
32: /**
33: * Collection of useful static method concerning string manipulation.
34: *
35: * @author Franz-Josef Elmer
36: */
37: public class Text {
38: private static final String ESCAPE_CHARACTERS = "<>&\"'";
39: private static final String[] ESCAPE_SEQUENCES = new String[] {
40: "<", ">", "&", """, "'" };
41:
42: private Text() {
43: }
44:
45: /**
46: * Escapes special XML characters in the specified text.
47: * @param text Text to be escaped. Must be not <tt>null</tt>.
48: * @return copy of the text where the special XML characters has been
49: * replaced by the escape sequences.
50: */
51: public static String excapeForXML(String text) {
52: StringBuffer buffer = new StringBuffer();
53: for (int i = 0, n = text.length(); i < n; i++) {
54: char c = text.charAt(i);
55: int index = ESCAPE_CHARACTERS.indexOf(c);
56: if (index < 0) {
57: buffer.append(c);
58: } else {
59: buffer.append(ESCAPE_SEQUENCES[index]);
60: }
61: }
62: return new String(buffer);
63: }
64:
65: /**
66: * Reads multi-line text from the specified file.
67: * @param file Text file.
68: * @return read text file with standard Java newline characters.
69: * @throws IOException if some reading error occurs.
70: */
71: public static String readTextFile(File file) throws IOException {
72: StringBuffer buffer = new StringBuffer();
73: BufferedReader reader = new BufferedReader(new FileReader(file));
74: String line;
75: while ((line = reader.readLine()) != null) {
76: buffer.append(line).append('\n');
77: }
78: String result = new String(buffer);
79: return result;
80: }
81:
82: }
|