001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.beanutils; 018 019/** 020 * <p>Decorates a {@link DynaBean} to provide <code>Map</code> behavior.</p> 021 * 022 * <p>The motivation for this implementation is to provide access to {@link DynaBean} 023 * properties in technologies that are unaware of BeanUtils and {@link DynaBean}s - 024 * such as the expression languages of JSTL and JSF.</p> 025 * 026 * <p>This can be achieved either by wrapping the {@link DynaBean} prior to 027 * providing it to the technology to process or by providing a <code>Map</code> 028 * accessor method on the DynaBean implementation:</p> 029 * <pre><code> 030 * public Map<String, Object> getMap() { 031 * return new DynaBeanPropertyMapDecorator(this); 032 * }</code></pre> 033 * 034 * <p>This, for example, could be used in JSTL in the following way to access 035 * a DynaBean's <code>fooProperty</code>:</p> 036 * <ul><li><code>${myDynaBean.<b>map</b>.fooProperty}</code></li></ul> 037 * 038 * <h3>Usage</h3> 039 * 040 * <p>To decorate a {@link DynaBean} simply instantiate this class with the 041 * target {@link DynaBean}:</p> 042 * 043 * <ul><li><code>Map<String, Object> fooMap = new DynaBeanPropertyMapDecorator(fooDynaBean);</code></li></ul> 044 * 045 * <p>The above example creates a <b><i>read only</i></b> <code>Map</code>. 046 * To create a <code>Map</code> which can be modified, construct a 047 * <code>DynaBeanPropertyMapDecorator</code> with the <b><i>read only</i></b> 048 * attribute set to <code>false</code>:</p> 049 * 050 * <ul><li><code>Map<String, Object> fooMap = 051 * new DynaBeanPropertyMapDecorator(fooDynaBean, false);</code></li></ul> 052 * 053 * <h3>Limitations</h3> 054 * <p>In this implementation the <code>entrySet()</code>, <code>keySet()</code> 055 * and <code>values()</code> methods create an <b><i>unmodifiable</i></b> 056 * <code>Set</code> and it does not support the Map's <code>clear()</code> 057 * and <code>remove()</code> operations.</p> 058 * 059 * @since BeanUtils 1.9.0 060 * @version $Id$ 061 */ 062public class DynaBeanPropertyMapDecorator extends BaseDynaBeanMapDecorator<String> { 063 /** 064 * Construct a Map for the specified {@link DynaBean}. 065 * 066 * @param dynaBean The dyna bean being decorated 067 * @param readOnly <code>true</code> if the Map is read only 068 * otherwise <code>false</code> 069 * @throws IllegalArgumentException if the {@link DynaBean} is null. 070 */ 071 public DynaBeanPropertyMapDecorator(final DynaBean dynaBean, final boolean readOnly) { 072 super(dynaBean, readOnly); 073 } 074 075 /** 076 * Constructs a read only Map for the specified 077 * {@link DynaBean}. 078 * 079 * @param dynaBean The dyna bean being decorated 080 * @throws IllegalArgumentException if the {@link DynaBean} is null. 081 */ 082 public DynaBeanPropertyMapDecorator(final DynaBean dynaBean) { 083 super(dynaBean); 084 } 085 086 @Override 087 protected String convertKey(final String propertyName) { 088 return propertyName; 089 } 090}