1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -175,9 +175,8 @@ else if (isEscaped(value, startIndex)) { // Not a valid index, accumulate and sk
175
175
}
176
176
177
177
private SimplePlaceholderPart createSimplePlaceholderPart (String text ) {
178
- String [] keyAndDefault = splitKeyAndDefault (text );
179
- return ((keyAndDefault != null ) ? new SimplePlaceholderPart (text , keyAndDefault [0 ], keyAndDefault [1 ]) :
180
- new SimplePlaceholderPart (text , text , null ));
178
+ ParsedSection section = parseSection (text );
179
+ return new SimplePlaceholderPart (text , section .key (), section .fallback ());
181
180
}
182
181
183
182
private NestedPlaceholderPart createNestedPlaceholderPart (String text , List <Part > parts ) {
@@ -193,28 +192,32 @@ private NestedPlaceholderPart createNestedPlaceholderPart(String text, List<Part
193
192
}
194
193
else {
195
194
String candidate = part .text ();
196
- String [] keyAndDefault = splitKeyAndDefault (candidate );
197
- if (keyAndDefault != null ) {
198
- keyParts .add (new TextPart (keyAndDefault [0 ]));
199
- if (keyAndDefault [1 ] != null ) {
200
- defaultParts .add (new TextPart (keyAndDefault [1 ]));
201
- }
195
+ ParsedSection section = parseSection (candidate );
196
+ keyParts .add (new TextPart (section .key ()));
197
+ if (section .fallback () != null ) {
198
+ defaultParts .add (new TextPart (section .fallback ()));
202
199
defaultParts .addAll (parts .subList (i + 1 , parts .size ()));
203
200
return new NestedPlaceholderPart (text , keyParts , defaultParts );
204
201
}
205
- else {
206
- keyParts .add (part );
207
- }
208
202
}
209
203
}
210
- // No separator found
211
- return new NestedPlaceholderPart (text , parts , null );
204
+ return new NestedPlaceholderPart (text , keyParts , null );
212
205
}
213
206
214
- @ Nullable
215
- private String [] splitKeyAndDefault (String value ) {
207
+ /**
208
+ * Parse an input value that may contain a separator character and return a
209
+ * {@link ParsedValue}. If a valid separator character has been identified, the
210
+ * given {@code value} is split between a {@code key} and a {@code fallback}. If not,
211
+ * only the {@code key} is set.
212
+ * <p>
213
+ * The returned key may be different from the original value as escaped
214
+ * separators, if any, are resolved.
215
+ * @param value the value to parse
216
+ * @return the parsed section
217
+ */
218
+ private ParsedSection parseSection (String value ) {
216
219
if (this .separator == null || !value .contains (this .separator )) {
217
- return null ;
220
+ return new ParsedSection ( value , null ) ;
218
221
}
219
222
int position = 0 ;
220
223
int index = value .indexOf (this .separator , position );
@@ -231,11 +234,11 @@ private String[] splitKeyAndDefault(String value) {
231
234
buffer .append (value , position , index );
232
235
String key = buffer .toString ();
233
236
String fallback = value .substring (index + this .separator .length ());
234
- return new String [] { key , fallback } ;
237
+ return new ParsedSection ( key , fallback ) ;
235
238
}
236
239
}
237
240
buffer .append (value , position , value .length ());
238
- return new String [] { buffer .toString (), null } ;
241
+ return new ParsedSection ( buffer .toString (), null ) ;
239
242
}
240
243
241
244
private static void addText (String value , int start , int end , LinkedList <Part > parts ) {
@@ -293,6 +296,10 @@ private boolean isEscaped(String value, int index) {
293
296
return (this .escape != null && index > 0 && value .charAt (index - 1 ) == this .escape );
294
297
}
295
298
299
+ record ParsedSection (String key , @ Nullable String fallback ) {
300
+
301
+ }
302
+
296
303
297
304
/**
298
305
* Provide the necessary context to handle and resolve underlying placeholders.
0 commit comments