Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 49707 invoked from network); 5 Apr 2008 15:28:06 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 5 Apr 2008 15:28:06 -0000 Received: (qmail 5700 invoked by uid 500); 5 Apr 2008 15:28:05 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 5652 invoked by uid 500); 5 Apr 2008 15:28:04 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 5643 invoked by uid 99); 5 Apr 2008 15:28:04 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 05 Apr 2008 08:28:04 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 05 Apr 2008 15:27:31 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5EC731A9832; Sat, 5 Apr 2008 08:27:43 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r645121 - /commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNodeHandler.java Date: Sat, 05 Apr 2008 15:27:43 -0000 To: commits@commons.apache.org From: oheger@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080405152743.5EC731A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: oheger Date: Sat Apr 5 08:27:42 2008 New Revision: 645121 URL: http://svn.apache.org/viewvc?rev=645121&view=rev Log: New node handler implementation for flat nodes Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNodeHandler.java (with props) Added: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNodeHandler.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNodeHandler.java?rev=645121&view=auto ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNodeHandler.java (added) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNodeHandler.java Sat Apr 5 08:27:42 2008 @@ -0,0 +1,309 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.configuration2.flat; + +import java.util.Collections; +import java.util.List; + +import org.apache.commons.configuration2.ConfigurationRuntimeException; +import org.apache.commons.configuration2.expr.AbstractNodeHandler; +import org.apache.commons.configuration2.expr.NodeHandler; + +/** + *

+ * A NodeHandler implementation for dealing with the nodes of a + * {@link AbstractFlatConfiguration}. + *

+ *

+ * This class is used internally by a AbstractFlatConfiguration + * to deal with its nodes. Note that a node structure is only constructed if the + * configuration is treated as a hierarchical configuration, for instance if it + * is added to a CombinedConfiguration. + *

+ *

+ * The implementation of the methods required by the + * {@link NodeHandler} interface is straight forward. In most + * cases, it is possible to simply delegate to the corresponding + * FlatNode method. Attributes are not supported by flat nodes, + * so in this area there are only dummy implementations. + *

+ *

+ * Actions caused by this node handler may modify the associated configuration + * and thus trigger change events. Per default the configuration will invalidate + * its node structure if a change event is received. Because of that the node + * handler has to keep track of the updated caused by itself to avoid + * unnecessary invalidation of nodes. (The configuration asks the node handler + * for each change event whether the node structure should be invalidated.) Note + * that modifications of a configuration are not thread-safe. So no additional + * synchronization is done in this class. + *

+ * + * @author Commons + * Configuration team + * @version $Id$ + * @since 2.0 + * @see FlatNode + */ +class FlatNodeHandler extends AbstractNodeHandler +{ + /** Stores the associated flat configuration. */ + private AbstractFlatConfiguration configuration; + + /** + * A flag whether an update of the configuration was caused by an operation + * on its node structure. + */ + private boolean internalUpdate; + + /** + * Creates a new instance of FlatNodeHandler and initializes + * it with the associated configuration. + * + * @param config the configuration + */ + public FlatNodeHandler(AbstractFlatConfiguration config) + { + configuration = config; + } + + /** + * Returns the configuration associated with this node handler. + * + * @return the associated configuration + */ + public AbstractFlatConfiguration getConfiguration() + { + return configuration; + } + + /** + * Returns a flag whether an update of the associated configuration was + * caused by this node handler. Whenever the configuration receives a change + * event, it asks the node hander whether it is responsible for this event. + * The result of this method determines whether the configuration's node + * structure has to be invalidated: if the event was caused by the node + * handler, the structure has already been updated and there is no need to + * invalidate it. Otherwise the configuration was directly manipulated, and + * the node structure is now out of sync. + * + * @return a flag whether an internal update was caused by this node handler + */ + public boolean isInternalUpdate() + { + return internalUpdate; + } + + /** + * Adds an attribute to the specified node. Flat nodes do not support + * attributes, so this implementation just throws an exception. + * + * @param node the node + * @param name the name of the attribute + * @param value the new value + * @throws ConfigurationRuntimeException if the attribute value cannot be + * added + */ + public void addAttributeValue(FlatNode node, String name, Object value) + { + throw new ConfigurationRuntimeException( + "Cannot add an attribute to a flat node!"); + } + + /** + * Adds a new child to the given node. + * + * @param node the node + * @param name the name of the new child + * @return the newly created child node + */ + public FlatNode addChild(FlatNode node, String name) + { + return node.addChild(name); + } + + /** + * Returns the value of an attribute of the specified node. Flat nodes do + * not support attributes, so this implementation always returns null. + * + * @param node the node + * @param name the name of the attribute + * @return the value of this attribute + */ + public Object getAttributeValue(FlatNode node, String name) + { + return null; + } + + /** + * Returns a list with the names of the attributes of the specified node. + * Flat nodes do not support attributes, so this implementation always + * returns an empty list. + * + * @param node the node + * @return a list with the names of the existing attributes + */ + public List getAttributes(FlatNode node) + { + return Collections.emptyList(); + } + + /** + * Returns the child of the specified node with the given index. + * + * @param node the node + * @param index the index + * @return the child node with this index + */ + public FlatNode getChild(FlatNode node, int index) + { + return node.getChild(index); + } + + /** + * Returns a list with all children of the specified node. + * + * @param node the node + * @return a list with all child nodes of this node + */ + public List getChildren(FlatNode node) + { + return node.getChildren(); + } + + /** + * Returns a list with all children of the specified node with the given + * name. + * + * @param node the node + * @param name the desired name + * @return a list with the child nodes with this name + */ + public List getChildren(FlatNode node, String name) + { + return node.getChildren(name); + } + + /** + * Returns the number of children with the given name of the specified node. + * + * @param node the node + * @param name the name of the desired child nodes + * @return the number of the child nodes with this name + */ + public int getChildrenCount(FlatNode node, String name) + { + return node.getChildrenCount(name); + } + + /** + * Returns the parent of the specified node. + * + * @param node the node + * @return the parent node + */ + public FlatNode getParent(FlatNode node) + { + return node.getParent(); + } + + /** + * Returns the value of the specified node. + * + * @param node the node + * @return the value of this node + */ + public Object getValue(FlatNode node) + { + return node.getValue(getConfiguration()); + } + + /** + * Returns the name of the specified node. + * + * @param node the node + * @return the name of this node + */ + public String nodeName(FlatNode node) + { + return node.getName(); + } + + /** + * Removes an attribute of the specified node. Flat nodes do not have + * attributes, so this implementation is just an empty dummy. + * + * @param node the node + * @param name the name of the attribute + */ + public void removeAttribute(FlatNode node, String name) + { + } + + /** + * Removes a child of the given parent node. + * + * @param node the parent node + * @param child the child node to be removed + */ + public void removeChild(FlatNode node, FlatNode child) + { + internalUpdate = true; + try + { + node.removeChild(getConfiguration(), child); + } + finally + { + internalUpdate = false; + } + } + + /** + * Sets an attribute of the specified node. Flat nodes do not support + * attributes, so this implementation just throws an exception. + * + * @param node the node + * @param name the name of the attribute + * @param value the new value + * @throws ConfigurationRuntimeException if the attribute value cannot be + * set + */ + public void setAttributeValue(FlatNode node, String name, Object value) + { + throw new ConfigurationRuntimeException( + "Cannot set an attribute of a flat node!"); + } + + /** + * Sets the value of the specified node. + * + * @param node the node + * @param value the new value + */ + public void setValue(FlatNode node, Object value) + { + internalUpdate = true; + try + { + node.setValue(getConfiguration(), value); + } + finally + { + internalUpdate = false; + } + } +} Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNodeHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNodeHandler.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNodeHandler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain