001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.entities.configuration;
014:
015: import java.io.Serializable;
016:
017: import org.apache.avalon.framework.configuration.Configuration;
018: import org.apache.avalon.framework.configuration.ConfigurationException;
019: import org.apache.avalon.framework.configuration.DefaultConfiguration;
020: import org.rapla.entities.RaplaObject;
021: import org.rapla.entities.RaplaType;
022:
023: /**
024: *
025: * @author ckohlhaas
026: * @version 1.00.00
027: * @since 2.03.00
028: */
029: public class RaplaConfiguration implements RaplaObject, Serializable {
030: // Don't forget to increase the serialVersionUID when you change the fields
031: private static final long serialVersionUID = 1;
032:
033: public static final RaplaType TYPE = new RaplaType(
034: RaplaConfiguration.class, "config");
035: private Configuration conf;
036:
037: /** Creates a RaplaConfinguration with one element of the specified name
038: * @param name the element name
039: * @param content The content of the element. Can be null.
040: */
041: public RaplaConfiguration(String name, String content) {
042: this .conf = new DefaultConfiguration(name);
043: if (content != null) {
044: ((DefaultConfiguration) this .conf).setValue(content);
045: }
046: }
047:
048: public RaplaConfiguration(Configuration conf) {
049: this .conf = conf;
050: }
051:
052: public Configuration getConfig() {
053: return conf;
054: }
055:
056: public RaplaType getRaplaType() {
057: return TYPE;
058: }
059:
060: public Configuration find(String localName) {
061: Configuration[] childList = getConfig().getChildren();
062: for (int i = 0; i < childList.length; i++) {
063: if (childList[i].getName().equals(localName)) {
064: return childList[i];
065: }
066: }
067: return null;
068: }
069:
070: public Configuration find(String attributeName,
071: String attributeValue) {
072: Configuration[] childList = getConfig().getChildren();
073: for (int i = 0; i < childList.length; i++) {
074: String attribute = childList[i].getAttribute(attributeName,
075: null);
076: if (attributeValue.equals(attribute)) {
077: return childList[i];
078: }
079: }
080: return null;
081: }
082:
083: public String getNamespace() {
084: try {
085: return getConfig().getNamespace();
086: } catch (ConfigurationException ex) {
087: return null;
088: }
089: }
090:
091: public RaplaConfiguration replace(Configuration newChild)
092: throws ConfigurationException {
093: return replace(find(newChild.getName()), newChild);
094: }
095:
096: public RaplaConfiguration replace(Configuration oldChild,
097: Configuration newChild) {
098: String localName = getConfig().getName();
099: String nameSpace = getNamespace();
100: DefaultConfiguration newConfig = new DefaultConfiguration(
101: localName, nameSpace);
102: boolean present = false;
103: Configuration[] childList = getConfig().getChildren();
104: for (int i = 0; i < childList.length; i++) {
105: if (childList[i] != oldChild) {
106: newConfig.addChild(childList[i]);
107: } else {
108: present = true;
109: newConfig.addChild(newChild);
110: }
111: }
112: if (!present) {
113: newConfig.addChild(newChild);
114: }
115: return new RaplaConfiguration(newConfig);
116: }
117:
118: public RaplaConfiguration add(Configuration newChild) {
119: String localName = getConfig().getName();
120: String nameSpace = getNamespace();
121: DefaultConfiguration newConfig = new DefaultConfiguration(
122: localName, nameSpace);
123: boolean present = false;
124: Configuration[] childList = getConfig().getChildren();
125: for (int i = 0; i < childList.length; i++) {
126: if (childList[i] == newChild) {
127: present = true;
128: }
129: }
130: if (!present) {
131: newConfig.addChild(newChild);
132: }
133: return new RaplaConfiguration(newConfig);
134: }
135:
136: /**
137: * @param configuration
138: * @throws ConfigurationException
139: */
140: public RaplaConfiguration remove(Configuration configuration) {
141: String localName = getConfig().getName();
142: String nameSpace = getNamespace();
143: DefaultConfiguration newConfig = new DefaultConfiguration(
144: localName, nameSpace);
145: Configuration[] childList = getConfig().getChildren();
146: for (int i = 0; i < childList.length; i++) {
147: if (childList[i] != configuration) {
148: newConfig.addChild(childList[i]);
149: }
150: }
151: return new RaplaConfiguration(newConfig);
152: }
153:
154: public boolean equals(Object obj) {
155: if (obj == null || !(obj instanceof RaplaConfiguration)) {
156: return false;
157: }
158: if (obj == this ) {
159: return true;
160: }
161: RaplaConfiguration other = (RaplaConfiguration) obj;
162: return this.conf != null && other.conf != null
163: && this.conf.equals(other.conf);
164: }
165:
166: }
|