/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.struts.forms.legacy;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import de.odysseus.calyxo.base.util.PropertyUtils;
import de.odysseus.calyxo.forms.FormInputValues;
/**
* Access String and String[] properties.
*
* @author Christoph Beck
*/
public class FormAccessor implements FormInputValues, Serializable {
private Object bean;
/**
*
*/
public FormAccessor(Object form) {
super();
this.bean = form;
}
public String _getInput(String name) {
PropertyDescriptor descr =
PropertyUtils.getPropertyDescriptor(bean.getClass(), name);
if (descr != null) {
return _getInput(descr);
} else {
return null;
}
}
public String[] _getInputs(String name) {
PropertyDescriptor descr =
PropertyUtils.getPropertyDescriptor(bean.getClass(), name);
if (descr != null) {
return _getInputs(descr);
} else {
return null;
}
}
public void _setInput(String name, String value) {
PropertyDescriptor descr =
PropertyUtils.getPropertyDescriptor(bean.getClass(), name);
if (descr != null) {
_setInput(descr, value);
}
}
public void _setInputs(String name, String[] values) {
PropertyDescriptor descr =
PropertyUtils.getPropertyDescriptor(bean.getClass(), name);
if (descr != null) {
_setInputs(descr, values);
}
}
private String[] _getInputs(PropertyDescriptor descr) {
if (descr.getPropertyType() == String[].class) {
try {
return (String[])PropertyUtils.getProperty(bean, descr.getName());
} catch (Exception e) {
return null;
}
} else if (descr.getPropertyType() == String.class) {
String value = _getInput(descr);
return value == null ? new String[0] : new String[]{value};
} else {
return null;
}
}
private String _getInput(PropertyDescriptor descr) {
if (descr.getPropertyType() == String.class) {
try {
return (String)PropertyUtils.getProperty(bean, descr.getName());
} catch (Exception e) {
return null;
}
} else if (descr.getPropertyType() == String[].class) {
String[] values = _getInputs(descr);
return values == null || values.length == 0 ? null : values[0];
} else {
return null;
}
}
private void _setInput(PropertyDescriptor descr, String value) {
if (descr.getPropertyType() == String.class) {
try {
PropertyUtils.setProperty(bean, descr.getName(), value);
} catch (Exception e) {
return;
}
} else if (descr.getPropertyType() == String[].class) {
String[] values = _getInputs(descr);
if (values != null && values.length > 0) {
values[0] = value;
} else {
values = new String[]{value};
}
_setInputs(descr, values);
}
}
private void _setInputs(PropertyDescriptor descr, String[] values) {
if (descr.getPropertyType() == String[].class) {
try {
PropertyUtils.setProperty(bean, descr.getName(), values);
} catch (Exception e) {
return;
}
} else if (descr.getPropertyType() == String.class) {
if (values != null && values.length > 0) {
_setInput(descr, values[0]);
}
}
}
}