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    *      https://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.Path;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.commons.text.StringSubstitutor;
25  
26  /**
27   * Looks up file contents.
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   * 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}.
37   * </p>
38   * <p>
39   * Using a {@link StringSubstitutor}:
40   * </p>
41   *
42   * <pre>
43   * StringSubstitutor.createInterpolator().replace("... ${file:UTF-8:com/domain/document.properties} ..."));
44   * </pre>
45   * <p>
46   * The above example converts {@code "UTF-8:SomePath"} to the UTF-8 contents of the file at {@code SomePath}.
47   * </p>
48   * <p>
49   * Public access is through {@link StringLookupFactory}.
50   * </p>
51   *
52   * @see StringLookupFactory
53   * @since 1.5
54   */
55  final class FileStringLookup extends AbstractPathFencedLookup {
56  
57      /**
58       * Defines the singleton for this class.
59       */
60      static final AbstractStringLookup INSTANCE = new FileStringLookup((Path[]) null);
61  
62      /**
63       * Constructs a new instance.
64       *
65       * @param fences The fences guarding Path resolution.
66       */
67      FileStringLookup(final Path... fences) {
68          super(fences);
69      }
70  
71      /**
72       * Looks up the value for the key in the format "charsetName:DocumentPath".
73       * <p>
74       * For example: "UTF-8:com/domain/document.properties".
75       * </p>
76       *
77       * @param key the key to be looked up, may be null
78       * @return The value associated with the key.
79       */
80      @Override
81      public String lookup(final String key) {
82          if (key == null) {
83              return null;
84          }
85          final String[] keys = key.split(String.valueOf(SPLIT_CH));
86          final int keyLen = keys.length;
87          if (keyLen < 2) {
88              throw IllegalArgumentExceptions.format("Bad file key format [%s], expected format is CharsetName:DocumentPath.", key);
89          }
90          final String charsetName = keys[0];
91          final String fileName = StringUtils.substringAfter(key, SPLIT_CH);
92          try {
93              return new String(Files.readAllBytes(getPath(fileName)), charsetName);
94          } catch (final Exception e) {
95              throw IllegalArgumentExceptions.format(e, "Error looking up file [%s] with charset [%s].", fileName, charsetName);
96          }
97      }
98  
99  }