Source Code Cross Referenced for Expression.java in  » Testing » webtest » com » canoo » webtest » util » 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 » Testing » webtest » com.canoo.webtest.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright © 2004-2005 ASERT. Released under the Canoo Webtest license.
002:        package com.canoo.webtest.util;
003:
004:        /**
005:         * Evaluates a mathematical expression.
006:         *
007:         * @author Paul King
008:         * @author Rob Nielsen
009:         */
010:        public class Expression {
011:
012:            private Evaluator fEvaluator;
013:            /**
014:             * a private instance for the static evaluate method
015:             */
016:            private static Expression sInstance;
017:
018:            /**
019:             * Constructs a new blank expression
020:             */
021:            public Expression() {
022:            }
023:
024:            /**
025:             * Constructor for the Expression object
026:             *
027:             * @param eval the fEvaluator to use for unknown expressions
028:             */
029:            public Expression(Evaluator eval) {
030:                setEvaluator(eval);
031:            }
032:
033:            /**
034:             * Sets the Evaluator attribute of the Expression object
035:             *
036:             * @param evaluator The new Evaluator value
037:             */
038:            public void setEvaluator(Evaluator evaluator) {
039:                this .fEvaluator = evaluator;
040:            }
041:
042:            /**
043:             * Evaluates the expression string and returns the result (which is also available
044:             * with getValue()).  Any parse errors will throw a IllegalArgumentException
045:             *
046:             * @param exp the string to parse
047:             * @return the double value of the expression
048:             * @throws IllegalArgumentException
049:             */
050:            public double evaluate(String exp) {
051:                if (exp == null) {
052:                    return 0.0;
053:                }
054:                try {
055:                    return Double.parseDouble(exp);
056:                } catch (NumberFormatException e) {
057:                    return evaluate(exp.toCharArray(), 0, exp.length());
058:                }
059:            }
060:
061:            /**
062:             * Evaluates a string found in the expression which isn't an arithmetic
063:             * expression.  Calls out to the given fEvaluator.
064:             *
065:             * @param s the substring to evaluate
066:             * @return the double value of the evaluation
067:             */
068:            protected double evaluateString(String s) {
069:                if (fEvaluator != null) {
070:                    return fEvaluator.evaluate(s.trim());
071:                }
072:                throw new IllegalArgumentException("Cannot parse: '" + s
073:                        + "': No evaluator");
074:            }
075:
076:            /**
077:             * Recursively evaluates the exp contained in the char array e between
078:             * start and end.
079:             *
080:             * @param e     the exp
081:             * @param origStart the start index (inclusive) to evaluate from
082:             * @param origEnd   the end index (exclusive) to evaluate to
083:             * @return the evaluated exp
084:             * @throws IllegalArgumentException if the exp can't be parsed.
085:             */
086:            private double evaluate(char[] e, int origStart, int origEnd) {
087:                int start = origStart;
088:                int end = origEnd;
089:                while (start < end && e[start] == ' ') {
090:                    start++;
091:                }
092:                if (end == start) {
093:                    return 0;
094:                }
095:
096:                while (e[end - 1] == ' ') {
097:                    end--;
098:                }
099:
100:                boolean number = true;
101:                int bracket = 0;
102:                for (int add = 0; add < 2; add++) {
103:                    for (int i = end - 1; i >= start; i--) {
104:                        if (e[i] == ')') {
105:                            bracket++;
106:                        } else if (e[i] == '(') {
107:                            bracket--;
108:                        } else if (bracket == 0) {
109:                            if (add == 0) {
110:                                if (e[i] == '+') {
111:                                    return evaluate(e, start, i)
112:                                            + evaluate(e, i + 1, end);
113:                                }
114:                                if (e[i] == '-') {
115:                                    return evaluate(e, start, i)
116:                                            - evaluate(e, i + 1, end);
117:                                }
118:                            } else {
119:                                if (e[i] == '*') {
120:                                    return evaluate(e, start, i)
121:                                            * evaluate(e, i + 1, end);
122:                                }
123:                                if (e[i] == '/') {
124:                                    return evaluate(e, start, i)
125:                                            / evaluate(e, i + 1, end);
126:                                }
127:                                if (e[i] == '%') {
128:                                    return evaluate(e, start, i)
129:                                            % evaluate(e, i + 1, end);
130:                                }
131:                            }
132:                        }
133:                        if ((e[i] < '0' || e[i] > '9') && e[i] != '.') {
134:                            number = false;
135:                        }
136:                    }
137:                }
138:                if (e[end - 1] == ')' && e[start] == '(') {
139:                    start++;
140:                    end--;
141:                    if (end == start) {
142:                        return 0;
143:                    }
144:                    return evaluate(e, start, end);
145:                }
146:
147:                String s = new String(e, start, end - start);
148:                if (number) {
149:                    return Double.parseDouble(s);
150:                }
151:                return evaluateString(s);
152:            }
153:
154:            /**
155:             * Evaluates a string expression without requiring the creation of an instance.
156:             *
157:             * @param s the expression to evaluate
158:             * @return the double value of the expression
159:             */
160:            public static double evaluateExpression(String s) {
161:                return evaluateExpression(s, null);
162:            }
163:
164:            /**
165:             * Evaluates a string expression without requiring the creation of an instance.
166:             *
167:             * @param s    the expression to evaluate
168:             * @param eval the fEvaluator to parse special values
169:             * @return the double value of the expression
170:             */
171:            public static double evaluateExpression(String s, Evaluator eval) {
172:                if (sInstance == null) {
173:                    sInstance = new Expression(eval);
174:                } else {
175:                    sInstance.setEvaluator(eval);
176:                }
177:
178:                return sInstance.evaluate(s);
179:            }
180:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.