litesoft
@ 943
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 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.commonfoundation.base; /** * Takes a 'CamelCased' string and produces a space separated string. * <p/> * Leading and trailing spaces are removed. * First Character is upper cased. * More than one Upper case letter in a row are considered an abbreviation (and spaces are not injected before the non-1st upper case letter). * A Space is injected before an upper case letter. */ public class DeCamelizer { public static String resolve( String pID ) { return (pID == null) ? null : new DeCamelizer( pID.length() ).process( pID ); } private final StringBuilder sb; private DeCamelizer( int pAssumedLength ) { sb = new StringBuilder( pAssumedLength ); } private String process( String pID ) { return normalizeIntoBuffer( pID ) ? deCamelize() : null; } private boolean normalizeIntoBuffer( String pID ) { int zSpaces = -pID.length(); // This number will force all leading spaces to be skipped for ( int at = 0; at < pID.length(); at++ ) { char c = pID.charAt( at ); if ( (c == '_') || (c == ' ') ) { zSpaces++; } else { if ( zSpaces > 0 ) { sb.append( ' ' ); } sb.append( c ); zSpaces = 0; } } return (sb.length() != 0); } private String deCamelize() { boolean zUpperNext = true; // To make the first character Uppercase boolean zLastUpper = false; for ( int at = 0; at < sb.length(); at++ ) { char c = sb.charAt( at ); if ( zUpperNext ) { sb.setCharAt( at, Character.toUpperCase( c ) ); zLastUpper = true; zUpperNext = false; } else if ( Character.isUpperCase( c ) ) { if ( !zLastUpper ) { sb.insert( at++, ' ' ); } zLastUpper = true; } else { if ( c == ' ' ) { zUpperNext = true; } zLastUpper = false; } } return sb.toString(); } } |