001: /**********************************************************************
002: Copyright (c) 2006 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.plugin;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: /**
024: * Represents XML elements declared nested in the extension element
025: * @version $Revision: 1.6 $
026: */
027: public class ConfigurationElement {
028: /** parent element **/
029: final private ConfigurationElement parent;
030: /** child elements **/
031: private ConfigurationElement[] children;
032: /** attributes **/
033: final private Map attributes = new HashMap();
034: /** attributes **/
035: private String[] attributeNames;
036: /** element name **/
037: private String name;
038: /** text of element **/
039: private String text;
040: /** the Extension **/
041: private Extension extension;
042:
043: /**
044: * Constructor
045: * @param name the element's name
046: * @param parent the parent. null if there is no parent
047: */
048: public ConfigurationElement(Extension extension, String name,
049: ConfigurationElement parent) {
050: this .extension = extension;
051: this .name = name;
052: this .parent = parent;
053: this .attributeNames = new String[0];
054: this .children = new ConfigurationElement[0];
055: }
056:
057: /**
058: * Acessor for the name of this element
059: * @return the name of this element
060: */
061: public String getName() {
062: return name;
063: }
064:
065: /**
066: * Acessor for the parent of this ConfigurationElement
067: * @return can return null if there is no parent, or the parent is the Extension
068: */
069: public ConfigurationElement getParent() {
070: return parent;
071: }
072:
073: /**
074: * Acessor for all children of this ConfigurationElement
075: * @return the ConfigurationElement declared nested in this element
076: */
077: public ConfigurationElement[] getChildren() {
078: return children;
079: }
080:
081: /**
082: * Acessor for the attribute value by a given name
083: * @param name the attribute name
084: * @return null if the attribute cannot be found
085: */
086: public String getAttribute(String name) {
087: return (String) attributes.get(name);
088: }
089:
090: /**
091: * Put a new attribute to this element
092: * @param name the attribute's name
093: * @param value the attribute's value
094: */
095: public void putAttribute(String name, String value) {
096: String[] names = new String[attributeNames.length + 1];
097: System.arraycopy(attributeNames, 0, names, 0,
098: attributeNames.length);
099: names[attributeNames.length] = name;
100: attributeNames = names;
101: attributes.put(name, value);
102: }
103:
104: /**
105: * Add a new children ConfigurationElement to this element
106: * @param confElm the ConfigurationElement
107: */
108: public void addConfigurationElement(ConfigurationElement confElm) {
109: ConfigurationElement[] elm = new ConfigurationElement[children.length + 1];
110: System.arraycopy(children, 0, elm, 0, children.length);
111: elm[children.length] = confElm;
112: children = elm;
113: }
114:
115: /**
116: * Acessor for all attribute names declared in this element
117: * @return the attribute names
118: */
119: public String[] getAttributeNames() {
120: return attributeNames;
121: }
122:
123: /**
124: * Setter to the text
125: * @param text the text
126: */
127: public void setText(String text) {
128: this .text = text;
129: }
130:
131: /**
132: * Accessor to the text
133: * @return the text
134: */
135: public String getText() {
136: return text;
137: }
138:
139: /**
140: * Accesstor to the {@link Extension}
141: * @return the {@link Extension}
142: */
143: public Extension getExtension() {
144: return extension;
145: }
146:
147: public String toString() {
148: return name + " " + attributes;
149: }
150: }
|