001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.woody.formmodel;
018:
019: import org.apache.cocoon.woody.FormContext;
020: import org.apache.cocoon.woody.Constants;
021: import org.apache.cocoon.woody.datatype.Datatype;
022: import org.apache.cocoon.xml.AttributesImpl;
023: import org.xml.sax.ContentHandler;
024: import org.xml.sax.SAXException;
025:
026: import java.util.Locale;
027:
028: /**
029: * An Output widget can be used to show a non-editable value to the user.
030: * An Output widget is associated with a certain
031: * {@link org.apache.cocoon.woody.datatype.Datatype Datatype}.
032: *
033: * <p>An Output widget is always valid and never required.
034: *
035: * @version $Id: Output.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037: public class Output extends AbstractWidget implements DataWidget {
038: private OutputDefinition definition;
039: private Object value;
040:
041: public OutputDefinition getOutputDefinition() {
042: return definition;
043: }
044:
045: public Datatype getDatatype() {
046: return definition.getDatatype();
047: }
048:
049: protected Output(OutputDefinition definition) {
050: this .definition = definition;
051: setLocation(definition.getLocation());
052: }
053:
054: public String getId() {
055: return definition.getId();
056: }
057:
058: public void readFromRequest(FormContext formContext) {
059: // do nothing
060: }
061:
062: public boolean validate(FormContext formContext) {
063: return true;
064: }
065:
066: private static final String OUTPUT_EL = "output";
067: private static final String VALUE_EL = "value";
068:
069: public void generateSaxFragment(ContentHandler contentHandler,
070: Locale locale) throws SAXException {
071: AttributesImpl outputAttrs = new AttributesImpl();
072: outputAttrs.addCDATAAttribute("id", getFullyQualifiedId());
073: contentHandler.startElement(Constants.WI_NS, OUTPUT_EL,
074: Constants.WI_PREFIX_COLON + OUTPUT_EL, outputAttrs);
075:
076: // the value
077: if (value != null) {
078: contentHandler.startElement(Constants.WI_NS, VALUE_EL,
079: Constants.WI_PREFIX_COLON + VALUE_EL,
080: Constants.EMPTY_ATTRS);
081: String stringValue;
082: stringValue = definition.getDatatype().convertToString(
083: value, locale);
084: contentHandler.characters(stringValue.toCharArray(), 0,
085: stringValue.length());
086: contentHandler.endElement(Constants.WI_NS, VALUE_EL,
087: Constants.WI_PREFIX_COLON + VALUE_EL);
088: }
089:
090: // generate label, help, hint, etc.
091: definition.generateDisplayData(contentHandler);
092:
093: contentHandler.endElement(Constants.WI_NS, OUTPUT_EL,
094: Constants.WI_PREFIX_COLON + OUTPUT_EL);
095: }
096:
097: public void generateLabel(ContentHandler contentHandler)
098: throws SAXException {
099: definition.generateLabel(contentHandler);
100: }
101:
102: public Object getValue() {
103: return value;
104: }
105:
106: public void setValue(Object object) {
107: if (object != null
108: && !definition.getDatatype().getTypeClass()
109: .isAssignableFrom(object.getClass())) {
110: throw new RuntimeException(
111: "Tried to set value of output widget \""
112: + getFullyQualifiedId()
113: + "\" with an object of an incorrect type: "
114: + "expected "
115: + definition.getDatatype().getTypeClass()
116: + ", received " + object.getClass() + ".");
117: }
118: value = object;
119: }
120: }
|