001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * XMLConfigurationEntry.java
020: *
021: * The object that stores the value for the xml configuration entry in memory.
022: */
023:
024: // The package
025: package com.rift.coad.lib.configuration.xml;
026:
027: /**
028: * The object that stores the value for the xml configuration entry in memory.
029: *
030: * @author Brett Chaldecott
031: */
032: public class XMLConfigurationEntry {
033:
034: // member variables
035: private String key = null;
036: private XMLConfigurationType type = null;
037: private String stringValue = null;
038: private long longValue = 0;
039: private boolean booleanValue = false;
040:
041: /**
042: * Creates a new instance of XMLConfigurationEntry
043: */
044: public XMLConfigurationEntry() {
045: }
046:
047: /**
048: * The getter method for the key member variable.
049: *
050: * @return The string containing the key value.
051: */
052: public String getKey() {
053: return this .key;
054: }
055:
056: /**
057: * The setter method for the key member variable.
058: *
059: * @param key The key value to set.
060: */
061: public void setKey(String key) {
062: this .key = key;
063: }
064:
065: /**
066: * The getter method for the type information.
067: *
068: * @return The object containing the type information.
069: */
070: public XMLConfigurationType getType() {
071: return type;
072: }
073:
074: /**
075: * The setter method for the type information.
076: *
077: * @parm type The object containing the type information.
078: */
079: public void setType(XMLConfigurationType type) {
080: this .type = type;
081: }
082:
083: /**
084: * The getter method for the string value.
085: *
086: * @return The string containing the value.
087: * @exception XMLConfigurationException
088: */
089: public String getStringValue() throws XMLConfigurationException {
090: if (type == null) {
091: throw new XMLConfigurationException(
092: "The type has not been initialized");
093: } else if (XMLConfigurationType.STRING_VALUE == type.getType()) {
094: return stringValue;
095: } else {
096: throw new XMLConfigurationException("The value for [" + key
097: + "]is not of type [string].");
098: }
099: }
100:
101: /**
102: * The setter method for the string value.
103: *
104: * @param value The string containing the value.
105: * @exception XMLConfigurationException
106: */
107: public void setStringValue(String value)
108: throws XMLConfigurationException {
109: if (type == null) {
110: throw new XMLConfigurationException(
111: "The type has not been initialized");
112: } else if (XMLConfigurationType.STRING_VALUE == type.getType()) {
113: stringValue = value;
114: } else {
115: throw new XMLConfigurationException(
116: "The value is not of type [string].");
117: }
118: }
119:
120: /**
121: * The getter method for the long value.
122: *
123: * @return The long value.
124: * @exception XMLConfigurationException
125: */
126: public long getLongValue() throws XMLConfigurationException {
127: if (type == null) {
128: throw new XMLConfigurationException(
129: "The type has not been initialized");
130: } else if (XMLConfigurationType.LONG_VALUE == type.getType()) {
131: return longValue;
132: } else {
133: throw new XMLConfigurationException(
134: "The value is not of type [long].");
135: }
136: }
137:
138: /**
139: * The setter method for the long value.
140: *
141: * @param value The long containing the value.
142: * @exception XMLConfigurationException
143: */
144: public void setLongValue(long value)
145: throws XMLConfigurationException {
146: if (type == null) {
147: throw new XMLConfigurationException(
148: "The type has not been initialized");
149: } else if (XMLConfigurationType.LONG_VALUE == type.getType()) {
150: longValue = value;
151: } else {
152: throw new XMLConfigurationException(
153: "The value is not of type [long].");
154: }
155: }
156:
157: /**
158: * The getter method for the boolean value.
159: *
160: * @return The boolean value.
161: * @exception XMLConfigurationException
162: */
163: public boolean getBooleanValue() throws XMLConfigurationException {
164: if (type == null) {
165: throw new XMLConfigurationException(
166: "The type has not been initialized");
167: } else if (XMLConfigurationType.BOOLEAN_VALUE == type.getType()) {
168: return booleanValue;
169: } else {
170: throw new XMLConfigurationException(
171: "The value is not of type [boolean].");
172: }
173: }
174:
175: /**
176: * The setter method for the boolean value.
177: *
178: * @param value The boolean containing the value.
179: * @exception XMLConfigurationException
180: */
181: public void setBooleanValue(boolean value)
182: throws XMLConfigurationException {
183: if (type == null) {
184: throw new XMLConfigurationException(
185: "The type has not been initialized");
186: } else if (XMLConfigurationType.BOOLEAN_VALUE == type.getType()) {
187: booleanValue = value;
188: } else {
189: throw new XMLConfigurationException(
190: "The value is not of type [boolean].");
191: }
192: }
193:
194: /**
195: * The operator that will set the internal long or string value
196: *
197: * @param value The string containing the value.
198: * @exception XMLConfigurationException
199: */
200: public void setValueFromString(String value)
201: throws XMLConfigurationException {
202: try {
203: if (type == null) {
204: throw new XMLConfigurationException(
205: "The type has not been initialized");
206: } else if (XMLConfigurationType.STRING_VALUE == type
207: .getType()) {
208: stringValue = value;
209: } else if (XMLConfigurationType.BOOLEAN_VALUE == type
210: .getType()) {
211: if (value.trim().equalsIgnoreCase("TRUE")) {
212: booleanValue = true;
213: } else {
214: booleanValue = false;
215: }
216: } else {
217: longValue = Long.parseLong(value);
218: }
219: } catch (Exception ex) {
220: throw new XMLConfigurationException(
221: "Failed to store the value [" + value
222: + "] because :" + ex.getMessage(), ex);
223: }
224: }
225:
226: /**
227: * This method will return true if all the member variables have been
228: * initialized.
229: *
230: * @return TRUE if all the member variables have been initialized.
231: */
232: public boolean isIntiailized() {
233: if ((key == null) || (type == null)) {
234: return false;
235: } else if ((type.getType() == XMLConfigurationType.STRING_VALUE)
236: && stringValue == null) {
237: return false;
238: }
239: return true;
240: }
241: }
|