KickJava   Java API By Example, From Geeks To Geeks.

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


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

30 public class NumericConverter
31 {
32     private static final Validator _non_negative_validator = new Validator()
33     {
34         public IOException validate(final Number JavaDoc number) {
35             IOException e = null;
36
37             if (number.intValue() < 0) {
38                 e = new IOException("\"" + number.intValue()
39                                     + "\" is not a non-negative integer");
40             }
41             return e;
42         }
43     };
44     private static final Validator _positive_validator = new Validator()
45     {
46         public IOException validate(final Number JavaDoc number) {
47             IOException e = null;
48
49             if (number.intValue() < 1) {
50                 e = new IOException("\"" + number.intValue()
51                                     + "\" is not a positive integer");
52             }
53             return e;
54         }
55     };
56
57     private NumericConverter() {
58     }
59
60     /**
61      * Shortcut for extractDouble without a Validator
62      *
63      * @param value the string holding the double
64      * @return a NumericResult object containing either the double
65      * value or an exception generated if there was a problem
66      * with the value;
67      */

68
69     public static NumericResult extractDouble(final String JavaDoc value) {
70         return extractDouble(value, null);
71     }
72
73     /**
74      * Given a string that is expected to hold a double, get the double value.
75      *
76      * @param value the string holding the double
77      * @param validator a Validator object; if null, no additional
78      * validation will be performed
79      *
80      * @return a NumericResult object containing either the double
81      * value or an exception generated if there was a problem
82      * with the value;
83      */

84
85     public static NumericResult extractDouble(final String JavaDoc value,
86                 final Validator validator) {
87         String JavaDoc input = (value == null) ? "" : value.trim();
88         NumericResult result = null;
89
90         try {
91             Number JavaDoc number = new Double JavaDoc(input);
92             IOException exception = null;
93
94             if (validator != null) {
95                 exception = validator.validate(number);
96             }
97             if (exception == null) {
98                 result = new NumericResult(number);
99             } else {
100                 result = new NumericResult(exception);
101             }
102         } catch (NumberFormatException JavaDoc ignored) {
103             result = new NumericResult(
104                 new CascadingIOException(
105                     "\"" + input + "\" does not represent a double value", ignored));
106         }
107         return result;
108     }
109
110     /**
111      * Shortcut for extractInteger without a Validator
112      *
113      * @param value the string holding the integer
114      * @return a NumericResult object containing either the integer
115      * value or an exception generated if there was a problem
116      * with the value;
117      */

118
119     public static NumericResult extractInteger(final String JavaDoc value) {
120         return extractInteger(value, null);
121     }
122
123     /**
124      * Given a string that is expected to hold a integer, get the integer value.
125      *
126      * @param value the string holding the integer
127      * @param validator a Validator object; if null, no additional
128      * validation will be performed
129      *
130      * @return a NumericResult object containing either the integer
131      * value or an exception generated if there was a problem
132      * with the value;
133      */

134
135     public static NumericResult extractInteger(final String JavaDoc value,
136             final Validator validator) {
137         String JavaDoc input = (value == null) ? "" : value.trim();
138         NumericResult result = null;
139
140         try {
141             Number JavaDoc number = new Integer JavaDoc(input);
142             IOException exception = null;
143
144             if (validator != null) {
145                 exception = validator.validate(number);
146             }
147             if (exception == null) {
148                 result = new NumericResult(number);
149             } else {
150                 result = new NumericResult(exception);
151             }
152         } catch (NumberFormatException JavaDoc ignored) {
153             result = new NumericResult(
154                 new CascadingIOException(
155                     "\"" + input + "\" does not represent an integer value", ignored));
156         }
157         return result;
158     }
159
160     /**
161      * extract a positive integer (i.e., an integer with a range of 1
162      * ... MAX_VALUE)
163      *
164      * @param value the string holding the value
165      *
166      * @return a NumericResult object containing either the integer
167      * value or an exception generated if there was a problem
168      * with the value;
169      */

170
171     public static NumericResult extractPositiveInteger(final String JavaDoc value) {
172         return extractInteger(value, _positive_validator);
173     }
174
175     /**
176      * extract a non-negative integer (i.e., an integer with a range
177      * of 1 ... MAX_VALUE)
178      *
179      * @param value the string holding the value
180      *
181      * @return a NumericResult object containing either the integer
182      * value or an exception generated if there was a problem
183      * with the value;
184      */

185
186     public static NumericResult extractNonNegativeInteger(final String JavaDoc value) {
187         return extractInteger(value, _non_negative_validator);
188     }
189 } // end public class NumericConverter
190
Popular Tags