KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > util > Text


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - 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 JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 /**
33  * Collection of useful static method concerning string manipulation.
34  *
35  * @author Franz-Josef Elmer
36  */

37 public class Text
38 {
39   private static final String JavaDoc ESCAPE_CHARACTERS = "<>&\"'";
40   private static final String JavaDoc[] ESCAPE_SEQUENCES = new String JavaDoc[] {
41           "&lt;", "&gt;", "&amp;", "&quot;", "&apos;"};
42   private Text() {}
43   
44   /**
45    * Escapes special XML characters in the specified text.
46    * @param text Text to be escaped. Must be not <tt>null</tt>.
47    * @return copy of the text where the special XML characters has been
48    * replaced by the escape sequences.
49    */

50   public static String JavaDoc excapeForXML(String JavaDoc text)
51   {
52     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
53     for (int i = 0, n = text.length(); i < n; i++)
54     {
55       char c = text.charAt(i);
56       int index = ESCAPE_CHARACTERS.indexOf(c);
57       if (index < 0)
58       {
59         buffer.append(c);
60       } else
61       {
62         buffer.append(ESCAPE_SEQUENCES[index]);
63       }
64     }
65     return new String JavaDoc(buffer);
66   }
67
68   /**
69    * Reads multi-line text from the specified file.
70    * @param file Text file.
71    * @return read text file with standard Java newline characters.
72    * @throws IOException if some reading error occurs.
73    */

74   public static String JavaDoc readTextFile(File JavaDoc file) throws IOException JavaDoc
75   {
76     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
77     BufferedReader JavaDoc reader
78         = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
79     String JavaDoc line;
80     while ((line = reader.readLine()) != null)
81     {
82       buffer.append(line).append('\n');
83     }
84     String JavaDoc result = new String JavaDoc(buffer);
85     return result;
86   }
87
88 }
89
Popular Tags