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