KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > DataInfo


1 /*
2  * Copyright 2003 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
17
18 package org.apache.velocity.tools.view;
19
20
21 /**
22  * <p>ToolInfo implementation to handle "primitive" data types.
23  * It currently supports String, Number, and Boolean data.</p>
24  *
25  * <p>An example of data elements specified in your toolbox.xml
26  * might be:
27  * <pre>
28  * &lt;data type="string"&gt;
29  * &lt;key&gt;app_name&lt;/key&gt;
30  * &lt;value&gt;FooWeb Deluxe&lt;/value&gt;
31  * &lt;/data&gt;
32  * &lt;data type="number"&gt;
33  * &lt;key&gt;app_version&lt;/key&gt;
34  * &lt;value&gt;4.2&lt;/value&gt;
35  * &lt;/data&gt;
36  * &lt;data type="boolean"&gt;
37  * &lt;key&gt;debug&lt;/key&gt;
38  * &lt;value&gt;true&lt;/value&gt;
39  * &lt;/data&gt;
40  * &lt;data type="number"&gt;
41  * &lt;key&gt;screen_width&lt;/key&gt;
42  * &lt;value&gt;400&lt;/value&gt;
43  * &lt;/data&gt;
44  * </pre></p>
45  *
46  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
47  *
48  * @version $Id: DataInfo.java,v 1.5 2004/02/18 20:08:29 nbubna Exp $
49  */

50 public class DataInfo implements ToolInfo
51 {
52
53     public static final String JavaDoc TYPE_STRING = "string";
54     public static final String JavaDoc TYPE_NUMBER = "number";
55     public static final String JavaDoc TYPE_BOOLEAN = "boolean";
56
57     private static final int TYPE_ID_STRING = 0;
58     private static final int TYPE_ID_NUMBER = 1;
59     private static final int TYPE_ID_BOOLEAN = 2;
60
61     private String JavaDoc key;
62     private int type_id;
63     private Object JavaDoc data;
64
65
66     public DataInfo() {}
67
68
69     /*********************** Mutators *************************/
70
71     public void setKey(String JavaDoc key)
72     {
73         this.key = key;
74     }
75
76
77     public void setType(String JavaDoc type)
78     {
79         if (TYPE_BOOLEAN.equalsIgnoreCase(type))
80         {
81             this.type_id = TYPE_ID_BOOLEAN;
82         }
83         else if (TYPE_NUMBER.equalsIgnoreCase(type))
84         {
85             this.type_id = TYPE_ID_NUMBER;
86         }
87         else /* if no type or type="string" */
88         {
89             this.type_id = TYPE_ID_STRING;
90         }
91     }
92
93
94     public void setValue(String JavaDoc value)
95     {
96         if (type_id == TYPE_ID_BOOLEAN)
97         {
98             this.data = Boolean.valueOf(value);
99         }
100         else if (type_id == TYPE_ID_NUMBER)
101         {
102             if (value.indexOf('.') >= 0)
103             {
104                 this.data = new Double JavaDoc(value);
105             }
106             else
107             {
108                 this.data = new Integer JavaDoc(value);
109             }
110         }
111         else /* type is "string" */
112         {
113             this.data = value;
114         }
115     }
116
117
118     /*********************** Accessors *************************/
119
120     public String JavaDoc getKey()
121     {
122         return key;
123     }
124
125
126     public String JavaDoc getClassname()
127     {
128         return data.getClass().getName();
129     }
130
131
132     /**
133      * Returns the data. Always returns the same
134      * object since the data is a constant. Initialization
135      * data is ignored.
136      */

137     public Object JavaDoc getInstance(Object JavaDoc initData)
138     {
139         return data;
140     }
141
142 }
143
Popular Tags