    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 the {@code Lookup} objects available for each {@code Configuration}
    024 * object per default.
    025 * </p>
    026 * <p>
    027 * When a new configuration object derived from {@code AbstractConfiguration} is created it installs a
    028 * {@link ConfigurationInterpolator} with a default set of {@link Lookup} objects. These lookups are defined by this
    029 * enumeration class.
    030 * </p>
    031 * <p>
    032 * All the default {@code Lookup} classes are state-less, thus their instances can be shared between multiple
    033 * configuration objects. Therefore, it makes sense to keep shared instances in this enumeration class.
    034 * </p>
    035 *
    036 * Provides access to lookups defined in Apache Commons Text:
    037 * <ul>
    038 * <li>"base64Decoder" for the {@code Base64DecoderStringLookup} since Apache Commons Text 1.6.</li>
    039 * <li>"base64Encoder" for the {@code Base64EncoderStringLookup} since Apache Commons Text 1.6.</li>
    040 * <li>"const" for the {@code ConstantStringLookup} since Apache Commons Text 1.5.</li>
    041 * <li>"date" for the {@code DateStringLookup}.</li>
    042 * <li>"env" for the {@code EnvironmentVariableStringLookup}.</li>
    043 * <li>"file" for the {@code FileStringLookup} since Apache Commons Text 1.5.</li>
    044 * <li>"java" for the {@code JavaPlatformStringLookup}.</li>
    045 * <li>"localhost" for the {@code LocalHostStringLookup}, see {@code #localHostStringLookup()} for key names; since
    046 * Apache Commons Text 1.3.</li>
    047 * <li>"properties" for the {@code PropertiesStringLookup} since Apache Commons Text 1.5.</li>
    048 * <li>"resourceBundle" for the {@code ResourceBundleStringLookup} since Apache Commons Text 1.5.</li>
    049 * <li>"script" for the {@code ScriptStringLookup} since Apache Commons Text 1.5.</li>
    050 * <li>"sys" for the {@code SystemPropertyStringLookup}.</li>
    051 * <li>"url" for the {@code UrlStringLookup} since Apache Commons Text 1.5.</li>
    052 * <li>"urlDecoder" for the {@code UrlDecoderStringLookup} since Apache Commons Text 1.6.</li>
    053 * <li>"urlEncoder" for the {@code UrlEncoderStringLookup} since Apache Commons Text 1.6.</li>
    054 * <li>"xml" for the {@code XmlStringLookup} since Apache Commons Text 1.5.</li>
    055 * </ul>
    056 *
    057 * @since 2.0
    058 */
    059public enum DefaultLookups
    060{
    061
    062    /**
    063     * The lookup for Base64 decoding.
    064     *
    065     * @since 2.4
    066     */
    067    BASE64_DECODER(StringLookupFactory.KEY_BASE64_DECODER,
    068            new StringLookupAdapter(StringLookupFactory.INSTANCE.base64DecoderStringLookup())),
    069
    070    /**
    071     * The lookup for Base64 decoding.
    072     *
    073     * @since 2.4
    074     */
    075    BASE64_ENCODER(StringLookupFactory.KEY_BASE64_ENCODER,
    076            new StringLookupAdapter(StringLookupFactory.INSTANCE.base64EncoderStringLookup())),
    077
    078    /**
    079     * The lookup for constants.
    080     *
    081     * @since 2.4
    082     */
    083    CONST(StringLookupFactory.KEY_CONST, new StringLookupAdapter(StringLookupFactory.INSTANCE.constantStringLookup())),
    084
    085    /**
    086     * The lookup for dates.
    087     *
    088     * @since 2.4
    089     */
    090    DATE(StringLookupFactory.KEY_DATE, new StringLookupAdapter(StringLookupFactory.INSTANCE.dateStringLookup())),
    091
    092    /**
    093     * The lookup for DNS.
    094     *
    095     * @since 2.6
    096     */
    097    DNS(StringLookupFactory.KEY_DNS, new StringLookupAdapter(StringLookupFactory.INSTANCE.dnsStringLookup())),
    098
    099    /**
    100     * The lookup for environment properties.
    101     */
    102    ENVIRONMENT(StringLookupFactory.KEY_ENV,
    103            new StringLookupAdapter(StringLookupFactory.INSTANCE.environmentVariableStringLookup())),
    104
    105    /**
    106     * The lookup for files.
    107     *
    108     * @since 2.4
    109     */
    110    FILE(StringLookupFactory.KEY_FILE, new StringLookupAdapter(StringLookupFactory.INSTANCE.fileStringLookup())),
    111
    112    /**
    113     * The lookup for Java platform information.
    114     *
    115     * @since 2.4
    116     */
    117    JAVA(StringLookupFactory.KEY_JAVA,
    118            new StringLookupAdapter(StringLookupFactory.INSTANCE.javaPlatformStringLookup())),
    119
    120    /**
    121     * The lookup for localhost information.
    122     *
    123     * @since 2.4
    124     */
    125    LOCAL_HOST(StringLookupFactory.KEY_LOCALHOST,
    126            new StringLookupAdapter(StringLookupFactory.INSTANCE.localHostStringLookup())),
    127
    128    /**
    129     * The lookup for properties.
    130     *
    131     * @since 2.4
    132     */
    133    PROPERTIES(StringLookupFactory.KEY_PROPERTIES,
    134            new StringLookupAdapter(StringLookupFactory.INSTANCE.propertiesStringLookup())),
    135
    136    /**
    137     * The lookup for resource bundles.
    138     *
    139     * @since 2.4
    140     */
    141    RESOURCE_BUNDLE(StringLookupFactory.KEY_RESOURCE_BUNDLE,
    142            new StringLookupAdapter(StringLookupFactory.INSTANCE.resourceBundleStringLookup())),
    143
    144    /**
    145     * The lookup for scripts.
    146     *
    147     * @since 2.4
    148     */
    149    SCRIPT(StringLookupFactory.KEY_SCRIPT, new StringLookupAdapter(StringLookupFactory.INSTANCE.scriptStringLookup())),
    150
    151    /**
    152     * The lookup for system properties.
    153     */
    154    SYSTEM_PROPERTIES(StringLookupFactory.KEY_SYS,
    155            new StringLookupAdapter(StringLookupFactory.INSTANCE.systemPropertyStringLookup())),
    156
    157    /**
    158     * The lookup for URLs.
    159     *
    160     * @since 2.4
    161     */
    162    URL(StringLookupFactory.KEY_URL, new StringLookupAdapter(StringLookupFactory.INSTANCE.urlStringLookup())),
    163
    164    /**
    165     * The lookup for URL decoding.
    166     *
    167     * @since 2.4
    168     */
    169    URL_DECODER(StringLookupFactory.KEY_URL_DECODER,
    170            new StringLookupAdapter(StringLookupFactory.INSTANCE.urlDecoderStringLookup())),
    171
    172    /**
    173     * The lookup for URL decoding.
    174     *
    175     * @since 2.4
    176     */
    177    URL_ENCODER(StringLookupFactory.KEY_URL_ENCODER,
    178            new StringLookupAdapter(StringLookupFactory.INSTANCE.urlEncoderStringLookup())),
    179
    180    /**
    181     * The lookup for URL decoding.
    182     *
    183     * @since 2.4
    184     */
    185    XML(StringLookupFactory.KEY_XML, new StringLookupAdapter(StringLookupFactory.INSTANCE.xmlStringLookup()));
    186
    187    /** The associated lookup instance. */
    188    private final Lookup lookup;
    189
    190    /** The prefix under which the associated lookup object is registered. */
    191    private final String prefix;
    192
    193    /**
    194     * Creates a new instance of {@code DefaultLookups} and sets the prefix and the associated lookup instance.
    195     *
    196     * @param prefix
    197     *            the prefix
    198     * @param lookup
    199     *            the {@code Lookup} instance
    200     */
    201    private DefaultLookups(final String prefix, final Lookup lookup)
    202    {
    203        this.prefix = prefix;
    204        this.lookup = lookup;
    205    }
    206
    207    /**
    208     * Returns the standard {@code Lookup} instance of this kind.
    209     *
    210     * @return the associated {@code Lookup} object
    211     */
    212    public Lookup getLookup()
    213    {
    214        return lookup;
    215    }
    216
    217    /**
    218     * Returns the standard prefix for the lookup object of this kind.
    219     *
    220     * @return the prefix
    221     */
    222    public String getPrefix()
    223    {
    224        return prefix;
    225    }
    226}