KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > configuration > xml > XMLConfigurationEntry


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * XMLConfigurationEntry.java
20  *
21  * The object that stores the value for the xml configuration entry in memory.
22  */

23
24 // The package
25
package com.rift.coad.lib.configuration.xml;
26
27 /**
28  * The object that stores the value for the xml configuration entry in memory.
29  *
30  * @author Brett Chaldecott
31  */

32 public class XMLConfigurationEntry {
33     
34     // member variables
35
private String JavaDoc key = null;
36     private XMLConfigurationType type = null;
37     private String JavaDoc stringValue = null;
38     private long longValue = 0;
39     private boolean booleanValue = false;
40     
41     /**
42      * Creates a new instance of XMLConfigurationEntry
43      */

44     public XMLConfigurationEntry() {
45     }
46     
47     
48     /**
49      * The getter method for the key member variable.
50      *
51      * @return The string containing the key value.
52      */

53     public String JavaDoc getKey() {
54         return this.key;
55     }
56     
57     
58     /**
59      * The setter method for the key member variable.
60      *
61      * @param key The key value to set.
62      */

63     public void setKey(String JavaDoc key) {
64         this.key = key;
65     }
66     
67     
68     /**
69      * The getter method for the type information.
70      *
71      * @return The object containing the type information.
72      */

73     public XMLConfigurationType getType() {
74         return type;
75     }
76     
77     
78     /**
79      * The setter method for the type information.
80      *
81      * @parm type The object containing the type information.
82      */

83     public void setType(XMLConfigurationType type) {
84         this.type = type;
85     }
86     
87     
88     /**
89      * The getter method for the string value.
90      *
91      * @return The string containing the value.
92      * @exception XMLConfigurationException
93      */

94     public String JavaDoc getStringValue() throws XMLConfigurationException {
95         if (type == null) {
96             throw new XMLConfigurationException(
97                     "The type has not been initialized");
98         } else if (XMLConfigurationType.STRING_VALUE == type.getType()) {
99             return stringValue;
100         } else {
101             throw new XMLConfigurationException(
102                     "The value for [" + key + "]is not of type [string].");
103         }
104     }
105     
106     
107     /**
108      * The setter method for the string value.
109      *
110      * @param value The string containing the value.
111      * @exception XMLConfigurationException
112      */

113     public void setStringValue(String JavaDoc value) throws XMLConfigurationException {
114         if (type == null) {
115             throw new XMLConfigurationException(
116                     "The type has not been initialized");
117         } else if (XMLConfigurationType.STRING_VALUE == type.getType()) {
118             stringValue = value;
119         } else {
120             throw new XMLConfigurationException(
121                     "The value is not of type [string].");
122         }
123     }
124     
125     
126     /**
127      * The getter method for the long value.
128      *
129      * @return The long value.
130      * @exception XMLConfigurationException
131      */

132     public long getLongValue() throws XMLConfigurationException {
133         if (type == null) {
134             throw new XMLConfigurationException(
135                     "The type has not been initialized");
136         } else if (XMLConfigurationType.LONG_VALUE == type.getType()) {
137             return longValue;
138         } else {
139             throw new XMLConfigurationException(
140                     "The value is not of type [long].");
141         }
142     }
143     
144     
145     /**
146      * The setter method for the long value.
147      *
148      * @param value The long containing the value.
149      * @exception XMLConfigurationException
150      */

151     public void setLongValue(long value) throws XMLConfigurationException {
152         if (type == null) {
153             throw new XMLConfigurationException(
154                     "The type has not been initialized");
155         } else if (XMLConfigurationType.LONG_VALUE == type.getType()) {
156             longValue = value;
157         } else {
158             throw new XMLConfigurationException(
159                     "The value is not of type [long].");
160         }
161     }
162     
163     
164     /**
165      * The getter method for the boolean value.
166      *
167      * @return The boolean value.
168      * @exception XMLConfigurationException
169      */

170     public boolean getBooleanValue() throws XMLConfigurationException {
171         if (type == null) {
172             throw new XMLConfigurationException(
173                     "The type has not been initialized");
174         } else if (XMLConfigurationType.BOOLEAN_VALUE == type.getType()) {
175             return booleanValue;
176         } else {
177             throw new XMLConfigurationException(
178                     "The value is not of type [boolean].");
179         }
180     }
181     
182     
183     /**
184      * The setter method for the boolean value.
185      *
186      * @param value The boolean containing the value.
187      * @exception XMLConfigurationException
188      */

189     public void setBooleanValue(boolean value) throws XMLConfigurationException {
190         if (type == null) {
191             throw new XMLConfigurationException(
192                     "The type has not been initialized");
193         } else if (XMLConfigurationType.BOOLEAN_VALUE == type.getType()) {
194             booleanValue = value;
195         } else {
196             throw new XMLConfigurationException(
197                     "The value is not of type [boolean].");
198         }
199     }
200     
201     
202     /**
203      * The operator that will set the internal long or string value
204      *
205      * @param value The string containing the value.
206      * @exception XMLConfigurationException
207      */

208     public void setValueFromString(String JavaDoc value) throws XMLConfigurationException {
209         try {
210             if (type == null) {
211                 throw new XMLConfigurationException(
212                         "The type has not been initialized");
213             } else if (XMLConfigurationType.STRING_VALUE == type.getType()) {
214                 stringValue = value;
215             } else if (XMLConfigurationType.BOOLEAN_VALUE == type.getType()) {
216                 if (value.trim().equalsIgnoreCase("TRUE")) {
217                     booleanValue = true;
218                 } else {
219                     booleanValue = false;
220                 }
221             } else {
222                 longValue = Long.parseLong(value);
223             }
224         } catch (Exception JavaDoc ex) {
225             throw new XMLConfigurationException("Failed to store the value [" +
226                     value + "] because :" + ex.getMessage(),ex);
227         }
228     }
229     
230     /**
231      * This method will return true if all the member variables have been
232      * initialized.
233      *
234      * @return TRUE if all the member variables have been initialized.
235      */

236     public boolean isIntiailized() {
237        if ((key == null) || (type == null)) {
238            return false;
239        } else if ((type.getType() == XMLConfigurationType.STRING_VALUE) &&
240                stringValue == null) {
241            return false;
242        }
243        return true;
244     }
245 }
246
Popular Tags