KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > properties > StringProperty


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

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