litesoft
@ 964
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / common / shared / utils / StringUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
package com.temp.common.shared.utils; import java.util.*; /** * Utility methods around Strings (either as parameters or return values) to * reduce the code in other places (e.g. the Activities). * * @author georgs */ public class StringUtils { public static final String[] EMPTY_ARRAY = new String[0]; public static final String EMPTY_STRING = ""; public static final String CRNL = "\r\n"; public static final char CRchar = '\r'; public static final char NLchar = '\n'; public static String join( Iterable<?> iterable, String joinString ) { Iterator<?> iter = iterable.iterator(); if ( iter.hasNext() ) { StringBuffer buf = new StringBuffer( translateToString( iter.next() ) ); while ( iter.hasNext() ) { buf.append( joinString ); buf.append( translateToString( iter.next() ) ); } return buf.toString(); } return ""; } private static String translateToString( Object next ) { return next == null ? "<NULL>" : next.toString(); } public static boolean isBlank( String... strings ) { if ( strings == null ) { return true; } for ( String string : strings ) { if ( string == null || string.trim().isEmpty() ) { return true; } } return false; } public static String noEmpty( String s ) { if ( s != null ) { if ( (s = s.trim()).isEmpty() ) { s = null; } } return s; } public static String noEmpty( String s, String defaultValue ) { return deNull( noEmpty( s ), defaultValue ); } public static String deNull( String s ) { return deNull( s, "" ); } public static String deNull( String s, String defaultValue ) { return (s != null) ? s : defaultValue; } public static String deNullToString( Object value, Object defaultValue ) { return deNull( noEmptyToString( value ), noEmptyToString( defaultValue ) ); } public static String noEmptyToString( Object value ) { return noEmpty( nullOKtoString( value ) ); } public static String nullOKtoString( Object value ) { return (value == null) ? null : value.toString(); } public static String nullToEmptytoString( Object value ) { return (value == null) ? "" : value.toString(); } public static String firstLines( int maxLines, String message ) { if ( null == (message = normalizeNewLines( message )) ) { return message; } if ( maxLines < 1 ) { return null; } StringBuilder sb = new StringBuilder(); int from = 0; for ( int at; -1 != (at = message.indexOf( NLchar, from )); from = at + 1 ) { String part = message.substring( from, at ); if ( sb.length() != 0 ) { sb.append( NLchar ); } if ( !isBlank( part ) ) { sb.append( part ); } if ( sb.length() != 0 ) { if ( --maxLines == 0 ) { return sb.toString(); } } } // Not full so add end as Line String end = message.substring( from ); if ( !isBlank( end ) ) { if ( sb.length() != 0 ) { sb.append( NLchar ); } sb.append( end ); } return sb.toString(); } public static String normalizeNewLines( String message ) { message = replaceAll( message, CRNL, NLchar ); message = replaceAll( message, CRchar, NLchar ); return message; } public static String replaceAll( String message, char of, char to ) { return (message == null) ? message : message.replace( of, to ); } /** * Non-RegEx version, as Java RegEx & Browser RegEx are NOT the same! */ public static String replaceAll( String message, String of, String to ) { if ( (message != null) && (of != null) && (to != null) ) { int removeFor = of.length(); int adjustBy = to.length(); for ( int at, from = 0; -1 != (at = message.indexOf( of, from )); from = at + adjustBy ) { message = message.substring( 0, at ) + to + message.substring( at + removeFor ); } } return message; } /** * Simple String to char of the replaceAll */ public static String replaceAll( String message, String of, char to ) { if ( (message != null) && (of != null) ) { int removeFor = of.length(); for ( int at, from = 0; -1 != (at = message.indexOf( of, from )); from = at + 1 ) { message = message.substring( 0, at ) + to + message.substring( at + removeFor ); } } return message; } /** * Return the simple class name for an Object. * <p/> * This behavior is built into Java, but not currently supported in GWT */ public static String simpleClassNameFor( Object o ) { if ( o == null ) { return "null"; } String name = "." + o.getClass().getName(); return name.substring( name.lastIndexOf( '.' ) + 1 ); } public static boolean toBoolean( String booleanAsString ) { return Boolean.TRUE.toString().equalsIgnoreCase( noEmpty( booleanAsString ) ); } private static String runningMaxIndentSpaces = ""; public static synchronized String indent( int indent ) { // 4 spaces per... if ( indent < 1 ) { return ""; } int spacesNeeded = indent + indent + indent + indent; if ( runningMaxIndentSpaces.length() < spacesNeeded ) { StringBuilder sb = new StringBuilder( spacesNeeded ); for ( int i = spacesNeeded; i > 0; i-- ) { sb.append( ' ' ); } return runningMaxIndentSpaces = sb.toString(); } return runningMaxIndentSpaces.substring( 0, spacesNeeded ); } /** * Wrap the text (into multiple lines as needed) such that no 'line' exceeds 'maxCharactersPerLine' in length. * <p/> * The wrapping algorithm is four fold (applied in the following order): * <li>1st New Line within the maxCharactersPerLine</li> * <li>last space within the maxCharactersPerLine</li> * <li>just beyond the last punctuation (non-AlphaNumeric character) within the 'maxCharactersPerLine'</li> * <li>hard wrapped such that the line length is 'within the maxCharactersPerLine'</li> * * @param text to Wrap. * @param maxCharactersPerLine Don't wrap if null or < 1! * * @return !null result with wrapped text (lines) if maxCharactersPerLine > 0, (otherwise don't wrap) */ public static String wrap( String text, Integer maxCharactersPerLine ) { text = StringUtils.deNull( text ).trim(); if ( (maxCharactersPerLine == null) || (maxCharactersPerLine < 1) ) { return text; } StringBuilder sb = new StringBuilder(); while ( maxCharactersPerLine < text.length() ) { int breakAt = findBreakAtPosition( text, maxCharactersPerLine ); sb.append( text.substring( 0, breakAt ) ).append( NLchar ); text = text.substring( breakAt ).trim(); } return sb.append( text ).toString(); } private static int findBreakAtPosition( String text, int maxCharactersPerLine ) { int at = text.indexOf( NLchar ); if ( at != -1 && (at <= maxCharactersPerLine) ) { return at; } text = text.substring( 0, maxCharactersPerLine ); at = text.lastIndexOf( ' ' ); if ( at != -1 ) { return at; } for ( at = maxCharactersPerLine - 1; 0 <= at; at-- ) { if ( !Character.isLetterOrDigit( text.charAt( at ) ) ) { return at + 1; } } return maxCharactersPerLine; } public static int indexOfControlCharacter( String text ) { if ( text != null ) { for ( int i = 0; i < text.length(); i++ ) { if ( CharacterUtils.isControlChar( text.charAt( i ) ) ) { return i; } } } return -1; } public static String makeIdFriendly( String text ) { text = noEmpty( text ); if ( text == null ) { return null; } StringBuilder sb = new StringBuilder(); boolean pendingUnderscore = false; for ( int i = 0; i < text.length(); i++ ) { char c = text.charAt( i ); if ( !CharacterUtils.is7BitAlphaNumeric( c ) ) { pendingUnderscore = true; } else { if ( pendingUnderscore && (sb.length() != 0) ) { sb.append( '_' ); } pendingUnderscore = false; sb.append( c ); } } return noEmpty( sb.toString() ); } /** * return the index of the first character that is unacceptable for that position to be part of an Identifier. * <p/> * An Identifier is a String that stars with a 7Bit Alpha or underscore, and is followed by any number of 7bit AlphaNumerics or underscores. * * @param toCheck not null or empty * * @return -1 if OK, otherwise the 'bad' character index. */ public static int checkIdentifier( String toCheck ) { if ( !CharacterUtils.is7BitAlphaUnderScore( toCheck.charAt( 0 ) ) ) { return 0; } for ( int i = 1; i < toCheck.length(); i++ ) { if ( !CharacterUtils.isAlphaNumericUnderScore7BitAscii( toCheck.charAt( i ) ) ) { return i; } } return -1; } /** * NBSPs are converted to a space. */ public static String convertNBSPsToSpaces( String text ) { if ( text != null ) { int at = text.indexOf( '&' ); if ( at != -1 ) { int sourceTextDelta = 0; String indexe = text.toLowerCase(); // . . . . . 0123456 for ( int from = at; -1 != (at = indexe.indexOf( " ", from )); from = at + 6 ) { int sourceAt = at + sourceTextDelta; text = text.substring( 0, sourceAt ) + ' ' + text.substring( sourceAt + 6 ); sourceTextDelta -= 5; } } } return text; } } |
Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/common/shared/utils/StringUtils.java
Revision | Author | Commited | Message |
---|---|---|---|
964 | GeorgeS | Fri 01 Aug, 2014 03:18:23 +0000 | ! |