001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.util.config;
018:
019: import org.compass.core.config.ConfigurationException;
020:
021: /**
022: * This is an abstract <code>Configuration</code> implementation that deals
023: * with methods that can be abstracted away from underlying implementations.
024: */
025: public abstract class AbstractConfigurationHelper implements
026: ConfigurationHelper {
027:
028: /**
029: * Returns the prefix of the namespace. This is only used as a serialization
030: * hint, therefore is not part of the client API. It should be included in
031: * all Configuration implementations though.
032: *
033: * @return A non-null String (defaults to "")
034: * @throws ConfigurationException
035: * if no prefix was defined (prefix is <code>null</code>.
036: */
037: protected abstract String getPrefix() throws ConfigurationException;
038:
039: /**
040: * Returns the value of the configuration element as an <code>int</code>.
041: * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
042: * numbers begin with 0b, all other values are assumed to be decimal.
043: */
044: public int getValueAsInteger() throws ConfigurationException {
045: final String value = getValue().trim();
046: try {
047: if (value.startsWith("0x")) {
048: return Integer.parseInt(value.substring(2), 16);
049: } else if (value.startsWith("0o")) {
050: return Integer.parseInt(value.substring(2), 8);
051: } else if (value.startsWith("0b")) {
052: return Integer.parseInt(value.substring(2), 2);
053: } else {
054: return Integer.parseInt(value);
055: }
056: } catch (final Exception nfe) {
057: final String message = "Cannot parse the value \""
058: + value
059: + "\" as an integer in the configuration element \""
060: + getName() + "\" at " + getLocation();
061: throw new ConfigurationException(message);
062: }
063: }
064:
065: /**
066: * Returns the value of the configuration element as an <code>int</code>.
067: * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
068: * numbers begin with 0b, all other values are assumed to be decimal.
069: */
070: public int getValueAsInteger(final int defaultValue) {
071: try {
072: return getValueAsInteger();
073: } catch (final ConfigurationException ce) {
074: return defaultValue;
075: }
076: }
077:
078: /**
079: * Returns the value of the configuration element as a <code>long</code>.
080: * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
081: * numbers begin with 0b, all other values are assumed to be decimal.
082: */
083: public long getValueAsLong() throws ConfigurationException {
084: final String value = getValue().trim();
085: try {
086: if (value.startsWith("0x")) {
087: return Long.parseLong(value.substring(2), 16);
088: } else if (value.startsWith("0o")) {
089: return Long.parseLong(value.substring(2), 8);
090: } else if (value.startsWith("0b")) {
091: return Long.parseLong(value.substring(2), 2);
092: } else {
093: return Long.parseLong(value);
094: }
095: } catch (final Exception nfe) {
096: final String message = "Cannot parse the value \"" + value
097: + "\" as a long in the configuration element \""
098: + getName() + "\" at " + getLocation();
099: throw new ConfigurationException(message);
100: }
101: }
102:
103: /**
104: * Returns the value of the configuration element as a <code>long</code>.
105: * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
106: * numbers begin with 0b, all other values are assumed to be decimal.
107: */
108: public long getValueAsLong(final long defaultValue) {
109: try {
110: return getValueAsLong();
111: } catch (final ConfigurationException ce) {
112: return defaultValue;
113: }
114: }
115:
116: /**
117: * Returns the value of the configuration element as a <code>float</code>.
118: */
119: public float getValueAsFloat() throws ConfigurationException {
120: final String value = getValue().trim();
121: try {
122: return Float.parseFloat(value);
123: } catch (final Exception nfe) {
124: final String message = "Cannot parse the value \"" + value
125: + "\" as a float in the configuration element \""
126: + getName() + "\" at " + getLocation();
127: throw new ConfigurationException(message);
128: }
129: }
130:
131: /**
132: * Returns the value of the configuration element as a <code>float</code>.
133: */
134: public float getValueAsFloat(final float defaultValue) {
135: try {
136: return getValueAsFloat();
137: } catch (final ConfigurationException ce) {
138: return (defaultValue);
139: }
140: }
141:
142: /**
143: * Returns the value of the configuration element as a <code>boolean</code>.
144: */
145: public boolean getValueAsBoolean() throws ConfigurationException {
146: final String value = getValue().trim();
147: if (isTrue(value)) {
148: return true;
149: } else if (isFalse(value)) {
150: return false;
151: } else {
152: final String message = "Cannot parse the value \"" + value
153: + "\" as a boolean in the configuration element \""
154: + getName() + "\" at " + getLocation();
155: throw new ConfigurationException(message);
156: }
157: }
158:
159: /**
160: * Returns the value of the configuration element as a <code>boolean</code>.
161: */
162: public boolean getValueAsBoolean(final boolean defaultValue) {
163: try {
164: return getValueAsBoolean();
165: } catch (final ConfigurationException ce) {
166: return defaultValue;
167: }
168: }
169:
170: /**
171: * Returns the value of the configuration element as a <code>String</code>.
172: */
173: public String getValue(final String defaultValue) {
174: try {
175: return getValue();
176: } catch (final ConfigurationException ce) {
177: return defaultValue;
178: }
179: }
180:
181: /**
182: * Returns the value of the attribute specified by its name as an
183: * <code>int</code>. Hexadecimal numbers begin with 0x, Octal numbers
184: * begin with 0o and binary numbers begin with 0b, all other values are
185: * assumed to be decimal.
186: */
187: public int getAttributeAsInteger(final String name)
188: throws ConfigurationException {
189: final String value = getAttribute(name).trim();
190: try {
191: if (value.startsWith("0x")) {
192: return Integer.parseInt(value.substring(2), 16);
193: } else if (value.startsWith("0o")) {
194: return Integer.parseInt(value.substring(2), 8);
195: } else if (value.startsWith("0b")) {
196: return Integer.parseInt(value.substring(2), 2);
197: } else {
198: return Integer.parseInt(value);
199: }
200: } catch (final Exception nfe) {
201: final String message = "Cannot parse the value \"" + value
202: + "\" as an integer in the attribute \"" + name
203: + "\" at " + getLocation();
204: throw new ConfigurationException(message);
205: }
206: }
207:
208: /**
209: * Returns the value of the attribute specified by its name as an
210: * <code>int</code>. Hexadecimal numbers begin with 0x, Octal numbers
211: * begin with 0o and binary numbers begin with 0b, all other values are
212: * assumed to be decimal.
213: */
214: public int getAttributeAsInteger(final String name,
215: final int defaultValue) {
216: try {
217: return getAttributeAsInteger(name);
218: } catch (final ConfigurationException ce) {
219: return defaultValue;
220: }
221: }
222:
223: /**
224: * Returns the value of the attribute specified by its name as a
225: * <code>long</code>. Hexadecimal numbers begin with 0x, Octal numbers
226: * begin with 0o and binary numbers begin with 0b, all other values are
227: * assumed to be decimal.
228: */
229: public long getAttributeAsLong(final String name)
230: throws ConfigurationException {
231: final String value = getAttribute(name);
232: try {
233: if (value.startsWith("0x")) {
234: return Long.parseLong(value.substring(2), 16);
235: } else if (value.startsWith("0o")) {
236: return Long.parseLong(value.substring(2), 8);
237: } else if (value.startsWith("0b")) {
238: return Long.parseLong(value.substring(2), 2);
239: } else {
240: return Long.parseLong(value);
241: }
242: } catch (final Exception nfe) {
243: final String message = "Cannot parse the value \"" + value
244: + "\" as a long in the attribute \"" + name
245: + "\" at " + getLocation();
246: throw new ConfigurationException(message);
247: }
248: }
249:
250: /**
251: * Returns the value of the attribute specified by its name as a
252: * <code>long</code>. Hexadecimal numbers begin with 0x, Octal numbers
253: * begin with 0o and binary numbers begin with 0b, all other values are
254: * assumed to be decimal.
255: */
256: public long getAttributeAsLong(final String name,
257: final long defaultValue) {
258: try {
259: return getAttributeAsLong(name);
260: } catch (final ConfigurationException ce) {
261: return defaultValue;
262: }
263: }
264:
265: /**
266: * Returns the value of the attribute specified by its name as a
267: * <code>float</code>.
268: */
269: public float getAttributeAsFloat(final String name)
270: throws ConfigurationException {
271: final String value = getAttribute(name);
272: try {
273: return Float.parseFloat(value);
274: } catch (final Exception e) {
275: final String message = "Cannot parse the value \"" + value
276: + "\" as a float in the attribute \"" + name
277: + "\" at " + getLocation();
278: throw new ConfigurationException(message);
279: }
280: }
281:
282: /**
283: * Returns the value of the attribute specified by its name as a
284: * <code>float</code>.
285: */
286: public float getAttributeAsFloat(final String name,
287: final float defaultValue) {
288: try {
289: return getAttributeAsFloat(name);
290: } catch (final ConfigurationException ce) {
291: return defaultValue;
292: }
293: }
294:
295: /**
296: * Returns the value of the attribute specified by its name as a
297: * <code>boolean</code>.
298: */
299: public boolean getAttributeAsBoolean(final String name)
300: throws ConfigurationException {
301: final String value = getAttribute(name);
302: if (isTrue(value)) {
303: return true;
304: } else if (isFalse(value)) {
305: return false;
306: } else {
307: final String message = "Cannot parse the value \"" + value
308: + "\" as a boolean in the attribute \"" + name
309: + "\" at " + getLocation();
310: throw new ConfigurationException(message);
311: }
312: }
313:
314: private boolean isTrue(final String value) {
315: return value.equalsIgnoreCase("true")
316: || value.equalsIgnoreCase("yes")
317: || value.equalsIgnoreCase("on")
318: || value.equalsIgnoreCase("1");
319: }
320:
321: private boolean isFalse(final String value) {
322: return value.equalsIgnoreCase("false")
323: || value.equalsIgnoreCase("no")
324: || value.equalsIgnoreCase("off")
325: || value.equalsIgnoreCase("0");
326: }
327:
328: /**
329: * Returns the value of the attribute specified by its name as a
330: * <code>boolean</code>.
331: */
332: public boolean getAttributeAsBoolean(final String name,
333: final boolean defaultValue) {
334: try {
335: return getAttributeAsBoolean(name);
336: } catch (final ConfigurationException ce) {
337: return defaultValue;
338: }
339: }
340:
341: /**
342: * Returns the value of the attribute specified by its name as a
343: * <code>String</code>.
344: */
345: public String getAttribute(final String name,
346: final String defaultValue) {
347: try {
348: return getAttribute(name);
349: } catch (final ConfigurationException ce) {
350: return defaultValue;
351: }
352: }
353:
354: /**
355: * Return the first <code>Configuration</code> object child of this
356: * associated with the given name. If no such child exists, a new one will
357: * be created.
358: */
359: public ConfigurationHelper getChild(final String name) {
360: return getChild(name, true);
361: }
362:
363: /**
364: * Return the first <code>Configuration</code> object child of this
365: * associated with the given name.
366: */
367: public ConfigurationHelper getChild(final String name,
368: final boolean createNew) {
369: final ConfigurationHelper[] children = getChildren(name);
370: if (children.length > 0) {
371: return children[0];
372: } else {
373: if (createNew) {
374: return new XmlConfigurationHelper(name, "-");
375: } else {
376: return null;
377: }
378: }
379: }
380:
381: /**
382: * The toString() operation is used for debugging information. It does not
383: * create a deep reproduction of this configuration and all child
384: * configurations, instead it displays the name, value, and location.
385: *
386: * @return getName() + "::" + getValue() + ":@" + getLocation();
387: */
388: public String toString() {
389: return getName() + "::" + getValue("<no value>") + ":@"
390: + getLocation();
391: }
392: }
|