PropertiesStringLookup.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. import java.io.InputStream;
  19. import java.nio.file.Files;
  20. import java.nio.file.Path;
  21. import java.util.Properties;

  22. import org.apache.commons.lang3.StringUtils;

  23. /**
  24.  * Looks up keys from a properties file.
  25.  * <p>
  26.  * Looks up the value for a given key in the format "Document::Key".
  27.  * </p>
  28.  * <p>
  29.  * Note the use of "::" instead of ":" to allow for "C:" drive letters in paths.
  30.  * </p>
  31.  * <p>
  32.  * For example: "com/domain/document.properties:key".
  33.  * </p>
  34.  *
  35.  * @see Properties
  36.  * @since 1.5
  37.  */
  38. final class PropertiesStringLookup extends AbstractPathFencedLookup {

  39.     /**
  40.      * Defines the singleton for this class.
  41.      */
  42.     static final PropertiesStringLookup INSTANCE = new PropertiesStringLookup((Path[]) null);

  43.     /** Separates file and key. */
  44.     static final String SEPARATOR = "::";

  45.     /**
  46.      * Creates a lookup key for a given file and key.
  47.      */
  48.     static String toPropertyKey(final String file, final String key) {
  49.         return AbstractStringLookup.toLookupKey(file, SEPARATOR, key);
  50.     }

  51.     /**
  52.      * Constructs a new instance.
  53.      *
  54.      * @param fences The fences guarding Path resolution.
  55.      */
  56.     PropertiesStringLookup(final Path... fences) {
  57.         super(fences);
  58.     }

  59.     /**
  60.      * Looks up the value for the key in the format "DocumentPath:XPath".
  61.      * <p>
  62.      * For example: "com/domain/document.xml::/path/to/node".
  63.      * </p>
  64.      * <p>
  65.      * Note the use of "::" instead of ":" to allow for "C:" drive letters in paths.
  66.      * </p>
  67.      *
  68.      * @param key the key to be looked up, may be null
  69.      * @return The value associated with the key.
  70.      */
  71.     @Override
  72.     public String lookup(final String key) {
  73.         if (key == null) {
  74.             return null;
  75.         }
  76.         final String[] keys = key.split(SEPARATOR);
  77.         final int keyLen = keys.length;
  78.         if (keyLen < 2) {
  79.             throw IllegalArgumentExceptions.format("Bad properties key format [%s]; expected format is %s.", key,
  80.                 toPropertyKey("DocumentPath", "Key"));
  81.         }
  82.         final String documentPath = keys[0];
  83.         final String propertyKey = StringUtils.substringAfter(key, SEPARATOR);
  84.         try {
  85.             final Properties properties = new Properties();
  86.             try (InputStream inputStream = Files.newInputStream(getPath(documentPath))) {
  87.                 properties.load(inputStream);
  88.             }
  89.             return properties.getProperty(propertyKey);
  90.         } catch (final Exception e) {
  91.             throw IllegalArgumentExceptions.format(e, "Error looking up properties [%s] and key [%s].", documentPath,
  92.                 propertyKey);
  93.         }
  94.     }

  95. }