001: /*
002: * Value.java
003: *
004: * Version: $Revision: 1.7 $
005: *
006: * Date: $Date: 2006/07/05 21:40:01 $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.app.xmlui.wing.element;
042:
043: import org.dspace.app.xmlui.wing.AttributeMap;
044: import org.dspace.app.xmlui.wing.WingContext;
045: import org.dspace.app.xmlui.wing.WingException;
046: import org.xml.sax.ContentHandler;
047: import org.xml.sax.SAXException;
048: import org.xml.sax.ext.LexicalHandler;
049: import org.xml.sax.helpers.NamespaceSupport;
050:
051: /**
052: * This class represents field values.
053: *
054: * @author Scott Phillips
055: */
056:
057: public class Value extends RichTextContainer {
058: /** The name of the value element */
059: public static final String E_VALUE = "value";
060:
061: /** The name of the value type attribute */
062: public static final String A_TYPE = "type";
063:
064: /** The name of the option value attribute */
065: public static final String A_OPTION = "option";
066:
067: /** The name of the checked attribute */
068: public static final String A_CHECKED = "checked";
069:
070: /** The possible value types */
071: public static final String TYPE_RAW = "raw";
072:
073: public static final String TYPE_INTERPRETED = "interpreted";
074:
075: public static final String TYPE_OPTION = "option";
076:
077: /** All the possible value types collected into one array. */
078: public static final String[] TYPES = { TYPE_RAW, TYPE_INTERPRETED,
079: TYPE_OPTION };
080:
081: /** The type of this value element */
082: private String type;
083:
084: /** The submited value for this option */
085: private String option;
086:
087: /** The checked attribute */
088: private boolean checked;
089:
090: /**
091: * Construct a new field value, when used in a multiple value context
092: *
093: * @param context
094: * (Required) The context this element is contained in
095: * @param type
096: * (may be null) Determine the value's type, raw, default or
097: * interpreted. If the value is null, then raw is used.
098: */
099: protected Value(WingContext context, String type)
100: throws WingException {
101: super (context);
102:
103: if (type == null) {
104: // if no type specified just default to raw.
105: type = TYPE_RAW;
106: }
107:
108: restrict(
109: type,
110: TYPES,
111: "The 'type' parameter must be one of these values: 'raw', 'interpreted', or 'option'");
112:
113: this .type = type;
114: }
115:
116: /**
117: * Construct a new field value, when used in a multiple value context
118: *
119: * @param context
120: * (Required) The context this element is contained in
121: * @param type
122: * (may be null) Determine the value's type, raw, default or
123: * interpreted. If the value is null, then raw is used.
124: * @param option
125: * (May be null) The option's return value that is selected.
126: */
127: protected Value(WingContext context, String type, String option)
128: throws WingException {
129: super (context);
130:
131: if (type == null) {
132: // if no type specified just default to raw.
133: type = TYPE_RAW;
134: }
135: restrict(
136: type,
137: TYPES,
138: "The 'type' parameter must be one of these values: 'raw', 'interpreted', or 'option'.");
139:
140: this .type = type;
141: this .option = option;
142: }
143:
144: /**
145: * Construct a new field value, when used in a multiple value context
146: *
147: * @param context
148: * (Required) The context this element is contained in
149: * @param type
150: * (may be null) Determine the value's type, raw, default or
151: * interpreted. If the value is null, then raw is used.
152: * @param checked
153: * (Required) Determine if the value is checked, only valid for
154: * checkboxes and radio buttons
155: */
156: protected Value(WingContext context, String type, boolean checked)
157: throws WingException {
158: super (context);
159:
160: if (type == null) {
161: // if no type specified just default to raw.
162: type = TYPE_RAW;
163: }
164: restrict(
165: type,
166: TYPES,
167: "The 'type' parameter must be one of these values: 'raw', 'interpreted', or 'option'.");
168:
169: this .type = type;
170: this .checked = checked;
171: }
172:
173: /**
174: * @return the type of this value.
175: */
176: protected String getType() {
177: return type;
178: }
179:
180: /**
181: * Translate this element and all contained elements into SAX events. The
182: * events should be routed to the contentHandler found in the WingContext.
183: *
184: * @param contentHandler
185: * (Required) The registered contentHandler where SAX events
186: * should be routed too.
187: * @param lexicalHandler
188: * (Required) The registered lexicalHandler where lexical events
189: * (such as CDATA, DTD, etc) should be routed too.
190: * @param namespaces
191: * (Required) SAX Helper class to keep track of namespaces able
192: * to determine the correct prefix for a given namespace URI.
193: */
194:
195: public void toSAX(ContentHandler contentHandler,
196: LexicalHandler lexicalHandler, NamespaceSupport namespaces)
197: throws SAXException {
198: AttributeMap attributes = new AttributeMap();
199: attributes.put(A_TYPE, this .type);
200:
201: if (this .option != null)
202: attributes.put(A_OPTION, this .option);
203: if (this .checked)
204: attributes.put(A_CHECKED, this .checked);
205:
206: startElement(contentHandler, namespaces, E_VALUE, attributes);
207: super .toSAX(contentHandler, lexicalHandler, namespaces);
208: endElement(contentHandler, namespaces, E_VALUE);
209: }
210:
211: /**
212: * dispose
213: */
214: public void dispose() {
215: super.dispose();
216: }
217:
218: }
|