Source Code Cross Referenced for NumericConverter.java in  » Content-Management-System » apache-lenya-2.0 » org » apache » cocoon » components » elementprocessor » types » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » apache lenya 2.0 » org.apache.cocoon.components.elementprocessor.types 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.