/
StringLookupFactory.java
1319 lines (1245 loc) · 44.2 KB
/
StringLookupFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache license, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license for the specific language governing permissions and
* limitations under the license.
*/
package org.apache.commons.text.lookup;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.apache.commons.text.StringSubstitutor;
/**
* Create instances of string lookups or access singleton string lookups implemented in this package.
* <p>
* The "classic" look up is {@link #mapStringLookup(Map)}.
* </p>
* <p>
* The methods for variable interpolation (A.K.A. variable substitution) are:
* </p>
* <ul>
* <li>{@link #interpolatorStringLookup()}.</li>
* <li>{@link #interpolatorStringLookup(Map)}.</li>
* <li>{@link #interpolatorStringLookup(StringLookup)}.</li>
* <li>{@link #interpolatorStringLookup(Map, StringLookup, boolean)}.</li>
* </ul>
* <p>
* Unless explicitly requested otherwise, a set of default lookups are included for convenience with these
* variable interpolation methods. These defaults are listed in the table below. However, the exact lookups
* included can be configured through the use of the {@value #DEFAULT_STRING_LOOKUPS_PROPERTY} system property.
* If present, this system property will be parsed as a comma-separated list of lookup names, with the names
* being those defined by the {@link DefaultStringLookup} enum. For example, setting this system property to
* {@code "BASE64_ENCODER,ENVIRONMENT"} will only include the
* {@link DefaultStringLookup#BASE64_ENCODER BASE64_ENCODER} and {@link DefaultStringLookup#ENVIRONMENT ENVIRONMENT}
* lookups. Setting the property to the empty string will cause no defaults to be configured.
* Note that not all lookups defined here and in {@link DefaultStringLookup} are included by default.
* Specifically, lookups that can execute code (e.g., {@link DefaultStringLookup#SCRIPT SCRIPT}) and those
* that can result in contact with remote servers (e.g., {@link DefaultStringLookup#URL URL} and
* {@link DefaultStringLookup#DNS DNS}) are not included by default. The current set of default lookups can
* be accessed directly with {@link #addDefaultStringLookups(Map)}.
* </p>
* <table>
* <caption>Default String Lookups</caption>
* <tr>
* <th>Key</th>
* <th>Interface</th>
* <th>Factory Method</th>
* <th>Since</th>
* </tr>
* <tr>
* <td>{@value #KEY_BASE64_DECODER}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #base64DecoderStringLookup()}</td>
* <td>1.6</td>
* </tr>
* <tr>
* <td>{@value #KEY_BASE64_ENCODER}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #base64EncoderStringLookup()}</td>
* <td>1.6</td>
* </tr>
* <tr>
* <td>{@value #KEY_CONST}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #constantStringLookup()}</td>
* <td>1.5</td>
* </tr>
* <tr>
* <td>{@value #KEY_DATE}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #dateStringLookup()}</td>
* <td>1.5</td>
* </tr>
* <tr>
* <td>{@value #KEY_ENV}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #environmentVariableStringLookup()}</td>
* <td>1.3</td>
* </tr>
* <tr>
* <td>{@value #KEY_FILE}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #fileStringLookup()}</td>
* <td>1.5</td>
* </tr>
* <tr>
* <td>{@value #KEY_JAVA}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #javaPlatformStringLookup()}</td>
* <td>1.5</td>
* </tr>
* <tr>
* <td>{@value #KEY_LOCALHOST}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #localHostStringLookup()}</td>
* <td>1.3</td>
* </tr>
* <tr>
* <td>{@value #KEY_PROPERTIES}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #propertiesStringLookup()}</td>
* <td>1.5</td>
* </tr>
* <tr>
* <td>{@value #KEY_RESOURCE_BUNDLE}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #resourceBundleStringLookup()}</td>
* <td>1.6</td>
* </tr>
* <tr>
* <td>{@value #KEY_SYS}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #systemPropertyStringLookup()}</td>
* <td>1.3</td>
* </tr>
* <tr>
* <td>{@value #KEY_URL_DECODER}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #urlDecoderStringLookup()}</td>
* <td>1.5</td>
* </tr>
* <tr>
* <td>{@value #KEY_URL_ENCODER}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #urlEncoderStringLookup()}</td>
* <td>1.5</td>
* </tr>
* <tr>
* <td>{@value #KEY_XML}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #xmlStringLookup()}</td>
* <td>1.5</td>
* </tr>
* </table>
*
* <table>
* <caption>Additional String Lookups (not included by default)</caption>
* <tr>
* <th>Key</th>
* <th>Interface</th>
* <th>Factory Method</th>
* <th>Since</th>
* </tr>
* <tr>
* <td>{@value #KEY_DNS}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #dnsStringLookup()}</td>
* <td>1.8</td>
* </tr>
* <tr>
* <td>{@value #KEY_URL}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #urlStringLookup()}</td>
* <td>1.5</td>
* </tr>
* <tr>
* <td>{@value #KEY_SCRIPT}</td>
* <td>{@link StringLookup}</td>
* <td>{@link #scriptStringLookup()}</td>
* <td>1.5</td>
* </tr>
* </table>
*
* <p>
* This class also provides functional lookups used as building blocks for other lookups.
* <table>
* <caption>Functional String Lookups</caption>
* <tr>
* <th>Interface</th>
* <th>Factory Method</th>
* <th>Since</th>
* </tr>
* <tr>
* <td>{@link BiStringLookup}</td>
* <td>{@link #biFunctionStringLookup(BiFunction)}</td>
* <td>1.9</td>
* </tr>
* <tr>
* <td>{@link StringLookup}</td>
* <td>{@link #functionStringLookup(Function)}</td>
* <td>1.9</td>
* </tr>
* </table>
*
* @since 1.3
*/
public final class StringLookupFactory {
/**
* Defines the singleton for this class.
*/
public static final StringLookupFactory INSTANCE = new StringLookupFactory();
/**
* Decodes Base64 Strings.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.base64DecoderStringLookup().lookup("SGVsbG9Xb3JsZCE=");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${base64Decoder:SGVsbG9Xb3JsZCE=} ..."));
* </pre>
* <p>
* The above examples convert {@code "SGVsbG9Xb3JsZCE="} to {@code "HelloWorld!"}.
* </p>
*/
static final FunctionStringLookup<String> INSTANCE_BASE64_DECODER = FunctionStringLookup
.on(key -> new String(Base64.getDecoder().decode(key), StandardCharsets.ISO_8859_1));
/**
* Encodes Base64 Strings.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.base64EncoderStringLookup().lookup("HelloWorld!");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${base64Encoder:HelloWorld!} ..."));
* </pre>
* <p>
* The above examples convert {@code "HelloWorld!"} to {@code "SGVsbG9Xb3JsZCE="}.
* </p>
* Defines the singleton for this class.
*/
static final FunctionStringLookup<String> INSTANCE_BASE64_ENCODER = FunctionStringLookup
.on(key -> Base64.getEncoder().encodeToString(key.getBytes(StandardCharsets.ISO_8859_1)));
/**
* Looks up keys from environment variables.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.environmentVariableStringLookup().lookup("USER");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${env:USER} ..."));
* </pre>
* <p>
* The above examples convert (on Linux) {@code "USER"} to the current user name. On Windows 10, you would use
* {@code "USERNAME"} to the same effect.
* </p>
*/
static final FunctionStringLookup<String> INSTANCE_ENVIRONMENT_VARIABLES = FunctionStringLookup.on(System::getenv);
/**
* Defines the FunctionStringLookup singleton that always returns null.
*/
static final FunctionStringLookup<String> INSTANCE_NULL = FunctionStringLookup.on(key -> null);
/**
* Defines the FunctionStringLookup singleton for looking up system properties.
*/
static final FunctionStringLookup<String> INSTANCE_SYSTEM_PROPERTIES = FunctionStringLookup.on(System::getProperty);
/**
* Default lookup key for interpolation {@value #KEY_BASE64_DECODER}.
*
* @since 1.6
*/
public static final String KEY_BASE64_DECODER = "base64Decoder";
/**
* Default lookup key for interpolation {@value #KEY_BASE64_ENCODER}.
*
* @since 1.6
*/
public static final String KEY_BASE64_ENCODER = "base64Encoder";
/**
* Default lookup key for interpolation {@value #KEY_CONST}.
*
* @since 1.6
*/
public static final String KEY_CONST = "const";
/**
* Default lookup key for interpolation {@value #KEY_DATE}.
*
* @since 1.6
*/
public static final String KEY_DATE = "date";
/**
* Default lookup key for interpolation {@value #KEY_DNS}.
*
* @since 1.8
*/
public static final String KEY_DNS = "dns";
/**
* Default lookup key for interpolation {@value #KEY_ENV}.
*
* @since 1.6
*/
public static final String KEY_ENV = "env";
/**
* Default lookup key for interpolation {@value #KEY_FILE}.
*
* @since 1.6
*/
public static final String KEY_FILE = "file";
/**
* Default lookup key for interpolation {@value #KEY_JAVA}.
*
* @since 1.6
*/
public static final String KEY_JAVA = "java";
/**
* Default lookup key for interpolation {@value #KEY_LOCALHOST}.
*
* @since 1.6
*/
public static final String KEY_LOCALHOST = "localhost";
/**
* Default lookup key for interpolation {@value #KEY_PROPERTIES}.
*
* @since 1.6
*/
public static final String KEY_PROPERTIES = "properties";
/**
* Default lookup key for interpolation {@value #KEY_RESOURCE_BUNDLE}.
*
* @since 1.6
*/
public static final String KEY_RESOURCE_BUNDLE = "resourceBundle";
/**
* Default lookup key for interpolation {@value #KEY_SCRIPT}.
*
* @since 1.6
*/
public static final String KEY_SCRIPT = "script";
/**
* Default lookup key for interpolation {@value #KEY_SYS}.
*
* @since 1.6
*/
public static final String KEY_SYS = "sys";
/**
* Default lookup key for interpolation {@value #KEY_URL}.
*
* @since 1.6
*/
public static final String KEY_URL = "url";
/**
* Default lookup key for interpolation {@value #KEY_URL_DECODER}.
*
* @since 1.6
*/
public static final String KEY_URL_DECODER = "urlDecoder";
/**
* Default lookup key for interpolation {@value #KEY_URL_ENCODER}.
*
* @since 1.6
*/
public static final String KEY_URL_ENCODER = "urlEncoder";
/**
* Default lookup key for interpolation {@value #KEY_XML}.
*
* @since 1.6
*/
public static final String KEY_XML = "xml";
/**
* Name of the system property used to determine the string lookups added by the
* {@link #addDefaultStringLookups(Map)} method. Use of this property is only required
* in cases where the set of default lookups must be modified. (See the class documentation
* for details.)
*
* @since 1.10
*/
public static final String DEFAULT_STRING_LOOKUPS_PROPERTY =
"org.apache.commons.text.lookup.StringLookupFactory.defaultStringLookups";
/**
* Clears any static resources.
*
* @since 1.5
*/
public static void clear() {
ConstantStringLookup.clear();
}
/**
* Returns the given map if the input is non-null or an empty immutable map if the input is null.
*
* @param <K> the class of the map keys
* @param <V> the class of the map values
* @param map The map to test
* @return the given map if the input is non-null or an empty immutable map if the input is null.
*/
static <K, V> Map<K, V> toMap(final Map<K, V> map) {
return map == null ? Collections.emptyMap() : map;
}
/**
* No need to build instances for now.
*/
private StringLookupFactory() {
// empty
}
/**
* Adds the default string lookups for this class to {@code stringLookupMap}. The default string
* lookups are a set of built-in lookups added for convenience during string interpolation. The
* defaults may be configured using the {@value #DEFAULT_STRING_LOOKUPS_PROPERTY} system property.
* See the class documentation for details and a list of lookups.
*
* @param stringLookupMap the map of string lookups to edit.
* @since 1.5
*/
public void addDefaultStringLookups(final Map<String, StringLookup> stringLookupMap) {
if (stringLookupMap != null) {
stringLookupMap.putAll(DefaultStringLookupsHolder.INSTANCE.getDefaultStringLookups());
}
}
/**
* Returns the Base64DecoderStringLookup singleton instance to decode Base64 strings.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.base64DecoderStringLookup().lookup("SGVsbG9Xb3JsZCE=");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${base64Decoder:SGVsbG9Xb3JsZCE=} ..."));
* </pre>
* <p>
* The above examples convert {@code "SGVsbG9Xb3JsZCE="} to {@code "HelloWorld!"}.
* </p>
*
* @return The Base64DecoderStringLookup singleton instance.
* @since 1.5
*/
public StringLookup base64DecoderStringLookup() {
return StringLookupFactory.INSTANCE_BASE64_DECODER;
}
/**
* Returns the Base64EncoderStringLookup singleton instance to encode strings to Base64.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.base64EncoderStringLookup().lookup("HelloWorld!");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${base64Encoder:HelloWorld!} ..."));
* </pre>
* <p>
* The above examples convert {@code } to {@code "SGVsbG9Xb3JsZCE="}.
* </p>
*
* @return The Base64EncoderStringLookup singleton instance.
* @since 1.6
*/
public StringLookup base64EncoderStringLookup() {
return StringLookupFactory.INSTANCE_BASE64_ENCODER;
}
/**
* Returns the Base64DecoderStringLookup singleton instance to decode Base64 strings.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.base64DecoderStringLookup().lookup("SGVsbG9Xb3JsZCE=");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${base64Decoder:SGVsbG9Xb3JsZCE=} ..."));
* </pre>
* <p>
* The above examples convert {@code "SGVsbG9Xb3JsZCE="} to {@code "HelloWorld!"}.
* </p>
*
* @return The Base64DecoderStringLookup singleton instance.
* @since 1.5
* @deprecated Use {@link #base64DecoderStringLookup()}.
*/
@Deprecated
public StringLookup base64StringLookup() {
return StringLookupFactory.INSTANCE_BASE64_DECODER;
}
/**
* Returns a new function-based lookup where the request for a lookup is answered by applying the function with a
* lookup key.
*
* @param <R> the function return type.
* @param <U> the function's second parameter type.
* @param biFunction the function.
* @return a new MapStringLookup.
* @since 1.9
*/
public <R, U> BiStringLookup<U> biFunctionStringLookup(final BiFunction<String, U, R> biFunction) {
return BiFunctionStringLookup.on(biFunction);
}
/**
* Returns the ConstantStringLookup singleton instance to look up the value of a fully-qualified static final value.
* <p>
* Sometimes it is necessary in a configuration file to refer to a constant defined in a class. This can be done
* with this lookup implementation. Variable names must be in the format {@code apackage.AClass.AFIELD}. The
* {@code lookup(String)} method will split the passed in string at the last dot, separating the fully qualified
* class name and the name of the constant (i.e. <b>static final</b>) member field. Then the class is loaded and the
* field's value is obtained using reflection.
* </p>
* <p>
* Once retrieved values are cached for fast access. This class is thread-safe. It can be used as a standard (i.e.
* global) lookup object and serve multiple clients concurrently.
* </p>
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.constantStringLookup().lookup("java.awt.event.KeyEvent.VK_ESCAPE");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${const:java.awt.event.KeyEvent.VK_ESCAPE} ..."));
* </pre>
* <p>
* The above examples convert {@code java.awt.event.KeyEvent.VK_ESCAPE} to {@code "27"}.
* </p>
*
* @return The ConstantStringLookup singleton instance.
* @since 1.5
*/
public StringLookup constantStringLookup() {
return ConstantStringLookup.INSTANCE;
}
/**
* Returns the DateStringLookup singleton instance to format the current date with the format given in the key in a
* format compatible with {@link java.text.SimpleDateFormat}.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.dateStringLookup().lookup("yyyy-MM-dd");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${date:yyyy-MM-dd} ..."));
* </pre>
* <p>
* The above examples convert {@code "yyyy-MM-dd"} to todays's date, for example, {@code "2019-08-04"}.
* </p>
*
* @return The DateStringLookup singleton instance.
*/
public StringLookup dateStringLookup() {
return DateStringLookup.INSTANCE;
}
/**
* Returns the DnsStringLookup singleton instance where the lookup key is one of:
* <ul>
* <li><b>name</b>: for the local host name, for example {@code EXAMPLE} but also {@code EXAMPLE.apache.org}.</li>
* <li><b>canonical-name</b>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li>
* <li><b>address</b>: for the local host address, for example {@code 192.168.56.1}.</li>
* </ul>
*
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.dnsStringLookup().lookup("address|apache.org");
* </pre>
* <p>
* When used through a {@link StringSubstitutor}, this lookup must either be added programmatically
* (as below) or enabled as a default lookup using the {@value #DEFAULT_STRING_LOOKUPS_PROPERTY} system property
* (see class documentation).
* </p>
*
* <pre>
* Map<String, StringLookup> lookupMap = new HashMap<>();
* lookupMap.put("dns", StringLookupFactory.INSTANCE.dnsStringLookup());
*
* StringLookup variableResolver = StringLookupFactory.INSTANCE.interpolatorStringLookup(lookupMap, null, false);
*
* new StringSubstitutor(variableResolver).replace("... ${dns:address|apache.org} ...");
* </pre>
* <p>
* The above examples convert {@code "address|apache.org"} to the IP address of {@code apache.org}.
* </p>
*
* @return the DnsStringLookup singleton instance.
* @since 1.8
*/
public StringLookup dnsStringLookup() {
return DnsStringLookup.INSTANCE;
}
/**
* Returns the EnvironmentVariableStringLookup singleton instance where the lookup key is an environment variable
* name.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.environmentVariableStringLookup().lookup("USER");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${env:USER} ..."));
* </pre>
* <p>
* The above examples convert (on Linux) {@code "USER"} to the current user name. On Windows 10, you would use
* {@code "USERNAME"} to the same effect.
* </p>
*
* @return The EnvironmentVariableStringLookup singleton instance.
*/
public StringLookup environmentVariableStringLookup() {
return StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES;
}
/**
* Returns the FileStringLookup singleton instance.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.fileStringLookup().lookup("UTF-8:com/domain/document.properties");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${file:UTF-8:com/domain/document.properties} ..."));
* </pre>
* <p>
* The above examples convert {@code "UTF-8:com/domain/document.properties"} to the contents of the file.
* </p>
*
* @return The FileStringLookup singleton instance.
* @since 1.5
*/
public StringLookup fileStringLookup() {
return FileStringLookup.INSTANCE;
}
/**
* Returns a new function-based lookup where the request for a lookup is answered by applying the function with a
* lookup key.
*
* @param <R> the function return type.
* @param function the function.
* @return a new MapStringLookup.
* @since 1.9
*/
public <R> StringLookup functionStringLookup(final Function<String, R> function) {
return FunctionStringLookup.on(function);
}
/**
* Returns a {@link InterpolatorStringLookup} containing the configured
* {@link #addDefaultStringLookups(Map) default lookups}. See the class documentation for
* details on how these defaults are configured.
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.interpolatorStringLookup().lookup("${sys:os.name}, ${env:USER}");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${sys:os.name}, ${env:USER} ..."));
* </pre>
* <p>
* The above examples convert {@code "${sys:os.name}, ${env:USER}"} to the OS name and Linux user name.
* </p>
*
* @return the default {@link InterpolatorStringLookup}.
*/
public StringLookup interpolatorStringLookup() {
return InterpolatorStringLookup.INSTANCE;
}
/**
* Returns a new InterpolatorStringLookup. If {@code addDefaultLookups} is {@code true}, the configured
* {@link #addDefaultStringLookups(Map) default lookups} are included in addition to the ones
* provided in {@code stringLookupMap}. (See the class documentation for details on how default lookups
* are configured.)
*
* @param stringLookupMap the map of string lookups.
* @param defaultStringLookup the default string lookup; this lookup is used when a variable cannot be
* resolved using the lookups in {@code stringLookupMap} or the configured default lookups (if enabled)
* @param addDefaultLookups whether to use default lookups as described above.
* @return a new InterpolatorStringLookup.
* @since 1.4
*/
public StringLookup interpolatorStringLookup(final Map<String, StringLookup> stringLookupMap,
final StringLookup defaultStringLookup, final boolean addDefaultLookups) {
return new InterpolatorStringLookup(stringLookupMap, defaultStringLookup, addDefaultLookups);
}
/**
* Returns a new InterpolatorStringLookup using the given key-value pairs and the configured
* {@link #addDefaultStringLookups(Map) default lookups} to resolve variables. (See the class
* documentation for details on how default lookups are configured.)
*
* @param <V> the value type the default string lookup's map.
* @param map the default map for string lookups.
* @return a new InterpolatorStringLookup.
*/
public <V> StringLookup interpolatorStringLookup(final Map<String, V> map) {
return new InterpolatorStringLookup(map);
}
/**
* Returns a new InterpolatorStringLookup using the given lookup and the configured
* {@link #addDefaultStringLookups(Map) default lookups} to resolve variables. (See the class
* documentation for details on how default lookups are configured.)
*
* @param defaultStringLookup the default string lookup.
* @return a new InterpolatorStringLookup.
*/
public StringLookup interpolatorStringLookup(final StringLookup defaultStringLookup) {
return new InterpolatorStringLookup(defaultStringLookup);
}
/**
* Returns the JavaPlatformStringLookup singleton instance. Looks up keys related to Java: Java version, JRE
* version, VM version, and so on.
* <p>
* The lookup keys with examples are:
* </p>
* <ul>
* <li><b>version</b>: "Java version 1.8.0_181"</li>
* <li><b>runtime</b>: "Java(TM) SE Runtime Environment (build 1.8.0_181-b13) from Oracle Corporation"</li>
* <li><b>vm</b>: "Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)"</li>
* <li><b>os</b>: "Windows 10 10.0, architecture: amd64-64"</li>
* <li><b>hardware</b>: "processors: 4, architecture: amd64-64, instruction sets: amd64"</li>
* <li><b>locale</b>: "default locale: en_US, platform encoding: iso-8859-1"</li>
* </ul>
*
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.javaPlatformStringLookup().lookup("version");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${java:version} ..."));
* </pre>
* <p>
* The above examples convert {@code "version"} to the current VM version, for example,
* {@code "Java version 1.8.0_181"}.
* </p>
*
* @return The JavaPlatformStringLookup singleton instance.
*/
public StringLookup javaPlatformStringLookup() {
return JavaPlatformStringLookup.INSTANCE;
}
/**
* Returns the LocalHostStringLookup singleton instance where the lookup key is one of:
* <ul>
* <li><b>name</b>: for the local host name, for example {@code EXAMPLE}.</li>
* <li><b>canonical-name</b>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li>
* <li><b>address</b>: for the local host address, for example {@code 192.168.56.1}.</li>
* </ul>
*
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.localHostStringLookup().lookup("canonical-name");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${localhost:canonical-name} ..."));
* </pre>
* <p>
* The above examples convert {@code "canonical-name"} to the current host name, for example,
* {@code "EXAMPLE.apache.org"}.
* </p>
*
* @return The DateStringLookup singleton instance.
*/
public StringLookup localHostStringLookup() {
return LocalHostStringLookup.INSTANCE;
}
/**
* Returns a new map-based lookup where the request for a lookup is answered with the value for that key.
*
* @param <V> the map value type.
* @param map the map.
* @return a new MapStringLookup.
*/
public <V> StringLookup mapStringLookup(final Map<String, V> map) {
return FunctionStringLookup.on(map);
}
/**
* Returns the NullStringLookup singleton instance which always returns null.
*
* @return The NullStringLookup singleton instance.
*/
public StringLookup nullStringLookup() {
return StringLookupFactory.INSTANCE_NULL;
}
/**
* Returns the PropertiesStringLookup singleton instance.
* <p>
* Looks up the value for the key in the format "DocumentPath::MyKey".
* </p>
* <p>
* Note the use of "::" instead of ":" to allow for "C:" drive letters in paths.
* </p>
* <p>
* For example: "com/domain/document.properties::MyKey".
* </p>
*
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.propertiesStringLookup().lookup("com/domain/document.properties::MyKey");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${properties:com/domain/document.properties::MyKey} ..."));
* </pre>
* <p>
* The above examples convert {@code "com/domain/document.properties::MyKey"} to the key value in the properties
* file at the path "com/domain/document.properties".
* </p>
*
* @return The PropertiesStringLookup singleton instance.
* @since 1.5
*/
public StringLookup propertiesStringLookup() {
return PropertiesStringLookup.INSTANCE;
}
/**
* Returns the ResourceBundleStringLookup singleton instance.
* <p>
* Looks up the value for a given key in the format "BundleName:BundleKey".
* </p>
* <p>
* For example: "com.domain.messages:MyKey".
* </p>
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.resourceBundleStringLookup().lookup("com.domain.messages:MyKey");
* </pre>
* <p>
* Using a {@link StringSubstitutor}:
* </p>
*
* <pre>
* StringSubstitutor.createInterpolator().replace("... ${resourceBundle:com.domain.messages:MyKey} ..."));
* </pre>
* <p>
* The above examples convert {@code "com.domain.messages:MyKey"} to the key value in the resource bundle at
* {@code "com.domain.messages"}.
* </p>
*
* @return The ResourceBundleStringLookup singleton instance.
*/
public StringLookup resourceBundleStringLookup() {
return ResourceBundleStringLookup.INSTANCE;
}
/**
* Returns a ResourceBundleStringLookup instance for the given bundle name.
* <p>
* Looks up the value for a given key in the format "MyKey".
* </p>
* <p>
* For example: "MyKey".
* </p>
* <p>
* Using a {@link StringLookup} from the {@link StringLookupFactory}:
* </p>
*
* <pre>
* StringLookupFactory.INSTANCE.resourceBundleStringLookup("com.domain.messages").lookup("MyKey");
* </pre>
* <p>
* The above example converts {@code "MyKey"} to the key value in the resource bundle at
* {@code "com.domain.messages"}.
* </p>
*
* @param bundleName Only lookup in this bundle.
* @return a ResourceBundleStringLookup instance for the given bundle name.
* @since 1.5
*/
public StringLookup resourceBundleStringLookup(final String bundleName) {
return new ResourceBundleStringLookup(bundleName);
}
/**
* Returns the ScriptStringLookup singleton instance. NOTE: This lookup is not included