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.io.InputStream;
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.util.Properties;
24  
25  import org.apache.commons.lang3.StringUtils;
26  
27  /**
28   * Looks up keys from a properties file.
29   * <p>
30   * Looks up the value for a given key in the format "Document::Key".
31   * </p>
32   * <p>
33   * Note the use of "::" instead of ":" to allow for "C:" drive letters in paths.
34   * </p>
35   * <p>
36   * For example: "com/domain/document.properties:key".
37   * </p>
38   *
39   * @see Properties
40   * @since 1.5
41   */
42  final class PropertiesStringLookup extends AbstractPathFencedLookup {
43  
44      /**
45       * Defines the singleton for this class.
46       */
47      static final PropertiesStringLookup INSTANCE = new PropertiesStringLookup((Path[]) null);
48  
49      /** Separates file and key. */
50      static final String SEPARATOR = "::";
51  
52      /**
53       * Creates a lookup key for a given file and key.
54       */
55      static String toPropertyKey(final String file, final String key) {
56          return AbstractStringLookup.toLookupKey(file, SEPARATOR, key);
57      }
58  
59      /**
60       * Constructs a new instance.
61       *
62       * @param fences The fences guarding Path resolution.
63       */
64      PropertiesStringLookup(final Path... fences) {
65          super(fences);
66      }
67  
68      /**
69       * Looks up the value for the key in the format "DocumentPath:XPath".
70       * <p>
71       * For example: "com/domain/document.xml::/path/to/node".
72       * </p>
73       * <p>
74       * Note the use of "::" instead of ":" to allow for "C:" drive letters in paths.
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(SEPARATOR);
86          final int keyLen = keys.length;
87          if (keyLen < 2) {
88              throw IllegalArgumentExceptions.format("Bad properties key format [%s]; expected format is %s.", key,
89                  toPropertyKey("DocumentPath", "Key"));
90          }
91          final String documentPath = keys[0];
92          final String propertyKey = StringUtils.substringAfter(key, SEPARATOR);
93          try {
94              final Properties properties = new Properties();
95              try (InputStream inputStream = Files.newInputStream(getPath(documentPath))) {
96                  properties.load(inputStream);
97              }
98              return properties.getProperty(propertyKey);
99          } catch (final Exception e) {
100             throw IllegalArgumentExceptions.format(e, "Error looking up properties [%s] and key [%s].", documentPath,
101                 propertyKey);
102         }
103     }
104 
105 }