01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.elementprocessor.types;
18:
19: import java.io.IOException;
20:
21: /**
22: * This class knows how to convert strings into numbers, and also
23: * knows how to check the results against certain criteria
24: *
25: * @author Marc Johnson (marc_johnson27591@hotmail.com)
26: * @version CVS $Id: BooleanConverter.java 433543 2006-08-22 06:22:54Z crossley $
27: */
28: public class BooleanConverter {
29: private static final String _true_values[] = { "1", "true" };
30: private static final String _false_values[] = { "0", "false" };
31: private static final BooleanResult _true_result = new BooleanResult(
32: true);
33: private static final BooleanResult _false_result = new BooleanResult(
34: false);
35:
36: private BooleanConverter() {
37: }
38:
39: /**
40: * Given a string that is expected to hold a boolean, get the
41: * boolean value.
42: *
43: * @param value the string holding the boolean
44: *
45: * @return a BooleanResult object containing either the boolean
46: * value or an exception generated if there was a problem
47: * with the value;
48: */
49:
50: public static BooleanResult extractBoolean(final String value) {
51: String input = (value == null) ? "" : value.trim();
52: BooleanResult result = null;
53:
54: for (int k = 0; k < _true_values.length; k++) {
55: if (_true_values[k].equalsIgnoreCase(input)) {
56: result = _true_result;
57: break;
58: }
59: }
60: if (result == null) {
61: for (int k = 0; k < _false_values.length; k++) {
62: if (_false_values[k].equalsIgnoreCase(input)) {
63: result = _false_result;
64: break;
65: }
66: }
67: }
68: if (result == null) {
69: result = new BooleanResult(new IOException("\"" + input
70: + "\" is not a boolean value"));
71: }
72: return result;
73: }
74: } // end public class BooleanConverter
|