UrlStringLookup.java

  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. package org.apache.commons.text.lookup;

  18. import java.io.BufferedInputStream;
  19. import java.io.InputStreamReader;
  20. import java.io.StringWriter;
  21. import java.net.URL;

  22. import org.apache.commons.lang3.StringUtils;

  23. /**
  24.  * Looks up keys from an XML document.
  25.  * <p>
  26.  * Looks up the value for a given key in the format "Charset:URL".
  27.  * </p>
  28.  * <p>
  29.  * For example: "UTF-8:https://www.apache.org".
  30.  * </p>
  31.  *
  32.  * @since 1.5
  33.  */
  34. final class UrlStringLookup extends AbstractStringLookup {

  35.     /**
  36.      * Defines the singleton for this class.
  37.      */
  38.     static final UrlStringLookup INSTANCE = new UrlStringLookup();

  39.     /**
  40.      * No need to build instances for now.
  41.      */
  42.     private UrlStringLookup() {
  43.         // empty
  44.     }

  45.     /**
  46.      * Looks up the value for the key in the format "DocumentPath:URL".
  47.      * <p>
  48.      * For example: "UTF-8:https://www.apache.org".
  49.      * </p>
  50.      *
  51.      * @param key the key to be looked up, may be null
  52.      * @return The value associated with the key.
  53.      */
  54.     @Override
  55.     public String lookup(final String key) {
  56.         if (key == null) {
  57.             return null;
  58.         }
  59.         final String[] keys = key.split(SPLIT_STR);
  60.         final int keyLen = keys.length;
  61.         if (keyLen < 2) {
  62.             throw IllegalArgumentExceptions.format("Bad URL key format [%s]; expected format is DocumentPath:Key.",
  63.                 key);
  64.         }
  65.         final String charsetName = keys[0];
  66.         final String urlStr = StringUtils.substringAfter(key, SPLIT_CH);
  67.         try {
  68.             final URL url = new URL(urlStr);
  69.             final int size = 8192;
  70.             final StringWriter writer = new StringWriter(size);
  71.             final char[] buffer = new char[size];
  72.             try (BufferedInputStream bis = new BufferedInputStream(url.openStream());
  73.                 InputStreamReader reader = new InputStreamReader(bis, charsetName)) {
  74.                 int n;
  75.                 while (-1 != (n = reader.read(buffer))) {
  76.                     writer.write(buffer, 0, n);
  77.                 }
  78.             }
  79.             return writer.toString();
  80.         } catch (final Exception e) {
  81.             throw IllegalArgumentExceptions.format(e, "Error looking up URL [%s] with Charset [%s].", urlStr,
  82.                 charsetName);
  83.         }
  84.     }

  85. }