KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > elementprocessor > types > BooleanConverter


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.components.elementprocessor.types;
17
18 import java.io.IOException JavaDoc;
19
20 /**
21  * This class knows how to convert strings into numbers, and also
22  * knows how to check the results against certain criteria
23  *
24  * @author Marc Johnson (marc_johnson27591@hotmail.com)
25  * @version CVS $Id: BooleanConverter.java 30932 2004-07-29 17:35:38Z vgritsenko $
26  */

27 public class BooleanConverter
28 {
29     private static final String JavaDoc _true_values[] =
30     {
31         "1", "true"
32     };
33     private static final String JavaDoc _false_values[] =
34     {
35         "0", "false"
36     };
37     private static final BooleanResult _true_result =
38         new BooleanResult(true);
39     private static final BooleanResult _false_result =
40         new BooleanResult(false);
41
42     private BooleanConverter() {
43     }
44
45     /**
46      * Given a string that is expected to hold a boolean, get the
47      * boolean value.
48      *
49      * @param value the string holding the boolean
50      *
51      * @return a BooleanResult object containing either the boolean
52      * value or an exception generated if there was a problem
53      * with the value;
54      */

55
56     public static BooleanResult extractBoolean(final String JavaDoc value) {
57         String JavaDoc input = (value == null) ? "" : value.trim();
58         BooleanResult result = null;
59
60         for (int k = 0; k < _true_values.length; k++) {
61             if (_true_values[k].equalsIgnoreCase(input)) {
62                 result = _true_result;
63                 break;
64             }
65         }
66         if (result == null) {
67             for (int k = 0; k < _false_values.length; k++) {
68                 if (_false_values[k].equalsIgnoreCase(input)) {
69                     result = _false_result;
70                     break;
71                 }
72             }
73         }
74         if (result == null) {
75             result = new BooleanResult(
76                 new IOException JavaDoc("\"" + input + "\" is not a boolean value"));
77         }
78         return result;
79     }
80 } // end public class BooleanConverter
81
Popular Tags