1 16 package org.apache.cocoon.woody.formmodel; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.cocoon.woody.Constants; 24 import org.apache.cocoon.woody.FormContext; 25 import org.apache.cocoon.woody.validation.WidgetValidator; 26 import org.apache.excalibur.xml.sax.XMLizable; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.SAXException ; 29 30 35 public abstract class AbstractWidgetDefinition implements WidgetDefinition { 36 private FormDefinition formDefinition; 37 protected WidgetDefinition parent; 38 private String location = null; 39 private String id; 40 private Map displayData; 41 private List validators; 42 43 public FormDefinition getFormDefinition() { 44 if (this.formDefinition == null) { 45 if (this instanceof FormDefinition) { 46 this.formDefinition = (FormDefinition)this; 47 } else { 48 this.formDefinition = this.parent.getFormDefinition(); 49 } 50 } 51 return this.formDefinition; 52 } 53 54 57 public void setParent(WidgetDefinition definition) { 58 this.parent = definition; 59 } 60 61 65 public WidgetDefinition getParent() { 66 return this.parent; 67 } 68 69 protected void setLocation(String location) { 70 this.location = location; 71 } 72 73 public String getLocation() { 74 return location; 75 } 76 77 public String getId() { 78 return id; 79 } 80 81 protected void setId(String id) { 82 this.id = id; 83 } 84 85 public void generateLabel(ContentHandler contentHandler) throws SAXException { 86 generateDisplayData("label", contentHandler); 87 } 88 89 96 public void setDisplayData(Map displayData) { 97 this.displayData = displayData; 98 } 99 100 public void addValidator(WidgetValidator validator) { 101 if (this.validators == null) { 102 this.validators = new ArrayList (); 103 } 104 105 this.validators.add(validator); 106 } 107 108 public void generateDisplayData(String name, ContentHandler contentHandler) throws SAXException { 109 Object data = this.displayData.get(name); 110 if (data != null) { 111 ((XMLizable)data).toSAX(contentHandler); 112 } else if (!this.displayData.containsKey(name)) { 113 throw new IllegalArgumentException ("Unknown display data name '" + name + "'"); 114 } 115 } 116 117 public void generateDisplayData(ContentHandler contentHandler) throws SAXException { 118 Iterator iter = this.displayData.entrySet().iterator(); 120 while (iter.hasNext()) { 121 Map.Entry entry = (Map.Entry )iter.next(); 122 if (entry.getValue() != null) { 123 String name = (String )entry.getKey(); 124 125 contentHandler.startElement(Constants.WI_NS, name, Constants.WI_PREFIX_COLON + name, Constants.EMPTY_ATTRS); 127 128 ((XMLizable)entry.getValue()).toSAX(contentHandler); 129 130 contentHandler.endElement(Constants.WI_NS, name, Constants.WI_PREFIX_COLON + name); 131 } 132 } 133 } 134 135 143 public boolean validate(Widget widget, FormContext context) { 144 if (this.validators == null) { 145 return true; 147 148 } else { 149 Iterator iter = this.validators.iterator(); 150 while(iter.hasNext()) { 151 WidgetValidator validator = (WidgetValidator)iter.next(); 152 if (! validator.validate(widget, context)) { 153 return false; 155 } 156 } 157 return true; 159 } 160 } 161 } 162 | Popular Tags |