001: package org.apache.velocity.tools.view;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: /**
023: * <p>ToolInfo implementation to handle "primitive" data types.
024: * It currently supports String, Number, and Boolean data.</p>
025: *
026: * <p>An example of data elements specified in your toolbox.xml
027: * might be:
028: * <pre>
029: * <data type="string">
030: * <key>app_name</key>
031: * <value>FooWeb Deluxe</value>
032: * </data>
033: * <data type="number">
034: * <key>app_version</key>
035: * <value>4.2</value>
036: * </data>
037: * <data type="boolean">
038: * <key>debug</key>
039: * <value>true</value>
040: * </data>
041: * <data type="number">
042: * <key>screen_width</key>
043: * <value>400</value>
044: * </data>
045: * </pre></p>
046: *
047: * @author Nathan Bubna
048: *
049: * @version $Id: DataInfo.java 479724 2006-11-27 18:49:37Z nbubna $
050: */
051: public class DataInfo implements ToolInfo {
052:
053: public static final String TYPE_STRING = "string";
054: public static final String TYPE_NUMBER = "number";
055: public static final String TYPE_BOOLEAN = "boolean";
056:
057: private static final int TYPE_ID_STRING = 0;
058: private static final int TYPE_ID_NUMBER = 1;
059: private static final int TYPE_ID_BOOLEAN = 2;
060:
061: private String key = null;
062: private int type_id = TYPE_ID_STRING;
063: private Object data = null;
064:
065: public DataInfo() {
066: }
067:
068: /*********************** Mutators *************************/
069:
070: public void setKey(String key) {
071: this .key = key;
072: }
073:
074: public void setType(String type) {
075: if (TYPE_BOOLEAN.equalsIgnoreCase(type)) {
076: this .type_id = TYPE_ID_BOOLEAN;
077: } else if (TYPE_NUMBER.equalsIgnoreCase(type)) {
078: this .type_id = TYPE_ID_NUMBER;
079: } else /* if no type or type="string" */
080: {
081: this .type_id = TYPE_ID_STRING;
082: }
083: }
084:
085: public void setValue(String value) {
086: if (type_id == TYPE_ID_BOOLEAN) {
087: this .data = Boolean.valueOf(value);
088: } else if (type_id == TYPE_ID_NUMBER) {
089: if (value.indexOf('.') >= 0) {
090: this .data = new Double(value);
091: } else {
092: this .data = new Integer(value);
093: }
094: } else /* type is "string" */
095: {
096: this .data = value;
097: }
098: }
099:
100: /*********************** Accessors *************************/
101:
102: public String getKey() {
103: return key;
104: }
105:
106: public String getClassname() {
107: return data != null ? data.getClass().getName() : null;
108: }
109:
110: /**
111: * Returns the data. Always returns the same
112: * object since the data is a constant. Initialization
113: * data is ignored.
114: */
115: public Object getInstance(Object initData) {
116: return data;
117: }
118:
119: }
|