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