KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.cocoon.woody.Constants;
24 import org.apache.cocoon.woody.FormContext;
25 import org.apache.cocoon.woody.validation.WidgetValidator;
26 import org.apache.excalibur.xml.sax.XMLizable;
27 import org.xml.sax.ContentHandler JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * Provides functionality that is common across many WidgetDefinition implementations.
32  *
33  * @version $Id: AbstractWidgetDefinition.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public abstract class AbstractWidgetDefinition implements WidgetDefinition {
36     private FormDefinition formDefinition;
37     protected WidgetDefinition parent;
38     private String JavaDoc location = null;
39     private String JavaDoc id;
40     private Map JavaDoc displayData;
41     private List JavaDoc validators;
42
43     public FormDefinition getFormDefinition() {
44         if (this.formDefinition == null) {
45             if (this instanceof FormDefinition) {
46                 this.formDefinition = (FormDefinition)this;
47             } else {
48                 this.formDefinition = this.parent.getFormDefinition();
49             }
50         }
51         return this.formDefinition;
52     }
53
54     /**
55      * Sets the parent of this definition
56      */

57     public void setParent(WidgetDefinition definition) {
58         this.parent = definition;
59     }
60     
61     /**
62      * Gets the parent of this definition.
63      * This method returns null for the root definition.
64      */

65     public WidgetDefinition getParent() {
66         return this.parent;
67     }
68
69     protected void setLocation(String JavaDoc location) {
70         this.location = location;
71     }
72
73     public String JavaDoc getLocation() {
74         return location;
75     }
76
77     public String JavaDoc getId() {
78         return id;
79     }
80
81     protected void setId(String JavaDoc id) {
82         this.id = id;
83     }
84
85     public void generateLabel(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
86         generateDisplayData("label", contentHandler);
87     }
88
89     /**
90      * Sets the various display data for this widget. This includes the label, hint and help.
91      * They must all be objects implementing the XMLizable interface. This approach
92      * allows to have mixed content in these data.
93      *
94      * @param displayData an association of {name, sax fragment}
95      */

96     public void setDisplayData(Map JavaDoc displayData) {
97         this.displayData = displayData;
98     }
99     
100     public void addValidator(WidgetValidator validator) {
101         if (this.validators == null) {
102             this.validators = new ArrayList JavaDoc();
103         }
104         
105         this.validators.add(validator);
106     }
107     
108     public void generateDisplayData(String JavaDoc name, ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
109         Object JavaDoc data = this.displayData.get(name);
110         if (data != null) {
111             ((XMLizable)data).toSAX(contentHandler);
112         } else if (!this.displayData.containsKey(name)) {
113             throw new IllegalArgumentException JavaDoc("Unknown display data name '" + name + "'");
114         }
115     }
116     
117     public void generateDisplayData(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
118         // Output all non-null display data
119
Iterator JavaDoc iter = this.displayData.entrySet().iterator();
120         while (iter.hasNext()) {
121             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
122             if (entry.getValue() != null) {
123                 String JavaDoc name = (String JavaDoc)entry.getKey();
124                 
125                 // Enclose the data into a "wi:{name}" element
126
contentHandler.startElement(Constants.WI_NS, name, Constants.WI_PREFIX_COLON + name, Constants.EMPTY_ATTRS);
127
128                 ((XMLizable)entry.getValue()).toSAX(contentHandler);
129
130                 contentHandler.endElement(Constants.WI_NS, name, Constants.WI_PREFIX_COLON + name);
131             }
132         }
133     }
134     
135     /**
136      * Validate a widget using the validators that were defined in its definition. If validation
137      * fails, the validator has set a validation error on the widget or one of its children.
138      *
139      * @param widget the widget
140      * @param context the form context
141      * @return <code>true</code> if validation was successful.
142      */

143     public boolean validate(Widget widget, FormContext context) {
144         if (this.validators == null) {
145             // No validators
146
return true;
147             
148         } else {
149             Iterator JavaDoc iter = this.validators.iterator();
150             while(iter.hasNext()) {
151                 WidgetValidator validator = (WidgetValidator)iter.next();
152                 if (! validator.validate(widget, context)) {
153                     // Stop at the first validator that fails
154
return false;
155                 }
156             }
157             // All validators were sucessful
158
return true;
159         }
160     }
161 }
162
Popular Tags