KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > formmodel > Output


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.datatype.Datatype;
21 import org.apache.cocoon.xml.AttributesImpl;
22 import org.xml.sax.ContentHandler JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24
25 import java.util.Locale JavaDoc;
26
27 /**
28  * An Output widget can be used to show a non-editable value to the user.
29  * An Output widget is associated with a certain
30  * {@link org.apache.cocoon.woody.datatype.Datatype Datatype}.
31  *
32  * <p>An Output widget is always valid and never required.
33  *
34  * @version $Id: Output.java 30932 2004-07-29 17:35:38Z vgritsenko $
35  */

36 public class Output extends AbstractWidget implements DataWidget {
37     private OutputDefinition definition;
38     private Object JavaDoc 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 JavaDoc getId() {
54         return definition.getId();
55     }
56
57     public void readFromRequest(FormContext formContext) {
58         // do nothing
59
}
60
61     public boolean validate(FormContext formContext) {
62         return true;
63     }
64
65     private static final String JavaDoc OUTPUT_EL = "output";
66     private static final String JavaDoc VALUE_EL = "value";
67
68     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
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         // the value
74
if (value != null) {
75             contentHandler.startElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL, Constants.EMPTY_ATTRS);
76             String JavaDoc 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         // generate label, help, hint, etc.
83
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 JavaDoc contentHandler) throws SAXException JavaDoc {
89         definition.generateLabel(contentHandler);
90     }
91
92     public Object JavaDoc getValue() {
93         return value;
94     }
95
96     public void setValue(Object JavaDoc object) {
97         if (object != null && !definition.getDatatype().getTypeClass().isAssignableFrom(object.getClass())) {
98             throw new RuntimeException JavaDoc("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