|
@@ -22,12 +22,10 @@ |
22 |
22 |
|
* |
23 |
23 |
|
* @author georgs |
24 |
24 |
|
*/ |
25 |
|
- |
public class HelperE13nResolver |
26 |
|
- |
{ |
|
25 |
+ |
public class HelperE13nResolver { |
27 |
26 |
|
private static final int MAX_DEPTH_FLAG_AS_CYCLE = 5; |
28 |
27 |
|
|
29 |
|
- |
public interface NonCompleting |
30 |
|
- |
{ |
|
28 |
+ |
public interface NonCompleting { |
31 |
29 |
|
/** |
32 |
30 |
|
* @param key !empty |
33 |
31 |
|
* |
|
@@ -44,11 +42,9 @@ |
44 |
42 |
|
* |
45 |
43 |
|
* @return proxied cast to a NonCompleting |
46 |
44 |
|
*/ |
47 |
|
- |
public static NonCompleting validateProxy( E13nResolver proxied ) |
48 |
|
- |
{ |
|
45 |
+ |
public static NonCompleting validateProxy( E13nResolver proxied ) { |
49 |
46 |
|
Assert.notNull( "Proxied E13nResolver", proxied ); |
50 |
|
- |
if ( proxied instanceof NonCompleting ) |
51 |
|
- |
{ |
|
47 |
+ |
if ( proxied instanceof NonCompleting ) { |
52 |
48 |
|
return (NonCompleting) proxied; |
53 |
49 |
|
} |
54 |
50 |
|
throw new IllegalArgumentException( "Proxied E13nResolver not a NonCompleting instance: " + proxied.getClass().getName() ); |
|
@@ -60,8 +56,7 @@ |
60 |
56 |
|
* @param data !null |
61 |
57 |
|
* @param resolver !null |
62 |
58 |
|
*/ |
63 |
|
- |
public static String resolveDataWith( E13nData data, NonCompleting resolver ) |
64 |
|
- |
{ |
|
59 |
+ |
public static String resolveDataWith( E13nData data, NonCompleting resolver ) { |
65 |
60 |
|
Assert.notNull( "E13nData data", data ); |
66 |
61 |
|
String key = data.getTemplateIdCode(); |
67 |
62 |
|
return processResolvedWithSubKeys( key, resolver.resolveWithoutCompleting( key ), resolver, data.getTemplateSubstitutionNamedValues() ); |
|
@@ -73,14 +68,12 @@ |
73 |
68 |
|
* @param key !null |
74 |
69 |
|
* @param resolver !null |
75 |
70 |
|
*/ |
76 |
|
- |
public static String resolveEnumWith( Enum<?> key, NonCompleting resolver ) |
77 |
|
- |
{ |
|
71 |
+ |
public static String resolveEnumWith( Enum<?> key, NonCompleting resolver ) { |
78 |
72 |
|
Assert.notNull( "Enum key", key ); |
79 |
73 |
|
String keyName = key.name(); |
80 |
74 |
|
String prefix = ObjectUtils.getSimpleClassName( key ); |
81 |
75 |
|
String value = resolver.resolveWithoutCompleting( prefix + E13nResolver.PREFIX_SEP + keyName ); |
82 |
|
- |
if ( value == null ) |
83 |
|
- |
{ |
|
76 |
+ |
if ( value == null ) { |
84 |
77 |
|
value = resolver.resolveWithoutCompleting( keyName ); |
85 |
78 |
|
} |
86 |
79 |
|
return processResolvedWithSubKeys( keyName, value, resolver, Collections.<String, SubstitutionValue>emptyMap() ); |
|
@@ -92,8 +85,7 @@ |
92 |
85 |
|
* @param key !null |
93 |
86 |
|
* @param resolver !null |
94 |
87 |
|
*/ |
95 |
|
- |
public static String resolveStringWith( String key, NonCompleting resolver ) |
96 |
|
- |
{ |
|
88 |
+ |
public static String resolveStringWith( String key, NonCompleting resolver ) { |
97 |
89 |
|
key = Assert.noEmpty( "String key", key ); |
98 |
90 |
|
return processResolvedWithSubKeys( key, resolver.resolveWithoutCompleting( key ), resolver, Collections.<String, SubstitutionValue>emptyMap() ); |
99 |
91 |
|
} |
|
@@ -120,14 +112,11 @@ |
120 |
112 |
|
* |
121 |
113 |
|
* @return resolved string (!null) |
122 |
114 |
|
*/ |
123 |
|
- |
private static String processResolvedWithSubKeys( String key, String value, NonCompleting resolver, Map<String, SubstitutionValue> overrides ) |
124 |
|
- |
{ |
125 |
|
- |
if ( value == null ) |
126 |
|
- |
{ |
|
115 |
+ |
private static String processResolvedWithSubKeys( String key, String value, NonCompleting resolver, Map<String, SubstitutionValue> overrides ) { |
|
116 |
+ |
if ( value == null ) { |
127 |
117 |
|
StringBuilder sb = new StringBuilder().append( '[' ).append( key ); |
128 |
118 |
|
char prefix = ':'; |
129 |
|
- |
for ( String name : overrides.keySet() ) |
130 |
|
- |
{ |
|
119 |
+ |
for ( String name : overrides.keySet() ) { |
131 |
120 |
|
sb.append( prefix ).append( name ).append( "='" ).append( E13nResolver.INIT ).append( name ).append( E13nResolver.FINI ).append( "'" ); |
132 |
121 |
|
prefix = ','; |
133 |
122 |
|
} |
|
@@ -147,36 +136,27 @@ |
147 |
136 |
|
* |
148 |
137 |
|
* @return resolved string (!null) |
149 |
138 |
|
*/ |
150 |
|
- |
private static String processSubKeys( int depth, String value, NonCompleting resolver, Map<String, SubstitutionValue> overrides ) |
151 |
|
- |
{ |
|
139 |
+ |
private static String processSubKeys( int depth, String value, NonCompleting resolver, Map<String, SubstitutionValue> overrides ) { |
152 |
140 |
|
int finiAt = value.indexOf( E13nResolver.FINI ); |
153 |
|
- |
if ( finiAt == -1 ) |
154 |
|
- |
{ |
|
141 |
+ |
if ( finiAt == -1 ) { |
155 |
142 |
|
return value; // Happy case, no sub-keys |
156 |
143 |
|
} |
157 |
144 |
|
StringBuilder sb = new StringBuilder(); |
158 |
|
- |
do |
159 |
|
- |
{ |
|
145 |
+ |
do { |
160 |
146 |
|
// extract each sub-key |
161 |
147 |
|
String left = value.substring( 0, ++finiAt ); |
162 |
148 |
|
value = value.substring( finiAt ); |
163 |
149 |
|
int initAt = left.indexOf( E13nResolver.INIT ); |
164 |
|
- |
if ( initAt == -1 ) |
165 |
|
- |
{ |
|
150 |
+ |
if ( initAt == -1 ) { |
166 |
151 |
|
sb.append( left ); // Dangling "FINI" (No "INIT") |
167 |
|
- |
} |
168 |
|
- |
else |
169 |
|
- |
{ |
|
152 |
+ |
} else { |
170 |
153 |
|
sb.append( left.substring( 0, initAt ) ); |
171 |
154 |
|
String wrappedKey = left.substring( initAt ); |
172 |
155 |
|
// process the potential "wrapped" sub-key |
173 |
156 |
|
String substitutionText = wrappedResolveWith( depth, wrappedKey, resolver, overrides ); |
174 |
|
- |
if ( substitutionText != null ) |
175 |
|
- |
{ |
|
157 |
+ |
if ( substitutionText != null ) { |
176 |
158 |
|
sb.append( substitutionText ); // Success - resolved the key! |
177 |
|
- |
} |
178 |
|
- |
else |
179 |
|
- |
{ |
|
159 |
+ |
} else { |
180 |
160 |
|
sb.append( wrappedKey ); // Couldn't resolve - simply add the wrapped key (not a key?) |
181 |
161 |
|
} |
182 |
162 |
|
} |
|
@@ -199,14 +179,11 @@ |
199 |
179 |
|
* |
200 |
180 |
|
* @return resolved string (!null) |
201 |
181 |
|
*/ |
202 |
|
- |
private static String wrappedResolveWith( int depth, String wrappedKey, NonCompleting resolver, Map<String, SubstitutionValue> overrides ) |
203 |
|
- |
{ |
204 |
|
- |
if ( E13nResolver.DONT_SHOW_SUBSTITUTION_ID.equals( wrappedKey ) ) |
205 |
|
- |
{ // Special sub-key for Empty String |
|
182 |
+ |
private static String wrappedResolveWith( int depth, String wrappedKey, NonCompleting resolver, Map<String, SubstitutionValue> overrides ) { |
|
183 |
+ |
if ( E13nResolver.DONT_SHOW_SUBSTITUTION_ID.equals( wrappedKey ) ) { // Special sub-key for Empty String |
206 |
184 |
|
return ""; // Empty String |
207 |
185 |
|
} |
208 |
|
- |
if ( E13nResolver.SPACE_SUBSTITUTION_ID.equals( wrappedKey ) ) |
209 |
|
- |
{ // Special sub-key for Space |
|
186 |
+ |
if ( E13nResolver.SPACE_SUBSTITUTION_ID.equals( wrappedKey ) ) { // Special sub-key for Space |
210 |
187 |
|
return " "; // Space |
211 |
188 |
|
} |
212 |
189 |
|
// Unwrap & attempt to resolve |
|
@@ -233,18 +210,14 @@ |
233 |
210 |
|
* |
234 |
211 |
|
* @return resolved string (!null) |
235 |
212 |
|
*/ |
236 |
|
- |
private static String unwrappedResolveWith( int depth, String key, NonCompleting resolver, Map<String, SubstitutionValue> overrides ) |
237 |
|
- |
{ |
238 |
|
- |
if ( depth > MAX_DEPTH_FLAG_AS_CYCLE ) |
239 |
|
- |
{ |
|
213 |
+ |
private static String unwrappedResolveWith( int depth, String key, NonCompleting resolver, Map<String, SubstitutionValue> overrides ) { |
|
214 |
+ |
if ( depth > MAX_DEPTH_FLAG_AS_CYCLE ) { |
240 |
215 |
|
return "[?" + key + " - Cycle?]"; |
241 |
216 |
|
} |
242 |
217 |
|
SubstitutionValue substitutionValue = overrides.get( key ); |
243 |
|
- |
if ( substitutionValue != null ) |
244 |
|
- |
{ |
|
218 |
+ |
if ( substitutionValue != null ) { |
245 |
219 |
|
String value = substitutionValue.getValue(); |
246 |
|
- |
if ( substitutionValue.isUserData() ) |
247 |
|
- |
{ |
|
220 |
+ |
if ( substitutionValue.isUserData() ) { |
248 |
221 |
|
return value; // User Value : No further processing |
249 |
222 |
|
} |
250 |
223 |
|
// !User Data : Assume Value is a Key |