FileStringLookup.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.nio.file.Files;
  19. import java.nio.file.Path;

  20. import org.apache.commons.lang3.StringUtils;
  21. import org.apache.commons.text.StringSubstitutor;

  22. /**
  23.  * Looks up file contents.
  24.  * <p>
  25.  * Using a {@link StringLookup} from the {@link StringLookupFactory}:
  26.  * </p>
  27.  *
  28.  * <pre>
  29.  * StringLookupFactory.INSTANCE.fileStringLookup().lookup("UTF-8:com/domain/document.properties");
  30.  * </pre>
  31.  * <p>
  32.  * The above example converts {@code "UTF-8:com/domain/document.properties"} to the UTF-8 contents of the file at {@code com/domain/document.properties}.
  33.  * </p>
  34.  * <p>
  35.  * Using a {@link StringSubstitutor}:
  36.  * </p>
  37.  *
  38.  * <pre>
  39.  * StringSubstitutor.createInterpolator().replace("... ${file:UTF-8:com/domain/document.properties} ..."));
  40.  * </pre>
  41.  * <p>
  42.  * The above example converts {@code "UTF-8:SomePath"} to the UTF-8 contents of the file at {@code SomePath}.
  43.  * </p>
  44.  *
  45.  * @since 1.5
  46.  */
  47. final class FileStringLookup extends AbstractPathFencedLookup {

  48.     /**
  49.      * Defines the singleton for this class.
  50.      */
  51.     static final AbstractStringLookup INSTANCE = new FileStringLookup((Path[]) null);

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

  60.     /**
  61.      * Looks up the value for the key in the format "charsetName:DocumentPath".
  62.      * <p>
  63.      * For example: "UTF-8:com/domain/document.properties".
  64.      * </p>
  65.      *
  66.      * @param key the key to be looked up, may be null
  67.      * @return The value associated with the key.
  68.      */
  69.     @Override
  70.     public String lookup(final String key) {
  71.         if (key == null) {
  72.             return null;
  73.         }
  74.         final String[] keys = key.split(String.valueOf(SPLIT_CH));
  75.         final int keyLen = keys.length;
  76.         if (keyLen < 2) {
  77.             throw IllegalArgumentExceptions.format("Bad file key format [%s], expected format is CharsetName:DocumentPath.", key);
  78.         }
  79.         final String charsetName = keys[0];
  80.         final String fileName = StringUtils.substringAfter(key, SPLIT_CH);
  81.         try {
  82.             return new String(Files.readAllBytes(getPath(fileName)), charsetName);
  83.         } catch (final Exception e) {
  84.             throw IllegalArgumentExceptions.format(e, "Error looking up file [%s] with charset [%s].", fileName, charsetName);
  85.         }
  86.     }

  87. }