KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > properties > StringProperty


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

18 package org.apache.geronimo.interop.properties;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.geronimo.interop.util.FileUtil;
28 import org.apache.geronimo.interop.util.ListUtil;
29 import org.apache.geronimo.interop.util.StringUtil;
30
31 public class StringProperty extends PropertyType {
32     private String JavaDoc defaultValue = "";
33     private List JavaDoc valueIsInList = null;
34     private Map JavaDoc displayValues = null;
35     private Class JavaDoc valueIsNameOf = null;
36     private boolean isDirName = false;
37     private boolean isFileName = false;
38     private boolean isList = false;
39     private boolean isReadOnly = false;
40     private boolean isRequired = false;
41
42     public StringProperty(Class JavaDoc componentClass, String JavaDoc propertyName) {
43         super(componentClass, propertyName);
44     }
45
46     public StringProperty displayName(String JavaDoc displayName) {
47         setDisplayName(displayName);
48         return this;
49     }
50
51     public StringProperty displayOnlyIf(PropertyType other, String JavaDoc value) {
52         setDisplayOnlyIf(other, value);
53         return this;
54     }
55
56     public StringProperty description(String JavaDoc description) {
57         setDescription(description);
58         return this;
59     }
60
61     public StringProperty consoleHelp(String JavaDoc consoleHelp) {
62         setConsoleHelp(consoleHelp);
63         return this;
64     }
65
66     public StringProperty sortOrder(int sortOrder) {
67         setSortOrder(sortOrder);
68         return this;
69     }
70
71     public StringProperty defaultValue(String JavaDoc defaultValue) {
72         this.defaultValue = defaultValue;
73         return this;
74     }
75
76     public StringProperty legalValues(Class JavaDoc valueIsNameOf) {
77         this.valueIsNameOf = valueIsNameOf;
78         return this;
79     }
80
81     public StringProperty legalValues(List JavaDoc valueIsInList) {
82         valueIsInList = Collections.unmodifiableList(valueIsInList);
83         return this;
84     }
85
86     public StringProperty legalValues(String JavaDoc valueIsInList) {
87         List JavaDoc list = ListUtil.getCommaSeparatedList(valueIsInList);
88         this.valueIsInList = new ArrayList JavaDoc(list.size());
89         for (Iterator JavaDoc i = list.iterator(); i.hasNext();) {
90             String JavaDoc value = (String JavaDoc) i.next();
91             if (value.indexOf('=') != -1) {
92                 String JavaDoc displayValue = StringUtil.afterFirst("=", value).trim();
93                 value = StringUtil.beforeFirst("=", value).trim();
94                 if (displayValues == null) {
95                     displayValues = new HashMap JavaDoc();
96                 }
97                 displayValues.put(value, displayValue);
98             }
99             this.valueIsInList.add(value);
100         }
101         return this;
102     }
103
104     public String JavaDoc getDisplayValue(String JavaDoc value) {
105         if (displayValues != null) {
106             String JavaDoc displayValue = (String JavaDoc) displayValues.get(value);
107             if (displayValue != null) {
108                 return displayValue;
109             }
110         }
111         return value;
112     }
113
114     public StringProperty isDirName() {
115         isDirName = true;
116         return this;
117     }
118
119     public StringProperty isFileName() {
120         isFileName = true;
121         return this;
122     }
123
124     public StringProperty list() {
125         isList = true;
126         return this;
127     }
128
129     public StringProperty readOnly() {
130         isReadOnly = true;
131         return this;
132     }
133
134     public StringProperty required() {
135         isRequired = true;
136         return this;
137     }
138
139     public boolean isList() {
140         return isList;
141     }
142
143     public boolean isReadOnly() {
144         return isReadOnly;
145     }
146
147     public boolean isRequired() {
148         return isRequired;
149     }
150
151     public String JavaDoc getDefaultValue() {
152         return defaultValue;
153     }
154
155     public String JavaDoc getDefaultValueAsString() {
156         return defaultValue;
157     }
158
159     public List JavaDoc getLegalValues() {
160         if (valueIsInList != null) {
161             return valueIsInList;
162         } else if (valueIsNameOf != null) {
163             //return Repository.getInstance().getInstanceNames(_valueIsNameOf);
164
return null;
165         } else {
166             return null;
167         }
168     }
169
170     public String JavaDoc getString() {
171         return getString(null, getComponentProperties());
172     }
173
174     public String JavaDoc getString(String JavaDoc instanceName, PropertyMap props) {
175         boolean ok = true, usingDefaultValue = false;
176         String JavaDoc s = props.getProperty(getPropertyName(), defaultValue);
177         if (s != null && s.startsWith("${")) {
178             // Value is contained in system property.
179
s = StringUtil.removePrefix(s, "${");
180             s = StringUtil.removeSuffix(s, "}");
181             StringProperty sp = new StringProperty(SystemProperties.class, s);
182             s = sp.getString();
183             if (s == null || s.length() == 0) {
184                 if (isRequired()) {
185                     String JavaDoc message = getLog(instanceName).errorMissingValueForRequiredSystemProperty(sp.getPropertyName(), getPropertyName(), getContext(instanceName));
186                     throw new MissingRequiredPropertyException(message);
187                 }
188             }
189         }
190         if (s == null && !isRequired()) {
191             s = "";
192         }
193         List JavaDoc legalValues = getLegalValues();
194         if (legalValues != null) {
195             ok = false;
196             for (Iterator JavaDoc i = legalValues.iterator(); i.hasNext();) {
197                 String JavaDoc legalValue = (String JavaDoc) i.next();
198                 if (s != null && s.equals(legalValue)) {
199                     ok = true;
200                     break;
201                 }
202             }
203             if (!isRequired() && s.equals("")) {
204                 ok = true;
205             }
206         }
207         if (!ok) {
208             badPropertyValue(instanceName, s, expectedValueInList(legalValues));
209         }
210         if (isDirName || isFileName) {
211             s = FileUtil.expandHomeRelativePath(s);
212             s = FileUtil.pretty(s);
213         }
214         if (s == null || s.length() == 0) {
215             if (isRequired()) {
216                 String JavaDoc message = getLog(instanceName).errorMissingValueForRequiredProperty(getPropertyName(), getContext(instanceName));
217                 throw new MissingRequiredPropertyException(message);
218             }
219         }
220         logPropertyValue(instanceName, s, s != null && s.equals(defaultValue));
221         return s;
222     }
223 }
224
Popular Tags