KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > widgets > CmsSelectWidgetOption


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/CmsSelectWidgetOption.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.8 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.widgets;
33
34 import org.opencms.main.CmsLog;
35 import org.opencms.util.CmsStringUtil;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.Collections JavaDoc;
39 import java.util.List JavaDoc;
40
41 import org.apache.commons.logging.Log;
42
43 /**
44  * An option of a select type widget.<p>
45  *
46  * If options are passed from XML content schema definitions as widget configuration options,
47  * the following syntax is used for defining the option values:<p>
48  *
49  * <code>value='{text}' default='{true|false}' option='{text}' help='{text}|{more option definitions}</code><p>
50  *
51  * For example:<p>
52  *
53  * <code>value='value1' default='true' option='option1' help='help1'|value='value2' option='option2' help='help2'</code><p>
54  *
55  * The elements <code>default</code>, <code>option</code> and <code>help</code> are all optional, only a
56  * <code>value</code> must be present in the input.
57  * There should be only one <code>default</code> set to <code>true</code>
58  * in the input, if more than one is detected, only the first <code>default</code> found is actually used.
59  * If no <code>option</code> is given, the value of <code>option</code> defaults to the value of the given <code>value</code>.
60  * If no <code>help</code> is given, the default is <code>null</code>.<p>
61  *
62  * Shortcut syntax options:<p>
63  *
64  * If you don't specify the <code>value</code> key, the value is assumed to start at the first position of an
65  * option definition. In this case the value must not be surrounded by the <code>'</code> chars.
66  * Example: <code>value='some value' default='true'</code> can also be written as <code>some value default='true'</code>.<p>
67  *
68  * Only if you use the short value definition as described above, a default value can be marked with a <code>*</code>
69  * at the end of the value definition.
70  * Example: <code>value='some value' default='true'</code> can also be written as <code>some value*</code>.<p>
71  *
72  * Only if you use the short value definition as described above, you can also append the <code>option</code>
73  * to the <code>value</code> using a <code>:</code>. In this case no <code>'</code> must surround the <code>option</code>.
74  * Please keep in mind that in this case the value
75  * itself can not longer contain a <code>:</code> char, since it would then be interpreted as a delimiter.
76  * Example: <code>value='some value' option='some option'</code> can also be written as <code>some value:some option</code>.<p>
77  *
78  * Any combinations of the above described shortcuts are allowed in the configuration option String.
79  * Here are some more examples of valid configuration option Strings:<p>
80  *
81  * <code>1*|2|3|4|5|6|7</code><br>
82  * <code>1 default='true'|2|3|4|5|6|7</code><br>
83  * <code>value='1' default='true'|value='2'|value='3'</code><br>
84  * <code>value='1'|2*|value='3'</code><br>
85  * <code>1*:option text|2|3|4</code><br>
86  * <code>1* option='option text' help='some'|2|3|4</code><p>
87  *
88  * Please note: If an entry in the configuration String is malformed, this error is silently ignored (but written
89  * to the log channel of this class at <code>INFO</code>level.<p>
90  *
91  * @author Alexander Kandzior
92  *
93  * @version $Revision: 1.8 $
94  *
95  * @since 6.0.0
96  */

97 public class CmsSelectWidgetOption {
98
99     /** Optional shortcut default marker. */
100     private static final char DEFAULT_MARKER = '*';
101
102     /** Delimiter between option sets. */
103     private static final char INPUT_DELIMITER = '|';
104
105     /** Key prefix for the 'default'. */
106     private static final String JavaDoc KEY_DEFAULT = "default='";
107
108     /** Key prefix for the 'help' text. */
109     private static final String JavaDoc KEY_HELP = "help='";
110
111     /** Key prefix for the 'option' text. */
112     private static final String JavaDoc KEY_OPTION = "option='";
113
114     /** Key prefix for the 'value'. */
115     private static final String JavaDoc KEY_VALUE = "value='";
116
117     /** The log object for this class. */
118     private static final Log LOG = CmsLog.getLog(CmsSelectWidgetOption.class);
119
120     /** Optional shortcut option delimiter. */
121     private static final char OPTION_DELIMITER = ':';
122
123     /** Indicates if this is the default value of the selection. */
124     private boolean m_default;
125
126     /** The hashcode of this object. */
127     private int m_hashcode;
128
129     /** The (optional) help text of this select option. */
130     private String JavaDoc m_help;
131
132     /** The (optional) display text of this select option. */
133     private String JavaDoc m_option;
134
135     /** The value of this select option. */
136     private String JavaDoc m_value;
137
138     /**
139      * Creates a new select option for the given value.<p>
140      *
141      * @param value the value of this select option
142      */

143     public CmsSelectWidgetOption(String JavaDoc value) {
144
145         this(value, false, null, null);
146     }
147
148     /**
149      * Creates a new select option form the given values.<p>
150      *
151      * @param value the value of this select option
152      * @param isDefault indicates if this is the default value of the selection (default is <code>false</code>)
153      */

154     public CmsSelectWidgetOption(String JavaDoc value, boolean isDefault) {
155
156         this(value, isDefault, null, null);
157     }
158
159     /**
160      * Creates a new select option form the given values.<p>
161      *
162      * @param value the value of this select option
163      * @param isDefault indicates if this is the default value of the selection (default is <code>false</code>)
164      * @param optionText the (optional) display text of this select option
165      */

166     public CmsSelectWidgetOption(String JavaDoc value, boolean isDefault, String JavaDoc optionText) {
167
168         this(value, isDefault, optionText, null);
169     }
170
171     /**
172      * Creates a new select option form the given values.<p>
173      *
174      * @param value the value of this select option
175      * @param isDefault indicates if this is the default value of the selection (default is <code>false</code>)
176      * @param optionText the (optional) display text of this select option
177      * @param helpText the (optional) help text of this select option
178      */

179     public CmsSelectWidgetOption(String JavaDoc value, boolean isDefault, String JavaDoc optionText, String JavaDoc helpText) {
180
181         m_default = isDefault;
182         m_value = value;
183         m_option = optionText;
184         m_help = helpText;
185     }
186
187     /**
188      * Returns a select widget configuration String created from the given list of select options.<p>
189      *
190      * If an element found in the given list is not of type
191      * <code>{@link CmsSelectWidgetOption}</code>, it is ignored.<p>
192      *
193      * @param options the list of select options to create the configuration String for
194      *
195      * @return a select widget configuration String created from the given list of select options
196      */

197     public static String JavaDoc createConfigurationString(List JavaDoc options) {
198
199         if ((options == null) || (options.size() == 0)) {
200             return "";
201         }
202         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
203         boolean first = true;
204         for (int i = 0; i < options.size(); i++) {
205             Object JavaDoc o = options.get(i);
206             if (o instanceof CmsSelectWidgetOption) {
207                 if (!first) {
208                     result.append(CmsSelectWidgetOption.INPUT_DELIMITER);
209                 } else {
210                     first = false;
211                 }
212                 result.append(o.toString());
213             }
214         }
215         return result.toString();
216     }
217
218     /**
219      * Returns the default option from the given list of select options,
220      * or <code>null</code> in case there is no default option in the given list.<p>
221      *
222      * If an element found in the given list is not of type
223      * <code>{@link CmsSelectWidgetOption}</code>, this is ignored.<p>
224      *
225      * @param options the list of select options to get the default from
226      *
227      * @return the default option from the given list of select options, or <code>null</code> in case there is no default option
228      */

229     public static CmsSelectWidgetOption getDefaultOption(List JavaDoc options) {
230
231         if ((options == null) || (options.size() == 0)) {
232             return null;
233         }
234         for (int i = 0; i < options.size(); i++) {
235             Object JavaDoc o = options.get(i);
236             if (o instanceof CmsSelectWidgetOption) {
237                 CmsSelectWidgetOption option = (CmsSelectWidgetOption)o;
238                 if (option.isDefault()) {
239                     return option;
240                 }
241             }
242         }
243         return null;
244     }
245
246     /**
247      * Returns a list of default options from the given list of select options.<p>
248      *
249      * If an element found in the given list is not of type
250      * <code>{@link CmsSelectWidgetOption}</code>, this is ignored.<p>
251      *
252      * @param options the list of select options to get the default from
253      *
254      * @return a list of <code>{@link CmsSelectWidgetOption}</code> objects
255      */

256     public static List JavaDoc getDefaultOptions(List JavaDoc options) {
257
258         List JavaDoc defaults = new ArrayList JavaDoc();
259         if ((options == null) || (options.size() == 0)) {
260             return defaults;
261         }
262         for (int i = 0; i < options.size(); i++) {
263             Object JavaDoc o = options.get(i);
264             if (o instanceof CmsSelectWidgetOption) {
265                 CmsSelectWidgetOption option = (CmsSelectWidgetOption)o;
266                 if (option.isDefault()) {
267                     defaults.add(option);
268                 }
269             }
270         }
271         return defaults;
272     }
273
274     /**
275      * Parses a widget configuration String for select option values.<p>
276      *
277      * If the input is <code>null</code> or empty, a <code>{@link Collections#EMPTY_LIST}</code>
278      * is returned.<p>
279      *
280      * Please note: No exception is thrown in case the input is malformed, all malformed entries are silently ignored.<p>
281      *
282      * @param input the widget input string to parse
283      *
284      * @return a List of <code>{@link CmsSelectWidgetOption}</code> elements
285      */

286     public static List JavaDoc parseOptions(String JavaDoc input) {
287
288         if (CmsStringUtil.isEmptyOrWhitespaceOnly(input)) {
289             // default result for empty input
290
return Collections.EMPTY_LIST;
291         }
292
293         // cut along the delimiter
294
String JavaDoc[] parts = CmsStringUtil.splitAsArray(input, INPUT_DELIMITER);
295         List JavaDoc result = new ArrayList JavaDoc();
296
297         // indicates if a default of 'true' was already set in this result list
298
boolean foundDefault = false;
299
300         for (int i = 0; i < parts.length; i++) {
301
302             String JavaDoc part = parts[i].trim();
303             if (part.length() == 0) {
304                 // skip empty parts
305
continue;
306             }
307
308             try {
309
310                 String JavaDoc value = null;
311                 String JavaDoc option = null;
312                 String JavaDoc help = null;
313                 boolean isDefault = false;
314
315                 int posValue = part.indexOf(KEY_VALUE);
316                 int posDefault = part.indexOf(KEY_DEFAULT);
317                 int posOption = part.indexOf(KEY_OPTION);
318                 int posHelp = part.indexOf(KEY_HELP);
319
320                 boolean shortValue = false;
321                 if (posValue < 0) {
322                     // shortcut syntax, value key must be at first position
323
if ((posDefault == 0) || (posOption == 0) || (posHelp == 0)) {
324                         // malformed part - no value given
325
throw new CmsWidgetException(Messages.get().container(
326                             Messages.ERR_MALFORMED_SELECT_OPTIONS_1,
327                             input));
328                     }
329                     posValue = 0;
330                     shortValue = true;
331                 }
332
333                 // a 'value' must be always present
334
int end = part.length();
335                 // check where the 'value' ends
336
if (posHelp > posValue) {
337                     end = posHelp;
338                 }
339                 if ((posDefault > posValue) && (posDefault < end)) {
340                     end = posDefault;
341                 }
342                 if ((posOption > posValue) && (posOption < end)) {
343                     end = posOption;
344                 }
345                 if (shortValue) {
346                     // no explicit setting using the key, value must be at the first position
347
value = part.substring(0, end).trim();
348                 } else {
349                     value = part.substring(posValue + KEY_VALUE.length(), end).trim();
350                     // cut of trailing '
351
value = value.substring(0, value.length() - 1);
352                 }
353
354                 boolean shortOption = false;
355                 // check if the option is appended using the ':' shortcut
356
if ((shortValue) && (posOption < 0)) {
357                     int pos = value.indexOf(OPTION_DELIMITER);
358                     if (pos >= 0) {
359                         // shortcut syntax is used
360
posOption = pos;
361                         shortOption = true;
362                         value = value.substring(0, pos);
363                     }
364                 }
365
366                 if (posDefault >= 0) {
367                     // there was an explicit 'default' setting using the key, check where it ends
368
end = part.length();
369                     if (posHelp > posDefault) {
370                         end = posHelp;
371                     }
372                     if ((posOption > posDefault) && (posOption < end)) {
373                         end = posOption;
374                     }
375                     if ((posValue > posDefault) && (posValue < end)) {
376                         end = posValue;
377                     }
378                     String JavaDoc sub = part.substring(posDefault + KEY_DEFAULT.length(), end).trim();
379                     // cut of trailing '
380
sub = sub.substring(0, sub.length() - 1);
381                     isDefault = Boolean.valueOf(sub).booleanValue();
382                 } else {
383                     // check for shortcut syntax, value must end with a '*'
384
if (value.charAt(value.length() - 1) == DEFAULT_MARKER) {
385                         isDefault = true;
386                         value = value.substring(0, value.length() - 1);
387                     }
388                 }
389
390                 if (posOption >= 0) {
391                     // an 'option' setting is available, check where it ends
392
end = part.length();
393                     if (posHelp > posOption) {
394                         end = posHelp;
395                     }
396                     if ((posDefault > posOption) && (posDefault < end)) {
397                         end = posDefault;
398                     }
399                     if ((posValue > posOption) && (posValue < end)) {
400                         end = posValue;
401                     }
402                     if (shortOption) {
403                         // shortcut syntax used for option with ':' appended to value
404
option = part.substring(posOption + 1, end).trim();
405                     } else {
406                         option = part.substring(posOption + KEY_OPTION.length(), end).trim();
407                         // cut of trailing '
408
option = option.substring(0, option.length() - 1);
409                     }
410                 }
411
412                 if (posHelp >= 0) {
413                     // a 'help' setting is available, check where it ends
414
end = part.length();
415                     if (posOption > posHelp) {
416                         end = posOption;
417                     }
418                     if ((posDefault > posHelp) && (posDefault < end)) {
419                         end = posDefault;
420                     }
421                     if ((posValue > posHelp) && (posValue < end)) {
422                         end = posValue;
423                     }
424                     help = part.substring(posHelp + KEY_HELP.length(), end).trim();
425                     // cut of trailing '
426
help = help.substring(0, help.length() - 1);
427                 }
428
429                 // check if there was already a 'true' default, if so all other entries are 'false'
430
if (foundDefault) {
431                     isDefault = false;
432                 } else if (isDefault) {
433                     foundDefault = true;
434                 }
435
436                 result.add(new CmsSelectWidgetOption(value, isDefault, option, help));
437
438             } catch (Exception JavaDoc e) {
439                 if (LOG.isInfoEnabled()) {
440                     LOG.info(Messages.get().getBundle().key(Messages.ERR_MALFORMED_SELECT_OPTIONS_1, input));
441                 }
442             }
443         }
444
445         return result;
446     }
447
448     /**
449      * @see java.lang.Object#equals(java.lang.Object)
450      */

451     public boolean equals(Object JavaDoc obj) {
452
453         if (!(obj instanceof CmsSelectWidgetOption)) {
454             return false;
455         }
456         CmsSelectWidgetOption other = (CmsSelectWidgetOption)obj;
457         if (m_default != other.m_default) {
458             return false;
459         }
460         if (m_value == null) {
461             if (other.m_value != null) {
462                 return false;
463             }
464         } else if (!m_value.equals(other.m_value)) {
465             return false;
466         }
467         if (m_option == null) {
468             if (other.m_option != null) {
469                 return false;
470             }
471         } else if (!m_option.equals(other.m_option)) {
472             return false;
473         }
474         if (m_help == null) {
475             if (other.m_help != null) {
476                 return false;
477             }
478         } else if (!m_help.equals(other.m_help)) {
479             return false;
480         }
481         return true;
482     }
483
484     /**
485      * Returns the (optional) help text of this select option.<p>
486      *
487      * @return the (optional) help text of this select option
488      */

489     public String JavaDoc getHelp() {
490
491         return m_help;
492     }
493
494     /**
495      * Returns the option text of this select option.<p>
496      *
497      * If this has not been set, the result of <code>{@link #getValue()}</code> is returned,
498      * there will always be a result other than <code>null</code> returned.<p>
499      *
500      * @return the option text of this select option
501      */

502     public String JavaDoc getOption() {
503
504         if (m_option == null) {
505             return getValue();
506         }
507         return m_option;
508     }
509
510     /**
511      * Returns the value of this select option.<p>
512      *
513      * @return the value of this select option
514      */

515     public String JavaDoc getValue() {
516
517         return m_value;
518     }
519
520     /**
521      * @see java.lang.Object#hashCode()
522      */

523     public int hashCode() {
524
525         if (m_hashcode == 0) {
526             StringBuffer JavaDoc hash = new StringBuffer JavaDoc(128);
527             hash.append(m_value);
528             hash.append('|');
529             hash.append(m_default);
530             hash.append('|');
531             hash.append(m_option);
532             hash.append('|');
533             hash.append(m_help);
534             m_hashcode = hash.toString().hashCode();
535         }
536         return m_hashcode;
537     }
538
539     /**
540      * Returns <code>true</code> if this is the default value of the selection.<p>
541      *
542      * @return <code>true</code> if this is the default value of the selection
543      */

544     public boolean isDefault() {
545
546         return m_default;
547     }
548
549     /**
550      * @see java.lang.Object#toString()
551      */

552     public String JavaDoc toString() {
553
554         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
555
556         result.append(KEY_VALUE);
557         result.append(m_value);
558         result.append('\'');
559         if (m_default) {
560             result.append(' ');
561             result.append(KEY_DEFAULT);
562             result.append(m_default);
563             result.append('\'');
564         }
565         if (m_option != null) {
566             result.append(' ');
567             result.append(KEY_OPTION);
568             result.append(m_option);
569             result.append('\'');
570         }
571         if (m_help != null) {
572             result.append(' ');
573             result.append(KEY_HELP);
574             result.append(m_help);
575             result.append('\'');
576         }
577         return result.toString();
578     }
579 }
Popular Tags