ConfigurationAttributePointer.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 org.apache.commons.configuration2.tree.NodeHandler;
  19. import org.apache.commons.configuration2.tree.QueryResult;
  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.NodePointer;

  25. /**
  26.  * <p>
  27.  * A specialized {@code NodePointer} implementation for the attributes of a configuration node.
  28.  * </p>
  29.  *
  30.  * @param <T> the type of the nodes this pointer deals with
  31.  * @since 2.0
  32.  */
  33. final class ConfigurationAttributePointer<T> extends NodePointer {
  34.     /**
  35.      * The serial version UID.
  36.      */
  37.     private static final long serialVersionUID = 5504551041716043748L;

  38.     /** Stores information about the represented attribute. */
  39.     private final QueryResult<T> attributeResult;

  40.     /**
  41.      * Creates a new instance of {@code ConfigurationAttributePointer}.
  42.      *
  43.      * @param parent the parent node pointer
  44.      * @param attrName the name of the managed attribute
  45.      */
  46.     public ConfigurationAttributePointer(final ConfigurationNodePointer<T> parent, final String attrName) {
  47.         super(parent);
  48.         attributeResult = QueryResult.createAttributeResult(parent.getConfigurationNode(), attrName);
  49.     }

  50.     /**
  51.      * Compares two child node pointers. Attributes do not have any children, so this is just a dummy implementation.
  52.      *
  53.      * @param p1 the first pointer
  54.      * @param p2 the second pointer
  55.      * @return the order of these pointers
  56.      */
  57.     @Override
  58.     public int compareChildNodePointers(final NodePointer p1, final NodePointer p2) {
  59.         return 0;
  60.     }

  61.     /**
  62.      * Gets the base value. We return the value.
  63.      *
  64.      * @return the base value
  65.      */
  66.     @Override
  67.     public Object getBaseValue() {
  68.         return getValue();
  69.     }

  70.     /**
  71.      * Gets the immediate node. This is actually a {@link QueryResult} object describing the represented attribute.
  72.      *
  73.      * @return the immediate node
  74.      */
  75.     @Override
  76.     public Object getImmediateNode() {
  77.         return attributeResult;
  78.     }

  79.     /**
  80.      * Gets the length of the represented node. This is always 1.
  81.      *
  82.      * @return the length
  83.      */
  84.     @Override
  85.     public int getLength() {
  86.         return 1;
  87.     }

  88.     /**
  89.      * Gets the name of this node. This is the attribute name.
  90.      *
  91.      * @return the name of this node
  92.      */
  93.     @Override
  94.     public QName getName() {
  95.         return new QName(null, attributeResult.getAttributeName());
  96.     }

  97.     /**
  98.      * Returns a reference to the current node handler. The handler is obtained from the parent pointer.
  99.      *
  100.      * @return the node handler
  101.      */
  102.     private NodeHandler<T> getNodeHandler() {
  103.         return getParentPointer().getNodeHandler();
  104.     }

  105.     /**
  106.      * Gets a reference to the parent node pointer.
  107.      *
  108.      * @return the parent pointer
  109.      */
  110.     public ConfigurationNodePointer<T> getParentPointer() {
  111.         // safe to cast because the constructor only expects pointers of this
  112.         // type
  113.         @SuppressWarnings("unchecked")
  114.         final ConfigurationNodePointer<T> configurationNodePointer = (ConfigurationNodePointer<T>) getParent();
  115.         return configurationNodePointer;
  116.     }

  117.     /**
  118.      * Returns the value of this node.
  119.      *
  120.      * @return this node's value
  121.      */
  122.     @Override
  123.     public Object getValue() {
  124.         return attributeResult.getAttributeValue(getNodeHandler());
  125.     }

  126.     /**
  127.      * Returns a flag whether this node is an attribute. Of course, this is the case.
  128.      *
  129.      * @return the attribute flag
  130.      */
  131.     @Override
  132.     public boolean isAttribute() {
  133.         return true;
  134.     }

  135.     /**
  136.      * Returns a flag whether the represented node is a collection. This is not the case.
  137.      *
  138.      * @return the collection flag
  139.      */
  140.     @Override
  141.     public boolean isCollection() {
  142.         return false;
  143.     }

  144.     /**
  145.      * Returns a flag whether the represented node is a leaf. This is the case for attributes.
  146.      *
  147.      * @return the leaf flag
  148.      */
  149.     @Override
  150.     public boolean isLeaf() {
  151.         return true;
  152.     }

  153.     /**
  154.      * Sets the value of this node. This is not supported because the classes of the {@code XPathExpressionEngine} are only
  155.      * used for queries. This implementation always throws an exception.
  156.      *
  157.      * @param value the new value
  158.      */
  159.     @Override
  160.     public void setValue(final Object value) {
  161.         throw new UnsupportedOperationException("Updating the value is not supported!");
  162.     }

  163.     /**
  164.      * Tests if this node matches the given test. Attribute nodes are text nodes, too, because they can contain a value.
  165.      *
  166.      * @param test the test object
  167.      * @return a flag if this node corresponds to the test
  168.      */
  169.     @Override
  170.     public boolean testNode(final NodeTest test) {
  171.         if (test instanceof NodeTypeTest && ((NodeTypeTest) test).getNodeType() == Compiler.NODE_TYPE_TEXT) {
  172.             return true;
  173.         }
  174.         return super.testNode(test);
  175.     }
  176. }