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: 029 * </p> 030 * <pre> 031 * public Map<String, Object> getMap() { 032 * return new DynaBeanPropertyMapDecorator(this); 033 * }</pre> 034 * 035 * <p>This, for example, could be used in JSTL in the following way to access 036 * a DynaBean's <code>fooProperty</code>: 037 * </p> 038 * <ul><li><code>${myDynaBean.<strong>map</strong>.fooProperty}</code></li></ul> 039 * 040 * <strong>Usage</strong> 041 * 042 * <p>To decorate a {@link DynaBean} simply instantiate this class with the 043 * target {@link DynaBean}:</p> 044 * 045 * <ul><li><code>Map<String, Object> fooMap = new DynaBeanPropertyMapDecorator(fooDynaBean);</code></li></ul> 046 * 047 * <p>The above example creates a <strong><em>read only</em></strong> <code>Map</code>. 048 * To create a <code>Map</code> which can be modified, construct a 049 * <code>DynaBeanPropertyMapDecorator</code> with the <strong><em>read only</em></strong> 050 * attribute set to <code>false</code>:</p> 051 * 052 * <ul><li><code>Map<String, Object> fooMap = 053 * new DynaBeanPropertyMapDecorator(fooDynaBean, false);</code></li></ul> 054 * 055 * <strong>Limitations</strong> 056 * <p>In this implementation the <code>entrySet()</code>, <code>keySet()</code> 057 * and <code>values()</code> methods create an <strong><em>unmodifiable</em></strong> 058 * <code>Set</code> and it does not support the Map's <code>clear()</code> 059 * and <code>remove()</code> operations.</p> 060 * 061 * @since BeanUtils 1.9.0 062 */ 063public class DynaBeanPropertyMapDecorator extends BaseDynaBeanMapDecorator<String> { 064 065 /** 066 * Constructs a read only Map for the specified 067 * {@link DynaBean}. 068 * 069 * @param dynaBean The dyna bean being decorated 070 * @throws IllegalArgumentException if the {@link DynaBean} is null. 071 */ 072 public DynaBeanPropertyMapDecorator(final DynaBean dynaBean) { 073 super(dynaBean); 074 } 075 076 /** 077 * Construct a Map for the specified {@link DynaBean}. 078 * 079 * @param dynaBean The dyna bean being decorated 080 * @param readOnly <code>true</code> if the Map is read only 081 * otherwise <code>false</code> 082 * @throws IllegalArgumentException if the {@link DynaBean} is null. 083 */ 084 public DynaBeanPropertyMapDecorator(final DynaBean dynaBean, final boolean readOnly) { 085 super(dynaBean, readOnly); 086 } 087 088 @Override 089 protected String convertKey(final String propertyName) { 090 return propertyName; 091 } 092}