KickJava   Java API By Example, From Geeks To Geeks.

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

38 public class Messages extends AbstractWidget {
39     private ArrayList JavaDoc messages = new ArrayList JavaDoc();
40     private MessagesDefinition definition;
41
42     private static final String JavaDoc MESSAGES_EL = "messages";
43     private static final String JavaDoc MESSAGE_EL = "message";
44
45     protected Messages(MessagesDefinition definition) {
46         this.definition = definition;
47         setLocation(definition.getLocation());
48     }
49
50     public String JavaDoc getId() {
51         return definition.getId();
52     }
53
54     public void readFromRequest(FormContext formContext) {
55         messages.clear();
56     }
57
58     public boolean validate(FormContext formContext) {
59         return messages.size() == 0;
60     }
61
62     /**
63      * Adds a string message.
64      */

65     public void addMessage(String JavaDoc message) {
66         messages.add(new StringMessage(message));
67     }
68
69     /**
70      * Adds a message in the form an object that implements the XMLizable interface.
71      * This allows to add messages that produce mixed content. The XMLizable should
72      * only generate a SAX fragment, i.e. without start/endDocument calls.
73      *
74      * <p>A useful implementation is {@link org.apache.cocoon.woody.util.I18nMessage I18nMesage}.
75      */

76     public void addMessage(XMLizable message) {
77         messages.add(message);
78     }
79
80     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
81         contentHandler.startElement(Constants.WI_NS, MESSAGES_EL, Constants.WI_PREFIX_COLON + MESSAGES_EL, Constants.EMPTY_ATTRS);
82
83         definition.generateDisplayData(contentHandler);
84
85         Iterator JavaDoc messagesIt = messages.iterator();
86         while (messagesIt.hasNext()) {
87             XMLizable message = (XMLizable)messagesIt.next();
88             contentHandler.startElement(Constants.WI_NS, MESSAGE_EL, Constants.WI_PREFIX_COLON + MESSAGE_EL, Constants.EMPTY_ATTRS);
89             message.toSAX(contentHandler);
90             contentHandler.endElement(Constants.WI_NS, MESSAGE_EL, Constants.WI_PREFIX_COLON + MESSAGE_EL);
91         }
92
93         contentHandler.endElement(Constants.WI_NS, MESSAGES_EL, Constants.WI_PREFIX_COLON + MESSAGES_EL);
94     }
95
96     public void generateLabel(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
97         definition.generateLabel(contentHandler);
98     }
99 }
100
Popular Tags