1 28 37 38 42 package org.planetamessenger.util; 43 44 45 49 public class JTextUtil { 50 58 public static String encodeHtml(String str) { 59 if( str == null ) 60 return null; 61 62 StringBuffer buf = new StringBuffer (); 63 64 for( int i = 0; i < str.length(); i++ ) { 65 char ch = str.charAt( i ); 66 if( ch == '&' ) { 67 buf.append( "&" ); 68 } else if( ch == '\n' || ch == '\r' ) { if( ch == '\r' && i + 1 < str.length() && str.charAt( i + 1 ) == '\n' ) { 71 i++; 73 } 74 buf.append( "<br/>" ); 75 } else if( ch == '<' ) { 76 buf.append( "<" ); 77 } else if( ch == '>' ) { 78 buf.append( ">" ); 79 } else if( ch == '\"' ) { 80 buf.append( """ ); 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 98 public static String quote(String str, char quote, boolean includeQuotesInResult) { 99 if( str == null ) 100 return null; 101 102 StringBuffer buf = new StringBuffer (); 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 |