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.io.BufferedInputStream;
21  import java.io.InputStreamReader;
22  import java.io.StringWriter;
23  import java.net.URL;
24  
25  import org.apache.commons.lang3.StringUtils;
26  
27  /**
28   * Looks up keys from an XML document.
29   * <p>
30   * Looks up the value for a given key in the format "Charset:URL".
31   * </p>
32   * <p>
33   * For example: "UTF-8:https://www.apache.org".
34   * </p>
35   *
36   * <p>
37   * Public access is through {@link StringLookupFactory}.
38   * </p>
39   *
40   * @see StringLookupFactory
41   * @since 1.5
42   */
43  final class UrlStringLookup extends AbstractStringLookup {
44  
45      /**
46       * Defines the singleton for this class.
47       */
48      static final UrlStringLookup INSTANCE = new UrlStringLookup();
49  
50      /**
51       * No need to build instances for now.
52       */
53      private UrlStringLookup() {
54          // empty
55      }
56  
57      /**
58       * Looks up the value for the key in the format "DocumentPath:URL".
59       * <p>
60       * For example: "UTF-8:https://www.apache.org".
61       * </p>
62       *
63       * @param key the key to be looked up, may be null.
64       * @return The value associated with the key.
65       */
66      @Override
67      public String lookup(final String key) {
68          if (key == null) {
69              return null;
70          }
71          final String[] keys = key.split(SPLIT_STR);
72          final int keyLen = keys.length;
73          if (keyLen < 2) {
74              throw IllegalArgumentExceptions.format("Bad URL key format [%s]; expected format is DocumentPath:Key.",
75                  key);
76          }
77          final String charsetName = keys[0];
78          final String urlStr = StringUtils.substringAfter(key, SPLIT_CH);
79          try {
80              final URL url = new URL(urlStr);
81              final int size = 8192;
82              final StringWriter writer = new StringWriter(size);
83              final char[] buffer = new char[size];
84              try (BufferedInputStream bis = new BufferedInputStream(url.openStream());
85                  InputStreamReader reader = new InputStreamReader(bis, charsetName)) {
86                  int n;
87                  while (-1 != (n = reader.read(buffer))) {
88                      writer.write(buffer, 0, n);
89                  }
90              }
91              return writer.toString();
92          } catch (final Exception e) {
93              throw IllegalArgumentExceptions.format(e, "Error looking up URL [%s] with Charset [%s].", urlStr,
94                  charsetName);
95          }
96      }
97  
98  }