1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.beanutils; 18 19 /** 20 * <p>Decorates a {@link DynaBean} to provide <code>Map</code> behavior.</p> 21 * 22 * <p>The motivation for this implementation is to provide access to {@link DynaBean} 23 * properties in technologies that are unaware of BeanUtils and {@link DynaBean}s - 24 * such as the expression languages of JSTL and JSF.</p> 25 * 26 * <p>This can be achieved either by wrapping the {@link DynaBean} prior to 27 * providing it to the technology to process or by providing a <code>Map</code> 28 * accessor method on the DynaBean implementation: 29 * <pre><code> 30 * public Map<String, Object> getMap() { 31 * return new DynaBeanPropertyMapDecorator(this); 32 * }</code></pre> 33 * </ul> 34 * </p> 35 * 36 * <p>This, for example, could be used in JSTL in the following way to access 37 * a DynaBean's <code>fooProperty</code>: 38 * <ul><li><code>${myDynaBean.<b>map</b>.fooProperty}</code></li></ul> 39 * </p> 40 * 41 * <h3>Usage</h3> 42 * 43 * <p>To decorate a {@link DynaBean} simply instantiate this class with the 44 * target {@link DynaBean}:</p> 45 * 46 * <ul><li><code>Map<String, Object> fooMap = new DynaBeanPropertyMapDecorator(fooDynaBean);</code></li></ul> 47 * 48 * <p>The above example creates a <b><i>read only</i></b> <code>Map</code>. 49 * To create a <code>Map</code> which can be modified, construct a 50 * <code>DynaBeanPropertyMapDecorator</code> with the <b><i>read only</i></b> 51 * attribute set to <code>false</code>:</p> 52 * 53 * <ul><li><code>Map<String, Object> fooMap = 54 * new DynaBeanPropertyMapDecorator(fooDynaBean, false);</code></li></ul> 55 * 56 * <h3>Limitations</h3> 57 * <p>In this implementation the <code>entrySet()</code>, <code>keySet()</code> 58 * and <code>values()</code> methods create an <b><i>unmodifiable</i></b> 59 * <code>Set</code> and it does not support the Map's <code>clear()</code> 60 * and <code>remove()</code> operations.</p> 61 * 62 * @since BeanUtils 1.9.0 63 * @version $Id$ 64 */ 65 public class DynaBeanPropertyMapDecorator extends BaseDynaBeanMapDecorator<String> { 66 /** 67 * Construct a Map for the specified {@link DynaBean}. 68 * 69 * @param dynaBean The dyna bean being decorated 70 * @param readOnly <code>true</code> if the Map is read only 71 * otherwise <code>false</code> 72 * @throws IllegalArgumentException if the {@link DynaBean} is null. 73 */ 74 public DynaBeanPropertyMapDecorator(final DynaBean dynaBean, final boolean readOnly) { 75 super(dynaBean, readOnly); 76 } 77 78 /** 79 * Constructs a read only Map for the specified 80 * {@link DynaBean}. 81 * 82 * @param dynaBean The dyna bean being decorated 83 * @throws IllegalArgumentException if the {@link DynaBean} is null. 84 */ 85 public DynaBeanPropertyMapDecorator(final DynaBean dynaBean) { 86 super(dynaBean); 87 } 88 89 @Override 90 protected String convertKey(final String propertyName) { 91 return propertyName; 92 } 93 }