| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| JstlPropertyHolder |
|
| 2.6;2.6 |
| 1 | /* | |
| 2 | * Copyright 2005-2008 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php | |
| 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 | package org.kuali.rice.kns.util; | |
| 17 | ||
| 18 | import java.util.Collection; | |
| 19 | import java.util.Map; | |
| 20 | import java.util.Properties; | |
| 21 | import java.util.Set; | |
| 22 | ||
| 23 | import org.kuali.rice.kns.util.properties.PropertyTree; | |
| 24 | ||
| 25 | /** | |
| 26 | * This class implements the Map interface for a Properties instance. Exports all properties from the given Properties instance as | |
| 27 | * constants, usable from jstl. Implements the Map interface (by delegating everything to the PropertyTree, which really implements | |
| 28 | * the Map methods directly) so that jstl can translate ${Constants.a} into a call to ConfigConstants.get( "a" ). | |
| 29 | * <p> | |
| 30 | * The contents of this Map cannot be changed once it has been initialized. Any calls to any of the Map methods made before the | |
| 31 | * propertyTree has been initialized (i.e. before setProperties has been called) will throw an IllegalStateException. | |
| 32 | * <p> | |
| 33 | * Jstl converts ${Constants.a.b.c} into get("a").get("b").get("c"), so the properties are stored in a PropertyTree, which converts | |
| 34 | * the initial set( "a.b.c", "value" ) into construction of the necessary tree structure to support get("a").get("b").get("c"). | |
| 35 | * <p> | |
| 36 | * Implicitly relies on the assumption that the JSP will be calling toString() on the result of the final <code>get</code>, since | |
| 37 | * <code>get</code> can only return one type, and that type must be the complex one so that further dereferencing will be | |
| 38 | * possible. | |
| 39 | * | |
| 40 | * | |
| 41 | */ | |
| 42 | ||
| 43 | public abstract class JstlPropertyHolder implements Map { | |
| 44 | private PropertyTree propertyTree; | |
| 45 | ||
| 46 | /** | |
| 47 | * Default constructor | |
| 48 | */ | |
| 49 | 0 | public JstlPropertyHolder() { |
| 50 | 0 | propertyTree = null; |
| 51 | 0 | } |
| 52 | ||
| 53 | /** | |
| 54 | * Creates a propertyTree to store the given properties | |
| 55 | * | |
| 56 | * @param properties | |
| 57 | */ | |
| 58 | protected void setProperties(Properties properties) { | |
| 59 | 0 | propertyTree = new PropertyTree(properties); |
| 60 | 0 | } |
| 61 | ||
| 62 | ||
| 63 | /** | |
| 64 | * Copies in the given propertyTree rather than building its own. Reasonably dangerous, since that tree might presumably be | |
| 65 | * modified, violating the readonlyness of this datastructure. | |
| 66 | * | |
| 67 | * @param properties | |
| 68 | */ | |
| 69 | protected void setPropertyTree(PropertyTree tree) { | |
| 70 | 0 | propertyTree = tree; |
| 71 | 0 | } |
| 72 | ||
| 73 | ||
| 74 | // delegated methods | |
| 75 | /** | |
| 76 | * @see org.kuali.rice.kns.util.properties.PropertyTree#get(java.lang.Object) | |
| 77 | */ | |
| 78 | public Object get(Object key) { | |
| 79 | 0 | if (propertyTree == null) { |
| 80 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 81 | } | |
| 82 | 0 | return this.propertyTree.get(key); |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * @see org.kuali.rice.kns.util.properties.PropertyTree#size() | |
| 87 | */ | |
| 88 | public int size() { | |
| 89 | 0 | if (propertyTree == null) { |
| 90 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 91 | } | |
| 92 | 0 | return this.propertyTree.size(); |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * @see org.kuali.rice.kns.util.properties.PropertyTree#clear() | |
| 97 | */ | |
| 98 | public void clear() { | |
| 99 | 0 | if (propertyTree == null) { |
| 100 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 101 | } | |
| 102 | 0 | this.propertyTree.clear(); |
| 103 | 0 | } |
| 104 | ||
| 105 | /** | |
| 106 | * @see org.kuali.rice.kns.util.properties.PropertyTree#isEmpty() | |
| 107 | */ | |
| 108 | public boolean isEmpty() { | |
| 109 | 0 | if (propertyTree == null) { |
| 110 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 111 | } | |
| 112 | 0 | return this.propertyTree.isEmpty(); |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * @see org.kuali.rice.kns.util.properties.PropertyTree#containsKey(java.lang.Object) | |
| 117 | */ | |
| 118 | public boolean containsKey(Object key) { | |
| 119 | 0 | if (propertyTree == null) { |
| 120 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 121 | } | |
| 122 | 0 | return this.propertyTree.containsKey(key); |
| 123 | } | |
| 124 | ||
| 125 | /** | |
| 126 | * @see org.kuali.rice.kns.util.properties.PropertyTree#containsValue(java.lang.Object) | |
| 127 | */ | |
| 128 | public boolean containsValue(Object value) { | |
| 129 | 0 | if (propertyTree == null) { |
| 130 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 131 | } | |
| 132 | 0 | return this.propertyTree.containsValue(value); |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * @see org.kuali.rice.kns.util.properties.PropertyTree#values() | |
| 137 | */ | |
| 138 | public Collection values() { | |
| 139 | 0 | if (propertyTree == null) { |
| 140 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 141 | } | |
| 142 | 0 | return this.propertyTree.values(); |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * @see org.kuali.rice.kns.util.properties.PropertyTree#putAll(java.util.Map) | |
| 147 | */ | |
| 148 | public void putAll(Map m) { | |
| 149 | 0 | if (propertyTree == null) { |
| 150 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 151 | } | |
| 152 | 0 | this.propertyTree.putAll(m); |
| 153 | 0 | } |
| 154 | ||
| 155 | /** | |
| 156 | * @see org.kuali.rice.kns.util.properties.PropertyTree#entrySet() | |
| 157 | */ | |
| 158 | public Set entrySet() { | |
| 159 | 0 | if (propertyTree == null) { |
| 160 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 161 | } | |
| 162 | 0 | return this.propertyTree.entrySet(); |
| 163 | } | |
| 164 | ||
| 165 | /** | |
| 166 | * @see org.kuali.rice.kns.util.properties.PropertyTree#keySet() | |
| 167 | */ | |
| 168 | public Set keySet() { | |
| 169 | 0 | if (propertyTree == null) { |
| 170 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 171 | } | |
| 172 | 0 | return this.propertyTree.keySet(); |
| 173 | } | |
| 174 | ||
| 175 | /** | |
| 176 | * @see org.kuali.rice.kns.util.properties.PropertyTree#remove(java.lang.Object) | |
| 177 | */ | |
| 178 | public Object remove(Object key) { | |
| 179 | 0 | if (propertyTree == null) { |
| 180 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 181 | } | |
| 182 | 0 | return this.propertyTree.remove(key); |
| 183 | } | |
| 184 | ||
| 185 | /** | |
| 186 | * @see org.kuali.rice.kns.util.properties.PropertyTree#put(java.lang.Object, java.lang.Object) | |
| 187 | */ | |
| 188 | public Object put(Object key, Object value) { | |
| 189 | 0 | if (propertyTree == null) { |
| 190 | 0 | throw new IllegalStateException("propertyTree has not been initialized"); |
| 191 | } | |
| 192 | 0 | return this.propertyTree.put(key, value); |
| 193 | } | |
| 194 | } |