DefaultStringLookup.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.text.lookup;

  18. /**
  19.  * An enumeration defining {@link StringLookup} objects available through {@link StringLookupFactory}.
  20.  * <p>
  21.  * This enum was adapted and expanded from Apache Commons Configuration 2.4.
  22.  * </p>
  23.  * <p><strong>NOTE:</strong> Starting in version 1.10.0, not all lookups defined in this class are
  24.  * included by default in the
  25.  * {@link StringLookupFactory#addDefaultStringLookups(java.util.Map) StringLookupFactory.addDefaultStringLookups}
  26.  * method. See the {@link StringLookupFactory} class documentation for details.
  27.  * </p>
  28.  *
  29.  * @see StringLookupFactory
  30.  * @see StringLookup
  31.  * @since 1.7
  32.  */
  33. public enum DefaultStringLookup {

  34.     /**
  35.      * The lookup for Base64 decoding using the key {@code "base64Decoder"}.
  36.      *
  37.      * @see StringLookupFactory#KEY_BASE64_DECODER
  38.      * @see StringLookupFactory#base64DecoderStringLookup()
  39.      */
  40.     BASE64_DECODER(StringLookupFactory.KEY_BASE64_DECODER, StringLookupFactory.INSTANCE.base64DecoderStringLookup()),

  41.     /**
  42.      * The lookup for Base64 encoding using the key {@code "base64Encoder"}.
  43.      *
  44.      * @see StringLookupFactory#KEY_BASE64_ENCODER
  45.      * @see StringLookupFactory#base64EncoderStringLookup()
  46.      */
  47.     BASE64_ENCODER(StringLookupFactory.KEY_BASE64_ENCODER, StringLookupFactory.INSTANCE.base64EncoderStringLookup()),

  48.     /**
  49.      * The lookup for Java static class member constants using the key {@code "const"}.
  50.      *
  51.      * @see StringLookupFactory#KEY_CONST
  52.      * @see StringLookupFactory#constantStringLookup()
  53.      */
  54.     CONST(StringLookupFactory.KEY_CONST, StringLookupFactory.INSTANCE.constantStringLookup()),

  55.     /**
  56.      * The lookup for formatting the current date using the key {@code "date"}.
  57.      *
  58.      * @see StringLookupFactory#KEY_DATE
  59.      * @see StringLookupFactory#dateStringLookup()
  60.      */
  61.     DATE(StringLookupFactory.KEY_DATE, StringLookupFactory.INSTANCE.dateStringLookup()),

  62.     /**
  63.      * The lookup for DNS using the key {@code "dns"}.
  64.      *
  65.      * @see StringLookupFactory#KEY_DNS
  66.      * @see StringLookupFactory#dnsStringLookup()
  67.      * @since 1.8
  68.      */
  69.     DNS(StringLookupFactory.KEY_DNS, StringLookupFactory.INSTANCE.dnsStringLookup()),

  70.     /**
  71.      * The lookup for environment properties using the key {@code "env"}.
  72.      *
  73.      * @see StringLookupFactory#KEY_ENV
  74.      * @see StringLookupFactory#environmentVariableStringLookup()
  75.      */
  76.     ENVIRONMENT(StringLookupFactory.KEY_ENV, StringLookupFactory.INSTANCE.environmentVariableStringLookup()),

  77.     /**
  78.      * The lookup for files using the key {@code "file"}.
  79.      *
  80.      * @see StringLookupFactory#KEY_FILE
  81.      * @see StringLookupFactory#fileStringLookup()
  82.      */
  83.     FILE(StringLookupFactory.KEY_FILE, StringLookupFactory.INSTANCE.fileStringLookup()),

  84.     /**
  85.      * The lookup for Java platform information using the key {@code "java"}.
  86.      *
  87.      * @see StringLookupFactory#KEY_JAVA
  88.      * @see StringLookupFactory#javaPlatformStringLookup()
  89.      */
  90.     JAVA(StringLookupFactory.KEY_JAVA, StringLookupFactory.INSTANCE.javaPlatformStringLookup()),

  91.     /**
  92.      * The lookup for local host information using the key {@code "localhost"}.
  93.      *
  94.      * @see StringLookupFactory#KEY_LOCALHOST
  95.      * @see StringLookupFactory#localHostStringLookup()
  96.      */
  97.     LOCAL_HOST(StringLookupFactory.KEY_LOCALHOST, StringLookupFactory.INSTANCE.localHostStringLookup()),

  98.     /**
  99.      * The lookup for local host information using the key {@code "loopbackAddress"}.
  100.      *
  101.      * @see StringLookupFactory#KEY_LOOPBACK_ADDRESS
  102.      * @see StringLookupFactory#loopbackAddressStringLookup()
  103.      */
  104.     LOOPBACK_ADDRESS(StringLookupFactory.KEY_LOOPBACK_ADDRESS, StringLookupFactory.INSTANCE.loopbackAddressStringLookup()),

  105.     /**
  106.      * The lookup for properties using the key {@code "properties"}.
  107.      *
  108.      * @see StringLookupFactory#KEY_PROPERTIES
  109.      * @see StringLookupFactory#propertiesStringLookup()
  110.      */
  111.     PROPERTIES(StringLookupFactory.KEY_PROPERTIES, StringLookupFactory.INSTANCE.propertiesStringLookup()),

  112.     /**
  113.      * The lookup for resource bundles using the key {@code "resourceBundle"}.
  114.      *
  115.      * @see StringLookupFactory#KEY_RESOURCE_BUNDLE
  116.      * @see StringLookupFactory#resourceBundleStringLookup()
  117.      */
  118.     RESOURCE_BUNDLE(StringLookupFactory.KEY_RESOURCE_BUNDLE, StringLookupFactory.INSTANCE.resourceBundleStringLookup()),

  119.     /**
  120.      * The lookup for scripts using the key {@code "script"}.
  121.      *
  122.      * @see StringLookupFactory#KEY_SCRIPT
  123.      * @see StringLookupFactory#scriptStringLookup()
  124.      */
  125.     SCRIPT(StringLookupFactory.KEY_SCRIPT, StringLookupFactory.INSTANCE.scriptStringLookup()),

  126.     /**
  127.      * The lookup for system properties using the key {@code "sys"}.
  128.      *
  129.      * @see StringLookupFactory#KEY_SYS
  130.      * @see StringLookupFactory#systemPropertyStringLookup()
  131.      */
  132.     SYSTEM_PROPERTIES(StringLookupFactory.KEY_SYS, StringLookupFactory.INSTANCE.systemPropertyStringLookup()),

  133.     /**
  134.      * The lookup for URLs using the key {@code "url"}.
  135.      *
  136.      * @see StringLookupFactory#KEY_URL
  137.      * @see StringLookupFactory#urlStringLookup()
  138.      */
  139.     URL(StringLookupFactory.KEY_URL, StringLookupFactory.INSTANCE.urlStringLookup()),

  140.     /**
  141.      * The lookup for URL decoding using the key {@code "urlDecoder"}.
  142.      *
  143.      * @see StringLookupFactory#KEY_URL_DECODER
  144.      * @see StringLookupFactory#urlDecoderStringLookup()
  145.      */
  146.     URL_DECODER(StringLookupFactory.KEY_URL_DECODER, StringLookupFactory.INSTANCE.urlDecoderStringLookup()),

  147.     /**
  148.      * The lookup for URL encoding using the key {@code "urlEncoder"}.
  149.      *
  150.      * @see StringLookupFactory#KEY_URL_ENCODER
  151.      * @see StringLookupFactory#urlEncoderStringLookup()
  152.      */
  153.     URL_ENCODER(StringLookupFactory.KEY_URL_ENCODER, StringLookupFactory.INSTANCE.urlEncoderStringLookup()),

  154.     /**
  155.      * The lookup for XML decoding using the key {@code "xml"}.
  156.      *
  157.      * @see StringLookupFactory#KEY_XML
  158.      * @see StringLookupFactory#xmlStringLookup()
  159.      */
  160.     XML(StringLookupFactory.KEY_XML, StringLookupFactory.INSTANCE.xmlStringLookup()),

  161.     /**
  162.      * The lookup for XML decoding using the key {@code "xmlDecoder"}.
  163.      *
  164.      * @see StringLookupFactory#KEY_XML_DECODER
  165.      * @see StringLookupFactory#xmlDecoderStringLookup()
  166.      * @since 1.11.0
  167.      */
  168.     XML_DECODER(StringLookupFactory.KEY_XML_DECODER, StringLookupFactory.INSTANCE.xmlDecoderStringLookup()),

  169.     /**
  170.      * The lookup for XML encoding using the key {@code "xmlEncoder"}.
  171.      *
  172.      * @see StringLookupFactory#KEY_XML_ENCODER
  173.      * @see StringLookupFactory#xmlEncoderStringLookup()
  174.      * @since 1.11.0
  175.      */
  176.     XML_ENCODER(StringLookupFactory.KEY_XML_ENCODER, StringLookupFactory.INSTANCE.xmlEncoderStringLookup());

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

  179.     /** The associated lookup instance. */
  180.     private final StringLookup lookup;

  181.     /**
  182.      * Constructs a new instance of {@link DefaultStringLookup} and sets the key and the associated lookup instance.
  183.      *
  184.      * @param prefix the prefix
  185.      * @param lookup the {@link StringLookup} instance
  186.      */
  187.     DefaultStringLookup(final String prefix, final StringLookup lookup) {
  188.         this.key = prefix;
  189.         this.lookup = lookup;
  190.     }

  191.     /**
  192.      * Gets the standard prefix for the lookup object of this kind.
  193.      *
  194.      * @return the prefix
  195.      */
  196.     public String getKey() {
  197.         return key;
  198.     }

  199.     /**
  200.      * Gets the standard {@link StringLookup} instance of this kind.
  201.      *
  202.      * @return the associated {@link StringLookup} object
  203.      */
  204.     public StringLookup getStringLookup() {
  205.         return lookup;
  206.     }
  207. }