1 16 package org.apache.cocoon.woody.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 32 public class ContainerDefinitionDelegate { 33 private List widgetDefinitions = new ArrayList (); 34 private Map widgetDefinitionsById = new HashMap (); 35 private WidgetDefinition definition; 36 private boolean resolving; 37 private ListIterator definitionsIt = widgetDefinitions.listIterator(); 38 39 42 public ContainerDefinitionDelegate(WidgetDefinition definition) { 43 this.definition = definition; 44 resolving = false; 45 } 46 47 public void addWidgetDefinition(WidgetDefinition widgetDefinition) throws DuplicateIdException { 48 String id = widgetDefinition.getId(); 49 if (!(widgetDefinition instanceof NewDefinition)) { 51 if (widgetDefinitionsById.containsKey(id)) { 52 String duplicateLocation = widgetDefinition.getLocation(); 53 String containerLocation = definition.getLocation(); 54 String firstLocation = getWidgetDefinition(id).getLocation(); 55 throw new DuplicateIdException( 56 "Duplicate widget id \"" + id + "\" detected at " + duplicateLocation + ".\n" + 57 "Container widget \"" + definition.getId() + "\" at " + containerLocation + "\n" + 58 "already contains a widget with id \"" + id + "\" at " + firstLocation + "."); 59 } 60 widgetDefinitionsById.put(widgetDefinition.getId(), widgetDefinition); 61 } 62 this.definitionsIt.add(widgetDefinition); 63 } 64 65 public List getWidgetDefinitions() { 66 return widgetDefinitions; 67 } 68 69 public boolean hasWidget(String id) { 70 return widgetDefinitionsById.containsKey(id); 71 } 72 73 public WidgetDefinition getWidgetDefinition(String id) { 74 return (WidgetDefinition)widgetDefinitionsById.get(id); 75 } 76 77 public boolean isResolving() { 78 return resolving; 79 } 80 81 public void resolve(List parents, WidgetDefinition parent) throws Exception { 82 if (!resolving) { 83 resolving = true; 84 this.definitionsIt = widgetDefinitions.listIterator(); 85 parents.add(definition); 86 while (this.definitionsIt.hasNext()) { 87 WidgetDefinition widgetDefinition = (WidgetDefinition)this.definitionsIt.next(); 88 if (!(widgetDefinition instanceof ClassDefinition)) { 90 if (widgetDefinition instanceof NewDefinition) { 91 this.definitionsIt.remove(); 93 ((NewDefinition)widgetDefinition).resolve(parents, definition); 94 } else { 95 if (widgetDefinition instanceof ContainerDefinition) 96 ((ContainerDefinition)widgetDefinition).resolve(parents, definition); 97 } 98 } 99 } 100 parents.remove(parents.size()-1); 101 resolving = false; 102 } else { 103 if (resolving == true) { 105 ListIterator parentsIt = parents.listIterator(parents.size()); 107 while(parentsIt.hasPrevious()) { 108 WidgetDefinition widgetDefinition = (WidgetDefinition)parentsIt.previous(); 109 if (widgetDefinition instanceof UnionDefinition) break; 110 if (widgetDefinition == definition) { 111 String location = definition.getLocation(); 112 if (parent instanceof FormDefinition) { 113 throw new Exception ("Container: Non-terminating recursion detected in form definition (" + location + ")"); 114 } else { 115 throw new Exception ("Container: Non-terminating recursion detected in widget definition: " 116 + parent.getId() + " (" + location + ")"); 117 } 118 } 119 } 120 } 121 } 122 } 123 124 public void createWidget(Widget parent, String id) { 125 WidgetDefinition widgetDefinition = (WidgetDefinition)widgetDefinitionsById.get(id); 126 if (widgetDefinition == null) { 127 throw new RuntimeException (definition.getId() + ": WidgetDefinition \"" + id + 128 "\" does not exist (" + definition.getLocation() + ")"); 129 } 130 Widget widget = widgetDefinition.createInstance(); 131 if (widget != null) 132 ((ContainerWidget)parent).addWidget(widget); 133 } 134 135 public void createWidgets(Widget parent) { 136 Iterator definitionsIt = widgetDefinitions.iterator(); 137 while (definitionsIt.hasNext()) { 138 WidgetDefinition widgetDefinition = (WidgetDefinition)definitionsIt.next(); 139 Widget widget = widgetDefinition.createInstance(); 140 if (widget != null) 141 ((ContainerWidget)parent).addWidget(widget); 142 } 143 } 144 } 145 | Popular Tags |