XmlStringLookup.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.InputStream;
  19. import java.nio.file.Files;
  20. import java.nio.file.Path;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.Objects;

  25. import javax.xml.XMLConstants;
  26. import javax.xml.xpath.XPathFactory;

  27. import org.apache.commons.lang3.StringUtils;
  28. import org.xml.sax.InputSource;

  29. /**
  30.  * Looks up keys from an XML document.
  31.  * <p>
  32.  * Looks up the value for a given key in the format "Document:XPath".
  33.  * </p>
  34.  * <p>
  35.  * For example: "com/domain/document.xml:/path/to/node".
  36.  * </p>
  37.  *
  38.  * @since 1.5
  39.  */
  40. final class XmlStringLookup extends AbstractPathFencedLookup {

  41.     /**
  42.      * Defines default XPath factory features.
  43.      */
  44.     static final Map<String, Boolean> DEFAULT_FEATURES;

  45.     static {
  46.         DEFAULT_FEATURES = new HashMap<>(1);
  47.         DEFAULT_FEATURES.put(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
  48.     }

  49.     /**
  50.      * Defines the singleton for this class.
  51.      */
  52.     static final XmlStringLookup INSTANCE = new XmlStringLookup(DEFAULT_FEATURES, (Path[]) null);

  53.     /**
  54.      * Defines XPath factory features.
  55.      */
  56.     private final Map<String, Boolean> xPathFactoryFeatures;

  57.     /**
  58.      * No need to build instances for now.
  59.      *
  60.      * @param xPathFactoryFeatures XPathFactory features to set.
  61.      * @see XPathFactory#setFeature(String, boolean)
  62.      */
  63.     XmlStringLookup(final Map<String, Boolean> xPathFactoryFeatures, final Path... fences) {
  64.         super(fences);
  65.         this.xPathFactoryFeatures = Objects.requireNonNull(xPathFactoryFeatures, "xPathFfactoryFeatures");
  66.     }

  67.     /**
  68.      * Looks up the value for the key in the format "DocumentPath:XPath".
  69.      * <p>
  70.      * For example: "com/domain/document.xml:/path/to/node".
  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(SPLIT_STR);
  82.         final int keyLen = keys.length;
  83.         if (keyLen != 2) {
  84.             throw IllegalArgumentExceptions.format("Bad XML key format [%s]; expected format is DocumentPath:XPath.",
  85.                     key);
  86.         }
  87.         final String documentPath = keys[0];
  88.         final String xpath = StringUtils.substringAfter(key, SPLIT_CH);
  89.         try (InputStream inputStream = Files.newInputStream(getPath(documentPath))) {
  90.             final XPathFactory factory = XPathFactory.newInstance();
  91.             for (final Entry<String, Boolean> p : xPathFactoryFeatures.entrySet()) {
  92.                 factory.setFeature(p.getKey(), p.getValue());
  93.             }
  94.             return factory.newXPath().evaluate(xpath, new InputSource(inputStream));
  95.         } catch (final Exception e) {
  96.             throw IllegalArgumentExceptions.format(e, "Error looking up XML document [%s] and XPath [%s].",
  97.                     documentPath, xpath);
  98.         }
  99.     }

  100. }