001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.config;
017:
018: import java.util.StringTokenizer;
019:
020: import org.columa.core.config.IDefaultItem;
021: import org.columba.core.xml.XmlElement;
022:
023: /**
024: * Composition wrapper for <code>XmlElement</code>. Provides many convinience
025: * methods for easy access.
026: *
027: * @author fdietz
028: */
029: public class DefaultItem implements Cloneable, IDefaultItem {
030: XmlElement root;
031:
032: public DefaultItem(final XmlElement theRoot) {
033: this .root = theRoot;
034: }
035:
036: public XmlElement getRoot() {
037: return root;
038: }
039:
040: /** ********************** composition pattern ********************* */
041: public XmlElement getElement(final String pathToElement) {
042:
043: /*
044: * XmlElement child = getRoot().getElement(pathToElement);
045: *
046: * return child;
047: */
048: return getElement(pathToElement, true);
049: }
050:
051: public XmlElement getElement(final String pathToElement,
052: final boolean create) {
053: XmlElement child = getRoot();
054:
055: final StringTokenizer tok = new StringTokenizer(pathToElement,
056: "/"); //$NON-NLS-1$
057: while (tok.hasMoreTokens()) {
058: final String token = tok.nextToken();
059: XmlElement e = child.getElement(token);
060: if ((e == null) && create) {
061: e = child.addSubElement(token);
062:
063: }
064: child = e;
065:
066: }
067:
068: return child;
069: }
070:
071: public XmlElement getChildElement(final int index) {
072: return getRoot().getElement(index);
073: }
074:
075: public int getChildCount() {
076: return getRoot().count();
077: }
078:
079: public XmlElement getChildElement(final String pathToElement,
080: final int index) {
081: return getRoot().getElement(pathToElement).getElement(index);
082: }
083:
084: public boolean contains(final String key) {
085: return getRoot().getAttributes().containsKey(key);
086: }
087:
088: public String get(final String key) {
089: return getRoot().getAttribute(key);
090: }
091:
092: public String getString(final String pathToElement, final String key) {
093: final XmlElement element = getElement(pathToElement);
094:
095: if (element != null) {
096: if ((key == null) || (key.length() == 0)) {
097: return element.getData();
098: }
099: return element.getAttribute(key);
100: }
101: return null;
102: }
103:
104: public void setString(final String key, final String newValue) {
105: getRoot().addAttribute(key, newValue);
106: }
107:
108: public void setString(final String pathToElement, final String key,
109: final String newValue) {
110: XmlElement element = getElement(pathToElement);
111: if (element == null) {
112: element = root.addSubElement(pathToElement);
113: }
114:
115: if (key == null) {
116: element.setData(newValue);
117: } else {
118: element.addAttribute(key, newValue);
119: }
120:
121: }
122:
123: /** ************************** helper classes ************************** */
124: public int getInteger(final String key) {
125: final String value = get(key);
126:
127: return Integer.parseInt(value);
128: }
129:
130: public int getIntegerWithDefault(final String key,
131: final int defaultValue) {
132: String value = get(key);
133:
134: if (value == null) {
135: value = new Integer(defaultValue).toString();
136: setString(key, value);
137: }
138:
139: try {
140: return Integer.parseInt(value);
141: } catch (final NumberFormatException e) {
142: return defaultValue;
143: }
144: }
145:
146: public int getInteger(final String pathToElement, final String key) {
147: final String value = getString(pathToElement, key);
148:
149: return Integer.parseInt(value);
150: }
151:
152: public int getIntegerWithDefault(final String pathToElement,
153: final String key, final int defaultValue) {
154: String value = getString(pathToElement, key);
155:
156: if (value == null) {
157: value = new Integer(defaultValue).toString();
158: setString(pathToElement, key, value);
159: }
160:
161: int result = -1;
162: try {
163: result = Integer.parseInt(value);
164: } catch (final NumberFormatException e) {
165: // this is no integer value
166: return defaultValue;
167: }
168:
169: return result;
170: }
171:
172: public void setInteger(final String key, final int value) {
173: setString(key, Integer.toString(value));
174: }
175:
176: public void setInteger(final String pathToElement,
177: final String key, final int value) {
178: setString(pathToElement, key, Integer.toString(value));
179: }
180:
181: public boolean getBooleanWithDefault(final String key,
182: final boolean defaultValue) {
183: String value = get(key);
184:
185: if (value == null) {
186: value = Boolean.toString(defaultValue);
187: setString(key, value);
188: }
189:
190: return Boolean.valueOf(value).booleanValue();
191: }
192:
193: public boolean getBoolean(final String key) {
194: final String value = get(key);
195:
196: return Boolean.valueOf(value).booleanValue();
197: }
198:
199: public boolean getBoolean(final String pathToElement,
200: final String key) {
201: final String value = getString(pathToElement, key);
202:
203: return Boolean.valueOf(value).booleanValue();
204: }
205:
206: public boolean getBooleanWithDefault(final String pathToElement,
207: final String key, final boolean defaultValue) {
208: String value = getString(pathToElement, key);
209:
210: if (value == null) {
211: value = Boolean.valueOf(defaultValue).toString();
212: setString(pathToElement, key, value);
213: }
214:
215: return Boolean.valueOf(value).booleanValue();
216: }
217:
218: public void setBoolean(final String key, final boolean value) {
219: setString(key, value ? Boolean.TRUE.toString() : Boolean.FALSE
220: .toString());
221: }
222:
223: public void setBoolean(final String pathToElement,
224: final String key, final boolean value) {
225: setString(pathToElement, key, value ? Boolean.TRUE.toString()
226: : Boolean.FALSE.toString());
227: }
228:
229: /** {@inheritDoc} */
230: @Override
231: public boolean equals(final Object obj) {
232: boolean equal = false;
233:
234: if ((obj != null) && (obj instanceof IDefaultItem)) {
235: final DefaultItem other = (DefaultItem) obj;
236:
237: if ((root == other.root)
238: || ((root != null) && root.equals(other.root))) {
239: equal = true;
240: }
241: }
242:
243: return equal;
244: }
245:
246: /** {@inheritDoc} */
247: @Override
248: public int hashCode() {
249: int hashCode = 43;
250:
251: if (root != null) {
252: hashCode += (root.hashCode() * 97);
253: }
254:
255: return hashCode;
256: }
257:
258: /** {@inheritDoc} */
259: @Override
260: public Object clone() {
261: try {
262: final DefaultItem other = (DefaultItem) super .clone();
263: other.root = (XmlElement) root.clone(); // make a deep copy
264:
265: return other;
266: } catch (final CloneNotSupportedException cnse) {
267: throw new InternalError(
268: "Could not clone DefaultItem: " + cnse); //$NON-NLS-1$
269: }
270: }
271:
272: /**
273: * @param string
274: * @param string2
275: * @return
276: */
277: public String getStringWithDefault(final String key,
278: final String defaultValue) {
279: String result = getRoot().getAttribute(key);
280: if (result == null) {
281: result = defaultValue;
282: }
283: return result;
284: }
285:
286: /**
287: * @see org.columa.core.config.IDefaultItem#getStringWithDefault(java.lang.String,
288: * java.lang.String, java.lang.String)
289: */
290: public String getStringWithDefault(final String pathToElement,
291: final String key, final String defaultValue) {
292: final String value = getString(pathToElement, key);
293:
294: if (value == null) {
295: setString(pathToElement, key, value);
296: return defaultValue;
297: }
298:
299: return value;
300: }
301:
302: public void notifyObservers(final String path) {
303: final XmlElement e = getElement(path);
304: e.notifyObservers();
305: }
306: }
|