View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration2.interpol;
18  
19  import org.apache.commons.text.lookup.StringLookupFactory;
20  
21  /**
22   * Enumerates built-in {@code Lookup} objects available for
23   * {@code Configuration} instances.
24   * <p>
25   * When a new configuration object derived from {@code AbstractConfiguration} is created, it installs a
26   * {@link ConfigurationInterpolator} containing a default set of {@link Lookup} objects. These lookups are
27   * defined by this enumeration class, however not all lookups may be included in the defaults. See
28   * {@link ConfigurationInterpolator#getDefaultPrefixLookups()} for details.
29   * </p>
30   * <p>
31   * All the {@code Lookup}s defined here are state-less, thus their instances can be shared between multiple
32   * configuration objects. Therefore, it makes sense to keep shared instances in this enumeration class.
33   * </p>
34   *
35   * Provides access to lookups defined in Apache Commons Text:
36   * <ul>
37   * <li>"base64Decoder" for the {@code Base64DecoderStringLookup} since Apache Commons Text 1.6.</li>
38   * <li>"base64Encoder" for the {@code Base64EncoderStringLookup} since Apache Commons Text 1.6.</li>
39   * <li>"const" for the {@code ConstantStringLookup} since Apache Commons Text 1.5.</li>
40   * <li>"date" for the {@code DateStringLookup}.</li>
41   * <li>"env" for the {@code EnvironmentVariableStringLookup}.</li>
42   * <li>"file" for the {@code FileStringLookup} since Apache Commons Text 1.5.</li>
43   * <li>"java" for the {@code JavaPlatformStringLookup}.</li>
44   * <li>"localhost" for the {@code LocalHostStringLookup}, see {@code #localHostStringLookup()} for key names; since
45   * Apache Commons Text 1.3.</li>
46   * <li>"properties" for the {@code PropertiesStringLookup} since Apache Commons Text 1.5.</li>
47   * <li>"resourceBundle" for the {@code ResourceBundleStringLookup} since Apache Commons Text 1.5.</li>
48   * <li>"script" for the {@code ScriptStringLookup} since Apache Commons Text 1.5.</li>
49   * <li>"sys" for the {@code SystemPropertyStringLookup}.</li>
50   * <li>"url" for the {@code UrlStringLookup} since Apache Commons Text 1.5.</li>
51   * <li>"urlDecoder" for the {@code UrlDecoderStringLookup} since Apache Commons Text 1.6.</li>
52   * <li>"urlEncoder" for the {@code UrlEncoderStringLookup} since Apache Commons Text 1.6.</li>
53   * <li>"xml" for the {@code XmlStringLookup} since Apache Commons Text 1.5.</li>
54   * </ul>
55   *
56   * @since 2.0
57   */
58  public enum DefaultLookups {
59  
60      /**
61       * The lookup for Base64 decoding, accessed using the prefix {@code "base64Decoder"}.
62       *
63       * @see StringLookupFactory#base64DecoderStringLookup()
64       * @since 2.4
65       */
66      BASE64_DECODER(StringLookupFactory.KEY_BASE64_DECODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.base64DecoderStringLookup())),
67  
68      /**
69       * The lookup for Base64 encoding, accessed using the prefix {@code "base64Encoder"}.
70       *
71       * @see StringLookupFactory#base64EncoderStringLookup()
72       * @since 2.4
73       */
74      BASE64_ENCODER(StringLookupFactory.KEY_BASE64_ENCODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.base64EncoderStringLookup())),
75  
76      /**
77       * The lookup for Java constants, accessed using the prefix {@code "const"}.
78       *
79       * @see StringLookupFactory#constantStringLookup()
80       * @since 2.4
81       */
82      CONST(StringLookupFactory.KEY_CONST, new StringLookupAdapter(StringLookupFactory.INSTANCE.constantStringLookup())),
83  
84      /**
85       * The lookup for the current date in a specified format, accessed using the prefix {@code "date"}.
86       *
87       * @see StringLookupFactory#dateStringLookup()
88       * @since 2.4
89       */
90      DATE(StringLookupFactory.KEY_DATE, new StringLookupAdapter(StringLookupFactory.INSTANCE.dateStringLookup())),
91  
92      /**
93       * The lookup for DNS, accessed using the prefix {@code "dns"}.
94       *
95       * @see StringLookupFactory#dnsStringLookup()
96       * @since 2.6
97       */
98      DNS(StringLookupFactory.KEY_DNS, new StringLookupAdapter(StringLookupFactory.INSTANCE.dnsStringLookup())),
99  
100     /**
101      * The lookup for environment properties, accessed using the prefix {@code "env"}.
102      *
103      * @see StringLookupFactory#environmentVariableStringLookup()
104      */
105     ENVIRONMENT(StringLookupFactory.KEY_ENV, new StringLookupAdapter(StringLookupFactory.INSTANCE.environmentVariableStringLookup())),
106 
107     /**
108      * The lookup for file content, accessed using the prefix {@code "file"}.
109      *
110      * @see StringLookupFactory#fileStringLookup()
111      * @since 2.4
112      */
113     FILE(StringLookupFactory.KEY_FILE, new StringLookupAdapter(StringLookupFactory.INSTANCE.fileStringLookup())),
114 
115     /**
116      * The lookup for Java platform information, accessed using the prefix {@code "java"}.
117      *
118      * @see StringLookupFactory#javaPlatformStringLookup()
119      * @since 2.4
120      */
121     JAVA(StringLookupFactory.KEY_JAVA, new StringLookupAdapter(StringLookupFactory.INSTANCE.javaPlatformStringLookup())),
122 
123     /**
124      * The lookup for localhost information, accessed using the prefix {@code "localhost"}.
125      *
126      * @see StringLookupFactory#localHostStringLookup()
127      * @since 2.4
128      */
129     LOCAL_HOST(StringLookupFactory.KEY_LOCALHOST, new StringLookupAdapter(StringLookupFactory.INSTANCE.localHostStringLookup())),
130 
131     /**
132      * The lookup for properties, accessed using the prefix {@code "properties"}.
133      *
134      * @see StringLookupFactory#propertiesStringLookup()
135      * @since 2.4
136      */
137     PROPERTIES(StringLookupFactory.KEY_PROPERTIES, new StringLookupAdapter(StringLookupFactory.INSTANCE.propertiesStringLookup())),
138 
139     /**
140      * The lookup for resource bundles, accessed using the prefix {@code "resourceBundle"}.
141      *
142      * @see StringLookupFactory#resourceBundleStringLookup()
143      * @since 2.4
144      */
145     RESOURCE_BUNDLE(StringLookupFactory.KEY_RESOURCE_BUNDLE, new StringLookupAdapter(StringLookupFactory.INSTANCE.resourceBundleStringLookup())),
146 
147     /**
148      * The lookup for scripts, accessed using the prefix {@code "script"}.
149      *
150      * @see StringLookupFactory#scriptStringLookup()
151      * @since 2.4
152      */
153     SCRIPT(StringLookupFactory.KEY_SCRIPT, new StringLookupAdapter(StringLookupFactory.INSTANCE.scriptStringLookup())),
154 
155     /**
156      * The lookup for system properties, accessed using the prefix {@code "sys"}.
157      *
158      * @see StringLookupFactory#systemPropertyStringLookup()
159      */
160     SYSTEM_PROPERTIES(StringLookupFactory.KEY_SYS, new StringLookupAdapter(StringLookupFactory.INSTANCE.systemPropertyStringLookup())),
161 
162     /**
163      * The lookup for URLs, accessed using the prefix {@code "url"}.
164      *
165      * @see StringLookupFactory#urlStringLookup()
166      * @since 2.4
167      */
168     URL(StringLookupFactory.KEY_URL, new StringLookupAdapter(StringLookupFactory.INSTANCE.urlStringLookup())),
169 
170     /**
171      * The lookup for URL decoding, accessed using the prefix {@code "urlDecoder"}.
172      *
173      * @see StringLookupFactory#urlDecoderStringLookup()
174      * @since 2.4
175      */
176     URL_DECODER(StringLookupFactory.KEY_URL_DECODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.urlDecoderStringLookup())),
177 
178     /**
179      * The lookup for URL encoding, accessed using the prefix {@code "urlEncoder"}.
180      *
181      * @see StringLookupFactory#urlEncoderStringLookup()
182      * @since 2.4
183      */
184     URL_ENCODER(StringLookupFactory.KEY_URL_ENCODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.urlEncoderStringLookup())),
185 
186     /**
187      * The lookup for XML content, accessed using the prefix {@code "xml"}.
188      *
189      * @see StringLookupFactory#xmlStringLookup()
190      * @since 2.4
191      */
192     XML(StringLookupFactory.KEY_XML, new StringLookupAdapter(StringLookupFactory.INSTANCE.xmlStringLookup()));
193 
194     /** The associated lookup instance. */
195     private final Lookup lookup;
196 
197     /** The prefix under which the associated lookup object is registered. */
198     private final String prefix;
199 
200     /**
201      * Creates a new instance of {@code DefaultLookups} and sets the prefix and the associated lookup instance.
202      *
203      * @param prefix the prefix
204      * @param lookup the {@code Lookup} instance
205      */
206     DefaultLookups(final String prefix, final Lookup lookup) {
207         this.prefix = prefix;
208         this.lookup = lookup;
209     }
210 
211     /**
212      * Gets the standard {@code Lookup} instance of this kind.
213      *
214      * @return the associated {@code Lookup} object
215      */
216     public Lookup getLookup() {
217         return lookup;
218     }
219 
220     /**
221      * Gets the standard prefix for the lookup object of this kind.
222      *
223      * @return the prefix
224      */
225     public String getPrefix() {
226         return prefix;
227     }
228 }