001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.forms.convert;
017:
018: import java.text.ParseException;
019:
020: /**
021: * Boolean converter.
022: *
023: * @author Christoph Beck
024: */
025: public class BooleanConverter extends SimpleConverter {
026: private String trueString;
027: private String falseString;
028: private Boolean parsedNullOrEmpty;
029:
030: /**
031: * Default constructor.
032: */
033: public BooleanConverter() {
034: this ("true", "false");
035: }
036:
037: /**
038: * Constructor.
039: */
040: public BooleanConverter(String trueValue, String falseValue) {
041: this .trueString = trueValue;
042: this .falseString = falseValue;
043: }
044:
045: /**
046: * Format method.
047: */
048: public String format(Object value) {
049: if (value == null) {
050: return null;
051: }
052: return ((Boolean) value).booleanValue() ? trueString
053: : falseString;
054: }
055:
056: /**
057: * Get the falseValue property
058: */
059: public String getFalseString() {
060: return falseString;
061: }
062:
063: /**
064: * Get the default property
065: */
066: public Boolean getDefault() {
067: return parsedNullOrEmpty;
068: }
069:
070: /**
071: * Get the trueValue property
072: */
073: public String getTrueString() {
074: return trueString;
075: }
076:
077: /**
078: * Parse method.
079: */
080: public Object parse(String value) throws ParseException {
081: if (value == null || value.length() == 0)
082: return parsedNullOrEmpty;
083: if (value.equals(trueString))
084: return Boolean.TRUE;
085: if (value.equals(falseString))
086: return Boolean.FALSE;
087: throw new ParseException("Cannot parse: " + value, 0);
088: }
089:
090: /**
091: * Set the falseValue property
092: */
093: public void setFalseString(String newFalseString) {
094: falseString = newFalseString;
095: }
096:
097: /**
098: * Set the default property
099: */
100: public void setDefault(Boolean value) {
101: parsedNullOrEmpty = value;
102: }
103:
104: /**
105: * Set the trueValue property
106: */
107: public void setTrueString(String newTrueString) {
108: trueString = newTrueString;
109: }
110:
111: /**
112: * This converter is thread-save, so answer <code>true</code>
113: */
114: public boolean isSharable() {
115: return true;
116: }
117: }
|