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