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 *     http://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 * <p>
023 * An enumeration class defining constants for built-in {@code Lookup} objects available for
024 * {@code Configuration} instances.
025 * </p>
026 * <p>
027 * When a new configuration object derived from {@code AbstractConfiguration} is created, it installs a
028 * {@link ConfigurationInterpolator} containing a default set of {@link Lookup} objects. These lookups are
029 * defined by this enumeration class, however not all lookups may be included in the defaults. See
030 * {@link ConfigurationInterpolator#getDefaultPrefixLookups()} for details.
031 * </p>
032 * <p>
033 * All the {@code Lookup}s defined here are state-less, thus their instances can be shared between multiple
034 * configuration objects. Therefore, it makes sense to keep shared instances in this enumeration class.
035 * </p>
036 *
037 * Provides access to lookups defined in Apache Commons Text:
038 * <ul>
039 * <li>"base64Decoder" for the {@code Base64DecoderStringLookup} since Apache Commons Text 1.6.</li>
040 * <li>"base64Encoder" for the {@code Base64EncoderStringLookup} since Apache Commons Text 1.6.</li>
041 * <li>"const" for the {@code ConstantStringLookup} since Apache Commons Text 1.5.</li>
042 * <li>"date" for the {@code DateStringLookup}.</li>
043 * <li>"env" for the {@code EnvironmentVariableStringLookup}.</li>
044 * <li>"file" for the {@code FileStringLookup} since Apache Commons Text 1.5.</li>
045 * <li>"java" for the {@code JavaPlatformStringLookup}.</li>
046 * <li>"localhost" for the {@code LocalHostStringLookup}, see {@code #localHostStringLookup()} for key names; since
047 * Apache Commons Text 1.3.</li>
048 * <li>"properties" for the {@code PropertiesStringLookup} since Apache Commons Text 1.5.</li>
049 * <li>"resourceBundle" for the {@code ResourceBundleStringLookup} since Apache Commons Text 1.5.</li>
050 * <li>"script" for the {@code ScriptStringLookup} since Apache Commons Text 1.5.</li>
051 * <li>"sys" for the {@code SystemPropertyStringLookup}.</li>
052 * <li>"url" for the {@code UrlStringLookup} since Apache Commons Text 1.5.</li>
053 * <li>"urlDecoder" for the {@code UrlDecoderStringLookup} since Apache Commons Text 1.6.</li>
054 * <li>"urlEncoder" for the {@code UrlEncoderStringLookup} since Apache Commons Text 1.6.</li>
055 * <li>"xml" for the {@code XmlStringLookup} since Apache Commons Text 1.5.</li>
056 * </ul>
057 *
058 * @since 2.0
059 */
060public enum DefaultLookups {
061
062    /**
063     * The lookup for Base64 decoding, accessed using the prefix {@code "base64Decoder"}.
064     *
065     * @see StringLookupFactory#base64DecoderStringLookup()
066     * @since 2.4
067     */
068    BASE64_DECODER(StringLookupFactory.KEY_BASE64_DECODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.base64DecoderStringLookup())),
069
070    /**
071     * The lookup for Base64 encoding, accessed using the prefix {@code "base64Encoder"}.
072     *
073     * @see StringLookupFactory#base64EncoderStringLookup()
074     * @since 2.4
075     */
076    BASE64_ENCODER(StringLookupFactory.KEY_BASE64_ENCODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.base64EncoderStringLookup())),
077
078    /**
079     * The lookup for Java constants, accessed using the prefix {@code "const"}.
080     *
081     * @see StringLookupFactory#constantStringLookup()
082     * @since 2.4
083     */
084    CONST(StringLookupFactory.KEY_CONST, new StringLookupAdapter(StringLookupFactory.INSTANCE.constantStringLookup())),
085
086    /**
087     * The lookup for the current date in a specified format, accessed using the prefix {@code "date"}.
088     *
089     * @see StringLookupFactory#dateStringLookup()
090     * @since 2.4
091     */
092    DATE(StringLookupFactory.KEY_DATE, new StringLookupAdapter(StringLookupFactory.INSTANCE.dateStringLookup())),
093
094    /**
095     * The lookup for DNS, accessed using the prefix {@code "dns"}.
096     *
097     * @see StringLookupFactory#dnsStringLookup()
098     * @since 2.6
099     */
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}