0,0 → 1,91 |
package ak.strutsx; |
|
import java.util.Iterator; |
import java.util.TreeSet; |
import java.lang.reflect.Array; |
import javax.servlet.http.HttpServletRequest; |
import org.apache.struts.validator.DynaValidatorForm; |
import org.apache.struts.action.ActionErrors; |
import org.apache.struts.action.ActionMapping; |
import org.apache.struts.config.FormBeanConfig; |
import org.apache.struts.config.FormPropertyConfig; |
|
public final class ResizeableDynaValidatorForm |
extends DynaValidatorForm |
{ |
public void reset(ActionMapping mapping, HttpServletRequest request) |
{ |
super.reset(mapping, request); |
resize(mapping, request); |
} |
|
protected void resize(ActionMapping mapping, HttpServletRequest request) |
{ |
String name = mapping.getName(); |
if (name == null) return; |
|
FormBeanConfig config = mapping.getModuleConfig().findFormBeanConfig(name); |
if (config == null) return; |
|
FormPropertyConfig props[] = config.findFormPropertyConfigs(); |
for (int i = 0; i < props.length; i++) { |
if(props[i].getSize() > 0) // for arrays only |
resize(mapping, request, props[i]); |
} |
} |
|
protected void resize(ActionMapping mapping, HttpServletRequest request, |
FormPropertyConfig prop) |
{ |
String nameStart = prop.getName() + "["; |
|
// get all indices |
TreeSet indices = new TreeSet(); |
for(Iterator i = request.getParameterMap().keySet().iterator(); i.hasNext(); ) { |
String name = (String)i.next(); |
if(name.startsWith(nameStart)) { |
int p = name.indexOf("]"); |
if(p > 0) { |
String index = name.substring(nameStart.length(), p); |
try { |
indices.add(new Integer(index)); |
} |
catch(NumberFormatException ex) { |
} |
} |
} |
} |
|
// find last index in sequence |
int lastIndex = -1; |
for(Iterator i = indices.iterator(); i.hasNext(); ) { |
Integer idx = (Integer)i.next(); |
if(idx.intValue() == lastIndex+1) |
lastIndex++; |
else |
break; |
} |
|
// grow |
if(lastIndex > 0) { |
Class clazz = prop.getTypeClass(); |
Object initialValue = Array.newInstance(clazz.getComponentType(), lastIndex+1); |
for (int i = 0; i < lastIndex+1; i++) { |
try { |
Array.set(initialValue, i, clazz.getComponentType().newInstance()); |
} |
catch (Throwable t) { |
; // Probably does not have a zero-args constructor |
} |
} |
set(prop.getName(), initialValue); |
} |
} |
|
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) |
{ |
ActionErrors errors = super.validate(mapping, request); |
|
return errors; |
} |
} |