View Javadoc

    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.configuration.tree;
    18
    19  import java.util.Iterator;
    20
    21  /***
    22   * <p>
    23   * A specialized node implementation to be used in view configurations.
    24   * </p>
    25   * <p>
    26   * Some configurations provide a logical view on the nodes of other
    27   * configurations. These configurations construct their own hierarchy of nodes
    28   * based on the node trees of their source configurations. This special node
    29   * class can be used for this purpose. It allows child nodes and attributes to
    30   * be added without changing their parent node. So a node can belong to a
    31   * hierarchy of nodes of a source configuration, but be also contained in a view
    32   * configuration.
    33   * </p>
    34   *
    35   * @author <a
    36   * href="http://jakarta.apache.org/commons/configuration/team-list.html">Commons
    37   * Configuration team</a>
    38   * @version $Id: ViewNode.java 439648 2006-09-02 20:42:10Z oheger $
    39   * @since 1.3
    40   */
    41  public class ViewNode extends DefaultConfigurationNode
    42  {
    43      /***
    44       * Adds an attribute to this view node. The new attribute's parent node will
    45       * be saved.
    46       *
    47       * @param attr the attribute node to be added
    48       */
    49      public void addAttribute(ConfigurationNode attr)
    50      {
    51          ConfigurationNode parent = null;
    52
    53          if (attr != null)
    54          {
    55              parent = attr.getParentNode();
    56              super.addAttribute(attr);
    57              attr.setParentNode(parent);
    58          }
    59          else
    60          {
    61              throw new IllegalArgumentException("Attribute node must not be null!");
    62          }
    63      }
    64
    65      /***
    66       * Adds a child node to this view node. The new child's parent node will be
    67       * saved.
    68       *
    69       * @param child the child node to be added
    70       */
    71      public void addChild(ConfigurationNode child)
    72      {
    73          ConfigurationNode parent = null;
    74
    75          if (child != null)
    76          {
    77              parent = child.getParentNode();
    78              super.addChild(child);
    79              child.setParentNode(parent);
    80          }
    81          else
    82          {
    83              throw new IllegalArgumentException("Child node must not be null!");
    84          }
    85      }
    86
    87      /***
    88       * Adds all attribute nodes of the given source node to this view node.
    89       *
    90       * @param source the source node
    91       */
    92      public void appendAttributes(ConfigurationNode source)
    93      {
    94          if (source != null)
    95          {
    96              for (Iterator it = source.getAttributes().iterator(); it.hasNext();)
    97              {
    98                  addAttribute((ConfigurationNode) it.next());
    99              }
    100         }
    101     }
    102
    103     /***
    104      * Adds all child nodes of the given source node to this view node.
    105      *
    106      * @param source the source node
    107      */
    108     public void appendChildren(ConfigurationNode source)
    109     {
    110         if (source != null)
    111         {
    112             for (Iterator it = source.getChildren().iterator(); it.hasNext();)
    113             {
    114                 addChild((ConfigurationNode) it.next());
    115             }
    116         }
    117     }
    118 }