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