001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.woody.formmodel;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.cocoon.woody.Constants;
025: import org.apache.cocoon.woody.FormContext;
026: import org.apache.cocoon.woody.validation.WidgetValidator;
027: import org.apache.excalibur.xml.sax.XMLizable;
028: import org.xml.sax.ContentHandler;
029: import org.xml.sax.SAXException;
030:
031: /**
032: * Provides functionality that is common across many WidgetDefinition implementations.
033: *
034: * @version $Id: AbstractWidgetDefinition.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public abstract class AbstractWidgetDefinition implements
037: WidgetDefinition {
038: private FormDefinition formDefinition;
039: protected WidgetDefinition parent;
040: private String location = null;
041: private String id;
042: private Map displayData;
043: private List validators;
044:
045: public FormDefinition getFormDefinition() {
046: if (this .formDefinition == null) {
047: if (this instanceof FormDefinition) {
048: this .formDefinition = (FormDefinition) this ;
049: } else {
050: this .formDefinition = this .parent.getFormDefinition();
051: }
052: }
053: return this .formDefinition;
054: }
055:
056: /**
057: * Sets the parent of this definition
058: */
059: public void setParent(WidgetDefinition definition) {
060: this .parent = definition;
061: }
062:
063: /**
064: * Gets the parent of this definition.
065: * This method returns null for the root definition.
066: */
067: public WidgetDefinition getParent() {
068: return this .parent;
069: }
070:
071: protected void setLocation(String location) {
072: this .location = location;
073: }
074:
075: public String getLocation() {
076: return location;
077: }
078:
079: public String getId() {
080: return id;
081: }
082:
083: protected void setId(String id) {
084: this .id = id;
085: }
086:
087: public void generateLabel(ContentHandler contentHandler)
088: throws SAXException {
089: generateDisplayData("label", contentHandler);
090: }
091:
092: /**
093: * Sets the various display data for this widget. This includes the label, hint and help.
094: * They must all be objects implementing the XMLizable interface. This approach
095: * allows to have mixed content in these data.
096: *
097: * @param displayData an association of {name, sax fragment}
098: */
099: public void setDisplayData(Map displayData) {
100: this .displayData = displayData;
101: }
102:
103: public void addValidator(WidgetValidator validator) {
104: if (this .validators == null) {
105: this .validators = new ArrayList();
106: }
107:
108: this .validators.add(validator);
109: }
110:
111: public void generateDisplayData(String name,
112: ContentHandler contentHandler) throws SAXException {
113: Object data = this .displayData.get(name);
114: if (data != null) {
115: ((XMLizable) data).toSAX(contentHandler);
116: } else if (!this .displayData.containsKey(name)) {
117: throw new IllegalArgumentException(
118: "Unknown display data name '" + name + "'");
119: }
120: }
121:
122: public void generateDisplayData(ContentHandler contentHandler)
123: throws SAXException {
124: // Output all non-null display data
125: Iterator iter = this .displayData.entrySet().iterator();
126: while (iter.hasNext()) {
127: Map.Entry entry = (Map.Entry) iter.next();
128: if (entry.getValue() != null) {
129: String name = (String) entry.getKey();
130:
131: // Enclose the data into a "wi:{name}" element
132: contentHandler.startElement(Constants.WI_NS, name,
133: Constants.WI_PREFIX_COLON + name,
134: Constants.EMPTY_ATTRS);
135:
136: ((XMLizable) entry.getValue()).toSAX(contentHandler);
137:
138: contentHandler.endElement(Constants.WI_NS, name,
139: Constants.WI_PREFIX_COLON + name);
140: }
141: }
142: }
143:
144: /**
145: * Validate a widget using the validators that were defined in its definition. If validation
146: * fails, the validator has set a validation error on the widget or one of its children.
147: *
148: * @param widget the widget
149: * @param context the form context
150: * @return <code>true</code> if validation was successful.
151: */
152: public boolean validate(Widget widget, FormContext context) {
153: if (this .validators == null) {
154: // No validators
155: return true;
156:
157: } else {
158: Iterator iter = this .validators.iterator();
159: while (iter.hasNext()) {
160: WidgetValidator validator = (WidgetValidator) iter
161: .next();
162: if (!validator.validate(widget, context)) {
163: // Stop at the first validator that fails
164: return false;
165: }
166: }
167: // All validators were sucessful
168: return true;
169: }
170: }
171: }
|