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