1 16 package org.apache.cocoon.forms.util; 17 18 import java.util.AbstractMap ; 19 import java.util.AbstractSet ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import org.apache.cocoon.forms.formmodel.AbstractContainerWidget; 27 import org.apache.cocoon.forms.formmodel.ContainerWidget; 28 import org.apache.cocoon.forms.formmodel.Repeater; 29 import org.apache.cocoon.forms.formmodel.Widget; 30 import org.apache.commons.collections.iterators.AbstractIteratorDecorator; 31 32 47 public class ContainerWidgetAsMap extends AbstractMap { 48 private AbstractContainerWidget container; 49 private boolean lowerCase; 50 51 62 public ContainerWidgetAsMap(AbstractContainerWidget container, boolean keysToLowerCase) { 63 this.container = container; 64 this.lowerCase = keysToLowerCase; 65 } 66 67 70 public ContainerWidgetAsMap(AbstractContainerWidget container) { 71 this(container, false); 72 } 73 74 79 public ContainerWidget getWidget() { 80 return this.container; 81 } 82 83 90 public Widget getWidget(String path) { 91 return this.container.lookupWidget(path); 92 } 93 94 105 public Object put(Object key, Object value) { 106 String name = (String )key; 107 if (lowerCase) name = name.toLowerCase(); 108 109 Widget w = container.getChild(name); 110 if (w != null) { 111 return setValue(w, value); 112 } else { 113 throw new UnsupportedOperationException (container + " has no child named '" + key + "'"); 114 } 115 } 116 117 public void putAll(Map map) { 118 Iterator iter = map.entrySet().iterator(); 119 while(iter.hasNext()) { 120 Map.Entry entry = (Map.Entry )iter.next(); 121 String name = (String )entry.getKey(); 122 if (lowerCase) name = name.toLowerCase(); 123 Widget w = container.getChild(name); 124 if (w != null) { 125 setValue(w, entry.getValue()); 126 } 127 } 128 } 129 130 public Object get(Object key) { 131 String name = (String )key; 132 if (lowerCase) name = name.toLowerCase(); 133 Widget w = container.getChild(name); 134 return w == null ? null : asValueOrMap(w); 135 } 136 137 public Set entrySet() { 138 return new ContainerEntrySet(); 139 } 140 141 private Object asValueOrMap(Widget w) { 142 if (w instanceof Repeater) { 143 return new RepeaterAsList((Repeater)w, lowerCase); 144 } else if (w instanceof AbstractContainerWidget) { 145 return new ContainerWidgetAsMap((AbstractContainerWidget)w, lowerCase); 146 } else { 147 try { 148 return w.getValue(); 149 } catch (UnsupportedOperationException uoe) { 150 return null; 152 } 153 } 154 } 155 156 159 private Object setValue(Widget w, Object value) { 160 if (w instanceof Repeater) { 161 if (!(value instanceof Collection )) { 163 throw new IllegalArgumentException ("A repeater cannot be filled with " + value); 164 } 165 List result = new RepeaterAsList((Repeater)w, lowerCase); 166 result.addAll((Collection )value); 167 return result; 168 169 } else if (w instanceof AbstractContainerWidget) { 170 if (!(value instanceof Map )) { 172 throw new IllegalArgumentException ("A container cannot be filled with " + value); 173 } 174 Map result = new ContainerWidgetAsMap((AbstractContainerWidget)w); 175 result.putAll((Map )value); 176 return result; 177 } else { 178 try { 179 Object result = w.getValue(); 180 w.setValue(value); 181 return result; 182 } catch (UnsupportedOperationException uoe) { 183 return null; 185 } 186 } 187 } 188 189 private class ContainerEntrySet extends AbstractSet { 190 public Iterator iterator() { 191 return new ContainerEntryIterator(); 192 } 193 194 public int size() { 195 return container.getSize(); 196 } 197 } 198 199 private class ContainerEntryIterator extends AbstractIteratorDecorator { 200 public ContainerEntryIterator() { 201 super(container.getChildren()); 202 } 203 204 public Object next() { 205 return new ContainerEntry((Widget)super.next()); 206 } 207 } 208 209 private class ContainerEntry implements Map.Entry { 210 Widget widget; 211 public ContainerEntry(Widget w) { 212 widget = w; 213 } 214 public Object getKey() { 215 return widget.getName(); 216 } 217 public Object getValue() { 218 return asValueOrMap(widget); 219 } 220 public Object setValue(Object value) { 221 Object result = asValueOrMap(widget); 222 ContainerWidgetAsMap.this.setValue(widget, value); 223 return result; 224 } 225 } 226 } | Popular Tags |