1 16 package org.apache.cocoon.forms.formmodel; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.ListIterator ; 23 import java.util.Map ; 24 25 import org.apache.cocoon.util.location.Location; 26 27 34 public class WidgetDefinitionList { 35 36 private List widgetDefinitions = new ArrayList (); 37 private Map widgetDefinitionsById = new HashMap (); 38 private WidgetDefinition containerDefinition; 39 private boolean wasHere; 40 private ListIterator definitionsIt = widgetDefinitions.listIterator(); 41 42 45 public WidgetDefinitionList(WidgetDefinition definition) { 46 this.containerDefinition = definition; 47 wasHere = false; 48 } 49 50 public int size() { 51 return widgetDefinitions.size(); 52 } 53 54 public void addWidgetDefinition(WidgetDefinition widgetDefinition) throws DuplicateIdException { 55 String id = widgetDefinition.getId(); 56 if (!(widgetDefinition instanceof NewDefinition)) { 58 if (widgetDefinitionsById.containsKey(id)) { 59 Location duplicateLocation = widgetDefinition.getLocation(); 60 Location containerLocation = containerDefinition.getLocation(); 61 Location firstLocation = getWidgetDefinition(id).getLocation(); 62 throw new DuplicateIdException( 63 "Duplicate widget id \"" + id + "\" detected at " + duplicateLocation + ".\n" + 64 "Container widget \"" + containerDefinition.getId() + "\" at " + containerLocation + "\n" + 65 "already contains a widget with id \"" + id + "\" at " + firstLocation + "."); 66 } 67 widgetDefinitionsById.put(widgetDefinition.getId(), widgetDefinition); 68 } 69 this.definitionsIt.add(widgetDefinition); 70 } 71 72 public List getWidgetDefinitions() { 73 return widgetDefinitions; 74 } 75 76 public boolean hasWidget(String id) { 77 return widgetDefinitionsById.containsKey(id); 78 } 79 80 public WidgetDefinition getWidgetDefinition(String id) { 81 return (WidgetDefinition)widgetDefinitionsById.get(id); 82 } 83 84 public void resolve(List parents, WidgetDefinition parent) throws Exception { 85 if (!wasHere) { 86 wasHere = true; 87 this.definitionsIt = widgetDefinitions.listIterator(); 88 parents.add(containerDefinition); 89 while (this.definitionsIt.hasNext()) { 90 WidgetDefinition widgetDefinition = (WidgetDefinition)this.definitionsIt.next(); 91 if (!(widgetDefinition instanceof ClassDefinition)) { 93 if (widgetDefinition instanceof NewDefinition) { 94 this.definitionsIt.remove(); 96 ((NewDefinition)widgetDefinition).resolve(parents, containerDefinition); 97 } else { 98 if (widgetDefinition instanceof ContainerDefinition) 99 ((ContainerDefinition)widgetDefinition).resolve(parents, containerDefinition); 100 } 101 } 102 } 103 parents.remove(parents.size()-1); 104 wasHere = false; 105 } else { 106 ListIterator parentsIt = parents.listIterator(parents.size()); 109 while(parentsIt.hasPrevious()) { 110 WidgetDefinition widgetDefinition = (WidgetDefinition)parentsIt.previous(); 111 if (widgetDefinition instanceof UnionDefinition) break; 112 if (widgetDefinition instanceof RepeaterDefinition) break; 113 if (widgetDefinition == containerDefinition) { 114 Location location = containerDefinition.getLocation(); 115 if (parent instanceof FormDefinition) { 116 throw new Exception ("Container: Non-terminating recursion detected in form definition (" + location + ")"); 117 } 118 throw new Exception ("Container: Non-terminating recursion detected in widget definition: " 119 + parent.getId() + " (" + location + ")"); 120 } 121 } 122 } 123 } 124 125 public void createWidget(Widget parent, String id) { 126 WidgetDefinition widgetDefinition = (WidgetDefinition)widgetDefinitionsById.get(id); 127 if (widgetDefinition == null) { 128 throw new RuntimeException (containerDefinition.getId() + ": WidgetDefinition \"" + id + 129 "\" does not exist (" + containerDefinition.getLocation() + ")"); 130 } 131 Widget widget = widgetDefinition.createInstance(); 132 if (widget != null) 133 ((ContainerWidget)parent).addChild(widget); 134 } 135 136 public void createWidgets(Widget parent) { 137 Iterator definitionsIt = widgetDefinitions.iterator(); 138 while (definitionsIt.hasNext()) { 139 WidgetDefinition widgetDefinition = (WidgetDefinition)definitionsIt.next(); 140 Widget widget = widgetDefinition.createInstance(); 141 if (widget != null) 142 ((ContainerWidget)parent).addChild(widget); 143 } 144 } 145 146 public void checkCompleteness() throws IncompletenessException { 147 if (!wasHere) { 148 wasHere = true; 149 154 Iterator it = widgetDefinitions.iterator(); 156 while(it.hasNext()) { 157 ((WidgetDefinition)it.next()).checkCompleteness(); 158 } 159 wasHere = false; 160 } 161 } 162 } 163 | Popular Tags |