DefaultLookups.java

  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. import org.apache.commons.text.lookup.StringLookupFactory;

  19. /**
  20.  * <p>
  21.  * An enumeration class defining constants for built-in {@code Lookup} objects available for
  22.  * {@code Configuration} instances.
  23.  * </p>
  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.      * The lookup for Base64 decoding, accessed using the prefix {@code "base64Decoder"}.
  61.      *
  62.      * @see StringLookupFactory#base64DecoderStringLookup()
  63.      * @since 2.4
  64.      */
  65.     BASE64_DECODER(StringLookupFactory.KEY_BASE64_DECODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.base64DecoderStringLookup())),

  66.     /**
  67.      * The lookup for Base64 encoding, accessed using the prefix {@code "base64Encoder"}.
  68.      *
  69.      * @see StringLookupFactory#base64EncoderStringLookup()
  70.      * @since 2.4
  71.      */
  72.     BASE64_ENCODER(StringLookupFactory.KEY_BASE64_ENCODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.base64EncoderStringLookup())),

  73.     /**
  74.      * The lookup for Java constants, accessed using the prefix {@code "const"}.
  75.      *
  76.      * @see StringLookupFactory#constantStringLookup()
  77.      * @since 2.4
  78.      */
  79.     CONST(StringLookupFactory.KEY_CONST, new StringLookupAdapter(StringLookupFactory.INSTANCE.constantStringLookup())),

  80.     /**
  81.      * The lookup for the current date in a specified format, accessed using the prefix {@code "date"}.
  82.      *
  83.      * @see StringLookupFactory#dateStringLookup()
  84.      * @since 2.4
  85.      */
  86.     DATE(StringLookupFactory.KEY_DATE, new StringLookupAdapter(StringLookupFactory.INSTANCE.dateStringLookup())),

  87.     /**
  88.      * The lookup for DNS, accessed using the prefix {@code "dns"}.
  89.      *
  90.      * @see StringLookupFactory#dnsStringLookup()
  91.      * @since 2.6
  92.      */
  93.     DNS(StringLookupFactory.KEY_DNS, new StringLookupAdapter(StringLookupFactory.INSTANCE.dnsStringLookup())),

  94.     /**
  95.      * The lookup for environment properties, accessed using the prefix {@code "env"}.
  96.      *
  97.      * @see StringLookupFactory#environmentVariableStringLookup()
  98.      */
  99.     ENVIRONMENT(StringLookupFactory.KEY_ENV, new StringLookupAdapter(StringLookupFactory.INSTANCE.environmentVariableStringLookup())),

  100.     /**
  101.      * The lookup for file content, accessed using the prefix {@code "file"}.
  102.      *
  103.      * @see StringLookupFactory#fileStringLookup()
  104.      * @since 2.4
  105.      */
  106.     FILE(StringLookupFactory.KEY_FILE, new StringLookupAdapter(StringLookupFactory.INSTANCE.fileStringLookup())),

  107.     /**
  108.      * The lookup for Java platform information, accessed using the prefix {@code "java"}.
  109.      *
  110.      * @see StringLookupFactory#javaPlatformStringLookup()
  111.      * @since 2.4
  112.      */
  113.     JAVA(StringLookupFactory.KEY_JAVA, new StringLookupAdapter(StringLookupFactory.INSTANCE.javaPlatformStringLookup())),

  114.     /**
  115.      * The lookup for localhost information, accessed using the prefix {@code "localhost"}.
  116.      *
  117.      * @see StringLookupFactory#localHostStringLookup()
  118.      * @since 2.4
  119.      */
  120.     LOCAL_HOST(StringLookupFactory.KEY_LOCALHOST, new StringLookupAdapter(StringLookupFactory.INSTANCE.localHostStringLookup())),

  121.     /**
  122.      * The lookup for properties, accessed using the prefix {@code "properties"}.
  123.      *
  124.      * @see StringLookupFactory#propertiesStringLookup()
  125.      * @since 2.4
  126.      */
  127.     PROPERTIES(StringLookupFactory.KEY_PROPERTIES, new StringLookupAdapter(StringLookupFactory.INSTANCE.propertiesStringLookup())),

  128.     /**
  129.      * The lookup for resource bundles, accessed using the prefix {@code "resourceBundle"}.
  130.      *
  131.      * @see StringLookupFactory#resourceBundleStringLookup()
  132.      * @since 2.4
  133.      */
  134.     RESOURCE_BUNDLE(StringLookupFactory.KEY_RESOURCE_BUNDLE, new StringLookupAdapter(StringLookupFactory.INSTANCE.resourceBundleStringLookup())),

  135.     /**
  136.      * The lookup for scripts, accessed using the prefix {@code "script"}.
  137.      *
  138.      * @see StringLookupFactory#scriptStringLookup()
  139.      * @since 2.4
  140.      */
  141.     SCRIPT(StringLookupFactory.KEY_SCRIPT, new StringLookupAdapter(StringLookupFactory.INSTANCE.scriptStringLookup())),

  142.     /**
  143.      * The lookup for system properties, accessed using the prefix {@code "sys"}.
  144.      *
  145.      * @see StringLookupFactory#systemPropertyStringLookup()
  146.      */
  147.     SYSTEM_PROPERTIES(StringLookupFactory.KEY_SYS, new StringLookupAdapter(StringLookupFactory.INSTANCE.systemPropertyStringLookup())),

  148.     /**
  149.      * The lookup for URLs, accessed using the prefix {@code "url"}.
  150.      *
  151.      * @see StringLookupFactory#urlStringLookup()
  152.      * @since 2.4
  153.      */
  154.     URL(StringLookupFactory.KEY_URL, new StringLookupAdapter(StringLookupFactory.INSTANCE.urlStringLookup())),

  155.     /**
  156.      * The lookup for URL decoding, accessed using the prefix {@code "urlDecoder"}.
  157.      *
  158.      * @see StringLookupFactory#urlDecoderStringLookup()
  159.      * @since 2.4
  160.      */
  161.     URL_DECODER(StringLookupFactory.KEY_URL_DECODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.urlDecoderStringLookup())),

  162.     /**
  163.      * The lookup for URL encoding, accessed using the prefix {@code "urlEncoder"}.
  164.      *
  165.      * @see StringLookupFactory#urlEncoderStringLookup()
  166.      * @since 2.4
  167.      */
  168.     URL_ENCODER(StringLookupFactory.KEY_URL_ENCODER, new StringLookupAdapter(StringLookupFactory.INSTANCE.urlEncoderStringLookup())),

  169.     /**
  170.      * The lookup for XML content, accessed using the prefix {@code "xml"}.
  171.      *
  172.      * @see StringLookupFactory#xmlStringLookup()
  173.      * @since 2.4
  174.      */
  175.     XML(StringLookupFactory.KEY_XML, new StringLookupAdapter(StringLookupFactory.INSTANCE.xmlStringLookup()));

  176.     /** The associated lookup instance. */
  177.     private final Lookup lookup;

  178.     /** The prefix under which the associated lookup object is registered. */
  179.     private final String prefix;

  180.     /**
  181.      * Creates a new instance of {@code DefaultLookups} and sets the prefix and the associated lookup instance.
  182.      *
  183.      * @param prefix the prefix
  184.      * @param lookup the {@code Lookup} instance
  185.      */
  186.     DefaultLookups(final String prefix, final Lookup lookup) {
  187.         this.prefix = prefix;
  188.         this.lookup = lookup;
  189.     }

  190.     /**
  191.      * Gets the standard {@code Lookup} instance of this kind.
  192.      *
  193.      * @return the associated {@code Lookup} object
  194.      */
  195.     public Lookup getLookup() {
  196.         return lookup;
  197.     }

  198.     /**
  199.      * Gets the standard prefix for the lookup object of this kind.
  200.      *
  201.      * @return the prefix
  202.      */
  203.     public String getPrefix() {
  204.         return prefix;
  205.     }
  206. }