ConfigurationNodePointer.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.tree.xpath;

  18. import java.util.Locale;

  19. import org.apache.commons.configuration2.tree.NodeHandler;
  20. import org.apache.commons.jxpath.ri.Compiler;
  21. import org.apache.commons.jxpath.ri.QName;
  22. import org.apache.commons.jxpath.ri.compiler.NodeTest;
  23. import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
  24. import org.apache.commons.jxpath.ri.model.NodeIterator;
  25. import org.apache.commons.jxpath.ri.model.NodePointer;

  26. /**
  27.  * <p>
  28.  * A specific {@code NodePointer} implementation for configuration nodes.
  29.  * </p>
  30.  * <p>
  31.  * This is needed for queries using JXPath.
  32.  * </p>
  33.  *
  34.  * @param <T> the type of the nodes this pointer deals with
  35.  * @since 1.3
  36.  */
  37. final class ConfigurationNodePointer<T> extends NodePointer {
  38.     /**
  39.      * The serial version UID.
  40.      */
  41.     private static final long serialVersionUID = -1087475639680007713L;

  42.     /** The node handler. */
  43.     private final NodeHandler<T> handler;

  44.     /** Stores the associated node. */
  45.     private final T node;

  46.     /**
  47.      * Creates a new instance of {@code ConfigurationNodePointer} and initializes it with its parent pointer.
  48.      *
  49.      * @param parent the parent pointer
  50.      * @param node the associated node
  51.      * @param handler the {@code NodeHandler}
  52.      */
  53.     public ConfigurationNodePointer(final ConfigurationNodePointer<T> parent, final T node, final NodeHandler<T> handler) {
  54.         super(parent);
  55.         this.node = node;
  56.         this.handler = handler;
  57.     }

  58.     /**
  59.      * Creates a new instance of {@code ConfigurationNodePointer} pointing to the specified node.
  60.      *
  61.      * @param node the wrapped node
  62.      * @param locale the locale
  63.      * @param handler the {@code NodeHandler}
  64.      */
  65.     public ConfigurationNodePointer(final T node, final Locale locale, final NodeHandler<T> handler) {
  66.         super(null, locale);
  67.         this.node = node;
  68.         this.handler = handler;
  69.     }

  70.     /**
  71.      * Returns an iterator for the attributes that match the given name.
  72.      *
  73.      * @param qName the attribute name
  74.      * @return the iterator for the attributes
  75.      */
  76.     @Override
  77.     public NodeIterator attributeIterator(final QName qName) {
  78.         return new ConfigurationNodeIteratorAttribute<>(this, qName);
  79.     }

  80.     /**
  81.      * Casts the given child pointer to a node pointer of this type. This is a bit dangerous. However, in a typical setup,
  82.      * child node pointers can only be created by this instance which ensures that they are of the correct type. Therefore,
  83.      * this cast is safe.
  84.      *
  85.      * @param p the {@code NodePointer} to cast
  86.      * @return the resulting {@code ConfigurationNodePointer}
  87.      */
  88.     private ConfigurationNodePointer<T> castPointer(final NodePointer p) {
  89.         @SuppressWarnings("unchecked") // see method comment
  90.         final ConfigurationNodePointer<T> result = (ConfigurationNodePointer<T>) p;
  91.         return result;
  92.     }

  93.     /**
  94.      * Returns an iterator for the children of this pointer that match the given test object.
  95.      *
  96.      * @param test the test object
  97.      * @param reverse the reverse flag
  98.      * @param startWith the start value of the iteration
  99.      */
  100.     @Override
  101.     public NodeIterator childIterator(final NodeTest test, final boolean reverse, final NodePointer startWith) {
  102.         return new ConfigurationNodeIteratorChildren<>(this, test, reverse, castPointer(startWith));
  103.     }

  104.     /**
  105.      * Compares two child node pointers.
  106.      *
  107.      * @param pointer1 one pointer
  108.      * @param pointer2 another pointer
  109.      * @return a flag, which pointer should be sorted first
  110.      */
  111.     @Override
  112.     public int compareChildNodePointers(final NodePointer pointer1, final NodePointer pointer2) {
  113.         final Object node1 = pointer1.getBaseValue();
  114.         final Object node2 = pointer2.getBaseValue();

  115.         // sort based on the occurrence in the sub node list
  116.         for (final T child : getNodeHandler().getChildren(node)) {
  117.             if (child == node1) {
  118.                 return -1;
  119.             }
  120.             if (child == node2) {
  121.                 return 1;
  122.             }
  123.         }
  124.         return 0; // should not happen
  125.     }

  126.     /**
  127.      * Gets this node's base value. This is the associated configuration node.
  128.      *
  129.      * @return the base value
  130.      */
  131.     @Override
  132.     public Object getBaseValue() {
  133.         return node;
  134.     }

  135.     /**
  136.      * Gets the wrapped configuration node.
  137.      *
  138.      * @return the wrapped node
  139.      */
  140.     public T getConfigurationNode() {
  141.         return node;
  142.     }

  143.     /**
  144.      * Gets the immediate node. This is the associated configuration node.
  145.      *
  146.      * @return the immediate node
  147.      */
  148.     @Override
  149.     public Object getImmediateNode() {
  150.         return node;
  151.     }

  152.     /**
  153.      * Gets this node's length. This is always 1.
  154.      *
  155.      * @return the node's length
  156.      */
  157.     @Override
  158.     public int getLength() {
  159.         return 1;
  160.     }

  161.     /**
  162.      * Gets this node's name.
  163.      *
  164.      * @return the name
  165.      */
  166.     @Override
  167.     public QName getName() {
  168.         return new QName(null, getNodeHandler().nodeName(node));
  169.     }

  170.     /**
  171.      * Gets the {@code NodeHandler} used by this instance.
  172.      *
  173.      * @return the {@code NodeHandler}
  174.      */
  175.     public NodeHandler<T> getNodeHandler() {
  176.         return handler;
  177.     }

  178.     /**
  179.      * Gets the value of this node.
  180.      *
  181.      * @return the represented node's value
  182.      */
  183.     @Override
  184.     public Object getValue() {
  185.         return getNodeHandler().getValue(node);
  186.     }

  187.     /**
  188.      * Checks whether this node pointer refers to an attribute node. This is not the case.
  189.      *
  190.      * @return the attribute flag
  191.      */
  192.     @Override
  193.     public boolean isAttribute() {
  194.         return false;
  195.     }

  196.     /**
  197.      * Returns a flag if this node is a collection. This is not the case.
  198.      *
  199.      * @return the collection flag
  200.      */
  201.     @Override
  202.     public boolean isCollection() {
  203.         return false;
  204.     }

  205.     /**
  206.      * Returns a flag whether this node is a leaf. This is the case if there are no child nodes.
  207.      *
  208.      * @return a flag if this node is a leaf
  209.      */
  210.     @Override
  211.     public boolean isLeaf() {
  212.         return getNodeHandler().getChildrenCount(node, null) < 1;
  213.     }

  214.     /**
  215.      * Sets the value of this node. This is not supported, so always an exception is thrown.
  216.      *
  217.      * @param value the new value
  218.      */
  219.     @Override
  220.     public void setValue(final Object value) {
  221.         throw new UnsupportedOperationException("Node value cannot be set!");
  222.     }

  223.     /**
  224.      * Tests if this node matches the given test. Configuration nodes are text nodes, too because they can contain a value.
  225.      *
  226.      * @param test the test object
  227.      * @return a flag if this node corresponds to the test
  228.      */
  229.     @Override
  230.     public boolean testNode(final NodeTest test) {
  231.         if (test instanceof NodeTypeTest && ((NodeTypeTest) test).getNodeType() == Compiler.NODE_TYPE_TEXT) {
  232.             return true;
  233.         }
  234.         return super.testNode(test);
  235.     }
  236. }