001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.components.elementprocessor.types;
019:
020: import org.apache.cocoon.CascadingIOException;
021:
022: import java.io.IOException;
023:
024: /**
025: * This class knows how to convert strings into numbers, and also
026: * knows how to check the results against certain criteria
027: *
028: * @author Marc Johnson (marc_johnson27591@hotmail.com)
029: * @version CVS $Id: NumericConverter.java 433543 2006-08-22 06:22:54Z crossley $
030: */
031: public class NumericConverter {
032: private static final Validator _non_negative_validator = new Validator() {
033: public IOException validate(final Number number) {
034: IOException e = null;
035:
036: if (number.intValue() < 0) {
037: e = new IOException("\"" + number.intValue()
038: + "\" is not a non-negative integer");
039: }
040: return e;
041: }
042: };
043: private static final Validator _positive_validator = new Validator() {
044: public IOException validate(final Number number) {
045: IOException e = null;
046:
047: if (number.intValue() < 1) {
048: e = new IOException("\"" + number.intValue()
049: + "\" is not a positive integer");
050: }
051: return e;
052: }
053: };
054:
055: private NumericConverter() {
056: }
057:
058: /**
059: * Shortcut for extractDouble without a Validator
060: *
061: * @param value the string holding the double
062: * @return a NumericResult object containing either the double
063: * value or an exception generated if there was a problem
064: * with the value;
065: */
066:
067: public static NumericResult extractDouble(final String value) {
068: return extractDouble(value, null);
069: }
070:
071: /**
072: * Given a string that is expected to hold a double, get the double value.
073: *
074: * @param value the string holding the double
075: * @param validator a Validator object; if null, no additional
076: * validation will be performed
077: *
078: * @return a NumericResult object containing either the double
079: * value or an exception generated if there was a problem
080: * with the value;
081: */
082:
083: public static NumericResult extractDouble(final String value,
084: final Validator validator) {
085: String input = (value == null) ? "" : value.trim();
086: NumericResult result = null;
087:
088: try {
089: Number number = new Double(input);
090: IOException exception = null;
091:
092: if (validator != null) {
093: exception = validator.validate(number);
094: }
095: if (exception == null) {
096: result = new NumericResult(number);
097: } else {
098: result = new NumericResult(exception);
099: }
100: } catch (NumberFormatException ignored) {
101: result = new NumericResult(new CascadingIOException("\""
102: + input + "\" does not represent a double value",
103: ignored));
104: }
105: return result;
106: }
107:
108: /**
109: * Shortcut for extractInteger without a Validator
110: *
111: * @param value the string holding the integer
112: * @return a NumericResult object containing either the integer
113: * value or an exception generated if there was a problem
114: * with the value;
115: */
116:
117: public static NumericResult extractInteger(final String value) {
118: return extractInteger(value, null);
119: }
120:
121: /**
122: * Given a string that is expected to hold a integer, get the integer value.
123: *
124: * @param value the string holding the integer
125: * @param validator a Validator object; if null, no additional
126: * validation will be performed
127: *
128: * @return a NumericResult object containing either the integer
129: * value or an exception generated if there was a problem
130: * with the value;
131: */
132:
133: public static NumericResult extractInteger(final String value,
134: final Validator validator) {
135: String input = (value == null) ? "" : value.trim();
136: NumericResult result = null;
137:
138: try {
139: Number number = new Integer(input);
140: IOException exception = null;
141:
142: if (validator != null) {
143: exception = validator.validate(number);
144: }
145: if (exception == null) {
146: result = new NumericResult(number);
147: } else {
148: result = new NumericResult(exception);
149: }
150: } catch (NumberFormatException ignored) {
151: result = new NumericResult(new CascadingIOException("\""
152: + input + "\" does not represent an integer value",
153: ignored));
154: }
155: return result;
156: }
157:
158: /**
159: * extract a positive integer (i.e., an integer with a range of 1
160: * ... MAX_VALUE)
161: *
162: * @param value the string holding the value
163: *
164: * @return a NumericResult object containing either the integer
165: * value or an exception generated if there was a problem
166: * with the value;
167: */
168:
169: public static NumericResult extractPositiveInteger(
170: final String value) {
171: return extractInteger(value, _positive_validator);
172: }
173:
174: /**
175: * extract a non-negative integer (i.e., an integer with a range
176: * of 1 ... MAX_VALUE)
177: *
178: * @param value the string holding the value
179: *
180: * @return a NumericResult object containing either the integer
181: * value or an exception generated if there was a problem
182: * with the value;
183: */
184:
185: public static NumericResult extractNonNegativeInteger(
186: final String value) {
187: return extractInteger(value, _non_negative_validator);
188: }
189: } // end public class NumericConverter
|