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.forms.formmodel;
018:
019: import org.apache.cocoon.forms.FormsConstants;
020: import org.apache.cocoon.forms.FormContext;
021: import org.apache.cocoon.forms.util.StringMessage;
022: import org.apache.cocoon.xml.XMLUtils;
023: import org.apache.excalibur.xml.sax.XMLizable;
024: import org.xml.sax.ContentHandler;
025: import org.xml.sax.SAXException;
026:
027: import java.util.Locale;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030:
031: /**
032: * A widget to output one or messages. This widget doesn't respond to input from the user, except
033: * that on each form submit the messages are cleared.
034: *
035: * <p>This widget is typically used to communicate extra validation errors or other messages
036: * to the user, that aren't associated with any other widget in particular.
037: *
038: * @version $Id: Messages.java 462520 2006-10-10 19:39:14Z vgritsenko $
039: */
040: public class Messages extends AbstractWidget {
041:
042: private static final String MESSAGES_EL = "messages";
043: private static final String MESSAGE_EL = "message";
044:
045: private final MessagesDefinition definition;
046: private ArrayList messages;
047:
048: protected Messages(MessagesDefinition definition) {
049: super (definition);
050: this .definition = definition;
051: this .messages = new ArrayList();
052: }
053:
054: public WidgetDefinition getDefinition() {
055: return this .definition;
056: }
057:
058: public void readFromRequest(FormContext formContext) {
059: if (getCombinedState().isAcceptingInputs()) {
060: messages.clear();
061: }
062: }
063:
064: public boolean validate() {
065: if (!getCombinedState().isValidatingValues()) {
066: this .wasValid = true;
067: return true;
068: }
069: this .wasValid = messages.size() == 0;
070: return this .wasValid;
071: }
072:
073: /**
074: * Adds a string message.
075: */
076: public void addMessage(String message) {
077: messages.add(new StringMessage(message));
078: getForm().addWidgetUpdate(this );
079: }
080:
081: /**
082: * Adds a message in the form an object that implements the XMLizable interface.
083: * This allows to add messages that produce mixed content. The XMLizable should
084: * only generate a SAX fragment, i.e. without start/endDocument calls.
085: *
086: * <p>A useful implementation is {@link org.apache.cocoon.forms.util.I18nMessage I18nMesage}.
087: */
088: public void addMessage(XMLizable message) {
089: messages.add(message);
090: getForm().addWidgetUpdate(this );
091: }
092:
093: /**
094: * @return "messages"
095: */
096: public String getXMLElementName() {
097: return MESSAGES_EL;
098: }
099:
100: public void generateItemSaxFragment(ContentHandler contentHandler,
101: Locale locale) throws SAXException {
102: Iterator i = messages.iterator();
103: while (i.hasNext()) {
104: XMLizable message = (XMLizable) i.next();
105: contentHandler.startElement(FormsConstants.INSTANCE_NS,
106: MESSAGE_EL, FormsConstants.INSTANCE_PREFIX_COLON
107: + MESSAGE_EL, XMLUtils.EMPTY_ATTRIBUTES);
108: message.toSAX(contentHandler);
109: contentHandler.endElement(FormsConstants.INSTANCE_NS,
110: MESSAGE_EL, FormsConstants.INSTANCE_PREFIX_COLON
111: + MESSAGE_EL);
112: }
113: }
114:
115: }
|