1 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.datatype.Datatype; 21 import org.apache.cocoon.xml.AttributesImpl; 22 import org.xml.sax.ContentHandler ; 23 import org.xml.sax.SAXException ; 24 25 import java.util.Locale ; 26 27 36 public class Output extends AbstractWidget implements DataWidget { 37 private OutputDefinition definition; 38 private Object value; 39 40 public OutputDefinition getOutputDefinition() { 41 return definition; 42 } 43 44 public Datatype getDatatype() { 45 return definition.getDatatype(); 46 } 47 48 protected Output(OutputDefinition definition) { 49 this.definition = definition; 50 setLocation(definition.getLocation()); 51 } 52 53 public String getId() { 54 return definition.getId(); 55 } 56 57 public void readFromRequest(FormContext formContext) { 58 } 60 61 public boolean validate(FormContext formContext) { 62 return true; 63 } 64 65 private static final String OUTPUT_EL = "output"; 66 private static final String VALUE_EL = "value"; 67 68 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 69 AttributesImpl outputAttrs = new AttributesImpl(); 70 outputAttrs.addCDATAAttribute("id", getFullyQualifiedId()); 71 contentHandler.startElement(Constants.WI_NS, OUTPUT_EL, Constants.WI_PREFIX_COLON + OUTPUT_EL, outputAttrs); 72 73 if (value != null) { 75 contentHandler.startElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL, Constants.EMPTY_ATTRS); 76 String stringValue; 77 stringValue = definition.getDatatype().convertToString(value, locale); 78 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length()); 79 contentHandler.endElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL); 80 } 81 82 definition.generateDisplayData(contentHandler); 84 85 contentHandler.endElement(Constants.WI_NS, OUTPUT_EL, Constants.WI_PREFIX_COLON + OUTPUT_EL); 86 } 87 88 public void generateLabel(ContentHandler contentHandler) throws SAXException { 89 definition.generateLabel(contentHandler); 90 } 91 92 public Object getValue() { 93 return value; 94 } 95 96 public void setValue(Object object) { 97 if (object != null && !definition.getDatatype().getTypeClass().isAssignableFrom(object.getClass())) { 98 throw new RuntimeException ("Tried to set value of output widget \"" 99 + getFullyQualifiedId() 100 + "\" with an object of an incorrect type: " 101 + "expected " + definition.getDatatype().getTypeClass() 102 + ", received " + object.getClass() + "."); 103 } 104 value = object; 105 } 106 } 107 | Popular Tags |