| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.configuration.beanutils.ConfigurationDynaClass |
|
|
| 1 | /* |
|
| 2 | * Copyright 2001-2004 The Apache Software Foundation. |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | ||
| 17 | package org.apache.commons.configuration.beanutils; |
|
| 18 | ||
| 19 | import java.util.Iterator; |
|
| 20 | import java.util.ArrayList; |
|
| 21 | import java.util.List; |
|
| 22 | ||
| 23 | import org.apache.commons.beanutils.DynaBean; |
|
| 24 | import org.apache.commons.beanutils.DynaClass; |
|
| 25 | import org.apache.commons.beanutils.DynaProperty; |
|
| 26 | import org.apache.commons.configuration.Configuration; |
|
| 27 | import org.apache.commons.logging.Log; |
|
| 28 | import org.apache.commons.logging.LogFactory; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * The <tt>ConfigurationDynaClass</tt> dynamically determines properties for |
|
| 32 | * a <code>ConfigurationDynaBean</code> from a wrapped configuration-collection |
|
| 33 | * {@link org.apache.commons.configuration.Configuration} instance. |
|
| 34 | * |
|
| 35 | * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a> |
|
| 36 | * @version $Revision: 1.4 $, $Date: 2004/09/21 17:49:39 $ |
|
| 37 | * @since 1.0-rc1 |
|
| 38 | */ |
|
| 39 | public class ConfigurationDynaClass implements DynaClass |
|
| 40 | { |
|
| 41 | 10 | private final static Log log = LogFactory.getLog(ConfigurationDynaClass.class); |
| 42 | ||
| 43 | Configuration configuration; |
|
| 44 | ||
| 45 | /** |
|
| 46 | * Construct an instance of a <code>ConfigurationDynaClass</code> |
|
| 47 | * wrapping the specified <code>Configuration</code> instance. |
|
| 48 | * @param configuration <code>Configuration</code> instance. |
|
| 49 | */ |
|
| 50 | public ConfigurationDynaClass(Configuration configuration) |
|
| 51 | { |
|
| 52 | 11 | super(); |
| 53 | 11 | if (log.isTraceEnabled()) log.trace("ConfigurationDynaClass(" + configuration + ")"); |
| 54 | 11 | this.configuration = configuration; |
| 55 | 11 | } |
| 56 | ||
| 57 | /** |
|
| 58 | * @see org.apache.commons.beanutils.DynaClass#getDynaProperty(java.lang.String) |
|
| 59 | */ |
|
| 60 | public DynaProperty getDynaProperty(String name) |
|
| 61 | { |
|
| 62 | 36 | if (log.isTraceEnabled()) |
| 63 | { |
|
| 64 | 0 | log.trace("getDynaProperty(" + name + ")"); |
| 65 | } |
|
| 66 | ||
| 67 | 36 | if (name == null) |
| 68 | { |
|
| 69 | 1 | throw new IllegalArgumentException("No such property name=[" + name + "]"); |
| 70 | } |
|
| 71 | ||
| 72 | 35 | Object value = configuration.getProperty(name); |
| 73 | 35 | if (value == null) |
| 74 | { |
|
| 75 | 1 | return null; |
| 76 | } |
|
| 77 | else |
|
| 78 | { |
|
| 79 | 34 | Class type = value.getClass(); |
| 80 | ||
| 81 | 34 | if (type == Byte.class) |
| 82 | { |
|
| 83 | 1 | type = Byte.TYPE; |
| 84 | } |
|
| 85 | 34 | if (type == Character.class) |
| 86 | { |
|
| 87 | 1 | type = Character.TYPE; |
| 88 | } |
|
| 89 | 33 | else if (type == Boolean.class) |
| 90 | { |
|
| 91 | 4 | type = Boolean.TYPE; |
| 92 | } |
|
| 93 | 29 | else if (type == Double.class) |
| 94 | { |
|
| 95 | 2 | type = Double.TYPE; |
| 96 | } |
|
| 97 | 27 | else if (type == Float.class) |
| 98 | { |
|
| 99 | 2 | type = Float.TYPE; |
| 100 | } |
|
| 101 | 25 | else if (type == Integer.class) |
| 102 | { |
|
| 103 | 3 | type = Integer.TYPE; |
| 104 | } |
|
| 105 | 22 | else if (type == Long.class) |
| 106 | { |
|
| 107 | 2 | type = Long.TYPE; |
| 108 | } |
|
| 109 | 20 | else if (type == Short.class) |
| 110 | { |
|
| 111 | 2 | type = Short.TYPE; |
| 112 | } |
|
| 113 | ||
| 114 | 34 | return new DynaProperty(name, type); |
| 115 | } |
|
| 116 | } |
|
| 117 | ||
| 118 | /** |
|
| 119 | * @see org.apache.commons.beanutils.DynaClass#getDynaProperties() |
|
| 120 | */ |
|
| 121 | public DynaProperty[] getDynaProperties() |
|
| 122 | { |
|
| 123 | 1 | if (log.isTraceEnabled()) |
| 124 | { |
|
| 125 | 0 | log.trace("getDynaProperties()"); |
| 126 | } |
|
| 127 | ||
| 128 | 1 | Iterator keys = configuration.getKeys(); |
| 129 | 1 | List properties = new ArrayList(); |
| 130 | 27 | while (keys.hasNext()) |
| 131 | { |
|
| 132 | 26 | String key = (String) keys.next(); |
| 133 | 26 | DynaProperty property = getDynaProperty(key); |
| 134 | 26 | properties.add(property); |
| 135 | } |
|
| 136 | ||
| 137 | 1 | DynaProperty[] propertyArray = new DynaProperty[properties.size()]; |
| 138 | 1 | properties.toArray(propertyArray); |
| 139 | 1 | if (log.isDebugEnabled()) |
| 140 | { |
|
| 141 | 0 | log.debug("Found " + properties.size() + " properties."); |
| 142 | } |
|
| 143 | ||
| 144 | 1 | return propertyArray; |
| 145 | } |
|
| 146 | ||
| 147 | /** |
|
| 148 | * @see org.apache.commons.beanutils.DynaClass#getName() |
|
| 149 | */ |
|
| 150 | public String getName() |
|
| 151 | { |
|
| 152 | 0 | return ConfigurationDynaBean.class.getName(); |
| 153 | } |
|
| 154 | ||
| 155 | /** |
|
| 156 | * @see org.apache.commons.beanutils.DynaClass#newInstance() |
|
| 157 | */ |
|
| 158 | public DynaBean newInstance() throws IllegalAccessException, InstantiationException |
|
| 159 | { |
|
| 160 | 0 | return new ConfigurationDynaBean(configuration); |
| 161 | } |
|
| 162 | ||
| 163 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |