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