View Javadoc
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  
18  package org.apache.commons.text.lookup;
19  
20  import java.nio.file.Files;
21  import java.nio.file.Paths;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.commons.text.StringSubstitutor;
25  
26  /**
27   * Looks up keys from a file.
28   * <p>
29   * Using a {@link StringLookup} from the {@link StringLookupFactory}:
30   * </p>
31   *
32   * <pre>
33   * StringLookupFactory.INSTANCE.fileStringLookup().lookup(UTF-8:com/domain/document.properties");
34   * </pre>
35   * <p>
36   * Using a {@link StringSubstitutor}:
37   * </p>
38   *
39   * <pre>
40   * StringSubstitutor.createInterpolator().replace("... ${file:UTF-8:com/domain/document.properties} ..."));
41   * </pre>
42   * <p>
43   * The above examples convert {@code "UTF-8:SomePath"} to the contents of the file.
44   * </p>
45   *
46   * @since 1.5
47   */
48  final class FileStringLookup extends AbstractStringLookup {
49  
50      /**
51       * Defines the singleton for this class.
52       */
53      static final AbstractStringLookup INSTANCE = new FileStringLookup();
54  
55      /**
56       * No need to build instances for now.
57       */
58      private FileStringLookup() {
59          // empty
60      }
61  
62      /**
63       * Looks up the value for the key in the format "charsetName:DocumentPath".
64       * <p>
65       * For example: "UTF-8:com/domain/document.properties".
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(String.valueOf(SPLIT_CH));
77          final int keyLen = keys.length;
78          if (keyLen < 2) {
79              throw IllegalArgumentExceptions
80                  .format("Bad file key format [%s], expected format is CharsetName:DocumentPath.", key);
81          }
82          final String charsetName = keys[0];
83          final String fileName = StringUtils.substringAfter(key, SPLIT_CH);
84          try {
85              return new String(Files.readAllBytes(Paths.get(fileName)), charsetName);
86          } catch (final Exception e) {
87              throw IllegalArgumentExceptions.format(e, "Error looking up file [%s] with charset [%s].", fileName,
88                  charsetName);
89          }
90      }
91  
92  }