001: ////////////////////////////////////////////////////////////////////////////////
002: // checkstyle: Checks Java source code for adherence to a set of rules.
003: // Copyright (C) 2001-2007 Oliver Burn
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: ////////////////////////////////////////////////////////////////////////////////
019: package com.puppycrawl.tools.checkstyle;
020:
021: import java.util.ArrayList;
022: import java.util.Map;
023: import java.util.HashMap;
024: import java.util.Set;
025:
026: import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
027: import com.puppycrawl.tools.checkstyle.api.Configuration;
028:
029: /**
030: * Default implementation of the Configuration interface.
031: * @author lkuehne
032: */
033: public final class DefaultConfiguration implements Configuration {
034: /** The name of this configuration */
035: private final String mName;
036:
037: /** the list of child Configurations */
038: private final ArrayList mChildren = new ArrayList();
039:
040: /** the map from attribute names to attribute values */
041: private final Map mAttributeMap = new HashMap();
042:
043: /**
044: * Instantiates a DefaultConfiguration.
045: * @param aName the name for this DefaultConfiguration.
046: */
047: public DefaultConfiguration(String aName) {
048: mName = aName;
049: }
050:
051: /** {@inheritDoc} */
052: public String[] getAttributeNames() {
053: final Set keySet = mAttributeMap.keySet();
054: return (String[]) keySet.toArray(new String[keySet.size()]);
055: }
056:
057: /** {@inheritDoc} */
058: public String getAttribute(String aName) throws CheckstyleException {
059: if (!mAttributeMap.containsKey(aName)) {
060: // TODO: i18n
061: throw new CheckstyleException("missing key '" + aName
062: + "' in " + getName());
063: }
064: return (String) mAttributeMap.get(aName);
065: }
066:
067: /** {@inheritDoc} */
068: public Configuration[] getChildren() {
069: return (Configuration[]) mChildren
070: .toArray(new Configuration[mChildren.size()]);
071: }
072:
073: /** {@inheritDoc} */
074: public String getName() {
075: return mName;
076: }
077:
078: /**
079: * Makes a configuration a child of this configuration.
080: * @param aConfiguration the child configuration.
081: */
082: public void addChild(Configuration aConfiguration) {
083: mChildren.add(aConfiguration);
084: }
085:
086: /**
087: * Removes a child of this configuration.
088: * @param aConfiguration the child configuration to remove.
089: */
090: public void removeChild(final Configuration aConfiguration) {
091: mChildren.remove(aConfiguration);
092: }
093:
094: /**
095: * Adds an attribute to this configuration.
096: * @param aName the name of the attribute.
097: * @param aValue the value of the attribute.
098: */
099: public void addAttribute(String aName, String aValue) {
100: mAttributeMap.put(aName, aValue);
101: }
102:
103: }
|