001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.struts.forms.legacy;
017:
018: import java.beans.PropertyDescriptor;
019: import java.io.Serializable;
020:
021: import de.odysseus.calyxo.base.util.PropertyUtils;
022: import de.odysseus.calyxo.forms.FormInputValues;
023:
024: /**
025: * Access String and String[] properties.
026: *
027: * @author Christoph Beck
028: */
029: public class FormAccessor implements FormInputValues, Serializable {
030: private Object bean;
031:
032: /**
033: *
034: */
035: public FormAccessor(Object form) {
036: super ();
037: this .bean = form;
038: }
039:
040: public String _getInput(String name) {
041: PropertyDescriptor descr = PropertyUtils.getPropertyDescriptor(
042: bean.getClass(), name);
043: if (descr != null) {
044: return _getInput(descr);
045: } else {
046: return null;
047: }
048: }
049:
050: public String[] _getInputs(String name) {
051: PropertyDescriptor descr = PropertyUtils.getPropertyDescriptor(
052: bean.getClass(), name);
053: if (descr != null) {
054: return _getInputs(descr);
055: } else {
056: return null;
057: }
058: }
059:
060: public void _setInput(String name, String value) {
061: PropertyDescriptor descr = PropertyUtils.getPropertyDescriptor(
062: bean.getClass(), name);
063: if (descr != null) {
064: _setInput(descr, value);
065: }
066: }
067:
068: public void _setInputs(String name, String[] values) {
069: PropertyDescriptor descr = PropertyUtils.getPropertyDescriptor(
070: bean.getClass(), name);
071: if (descr != null) {
072: _setInputs(descr, values);
073: }
074: }
075:
076: private String[] _getInputs(PropertyDescriptor descr) {
077: if (descr.getPropertyType() == String[].class) {
078: try {
079: return (String[]) PropertyUtils.getProperty(bean, descr
080: .getName());
081: } catch (Exception e) {
082: return null;
083: }
084: } else if (descr.getPropertyType() == String.class) {
085: String value = _getInput(descr);
086: return value == null ? new String[0]
087: : new String[] { value };
088: } else {
089: return null;
090: }
091: }
092:
093: private String _getInput(PropertyDescriptor descr) {
094: if (descr.getPropertyType() == String.class) {
095: try {
096: return (String) PropertyUtils.getProperty(bean, descr
097: .getName());
098: } catch (Exception e) {
099: return null;
100: }
101: } else if (descr.getPropertyType() == String[].class) {
102: String[] values = _getInputs(descr);
103: return values == null || values.length == 0 ? null
104: : values[0];
105: } else {
106: return null;
107: }
108: }
109:
110: private void _setInput(PropertyDescriptor descr, String value) {
111: if (descr.getPropertyType() == String.class) {
112: try {
113: PropertyUtils.setProperty(bean, descr.getName(), value);
114: } catch (Exception e) {
115: return;
116: }
117: } else if (descr.getPropertyType() == String[].class) {
118: String[] values = _getInputs(descr);
119: if (values != null && values.length > 0) {
120: values[0] = value;
121: } else {
122: values = new String[] { value };
123: }
124: _setInputs(descr, values);
125: }
126: }
127:
128: private void _setInputs(PropertyDescriptor descr, String[] values) {
129: if (descr.getPropertyType() == String[].class) {
130: try {
131: PropertyUtils
132: .setProperty(bean, descr.getName(), values);
133: } catch (Exception e) {
134: return;
135: }
136: } else if (descr.getPropertyType() == String.class) {
137: if (values != null && values.length > 0) {
138: _setInput(descr, values[0]);
139: }
140: }
141: }
142: }
|