|
@@ -1,127 +1,127 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.aokeyhole.objects.support; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.aokeyhole.objects.*; |
5 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
6 |
|
- |
|
7 |
|
- |
import java.util.*; |
8 |
|
- |
|
9 |
|
- |
public abstract class StringArrayValueStringConverter implements ValueStringConverter { |
10 |
|
- |
@Override |
11 |
|
- |
public Object validateAndNormalize( Object pValue ) |
12 |
|
- |
throws ParseException { |
13 |
|
- |
return parse( pValue ); |
14 |
|
- |
} |
15 |
|
- |
|
16 |
|
- |
@Override |
17 |
|
- |
public String valueToString( Object pValue ) { |
18 |
|
- |
if ( pValue == null ) { |
19 |
|
- |
return ""; |
20 |
|
- |
} |
21 |
|
- |
return (pValue instanceof String) ? (String) pValue : join( (String[]) pValue ); |
22 |
|
- |
} |
23 |
|
- |
|
24 |
|
- |
public Object parse( Object pValue ) |
25 |
|
- |
throws ParseException { |
26 |
|
- |
if ( pValue == null ) { |
27 |
|
- |
return pValue; |
28 |
|
- |
} |
29 |
|
- |
if ( pValue instanceof String ) { |
30 |
|
- |
return validateEntries( split( (String) pValue ) ); |
31 |
|
- |
} |
32 |
|
- |
if ( pValue instanceof String[] ) { |
33 |
|
- |
return validateEntries( (String[]) pValue ); |
34 |
|
- |
} |
35 |
|
- |
throw new ParseException( "" + pValue + " is not a String Array" ); |
36 |
|
- |
} |
37 |
|
- |
|
38 |
|
- |
/** |
39 |
|
- |
* Will remove all leading and trailing blank entries |
40 |
|
- |
*/ |
41 |
|
- |
private String[] validateEntries( String[] pEntries ) { |
42 |
|
- |
if ( (pEntries != null) && (pEntries.length != 0) ) { |
43 |
|
- |
int addBlanks = 0; |
44 |
|
- |
List<String> zOptions = new ArrayList<String>( pEntries.length ); |
45 |
|
- |
for ( String zEntry : pEntries ) { |
46 |
|
- |
zEntry = validateEntry( zEntry ); |
47 |
|
- |
if ( zEntry != null ) { |
48 |
|
- |
if ( zEntry.indexOf( '\n' ) != -1 ) { |
49 |
|
- |
throw new ParseException( "Array Entry may not be multi-lined" ); |
50 |
|
- |
} |
51 |
|
- |
if ( zEntry.indexOf( '|' ) != -1 ) { |
52 |
|
- |
throw new ParseException( "Array Entry may not contain '|' as it is entry seperator" ); |
53 |
|
- |
} |
54 |
|
- |
if ( zEntry.length() != 0 ) { |
55 |
|
- |
for (; addBlanks > 0; addBlanks-- ) { |
56 |
|
- |
zOptions.add( "" ); |
57 |
|
- |
} |
58 |
|
- |
zOptions.add( zEntry ); |
59 |
|
- |
} else if ( !zOptions.isEmpty() ) { |
60 |
|
- |
addBlanks++; |
61 |
|
- |
} |
62 |
|
- |
} |
63 |
|
- |
} |
64 |
|
- |
if ( !zOptions.isEmpty() ) { |
65 |
|
- |
return zOptions.toArray( new String[zOptions.size()] ); |
66 |
|
- |
} |
67 |
|
- |
} |
68 |
|
- |
return null; |
69 |
|
- |
} |
70 |
|
- |
|
71 |
|
- |
abstract protected String validateEntry( String pOption ); |
72 |
|
- |
|
73 |
|
- |
private String join( String[] pStrings ) { |
74 |
|
- |
if ( pStrings.length == 0 ) { |
75 |
|
- |
return null; |
76 |
|
- |
} |
77 |
|
- |
int len = -1; |
78 |
|
- |
for ( String string : pStrings ) { |
79 |
|
- |
len += 1 + string.length(); |
80 |
|
- |
} |
81 |
|
- |
StringBuilder sb = new StringBuilder( len ); |
82 |
|
- |
sb.append( pStrings[0] ); |
83 |
|
- |
for ( int i = 1; i < pStrings.length; i++ ) { |
84 |
|
- |
sb.append( '|' ); |
85 |
|
- |
sb.append( pStrings[i] ); |
86 |
|
- |
} |
87 |
|
- |
return sb.toString(); |
88 |
|
- |
} |
89 |
|
- |
|
90 |
|
- |
public String[] split( String pString ) { |
91 |
|
- |
if ( pString != null ) { |
92 |
|
- |
if ( (pString = Strings.trimTrailingSpaces( pString )).length() != 0 ) { |
93 |
|
- |
return splitOn( pString, (pString.indexOf( '\n' ) != -1) ? '\n' : '|' ); |
94 |
|
- |
} |
95 |
|
- |
} |
96 |
|
- |
return null; |
97 |
|
- |
} |
98 |
|
- |
|
99 |
|
- |
private static String[] splitOn( String pString, char pSeperator ) { |
100 |
|
- |
List<String> collector = new ArrayList<String>(); |
101 |
|
- |
int from = 0; |
102 |
|
- |
for ( int at; -1 != (at = pString.indexOf( pSeperator, from )); from = at + 1 ) { |
103 |
|
- |
collector.add( pString.substring( from, at ) ); |
104 |
|
- |
} |
105 |
|
- |
collector.add( pString.substring( from ) ); |
106 |
|
- |
return collector.toArray( new String[collector.size()] ); |
107 |
|
- |
} |
108 |
|
- |
|
109 |
|
- |
public static StringArrayValueStringConverter TRIM_TRAILING = new StringArrayValueStringConverter() { |
110 |
|
- |
@Override |
111 |
|
- |
protected String validateEntry( String pOption ) { |
112 |
|
- |
return pOption != null ? Strings.trimTrailingSpaces( pOption ) : null; |
113 |
|
- |
} |
114 |
|
- |
}; |
115 |
|
- |
|
116 |
|
- |
public static StringArrayValueStringConverter TRIM_DROP_EMPTY = new StringArrayValueStringConverter() { |
117 |
|
- |
@Override |
118 |
|
- |
protected String validateEntry( String pOption ) { |
119 |
|
- |
if ( pOption != null ) { |
120 |
|
- |
if ( (pOption = pOption.trim()).length() != 0 ) { |
121 |
|
- |
return pOption; |
122 |
|
- |
} |
123 |
|
- |
} |
124 |
|
- |
return null; |
125 |
|
- |
} |
126 |
|
- |
}; |
127 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.aokeyhole.objects.support; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.aokeyhole.objects.*; |
|
5 |
+ |
import org.litesoft.commonfoundation.typeutils.*; |
|
6 |
+ |
|
|
7 |
+ |
import java.util.*; |
|
8 |
+ |
|
|
9 |
+ |
public abstract class StringArrayValueStringConverter implements ValueStringConverter { |
|
10 |
+ |
@Override |
|
11 |
+ |
public Object validateAndNormalize( Object pValue ) |
|
12 |
+ |
throws ParseException { |
|
13 |
+ |
return parse( pValue ); |
|
14 |
+ |
} |
|
15 |
+ |
|
|
16 |
+ |
@Override |
|
17 |
+ |
public String valueToString( Object pValue ) { |
|
18 |
+ |
if ( pValue == null ) { |
|
19 |
+ |
return ""; |
|
20 |
+ |
} |
|
21 |
+ |
return (pValue instanceof String) ? (String) pValue : join( (String[]) pValue ); |
|
22 |
+ |
} |
|
23 |
+ |
|
|
24 |
+ |
public Object parse( Object pValue ) |
|
25 |
+ |
throws ParseException { |
|
26 |
+ |
if ( pValue == null ) { |
|
27 |
+ |
return pValue; |
|
28 |
+ |
} |
|
29 |
+ |
if ( pValue instanceof String ) { |
|
30 |
+ |
return validateEntries( split( (String) pValue ) ); |
|
31 |
+ |
} |
|
32 |
+ |
if ( pValue instanceof String[] ) { |
|
33 |
+ |
return validateEntries( (String[]) pValue ); |
|
34 |
+ |
} |
|
35 |
+ |
throw new ParseException( "" + pValue + " is not a String Array" ); |
|
36 |
+ |
} |
|
37 |
+ |
|
|
38 |
+ |
/** |
|
39 |
+ |
* Will remove all leading and trailing blank entries |
|
40 |
+ |
*/ |
|
41 |
+ |
private String[] validateEntries( String[] pEntries ) { |
|
42 |
+ |
if ( (pEntries != null) && (pEntries.length != 0) ) { |
|
43 |
+ |
int addBlanks = 0; |
|
44 |
+ |
List<String> zOptions = new ArrayList<String>( pEntries.length ); |
|
45 |
+ |
for ( String zEntry : pEntries ) { |
|
46 |
+ |
zEntry = validateEntry( zEntry ); |
|
47 |
+ |
if ( zEntry != null ) { |
|
48 |
+ |
if ( zEntry.indexOf( '\n' ) != -1 ) { |
|
49 |
+ |
throw new ParseException( "Array Entry may not be multi-lined" ); |
|
50 |
+ |
} |
|
51 |
+ |
if ( zEntry.indexOf( '|' ) != -1 ) { |
|
52 |
+ |
throw new ParseException( "Array Entry may not contain '|' as it is entry seperator" ); |
|
53 |
+ |
} |
|
54 |
+ |
if ( zEntry.length() != 0 ) { |
|
55 |
+ |
for (; addBlanks > 0; addBlanks-- ) { |
|
56 |
+ |
zOptions.add( "" ); |
|
57 |
+ |
} |
|
58 |
+ |
zOptions.add( zEntry ); |
|
59 |
+ |
} else if ( !zOptions.isEmpty() ) { |
|
60 |
+ |
addBlanks++; |
|
61 |
+ |
} |
|
62 |
+ |
} |
|
63 |
+ |
} |
|
64 |
+ |
if ( !zOptions.isEmpty() ) { |
|
65 |
+ |
return zOptions.toArray( new String[zOptions.size()] ); |
|
66 |
+ |
} |
|
67 |
+ |
} |
|
68 |
+ |
return null; |
|
69 |
+ |
} |
|
70 |
+ |
|
|
71 |
+ |
abstract protected String validateEntry( String pOption ); |
|
72 |
+ |
|
|
73 |
+ |
private String join( String[] pStrings ) { |
|
74 |
+ |
if ( pStrings.length == 0 ) { |
|
75 |
+ |
return null; |
|
76 |
+ |
} |
|
77 |
+ |
int len = -1; |
|
78 |
+ |
for ( String string : pStrings ) { |
|
79 |
+ |
len += 1 + string.length(); |
|
80 |
+ |
} |
|
81 |
+ |
StringBuilder sb = new StringBuilder( len ); |
|
82 |
+ |
sb.append( pStrings[0] ); |
|
83 |
+ |
for ( int i = 1; i < pStrings.length; i++ ) { |
|
84 |
+ |
sb.append( '|' ); |
|
85 |
+ |
sb.append( pStrings[i] ); |
|
86 |
+ |
} |
|
87 |
+ |
return sb.toString(); |
|
88 |
+ |
} |
|
89 |
+ |
|
|
90 |
+ |
public String[] split( String pString ) { |
|
91 |
+ |
if ( pString != null ) { |
|
92 |
+ |
if ( (pString = Strings.trimTrailingSpaces( pString )).length() != 0 ) { |
|
93 |
+ |
return splitOn( pString, (pString.indexOf( '\n' ) != -1) ? '\n' : '|' ); |
|
94 |
+ |
} |
|
95 |
+ |
} |
|
96 |
+ |
return null; |
|
97 |
+ |
} |
|
98 |
+ |
|
|
99 |
+ |
private static String[] splitOn( String pString, char pSeperator ) { |
|
100 |
+ |
List<String> collector = new ArrayList<String>(); |
|
101 |
+ |
int from = 0; |
|
102 |
+ |
for ( int at; -1 != (at = pString.indexOf( pSeperator, from )); from = at + 1 ) { |
|
103 |
+ |
collector.add( pString.substring( from, at ) ); |
|
104 |
+ |
} |
|
105 |
+ |
collector.add( pString.substring( from ) ); |
|
106 |
+ |
return collector.toArray( new String[collector.size()] ); |
|
107 |
+ |
} |
|
108 |
+ |
|
|
109 |
+ |
public static StringArrayValueStringConverter TRIM_TRAILING = new StringArrayValueStringConverter() { |
|
110 |
+ |
@Override |
|
111 |
+ |
protected String validateEntry( String pOption ) { |
|
112 |
+ |
return pOption != null ? Strings.trimTrailingSpaces( pOption ) : null; |
|
113 |
+ |
} |
|
114 |
+ |
}; |
|
115 |
+ |
|
|
116 |
+ |
public static StringArrayValueStringConverter TRIM_DROP_EMPTY = new StringArrayValueStringConverter() { |
|
117 |
+ |
@Override |
|
118 |
+ |
protected String validateEntry( String pOption ) { |
|
119 |
+ |
if ( pOption != null ) { |
|
120 |
+ |
if ( (pOption = pOption.trim()).length() != 0 ) { |
|
121 |
+ |
return pOption; |
|
122 |
+ |
} |
|
123 |
+ |
} |
|
124 |
+ |
return null; |
|
125 |
+ |
} |
|
126 |
+ |
}; |
|
127 |
+ |
} |