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 *
49 * @since 1.5
50 */
51 final class FileStringLookup extends AbstractPathFencedLookup {
52
53 /**
54 * Defines the singleton for this class.
55 */
56 static final AbstractStringLookup INSTANCE = new FileStringLookup((Path[]) null);
57
58 /**
59 * Constructs a new instance.
60 *
61 * @param fences The fences guarding Path resolution.
62 */
63 FileStringLookup(final Path... fences) {
64 super(fences);
65 }
66
67 /**
68 * Looks up the value for the key in the format "charsetName:DocumentPath".
69 * <p>
70 * For example: "UTF-8:com/domain/document.properties".
71 * </p>
72 *
73 * @param key the key to be looked up, may be null
74 * @return The value associated with the key.
75 */
76 @Override
77 public String lookup(final String key) {
78 if (key == null) {
79 return null;
80 }
81 final String[] keys = key.split(String.valueOf(SPLIT_CH));
82 final int keyLen = keys.length;
83 if (keyLen < 2) {
84 throw IllegalArgumentExceptions.format("Bad file key format [%s], expected format is CharsetName:DocumentPath.", key);
85 }
86 final String charsetName = keys[0];
87 final String fileName = StringUtils.substringAfter(key, SPLIT_CH);
88 try {
89 return new String(Files.readAllBytes(getPath(fileName)), charsetName);
90 } catch (final Exception e) {
91 throw IllegalArgumentExceptions.format(e, "Error looking up file [%s] with charset [%s].", fileName, charsetName);
92 }
93 }
94
95 }