001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.configuration.tree;
018:
019: import java.util.Collections;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: /**
024: * <p>
025: * A simple data class used by <code>{@link ExpressionEngine}</code> to store
026: * the results of the <code>prepareAdd()</code> operation.
027: * </p>
028: * <p>
029: * If a new property is to be added to a configuration, the affected
030: * <code>Configuration</code> object must know, where in its hierarchy of
031: * configuration nodes new elements have to be added. This information is
032: * obtained by an <code>ExpressionEngine</code> object that interprets the key
033: * of the new property. This expression engine will pack all information
034: * necessary for the configuration to perform the add operation in an instance
035: * of this class.
036: * </p>
037: * <p>
038: * Information managed by this class contains:
039: * <ul>
040: * <li>the configuration node, to which new elements must be added</li>
041: * <li>the name of the new node</li>
042: * <li>whether the new node is a child node or an attribute node</li>
043: * <li>if a whole branch is to be added at once, the names of all nodes between
044: * the parent node (the target of the add operation) and the new node</li>
045: * </ul>
046: * </p>
047: *
048: * @since 1.3
049: * @author Oliver Heger
050: */
051: public class NodeAddData {
052: /** Stores the parent node of the add operation. */
053: private ConfigurationNode parent;
054:
055: /**
056: * Stores a list with nodes that are on the path between the parent node and
057: * the new node.
058: */
059: private List pathNodes;
060:
061: /** Stores the name of the new node. */
062: private String newNodeName;
063:
064: /** Stores the attribute flag. */
065: private boolean attribute;
066:
067: /**
068: * Creates a new, uninitialized instance of <code>NodeAddData</code>.
069: */
070: public NodeAddData() {
071: this (null, null);
072: }
073:
074: /**
075: * Creates a new instance of <code>NodeAddData</code> and sets the most
076: * important data fields.
077: *
078: * @param parent the parent node
079: * @param nodeName the name of the new node
080: */
081: public NodeAddData(ConfigurationNode parent, String nodeName) {
082: setParent(parent);
083: setNewNodeName(nodeName);
084: }
085:
086: /**
087: * Returns a flag if the new node to be added is an attribute.
088: *
089: * @return <b>true</b> for an attribute node, <b>false</b> for a child
090: * node
091: */
092: public boolean isAttribute() {
093: return attribute;
094: }
095:
096: /**
097: * Sets the attribute flag. This flag determines whether an attribute or a
098: * child node will be added.
099: *
100: * @param attribute the attribute flag
101: */
102: public void setAttribute(boolean attribute) {
103: this .attribute = attribute;
104: }
105:
106: /**
107: * Returns the name of the new node.
108: *
109: * @return the new node's name
110: */
111: public String getNewNodeName() {
112: return newNodeName;
113: }
114:
115: /**
116: * Sets the name of the new node. A node with this name will be added to the
117: * configuration's node hierarchy.
118: *
119: * @param newNodeName the name of the new node
120: */
121: public void setNewNodeName(String newNodeName) {
122: this .newNodeName = newNodeName;
123: }
124:
125: /**
126: * Returns the parent node.
127: *
128: * @return the parent node
129: */
130: public ConfigurationNode getParent() {
131: return parent;
132: }
133:
134: /**
135: * Sets the parent node. New nodes will be added to this node.
136: *
137: * @param parent the parent node
138: */
139: public void setParent(ConfigurationNode parent) {
140: this .parent = parent;
141: }
142:
143: /**
144: * Returns a list with further nodes that must be added. This is needed if a
145: * complete branch is to be added at once. For instance imagine that there
146: * exists only a node <code>database</code>. Now the key
147: * <code>database.connection.settings.username</code> (assuming the syntax
148: * of the default expression engine) is to be added. Then
149: * <code>username</code> is the name of the new node, but the nodes
150: * <code>connection</code> and <code>settings</code> must be added to
151: * the parent node first. In this example these names would be returned by
152: * this method.
153: *
154: * @return a list with the names of nodes that must be added as parents of
155: * the new node (never <b>null</b>)
156: */
157: public List getPathNodes() {
158: return (pathNodes != null) ? Collections
159: .unmodifiableList(pathNodes) : Collections.EMPTY_LIST;
160: }
161:
162: /**
163: * Adds the name of a path node. With this method an additional node to be
164: * added can be defined.
165: *
166: * @param nodeName the name of the node
167: * @see #getPathNodes()
168: */
169: public void addPathNode(String nodeName) {
170: if (pathNodes == null) {
171: pathNodes = new LinkedList();
172: }
173: pathNodes.add(nodeName);
174: }
175: }
|