KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > Messages


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.forms.formmodel;
17
18 import org.apache.cocoon.forms.FormsConstants;
19 import org.apache.cocoon.forms.FormContext;
20 import org.apache.cocoon.forms.util.StringMessage;
21 import org.apache.cocoon.xml.XMLUtils;
22 import org.apache.excalibur.xml.sax.XMLizable;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 import java.util.Locale JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  * A widget to output one or messages. This widget doesn't respond to input from the user, except
32  * that on each form submit the messages are cleared.
33  *
34  * <p>This widget is typically used to communicate extra validation errors or other messages
35  * to the user, that aren't associated with any other widget in particular.
36  *
37  * @version $Id: Messages.java 326838 2005-10-20 06:26:53Z sylvain $
38  */

39 public class Messages extends AbstractWidget {
40     private ArrayList JavaDoc messages = new ArrayList JavaDoc();
41     private final MessagesDefinition definition;
42
43     private static final String JavaDoc MESSAGES_EL = "messages";
44     private static final String JavaDoc MESSAGE_EL = "message";
45
46     protected Messages(MessagesDefinition definition) {
47         super(definition);
48         this.definition = definition;
49     }
50
51     public WidgetDefinition getDefinition() {
52         return this.definition;
53     }
54
55     public void readFromRequest(FormContext formContext) {
56         if (getCombinedState().isAcceptingInputs()) {
57             messages.clear();
58         }
59     }
60
61     public boolean validate() {
62         if (!getCombinedState().isValidatingValues()) {
63             this.wasValid = true;
64             return true;
65         }
66         this.wasValid = messages.size() == 0;
67         return this.wasValid;
68     }
69
70     /**
71      * Adds a string message.
72      */

73     public void addMessage(String JavaDoc message) {
74         messages.add(new StringMessage(message));
75         getForm().addWidgetUpdate(this);
76     }
77
78     /**
79      * Adds a message in the form an object that implements the XMLizable interface.
80      * This allows to add messages that produce mixed content. The XMLizable should
81      * only generate a SAX fragment, i.e. without start/endDocument calls.
82      *
83      * <p>A useful implementation is {@link org.apache.cocoon.forms.util.I18nMessage I18nMesage}.
84      */

85     public void addMessage(XMLizable message) {
86         messages.add(message);
87         getForm().addWidgetUpdate(this);
88     }
89     
90     /**
91      * @return "messages"
92      */

93     public String JavaDoc getXMLElementName() {
94         return MESSAGES_EL;
95     }
96     
97     public void generateItemSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
98         Iterator JavaDoc messagesIt = messages.iterator();
99         while (messagesIt.hasNext()) {
100             XMLizable message = (XMLizable)messagesIt.next();
101             contentHandler.startElement(FormsConstants.INSTANCE_NS, MESSAGE_EL, FormsConstants.INSTANCE_PREFIX_COLON + MESSAGE_EL, XMLUtils.EMPTY_ATTRIBUTES);
102             message.toSAX(contentHandler);
103             contentHandler.endElement(FormsConstants.INSTANCE_NS, MESSAGE_EL, FormsConstants.INSTANCE_PREFIX_COLON + MESSAGE_EL);
104         }
105     }
106
107 }
108
Popular Tags