001: package org.drools.xml;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: /**
026: * Default implementation of <code>Configuration</code>.
027: *
028: * @author <a href="mailto:bob@werken.com">bob mcwhirter </a>
029: *
030: * @version $Id: DefaultConfiguration.java,v 1.2 2004/09/17 00:25:09 mproctor
031: * Exp $
032: */
033: public class DefaultConfiguration implements Configuration {
034: // ----------------------------------------------------------------------
035: // Class members
036: // ----------------------------------------------------------------------
037:
038: /** Empty <code>String</code> array. */
039: private static final String[] EMPTY_STRING_ARRAY = new String[0];
040:
041: // ----------------------------------------------------------------------
042: // Instance members
043: // ----------------------------------------------------------------------
044:
045: /** Node name. */
046: private final String name;
047:
048: /** Node text. */
049: private String text = "";
050:
051: /** Node attributes. */
052: private final Map attrs;
053:
054: /** Children nodes. */
055: private final List children;
056:
057: // ----------------------------------------------------------------------
058: // Constructors
059: // ----------------------------------------------------------------------
060:
061: /**
062: * Construct.
063: *
064: * @param name
065: * The name of the node.
066: */
067: public DefaultConfiguration(final String name) {
068: this .name = name;
069: this .attrs = new HashMap();
070: this .children = new ArrayList();
071: }
072:
073: /**
074: * @see Configuration
075: */
076: public String getName() {
077: return this .name;
078: }
079:
080: /**
081: * Set the node text.
082: *
083: * @param text
084: * The text.
085: */
086: public void setText(final String text) {
087: this .text = text;
088: }
089:
090: /**
091: * @see Configuration
092: */
093: public String getText() {
094: return this .text;
095: }
096:
097: /**
098: * Set an attribute value.
099: *
100: * @param name
101: * The attribute name.
102: * @param value
103: * The attribute value.
104: */
105: public void setAttribute(final String name, final String value) {
106: this .attrs.put(name, value);
107: }
108:
109: /**
110: * @see Configuration
111: */
112: public String getAttribute(final String name) {
113: return (String) this .attrs.get(name);
114: }
115:
116: /**
117: * @see Configuration
118: */
119: public String[] getAttributeNames() {
120: return (String[]) this .attrs.keySet().toArray(
121: DefaultConfiguration.EMPTY_STRING_ARRAY);
122: }
123:
124: /**
125: * Add a child <code>Configuration</code>.
126: *
127: * @param config
128: * The child.
129: */
130: public void addChild(final Configuration config) {
131: this .children.add(config);
132: }
133:
134: /**
135: * @see Configuration
136: */
137: public Configuration getChild(final String name) {
138: for (final Iterator childIter = this .children.iterator(); childIter
139: .hasNext();) {
140: final Configuration eachConfig = (Configuration) childIter
141: .next();
142:
143: if (eachConfig.getName().equals(name)) {
144: return eachConfig;
145: }
146: }
147:
148: return null;
149: }
150:
151: /**
152: * @see Configuration
153: */
154: public Configuration[] getChildren(final String name) {
155: final List result = new ArrayList();
156:
157: for (final Iterator childIter = this .children.iterator(); childIter
158: .hasNext();) {
159: final Configuration eachConfig = (Configuration) childIter
160: .next();
161:
162: if (eachConfig.getName().equals(name)) {
163: result.add(eachConfig);
164: }
165: }
166:
167: return (Configuration[]) result
168: .toArray(Configuration.EMPTY_ARRAY);
169: }
170:
171: /**
172: * @see Configuration
173: */
174: public Configuration[] getChildren() {
175: return (Configuration[]) this.children
176: .toArray(Configuration.EMPTY_ARRAY);
177: }
178: }
|