KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > util > JTextUtil


1 /*
2     =========================================================================
3
4     This module is developed and maintained by PlanetaMessenger.org.
5     Specs, New and updated versions can be found in
6     http://www.planetamessenger.org
7     If you want contact the Team please send a email to Project Manager
8     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
9
10     Copyright (C) since 2001 by PlanetaMessenger.org
11     
12     This library is free software; you can redistribute it and/or
13     modify it under the terms of the GNU Lesser General Public
14     License as published by the Free Software Foundation; either
15     version 2.1 of the License, or (at your option) any later version.
16
17     This library is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20     Lesser General Public License for more details.
21
22     You should have received a copy of the GNU Lesser General Public
23     License along with this library; if not, write to the Free Software
24     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26     =========================================================================
27 */

28 /**
29  *
30  * $Id: JTextUtil.java,v 1.5 2007/01/28 17:39:23 popolony2k Exp $
31  * $Author: popolony2k $
32  * $Name: $
33  * $Revision: 1.5 $
34  * $State: Exp $
35  *
36  */

37
38 /*
39  * Common html utility methods.
40  * Created on 9-Nov-06 by Peter Maloney
41  */

42 package org.planetamessenger.util;
43
44
45 /**
46  * Common text utility methods.
47  *
48  */

49 public class JTextUtil {
50   /**
51    * Escapes special characters into html codes. This should be used when
52    * placing text in a component that uses html, but the raw text should be used
53    * everywhere else, such as the database.
54    *
55    * @param str the plain text to convert to html
56    * @return str encoded into html
57    */

58   public static String JavaDoc encodeHtml(String JavaDoc str) {
59     if( str == null )
60       return null;
61
62     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
63
64     for( int i = 0; i < str.length(); i++ ) {
65       char ch = str.charAt( i );
66       if( ch == '&' ) {
67         buf.append( "&amp;" );
68       } else if( ch == '\n' || ch == '\r' ) { // handle \r (mac), \n (unix),
69
// \r\n (winblows)
70
if( ch == '\r' && i + 1 < str.length() && str.charAt( i + 1 ) == '\n' ) {
71           // handle winblows stupid double newline
72
i++;
73         }
74         buf.append( "<br/>" );
75       } else if( ch == '<' ) {
76         buf.append( "&lt;" );
77       } else if( ch == '>' ) {
78         buf.append( "&gt;" );
79       } else if( ch == '\"' ) {
80         buf.append( "&quot;" );
81       } else if( ch == '\'' ) {
82         buf.append( "&#" ).append( (int) ch ).append( ";" );
83       } else {
84         buf.append( ch );
85       }
86     }
87
88     return buf.toString();
89   }
90   
91   /** Escapes all quotes using backslashes.
92    *
93    * @param str input string
94    * @param quote the type of quote to escape
95    * @param includeQuotesInResult true to wrap the return value with quote characters
96    * @return str with quotes escaped with a backslash
97    */

98   public static String JavaDoc quote(String JavaDoc str, char quote, boolean includeQuotesInResult) {
99     if( str == null )
100       return null;
101     
102     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
103     
104     for(int i=0; i<str.length(); i++) {
105       char ch = str.charAt(i);
106       
107       if( ch == '\\' ) {
108         buf.append("\\\\");
109       }else if( ch == quote ) {
110         buf.append('\\').append(quote);
111       }else{
112         buf.append( ch );
113       }
114     }
115     
116     if( includeQuotesInResult ) {
117       buf.insert(0,quote);
118       buf.append(quote);
119     }
120     
121     return buf.toString();
122   }
123 }
Popular Tags