litesoft
@ 950
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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.aokeyhole.objects.support; import org.litesoft.aokeyhole.objects.*; import org.litesoft.commonfoundation.typeutils.*; import java.util.*; public abstract class StringArrayValueStringConverter implements ValueStringConverter { @Override public Object validateAndNormalize( Object pValue ) throws ParseException { return parse( pValue ); } @Override public String valueToString( Object pValue ) { if ( pValue == null ) { return ""; } return (pValue instanceof String) ? (String) pValue : join( (String[]) pValue ); } public Object parse( Object pValue ) throws ParseException { if ( pValue == null ) { return pValue; } if ( pValue instanceof String ) { return validateEntries( split( (String) pValue ) ); } if ( pValue instanceof String[] ) { return validateEntries( (String[]) pValue ); } throw new ParseException( "" + pValue + " is not a String Array" ); } /** * Will remove all leading and trailing blank entries */ private String[] validateEntries( String[] pEntries ) { if ( (pEntries != null) && (pEntries.length != 0) ) { int addBlanks = 0; List<String> zOptions = new ArrayList<String>( pEntries.length ); for ( String zEntry : pEntries ) { zEntry = validateEntry( zEntry ); if ( zEntry != null ) { if ( zEntry.indexOf( '\n' ) != -1 ) { throw new ParseException( "Array Entry may not be multi-lined" ); } if ( zEntry.indexOf( '|' ) != -1 ) { throw new ParseException( "Array Entry may not contain '|' as it is entry seperator" ); } if ( zEntry.length() != 0 ) { for (; addBlanks > 0; addBlanks-- ) { zOptions.add( "" ); } zOptions.add( zEntry ); } else if ( !zOptions.isEmpty() ) { addBlanks++; } } } if ( !zOptions.isEmpty() ) { return zOptions.toArray( new String[zOptions.size()] ); } } return null; } abstract protected String validateEntry( String pOption ); private String join( String[] pStrings ) { if ( pStrings.length == 0 ) { return null; } int len = -1; for ( String string : pStrings ) { len += 1 + string.length(); } StringBuilder sb = new StringBuilder( len ); sb.append( pStrings[0] ); for ( int i = 1; i < pStrings.length; i++ ) { sb.append( '|' ); sb.append( pStrings[i] ); } return sb.toString(); } public String[] split( String pString ) { if ( pString != null ) { if ( (pString = Strings.trimTrailingSpaces( pString )).length() != 0 ) { return splitOn( pString, (pString.indexOf( '\n' ) != -1) ? '\n' : '|' ); } } return null; } private static String[] splitOn( String pString, char pSeperator ) { List<String> collector = new ArrayList<String>(); int from = 0; for ( int at; -1 != (at = pString.indexOf( pSeperator, from )); from = at + 1 ) { collector.add( pString.substring( from, at ) ); } collector.add( pString.substring( from ) ); return collector.toArray( new String[collector.size()] ); } public static StringArrayValueStringConverter TRIM_TRAILING = new StringArrayValueStringConverter() { @Override protected String validateEntry( String pOption ) { return pOption != null ? Strings.trimTrailingSpaces( pOption ) : null; } }; public static StringArrayValueStringConverter TRIM_DROP_EMPTY = new StringArrayValueStringConverter() { @Override protected String validateEntry( String pOption ) { if ( pOption != null ) { if ( (pOption = pOption.trim()).length() != 0 ) { return pOption; } } return null; } }; } |
Commits for litesoft/trunk/Java/KeyHole/src/org/litesoft/aokeyhole/objects/support/StringArrayValueStringConverter.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
802 Diff | GeorgeS | Wed 15 Aug, 2012 04:04:47 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
17 Diff | GeorgeS | Thu 18 Feb, 2010 23:54:44 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |