1 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 ; 24 import org.xml.sax.SAXException ; 25 26 import java.util.Locale ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 30 39 public class Messages extends AbstractWidget { 40 private ArrayList messages = new ArrayList (); 41 private final MessagesDefinition definition; 42 43 private static final String MESSAGES_EL = "messages"; 44 private static final String 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 73 public void addMessage(String message) { 74 messages.add(new StringMessage(message)); 75 getForm().addWidgetUpdate(this); 76 } 77 78 85 public void addMessage(XMLizable message) { 86 messages.add(message); 87 getForm().addWidgetUpdate(this); 88 } 89 90 93 public String getXMLElementName() { 94 return MESSAGES_EL; 95 } 96 97 public void generateItemSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 98 Iterator 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 |