HierarchicalConfigurationXMLReader.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.configuration2;

  18. import org.apache.commons.configuration2.tree.ConfigurationNodeVisitorAdapter;
  19. import org.apache.commons.configuration2.tree.NodeHandler;
  20. import org.apache.commons.configuration2.tree.NodeTreeWalker;
  21. import org.xml.sax.Attributes;
  22. import org.xml.sax.helpers.AttributesImpl;

  23. /**
  24.  * <p>
  25.  * A specialized SAX2 XML parser that "parses" hierarchical configuration objects.
  26.  * </p>
  27.  * <p>
  28.  * This class mimics to be a SAX conform XML parser. Instead of parsing XML documents it processes a
  29.  * {@code Configuration} object and generates SAX events for the single properties defined there. This enables the whole
  30.  * world of XML processing for configuration objects.
  31.  * </p>
  32.  * <p>
  33.  * The {@code HierarchicalConfiguration} object to be parsed can be specified using a constructor or the
  34.  * {@code setConfiguration()} method. This object will be processed by the {@code parse()} methods. Note that these
  35.  * methods ignore their argument.
  36.  * </p>
  37.  *
  38.  * @param <T> the type of the nodes supported by this reader
  39.  */
  40. public class HierarchicalConfigurationXMLReader<T> extends ConfigurationXMLReader {
  41.     /**
  42.      * A specialized visitor class for generating SAX events for a hierarchical node structure.
  43.      */
  44.     private final class SAXVisitor extends ConfigurationNodeVisitorAdapter<T> {
  45.         /** Constant for the attribute type. */
  46.         private static final String ATTR_TYPE = "CDATA";

  47.         /**
  48.          * Returns an object with all attributes for the specified node.
  49.          *
  50.          * @param node the current node
  51.          * @param handler the node handler
  52.          * @return an object with all attributes of this node
  53.          */
  54.         protected Attributes fetchAttributes(final T node, final NodeHandler<T> handler) {
  55.             final AttributesImpl attrs = new AttributesImpl();

  56.             handler.getAttributes(node).forEach(attr -> {
  57.                 final Object value = handler.getAttributeValue(node, attr);
  58.                 if (value != null) {
  59.                     attrs.addAttribute(NS_URI, attr, attr, ATTR_TYPE, value.toString());
  60.                 }
  61.             });

  62.             return attrs;
  63.         }

  64.         /**
  65.          * Helper method for determining the name of a node. If a node has no name (which is true for the root node), the
  66.          * specified default name will be used.
  67.          *
  68.          * @param node the node to be checked
  69.          * @param handler the node handler
  70.          * @return the name for this node
  71.          */
  72.         private String nodeName(final T node, final NodeHandler<T> handler) {
  73.             final String nodeName = handler.nodeName(node);
  74.             return nodeName == null ? getRootName() : nodeName;
  75.         }

  76.         /**
  77.          * Checks if iteration should be terminated. This implementation stops iteration after an exception has occurred.
  78.          *
  79.          * @return a flag if iteration should be stopped
  80.          */
  81.         @Override
  82.         public boolean terminate() {
  83.             return getException() != null;
  84.         }

  85.         /**
  86.          * Visits the specified node after its children have been processed.
  87.          *
  88.          * @param node the actual node
  89.          * @param handler the node handler
  90.          */
  91.         @Override
  92.         public void visitAfterChildren(final T node, final NodeHandler<T> handler) {
  93.             fireElementEnd(nodeName(node, handler));
  94.         }

  95.         /**
  96.          * Visits the specified node.
  97.          *
  98.          * @param node the actual node
  99.          * @param handler the node handler
  100.          */
  101.         @Override
  102.         public void visitBeforeChildren(final T node, final NodeHandler<T> handler) {
  103.             fireElementStart(nodeName(node, handler), fetchAttributes(node, handler));

  104.             final Object value = handler.getValue(node);
  105.             if (value != null) {
  106.                 fireCharacters(value.toString());
  107.             }
  108.         }
  109.     }

  110.     /** Stores the configuration object to be parsed. */
  111.     private HierarchicalConfiguration<T> configuration;

  112.     /**
  113.      * Creates a new instance of {@code HierarchicalConfigurationXMLReader}.
  114.      */
  115.     public HierarchicalConfigurationXMLReader() {
  116.     }

  117.     /**
  118.      * Creates a new instance of {@code HierarchicalConfigurationXMLReader} and sets the configuration to be parsed.
  119.      *
  120.      * @param config the configuration object
  121.      */
  122.     public HierarchicalConfigurationXMLReader(final HierarchicalConfiguration<T> config) {
  123.         this();
  124.         setConfiguration(config);
  125.     }

  126.     /**
  127.      * Gets the configuration object to be parsed.
  128.      *
  129.      * @return the configuration object to be parsed
  130.      */
  131.     public HierarchicalConfiguration<T> getConfiguration() {
  132.         return configuration;
  133.     }

  134.     /**
  135.      * Gets the configuration object to be processed.
  136.      *
  137.      * @return the actual configuration object
  138.      */
  139.     @Override
  140.     public Configuration getParsedConfiguration() {
  141.         return getConfiguration();
  142.     }

  143.     /**
  144.      * Processes the actual configuration object to generate SAX parsing events.
  145.      */
  146.     @Override
  147.     protected void processKeys() {
  148.         final NodeHandler<T> nodeHandler = getConfiguration().getNodeModel().getNodeHandler();
  149.         NodeTreeWalker.INSTANCE.walkDFS(nodeHandler.getRootNode(), new SAXVisitor(), nodeHandler);
  150.     }

  151.     /**
  152.      * Sets the configuration object to be parsed.
  153.      *
  154.      * @param config the configuration object to be parsed
  155.      */
  156.     public void setConfiguration(final HierarchicalConfiguration<T> config) {
  157.         configuration = config;
  158.     }
  159. }