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.Locale;
023:
024: import org.apache.cocoon.woody.Constants;
025: import org.apache.cocoon.woody.FormContext;
026: import org.apache.cocoon.woody.event.WidgetEvent;
027: import org.apache.cocoon.woody.validation.WidgetValidator;
028: import org.apache.cocoon.xml.AttributesImpl;
029: import org.xml.sax.ContentHandler;
030: import org.xml.sax.SAXException;
031:
032: /**
033: * Abstract base class for Widget implementations. Provides functionality
034: * common to many widgets.
035: *
036: * @version $Id: AbstractWidget.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public abstract class AbstractWidget implements Widget {
039: private String location;
040: private Widget parent;
041: private Form form;
042: protected AbstractWidgetDefinition definition;
043:
044: private List validators;
045:
046: /**
047: * Sets the definition of this widget.
048: */
049: protected void setDefinition(AbstractWidgetDefinition definition) {
050: this .definition = definition;
051: }
052:
053: /**
054: * Gets the id of this widget.
055: */
056: public String getId() {
057: return definition.getId();
058: }
059:
060: /**
061: * Sets the source location of this widget.
062: */
063: protected void setLocation(String location) {
064: this .location = location;
065: }
066:
067: /**
068: * Gets the source location of this widget.
069: */
070: public String getLocation() {
071: return this .location;
072: }
073:
074: public Widget getParent() {
075: return parent;
076: }
077:
078: public void setParent(Widget widget) {
079: this .parent = widget;
080: }
081:
082: public Form getForm() {
083: if (this .form == null) {
084: if (parent == null) {
085: this .form = (Form) this ;
086: } else {
087: this .form = parent.getForm();
088: }
089: }
090: return this .form;
091: }
092:
093: public String getNamespace() {
094: if (getParent() != null
095: && getParent().getNamespace().length() > 0) {
096: return getParent().getNamespace() + "." + getId();
097: } else {
098: return getId();
099: }
100: }
101:
102: public String getFullyQualifiedId() {
103: if (parent != null) {
104: String namespace = parent.getNamespace();
105: if (namespace.length() > 0) {
106: return namespace + "." + getId();
107: }
108: }
109: return getId();
110: }
111:
112: public Object getValue() {
113: return null;
114: }
115:
116: public void setValue(Object object) {
117: throw new RuntimeException("Cannot set the value of widget "
118: + getFullyQualifiedId());
119: }
120:
121: public boolean isRequired() {
122: return false;
123: }
124:
125: public Widget getWidget(String id) {
126: return null;
127: }
128:
129: public void broadcastEvent(WidgetEvent event) {
130: throw new UnsupportedOperationException("Widget "
131: + this .getFullyQualifiedId()
132: + " doesn't handle events.");
133: }
134:
135: /**
136: * Add a validator to this widget instance.
137: *
138: * @param validator
139: */
140: public void addValidator(WidgetValidator validator) {
141: if (this .validators == null) {
142: this .validators = new ArrayList();
143: }
144:
145: this .validators.add(validator);
146: }
147:
148: /**
149: * Remove a validator from this widget instance
150: *
151: * @param validator
152: * @return <code>true</code> if the validator was found.
153: */
154: public boolean removeValidator(WidgetValidator validator) {
155: return (this .validators == null) ? false : this .validators
156: .remove(validator);
157: }
158:
159: public boolean validate(FormContext context) {
160: // Test validators from the widget definition
161: if (!this .definition.validate(this , context)) {
162: // Failed
163: return false;
164: } else {
165: // Definition sussessful, test local validators
166: if (this .validators == null) {
167: // No local validators
168: return true;
169: } else {
170: // Iterate on local validators
171: Iterator iter = this .validators.iterator();
172: while (iter.hasNext()) {
173: WidgetValidator validator = (WidgetValidator) iter
174: .next();
175: if (!validator.validate(this , context)) {
176: return false;
177: }
178: }
179: // All local iterators successful
180: return true;
181: }
182: }
183: }
184:
185: public void generateLabel(ContentHandler contentHandler)
186: throws SAXException {
187: if (definition != null) {
188: definition.generateDisplayData("label", contentHandler);
189: }
190: }
191:
192: public void generateItemSaxFragment(ContentHandler contentHandler,
193: Locale locale) throws SAXException {
194: // Do nothing
195: }
196:
197: public void generateSaxFragment(ContentHandler contentHandler,
198: Locale locale, String element, WidgetDefinition definition)
199: throws SAXException {
200: AttributesImpl attrs = new AttributesImpl();
201: attrs.addCDATAAttribute("id", getFullyQualifiedId());
202: contentHandler.startElement(Constants.WI_NS, element,
203: Constants.WI_PREFIX_COLON + element, attrs);
204: generateItemSaxFragment(contentHandler, locale);
205: contentHandler.endElement(Constants.WI_NS, element,
206: Constants.WI_PREFIX_COLON + element);
207: }
208: }
|