KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRStringUtil


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28
29 /*
30  * Contributors:
31  * Gaganis Giorgos - gaganis@users.sourceforge.net
32  */

33 package net.sf.jasperreports.engine.util;
34
35
36
37 /**
38  * @author Teodor Danciu (teodord@users.sourceforge.net)
39  * @version $Id: JRStringUtil.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
40  */

41 public class JRStringUtil
42 {
43
44
45     /**
46      *
47      */

48     public static String JavaDoc xmlEncode(String JavaDoc text)
49     {
50         int length = text.length();
51         if (text != null && length > 0)
52         {
53             StringBuffer JavaDoc ret = new StringBuffer JavaDoc(length * 12 / 10);
54
55             int last = 0;
56             for (int i = 0; i < length; i ++)
57             {
58                 char c = text.charAt(i);
59                 switch (c)
60                 {
61         // case ' ' : ret.append("&nbsp;"); break;
62
case '&' :
63                         if (last < i)
64                         {
65                             ret.append(text.substring(last, i));
66                         }
67                         last = i + 1;
68                         
69                         ret.append("&amp;");
70                         break;
71                     case '>' :
72                         if (last < i)
73                         {
74                             ret.append(text.substring(last, i));
75                         }
76                         last = i + 1;
77                         
78                         ret.append("&gt;");
79                         break;
80                     case '<' :
81                         if (last < i)
82                         {
83                             ret.append(text.substring(last, i));
84                         }
85                         last = i + 1;
86                         
87                         ret.append("&lt;");
88                         break;
89                     case '\"' :
90                         if (last < i)
91                         {
92                             ret.append(text.substring(last, i));
93                         }
94                         last = i + 1;
95                         
96                         ret.append("&quot;");
97                         break;
98
99                     default :
100                         break;
101                 }
102             }
103
104             if (last < length)
105             {
106                 ret.append(text.substring(last));
107             }
108             
109             return ret.toString();
110         }
111
112         return text;
113     }
114
115
116     /**
117      *
118      */

119     public static String JavaDoc htmlEncode(String JavaDoc text)
120     {
121         int length = text.length();
122         if (text != null && length > 0)
123         {
124             StringBuffer JavaDoc ret = new StringBuffer JavaDoc(length * 12 / 10);
125
126             boolean isEncodeSpace = true;
127             int last = 0;
128             for (int i = 0; i < length; i ++)
129             {
130                 char c = text.charAt(i);
131                 switch (c)
132                 {
133                     case ' ' :
134                         if (isEncodeSpace)
135                         {
136                             if (last < i)
137                             {
138                                 ret.append(text.substring(last, i));
139                             }
140                             last = i + 1;
141                             
142                             ret.append("&nbsp;");
143                             isEncodeSpace = false;
144                         }
145                         else
146                         {
147                             isEncodeSpace = true;
148                         }
149                         break;
150                     case '&' :
151                         if (last < i)
152                         {
153                             ret.append(text.substring(last, i));
154                         }
155                         last = i + 1;
156                         
157                         ret.append("&amp;");
158                         isEncodeSpace = false;
159                         break;
160                     case '>' :
161                         if (last < i)
162                         {
163                             ret.append(text.substring(last, i));
164                         }
165                         last = i + 1;
166                         
167                         ret.append("&gt;");
168                         isEncodeSpace = false;
169                         break;
170                     case '<' :
171                         if (last < i)
172                         {
173                             ret.append(text.substring(last, i));
174                         }
175                         last = i + 1;
176                         
177                         ret.append("&lt;");
178                         isEncodeSpace = false;
179                         break;
180                     case '\"' :
181                         if (last < i)
182                         {
183                             ret.append(text.substring(last, i));
184                         }
185                         last = i + 1;
186                         
187                         ret.append("&quot;");
188                         isEncodeSpace = false;
189                         break;
190                     case '\n' :
191                         if (last < i)
192                         {
193                             ret.append(text.substring(last, i));
194                         }
195                         last = i + 1;
196                         
197                         ret.append("<br/>");
198                         isEncodeSpace = false;
199                         break;
200
201                     default :
202                         isEncodeSpace = false;
203                     break;
204                 }
205             }
206
207             if (last < length)
208             {
209                 ret.append(text.substring(last));
210             }
211
212             return ret.toString();
213         }
214
215         return text;
216     }
217
218
219     /**
220      * Takes a name and returns the same if it is a Java identifier;
221      * else it substitutes the illegal characters so that it can be an identifier
222      *
223      * @param name
224      */

225     public static String JavaDoc getLiteral(String JavaDoc name)
226     {
227         if (isValidLiteral(name))
228         {
229             return name;
230         }
231
232         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(name.length() + 5);
233         
234         char[] literalChars = new char[name.length()];
235         name.getChars(0, literalChars.length, literalChars, 0);
236         
237         for (int i = 0; i < literalChars.length; i++)
238         {
239             if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i]))
240             {
241                 buffer.append((int)literalChars[i]);
242             }
243             else if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i]))
244             {
245                 buffer.append((int)literalChars[i]);
246             }
247             else
248             {
249                 buffer.append(literalChars[i]);
250             }
251         }
252         
253         return buffer.toString();
254     }
255     
256     
257     /**
258      * Checks if the input is a valid Java literal
259      * @param literal
260      * @author Gaganis Giorgos (gaganis@users.sourceforge.net)
261      */

262     private static boolean isValidLiteral(String JavaDoc literal)
263     {
264         boolean result = true;
265         
266         char[] literalChars = new char[literal.length()];
267         literal.getChars(0, literalChars.length, literalChars, 0);
268         
269         for (int i = 0; i < literalChars.length; i++)
270         {
271             if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i]))
272             {
273                 result = false;
274                 break;
275             }
276             
277             if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i]))
278             {
279                 result = false;
280                 break;
281             }
282         }
283         
284         return result;
285     }
286
287     
288     /**
289      * Replaces DOS end of line (CRLF) with Unix end of line (LF).
290      *
291      * @param text the text
292      * @return the text with CRLF replaced by LF; if no CRLF was found, the same object is returned.
293      */

294     public static String JavaDoc replaceDosEOL(String JavaDoc text)
295     {
296         if (text == null || text.length() < 2)
297         {
298             return text;
299         }
300         
301         int length = text.length();
302         char[] chars = text.toCharArray();
303         int r = 0;
304         for (int i = 0; i < length; ++i)
305         {
306             char ch = chars[i];
307             if (!(ch == '\r' && i + 1 < length && chars[i + 1] == '\n'))
308             {
309                 if (r > 0)
310                 {
311                     chars[i - r] = ch;
312                 }
313             }
314             else
315             {
316                 ++r;
317             }
318         }
319
320         return r > 0 ? new String JavaDoc(chars, 0, length - r) : text;
321     }
322 }
323
Popular Tags