KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > elementprocessor > types > Attribute


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.elementprocessor.types;
17
18 import org.apache.commons.lang.BooleanUtils;
19
20 /**
21  * Encapsulation of a single XML element attribute in a way that
22  * shields the consumer from the data's XML origins.
23  *
24  * @author Marc Johnson (marc_johnson27591@hotmail.com)
25  * @version CVS $Id: Attribute.java 30932 2004-07-29 17:35:38Z vgritsenko $
26  */

27 public class Attribute
28 {
29     private String JavaDoc _name;
30     private String JavaDoc _value;
31
32     /**
33      * Constructor
34      *
35      * @param name the name of the Attribute, the left hand side of
36      * the '=' of an XML element's attribute.
37      * @param value the value of the Attribute, the right hand side of
38      * the '=' of an XML element's attribute.
39      *
40      * @exception IllegalArgumentException is thrown if name is null
41      * or empty, or if value is null.
42      */

43
44     public Attribute(final String JavaDoc name, final String JavaDoc value) {
45         if ((name == null) || (name.length() == 0)) {
46             throw new IllegalArgumentException JavaDoc(
47                 "Attribute name is null or empty");
48         }
49         if (value == null) {
50             throw new IllegalArgumentException JavaDoc("Attribute value is null");
51         }
52         _name = name;
53         _value = value;
54     }
55
56     /**
57      * Get the name of the Attribute.
58      *
59      * @return the name of the Attribute
60      */

61
62     public String JavaDoc getName() {
63         return _name;
64     }
65
66     /**
67      * Get the value of the Attribute as a String.
68      *
69      * @return the value of the Attribute as a String
70      */

71
72     public String JavaDoc getValue() {
73         return _value;
74     }
75
76     /**
77      * A convenience method to get the value of the Attribute as an
78      * int.
79      *
80      * @return the value of the Attribute as an int
81      *
82      * @exception NumberFormatException if the value is not an int
83      */

84
85     public int getValueAsInt() {
86         return Integer.parseInt(_value);
87     }
88
89     /**
90      * A convenience method to get the value of the Attribute as a
91      * short.
92      *
93      * @return the value of the Attribute as a short
94      *
95      * @exception NumberFormatException if the value is not a short
96      */

97
98     public short getValueAsShort() {
99         return Short.parseShort(_value);
100     }
101
102     /**
103      * A convenience method to get the value of the Attribute as a long.
104      *
105      * @return the value of the Attribute as a long
106      *
107      * @exception NumberFormatException if the value is not a long
108      */

109
110     public long getValueAsLong() {
111         return Long.parseLong(_value);
112     }
113
114     /**
115      * A convenience method to get the value of the attribute as a
116      * boolean. Understands these value strings in a case-insensitive
117      * fashion:
118      * <ul>
119      * <li>t/f
120      * <li>true/false
121      * <li>y/n
122      * <li>yes/no
123      * <li>on/off
124      * </ul>
125      *
126      * @return the value of the Attribute as a boolean
127      *
128      * @exception IllegalArgumentException if the value does not
129      * represent a boolean
130      */

131
132     public boolean getValueAsBoolean() {
133         // Match case for: true, false, yes, no, on, off.
134
Boolean JavaDoc rvalue = BooleanUtils.toBooleanObject(_value);
135         if (rvalue != null) {
136             return rvalue.booleanValue();
137         }
138         // Lets try with "t", "f"
139
try {
140             rvalue = BooleanUtils.toBooleanObject(_value, "t", "f", null);
141         } catch (IllegalArgumentException JavaDoc iae) {
142             rvalue = null;
143         }
144         if (rvalue != null) {
145             return rvalue.booleanValue();
146         }
147         // Try now "y", "n"
148
try {
149             rvalue = BooleanUtils.toBooleanObject(_value, "y", "n", null);
150         } catch (IllegalArgumentException JavaDoc iae) {
151             rvalue = null;
152         }
153         if (rvalue != null) {
154             return rvalue.booleanValue();
155         } else {
156             throw new IllegalArgumentException JavaDoc(
157                 "Value [" + _value + "] does not represent a boolean value");
158         }
159     }
160 } // end public class Attribute
161
Popular Tags