001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.configuration2.interpol;
018
019import org.apache.commons.text.lookup.StringLookupFactory;
020
021/**
022 * Enumerates built-in {@code Lookup} objects available for
023 * {@code Configuration} instances.
024 * <p>
025 * When a new configuration object derived from {@code AbstractConfiguration} is created, it installs a
026 * {@link ConfigurationInterpolator} containing a default set of {@link Lookup} objects. These lookups are
027 * defined by this enumeration class, however not all lookups may be included in the defaults. See
028 * {@link ConfigurationInterpolator#getDefaultPrefixLookups()} for details.
029 * </p>
030 * <p>
031 * All the {@code Lookup}s defined here are state-less, thus their instances can be shared between multiple
032 * configuration objects. Therefore, it makes sense to keep shared instances in this enumeration class.
033 * </p>
034 *
035 * Provides access to lookups defined in Apache Commons Text:
036 * <ul>
037 * <li>"base64Decoder" for the {@code Base64DecoderStringLookup} since Apache Commons Text 1.6.</li>
038 * <li>"base64Encoder" for the {@code Base64EncoderStringLookup} since Apache Commons Text 1.6.</li>
039 * <li>"const" for the {@code ConstantStringLookup} since Apache Commons Text 1.5.</li>
040 * <li>"date" for the {@code DateStringLookup}.</li>
041 * <li>"env" for the {@code EnvironmentVariableStringLookup}.</li>
042 * <li>"file" for the {@code FileStringLookup} since Apache Commons Text 1.5.</li>
043 * <li>"java" for the {@code JavaPlatformStringLookup}.</li>
044 * <li>"localhost" for the {@code LocalHostStringLookup}, see {@code #localHostStringLookup()} for key names; since
045 * Apache Commons Text 1.3.</li>
046 * <li>"properties" for the {@code PropertiesStringLookup} since Apache Commons Text 1.5.</li>
047 * <li>"resourceBundle" for the {@code ResourceBundleStringLookup} since Apache Commons Text 1.5.</li>
048 * <li>"script" for the {@code ScriptStringLookup} since Apache Commons Text 1.5.</li>
049 * <li>"sys" for the {@code SystemPropertyStringLookup}.</li>
050 * <li>"url" for the {@code UrlStringLookup} since Apache Commons Text 1.5.</li>
051 * <li>"urlDecoder" for the {@code UrlDecoderStringLookup} since Apache Commons Text 1.6.</li>
052 * <li>"urlEncoder" for the {@code UrlEncoderStringLookup} since Apache Commons Text 1.6.</li>
053 * <li>"xml" for the {@code XmlStringLookup} since Apache Commons Text 1.5.</li>
054 * </ul>
055 *
056 * @since 2.0
057 */
058public enum DefaultLookups {
059
060    /**
061     * The lookup for Base64 decoding, accessed using the prefix {@code "base64Decoder"}.
062     *
063     * @see StringLookupFactory#base64DecoderStringLookup()
064     * @since 2.4
065     */
066    BASE64_DECODER(StringLookupFactory.KEY_BASE64_DECODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.base64DecoderStringLookup())),
067
068    /**
069     * The lookup for Base64 encoding, accessed using the prefix {@code "base64Encoder"}.
070     *
071     * @see StringLookupFactory#base64EncoderStringLookup()
072     * @since 2.4
073     */
074    BASE64_ENCODER(StringLookupFactory.KEY_BASE64_ENCODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.base64EncoderStringLookup())),
075
076    /**
077     * The lookup for Java constants, accessed using the prefix {@code "const"}.
078     *
079     * @see StringLookupFactory#constantStringLookup()
080     * @since 2.4
081     */
082    CONST(StringLookupFactory.KEY_CONST, new StringLookupAdapter(StringLookupFactory.INSTANCE.constantStringLookup())),
083
084    /**
085     * The lookup for the current date in a specified format, accessed using the prefix {@code "date"}.
086     *
087     * @see StringLookupFactory#dateStringLookup()
088     * @since 2.4
089     */
090    DATE(StringLookupFactory.KEY_DATE, new StringLookupAdapter(StringLookupFactory.INSTANCE.dateStringLookup())),
091
092    /**
093     * The lookup for DNS, accessed using the prefix {@code "dns"}.
094     *
095     * @see StringLookupFactory#dnsStringLookup()
096     * @since 2.6
097     */
098    DNS(StringLookupFactory.KEY_DNS, new StringLookupAdapter(StringLookupFactory.INSTANCE.dnsStringLookup())),
099
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}