| Line | Hits | Source |
|---|---|---|
| 1 | /* | |
| 2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
| 3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
| 4 | ||
| 5 | All rights reserved. | |
| 6 | ||
| 7 | Redistribution and use in source and binary forms, with or without | |
| 8 | modification, are permitted provided that the following conditions are met: | |
| 9 | ||
| 10 | 1. Redistributions of source code must retain the above copyright notice, this | |
| 11 | list of conditions and the following disclaimer. | |
| 12 | ||
| 13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 14 | this list of conditions and the following disclaimer in the documentation | |
| 15 | and/or other materials provided with the distribution. | |
| 16 | ||
| 17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
| 18 | may be used to endorse or promote products derived from this software without | |
| 19 | specific prior written permission. | |
| 20 | ||
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
| 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
| 28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | ||
| 33 | package org.chwf.plugin.defaults; | |
| 34 | ||
| 35 | import java.util.Map; | |
| 36 | ||
| 37 | import org.chwf.config.Config; | |
| 38 | import org.chwf.config.PolymorphicConfig; | |
| 39 | import org.chwf.converter.ConversionException; | |
| 40 | import org.chwf.converter.Converter; | |
| 41 | import org.chwf.plugin.OptionLister; | |
| 42 | import org.chwf.util.MiscUtils; | |
| 43 | import org.chwf.util.SequencedHashMap; | |
| 44 | ||
| 45 | /** | |
| 46 | * A default option lister plugin implementation that retrieves list data | |
| 47 | * from the {@link org.chwf.config.Config} class. | |
| 48 | * | |
| 49 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 50 | */ | |
| 51 | 1 | public class DefaultOptionLister extends OptionLister { |
| 52 | ||
| 53 | /** Suffix for option key properties. */ | |
| 54 | private static final String OPTION_TEXT_PROPERTY_SUFFIX = ".option.text"; | |
| 55 | ||
| 56 | /** Suffix for option key properties. */ | |
| 57 | private static final String OPTION_KEY_PROPERTY_SUFFIX = ".option.key"; | |
| 58 | ||
| 59 | /** Empty String array. */ | |
| 60 | 1 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 61 | ||
| 62 | /** | |
| 63 | * Load the option list for a JavaBean's property.<p> | |
| 64 | * | |
| 65 | * @param beanClass The class in question. | |
| 66 | * @param property The property. | |
| 67 | * @return The list of options. | |
| 68 | * @throws ConversionException If type conversions fail. | |
| 69 | */ | |
| 70 | public Map loadOptionList(Class beanClass, String property) | |
| 71 | throws ConversionException { | |
| 72 | ||
| 73 | 103 | Class type = null; |
| 74 | 103 | type = MiscUtils.deriveType(beanClass, property); |
| 75 | 103 | if (type == null) { |
| 76 | 0 | return null; |
| 77 | } | |
| 78 | 103 | Config config = PolymorphicConfig.getConfig(beanClass); |
| 79 | 103 | String optionKey = property + OPTION_KEY_PROPERTY_SUFFIX; |
| 80 | 103 | String[] keys = config.getList(optionKey, EMPTY_STRING_ARRAY); |
| 81 | 103 | String testKey = property + OPTION_TEXT_PROPERTY_SUFFIX; |
| 82 | 103 | String[] texts = config.getList(testKey, EMPTY_STRING_ARRAY); |
| 83 | ||
| 84 | 103 | if ((keys.length > 0) && (keys.length == texts.length)) { |
| 85 | 6 | Map options = new SequencedHashMap(); |
| 86 | 6 | Converter converter = Converter.getConverter(type); |
| 87 | 29 | for (int i = 0; i < keys.length; i++) { |
| 88 | 23 | String key = keys[i]; |
| 89 | 23 | options.put(converter.parse(key), texts[i]); |
| 90 | } | |
| 91 | 6 | return options; |
| 92 | } else { | |
| 93 | 97 | return null; |
| 94 | } | |
| 95 | } | |
| 96 | ||
| 97 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |