KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > attributes > XMLAttributeDefinition


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.properties.attributes;
21
22 import java.util.List JavaDoc;
23
24 import org.jdom.Element;
25 import org.jdom.JDOMException;
26
27 import com.sslexplorer.boot.PropertyDefinition;
28 import com.sslexplorer.boot.XMLPropertyDefinition;
29
30 /**
31  * Abstract implementation of an {@link AttributeDefinition} that is configured
32  * from an XML element.
33  *
34  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
35  * @see com.sslexplorer.security.UserDatabase
36  */

37 public class XMLAttributeDefinition extends XMLPropertyDefinition implements AttributeDefinition {
38
39     // Private instance variables
40

41     private String JavaDoc label;
42     private boolean system;
43     private String JavaDoc categoryLabel;
44     private String JavaDoc description;
45     private boolean replaceable;
46     private int visibility;
47     private String JavaDoc classSpecificKey;
48
49     /**
50      * Construct
51      *
52      * @param element XML element
53      * @throws JDOMException on parsing error
54      */

55     public XMLAttributeDefinition(Element element) throws JDOMException {
56         super(element);
57         replaceable = "true".equalsIgnoreCase(element.getAttributeValue("replaceable"));
58         categoryLabel = element.getAttributeValue("categoryLabel");
59         description = element.getAttributeValue("description");
60         label = element.getAttributeValue("label");
61         visibility = USER_USEABLE_ATTRIBUTE;
62         if (element.getAttribute("visibility") != null) {
63             visibility = element.getAttribute("visibility").getIntValue();
64         }
65
66         // Because elements are coming from XML they must be system
67
system = true;
68     }
69
70     /**
71      * Constructor.
72      *
73      * @param type type
74      * @param name name
75      * @param typeMeta type meta
76      * @param category category ID or <code>-1</code> to use the categoryLabel
77      * @param categoryLabel category label or <code>null</code>
78      * @param defaultValue default value
79      * @param visibility visibility. See class description
80      * @param sortOrder sort order
81      * @param messageResourcesKey message resource bundle key
82      * @param hidden hidden
83      * @param label label
84      * @param description description
85      * @param system system
86      * @param replaceable
87      * @param validationString
88      */

89     public XMLAttributeDefinition(int type, String JavaDoc name, String JavaDoc typeMeta, int category, String JavaDoc categoryLabel, String JavaDoc defaultValue,
90                                   int visibility, int sortOrder, String JavaDoc messageResourcesKey, boolean hidden, String JavaDoc label,
91                                   String JavaDoc description, boolean system, boolean replaceable, String JavaDoc validationString) {
92         super(type, name, typeMeta, category, defaultValue, sortOrder, messageResourcesKey, hidden);
93         this.label = label;
94         this.system = system;
95         this.hidden = hidden;
96         this.categoryLabel = categoryLabel;
97         this.description = description;
98         this.replaceable = replaceable;
99         this.validationString = validationString;
100         this.visibility = visibility;
101     }
102
103     /**
104      * Set the visibility. This should only be called on initial creation.
105      *
106      * @param visibility
107      * @see #getVisibility()
108      */

109     public void setVisibility(int visibility) {
110         this.visibility = visibility;
111     }
112
113     /**
114      * Get the visibility. This should only be called on initial creation.
115      *
116      * @return visibility
117      * @see #setVisibility(int)
118      */

119     public int getVisibility() {
120         return visibility;
121     }
122
123     /**
124      * Get if this is a <i>System</i> attribute definition. If it is then the
125      * def. cannot be edited or removed
126      *
127      * @return system definition
128      */

129     public boolean isSystem() {
130         return system;
131     }
132
133     /**
134      * Set if this is a <i>System</i> attribute definition. If it is then the
135      * def. cannot be edited or removed
136      *
137      * @param system system definition
138      */

139     public void setSystem(boolean system) {
140         this.system = system;
141     }
142
143     /**
144      * Get the label for this attribute. If an empty string is supplied the name
145      * will be used as the label in the user interface. Note that the actual
146      * label displayed to the use may be different if a message resource exists
147      * (see class documentation).
148      *
149      * @return label
150      */

151     public String JavaDoc getLabel() {
152         return label;
153     }
154
155     /**
156      * Set the label for this attribute. If an empty string is supplied the name
157      * will be used as the label in the user interface. Note that the actual
158      * label displayed to the use may be different if a message resource exists
159      * (see class documentation).
160      *
161      * @param label label
162      */

163     public void setLabel(String JavaDoc label) {
164         this.label = label;
165     }
166
167     /**
168      * Get the category label. If this is not <code>null</code> then the
169      * category ID will be used to derive the category label from message
170      * resources.
171      *
172      * @return category label
173      */

174     public String JavaDoc getCategoryLabel() {
175         return categoryLabel;
176     }
177
178     /**
179      * Set the category label. If this is not <code>null</code> then the
180      * category ID will be used to derive the category label from message
181      * resources.
182      *
183      * @param categoryLabel category label
184      */

185     public void setCategoryLabel(String JavaDoc categoryLabel) {
186         this.categoryLabel = categoryLabel;
187     }
188
189     /**
190      * Set the label for this attribute. If an empty string is supplied the name
191      * will be used as the label in the user interface. Note that the actual
192      * description displayed to the use may be different if a message resource
193      * exists (see class documentation).
194      *
195      * @return description
196      */

197     public String JavaDoc getDescription() {
198         return description;
199     }
200
201     /**
202      * Set the label for this attribute. If an empty string is supplied the name
203      * will be used as the label in the user interface. Note that the actual
204      * label displayed to the use may be different if there
205      *
206      * @param description description
207      */

208     public void setDescription(String JavaDoc description) {
209         this.description = description;
210     }
211
212     /**
213      * Set whether this this attribute may be used for replacements.
214      *
215      * @param replaceable replaceable
216      */

217     public void setReplaceable(boolean replaceable) {
218         this.replaceable = replaceable;
219     }
220
221     /**
222      * Get whether this this attribute may be used for replacements.
223      *
224      * @return replaceable
225      */

226     public boolean isReplaceable() {
227         return replaceable;
228     }
229
230     /**
231      * Set the validation string.
232      *
233      * @param validationString validation string
234      */

235     public void setValidationString(String JavaDoc validationString) {
236         this.validationString = validationString;
237     }
238
239     /**
240      * Get the value of this item as it should be stored as an attribute.
241      *
242      * @return formatted attribute value
243      */

244     public String JavaDoc formatAttributeValue(Object JavaDoc value) {
245         if (getType() == PropertyDefinition.TYPE_BOOLEAN) {
246             if (getTypeMetaObject() != null && !getTypeMetaObject().equals("")) {
247                 String JavaDoc trueVal = (String JavaDoc) (((List JavaDoc) getTypeMetaObject()).get(0));
248                 return value == Boolean.TRUE ? trueVal : (String JavaDoc) (((List JavaDoc) getTypeMetaObject()).get(1));
249             }
250         }
251         return value.toString();
252     }
253
254     /**
255      * Get the appropriate object for this type.
256      *
257      * @param value
258      * @return typed value
259      */

260     public Object JavaDoc parseValue(String JavaDoc value) {
261         if (getType() == PropertyDefinition.TYPE_BOOLEAN) {
262             if (getTypeMetaObject() != null) {
263                 String JavaDoc trueVal = (String JavaDoc) (((List JavaDoc) getTypeMetaObject()).get(0));
264                 return value.equals(trueVal) ? Boolean.TRUE : Boolean.FALSE;
265             }
266         }
267         return value;
268     }
269
270 }
271
Popular Tags