KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > formmodel > ContainerDefinitionDelegate


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.woody.formmodel;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ListIterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 // TODO: Refine and i18n the exception messages.
26
/**
27  * Helper class for the Definition implementation of widgets containing
28  * other widgets.
29  *
30  * @version $Id: ContainerDefinitionDelegate.java 30932 2004-07-29 17:35:38Z vgritsenko $
31  */

32 public class ContainerDefinitionDelegate {
33     private List JavaDoc widgetDefinitions = new ArrayList JavaDoc();
34     private Map JavaDoc widgetDefinitionsById = new HashMap JavaDoc();
35     private WidgetDefinition definition;
36     private boolean resolving;
37     private ListIterator JavaDoc definitionsIt = widgetDefinitions.listIterator();
38
39     /**
40      * @param definition the widget definition to which this container delegate belongs
41      */

42     public ContainerDefinitionDelegate(WidgetDefinition definition) {
43         this.definition = definition;
44         resolving = false;
45     }
46
47     public void addWidgetDefinition(WidgetDefinition widgetDefinition) throws DuplicateIdException {
48         String JavaDoc id = widgetDefinition.getId();
49         // Do not add NewDefinition id's hash.
50
if (!(widgetDefinition instanceof NewDefinition)) {
51             if (widgetDefinitionsById.containsKey(id)) {
52                 String JavaDoc duplicateLocation = widgetDefinition.getLocation();
53                 String JavaDoc containerLocation = definition.getLocation();
54                 String JavaDoc 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 JavaDoc getWidgetDefinitions() {
66         return widgetDefinitions;
67     }
68
69     public boolean hasWidget(String JavaDoc id) {
70         return widgetDefinitionsById.containsKey(id);
71     }
72
73     public WidgetDefinition getWidgetDefinition(String JavaDoc id) {
74         return (WidgetDefinition)widgetDefinitionsById.get(id);
75     }
76
77     public boolean isResolving() {
78         return resolving;
79     }
80
81     public void resolve(List JavaDoc parents, WidgetDefinition parent) throws Exception JavaDoc {
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                 // ClassDefinition's get resolved by NewDefinition rather than here.
89
if (!(widgetDefinition instanceof ClassDefinition)) {
90                     if (widgetDefinition instanceof NewDefinition) {
91                         // Remove NewDefinition in preparation for its referenced class of widget definitions to be added.
92
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             // Non-terminating recursion detection
104
if (resolving == true) {
105                // Search up parent list in hopes of finding a "Union" before finding previous "New" for this "Class".
106
ListIterator JavaDoc 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 JavaDoc location = definition.getLocation();
112                         if (parent instanceof FormDefinition) {
113                             throw new Exception JavaDoc("Container: Non-terminating recursion detected in form definition (" + location + ")");
114                         } else {
115                             throw new Exception JavaDoc("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 JavaDoc id) {
125         WidgetDefinition widgetDefinition = (WidgetDefinition)widgetDefinitionsById.get(id);
126         if (widgetDefinition == null) {
127             throw new RuntimeException JavaDoc(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 JavaDoc 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