public class SafsExpression
extends java.lang.Object
(Copied from RRAFS StringUtilities -- the original implementation.) Given an expression attempt to locate variable assignment references and other operators and process the expressions into a result string. Valid operators (default values): ^ '(Caret) Variable Prefix = 'Assignment operator " 'A single Double-Quote mark & 'String concatenate operator + 'Addition operator - 'Subtraction operator * 'Multiplication operator / 'Division operator % 'Modulus/Remainder operator ( 'Open Group operator ) 'Close Group operator Double-quote marks are used to enclose literal text strings or values. To include a double-quote inside a literal text string use two double-quote marks with no intervening characters. Precedence: Groupings ( ) are processed first. Within groupings, and after all groups have been processed, operators are processed in the following order: * / % + - & The expression will be trimmed of leading and trailing whitespace on entry. Use double-quote marks (literal text) to retain significant leading and trailing whitespace. Variable assignment references are identified by a leading caret (^) immediately followed by the name of the variable, an equal sign (=), and the value to assign to that variable. Whitespace can exist on either side of the equal sign. Variable names must conform to the SAFS standard (need link). Variable lookups(GET) and assignments(SET) will be done via the current SAFSVARS service or its equivalent. If the proposed value is NOT another variable reference then the proposed value will be trimmed of leading and trailing whitespace (spaces and tabs). And, yes, a variable can be assigned the value of another variable. Additionally, if there is text or operators that are not to be considered as variables they should be enclosed in double-quote marks. NOTE: Expressions containing groupings but no other legitimate operators may produce unexpected results that may not be the same on later versions of these routines.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ALL_OPERATORS
+ - * / % & =
|
protected boolean |
booErrors |
boolean |
debugPrint |
protected java.lang.String |
expEncodedQuote
((char) 11).toString()
|
protected java.lang.String |
expVarEncodedQuote
((char) 1).toString()
|
protected int |
iExpressionlength |
protected int |
numDecimalPlaces
14
|
static java.lang.String |
NUMERIC_OPERATORS
+ - * / %
|
protected java.lang.String |
qt
"\""
|
protected char |
quoteEncoded
(char) 11
|
protected java.lang.String |
sExpression |
protected java.lang.String |
strErrors |
protected boolean |
stripLeadZero |
protected boolean |
stripTrailingDecimalPoint |
protected boolean |
stripTrailingZeros |
protected SimpleVarsInterface |
varinterface |
protected char |
varQuoteEncoded
(char) 1
|
protected java.util.Vector |
vQuoteLocs |
protected java.util.Vector |
vSubStrings |
protected java.util.Vector |
vVariableNames |
Constructor and Description |
---|
SafsExpression()
Method SafsExpression.
|
SafsExpression(SimpleVarsInterface vi)
Method SafsExpression.
|
SafsExpression(java.lang.String sText)
Method SafsExpression.
|
SafsExpression(java.lang.String sText,
SimpleVarsInterface vi)
Method SafsExpression.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.String |
dereferenceVariable(java.lang.String strText)
Method dereferenceVariable.
|
protected java.lang.String |
encodeDereferencedVariable(java.lang.String strText)
Method encodeDereferencedVariable.
|
java.lang.String |
evalExpression()
The main entry point to process the overall expression.
|
protected java.lang.String |
evalExpression(java.lang.String strExpr)
Recursively called with each nested parenthetical expression, if any.
|
protected java.lang.String |
evalPrimative(java.lang.String strLeftOperand,
java.lang.String strOperator,
java.lang.String strRightOperand)
Method evalPrimative.
|
protected java.lang.String |
evalSimple(java.lang.String strExpr)
Method evalSimple.
|
java.lang.String |
evalVariables()
Method evalVariables.
|
protected java.lang.String |
evalVariables(java.lang.String strExpr)
Method evalVariables.
|
protected java.lang.String |
extractVariables(java.lang.String strExpr)
called internally by evalVariables
|
java.lang.String |
getExpression()
Method getExpression.
|
protected java.lang.String |
getLeftOperand(java.lang.String strExpr,
int iOperatorLoc)
Method getLeftOperand.
|
java.util.Vector |
getQuoteLocs()
Method getQuoteLocs.
|
protected java.lang.String |
getRightOperand(java.lang.String strExpr,
int iOperatorLoc)
Method getRightOperand.
|
java.util.Vector |
getSubStrings()
Method getSubStrings.
|
java.util.Vector |
getVariableNames()
Method getVariableNames.
|
SimpleVarsInterface |
getVarInterface()
Method getVarInterface.
|
protected java.lang.String |
getVarValue(java.lang.String strVarName)
Method getVarValue.
|
protected java.lang.String |
handleOperators(java.lang.String strExpr,
java.lang.String strOps)
Method handleOperators.
|
protected void |
initData(java.lang.String sText)
Method initData.
|
protected boolean |
isNumericOperator(java.lang.String sOperator) |
static void |
main(java.lang.String[] args) |
int |
numDecimalPlaces()
Method numDecimalPlaces.
|
void |
numDecimalPlaces(int i)
Method numDecimalPlaces.
|
void |
setExpression(java.lang.String sText)
Method setExpression.
|
protected void |
setVariableNames()
Method setVariableNames.
|
void |
setVarInterface(SimpleVarsInterface vi)
Method setVarInterface.
|
protected java.lang.String |
setVarValue(java.lang.String strVarName,
java.lang.String strVarValue)
Method setVarValue.
|
boolean |
stripLeadZero()
Method stripLeadZero.
|
void |
stripLeadZero(boolean b)
Method stripLeadZero.
|
boolean |
stripTrailingDecimalPoint()
Method stripTrailingDecimalPoint.
|
void |
stripTrailingDecimalPoint(boolean b)
Method stripTrailingDecimalPoint.
|
boolean |
stripTrailingZeros()
Method stripTrailingZeros.
|
void |
stripTrailingZeros(boolean b)
Method stripTrailingZeros.
|
protected java.lang.String |
substituteVariables(java.lang.String strExpr)
called internally by evalVariables
|
protected boolean |
validateParens()
Method validateParens.
|
protected void |
validateQuoteMatches() |
protected void |
validateVariableAssignments() |
protected void |
validateVariableNames()
Method validateVariableNames.
|
public boolean debugPrint
protected java.lang.String sExpression
protected int iExpressionlength
protected SimpleVarsInterface varinterface
protected java.util.Vector vQuoteLocs
protected java.util.Vector vSubStrings
protected java.util.Vector vVariableNames
protected java.lang.String strErrors
protected boolean booErrors
protected boolean stripLeadZero
protected boolean stripTrailingZeros
protected boolean stripTrailingDecimalPoint
protected int numDecimalPlaces
protected final java.lang.String qt
protected char quoteEncoded
protected java.lang.String expEncodedQuote
protected char varQuoteEncoded
protected java.lang.String expVarEncodedQuote
public static final java.lang.String NUMERIC_OPERATORS
public static final java.lang.String ALL_OPERATORS
public SafsExpression()
public SafsExpression(java.lang.String sText) throws java.lang.Exception
sText
- The SAFS string expressionjava.lang.Exception
- Error messages such as unmatched parentheses or illegal variable namespublic SafsExpression(SimpleVarsInterface vi)
vi
- Any object that implements org.safs.tools.vars.SimpleVarsInterface used to get and set SAFS variables.public SafsExpression(java.lang.String sText, SimpleVarsInterface vi) throws java.lang.Exception
sText
- The SAFS string expressionvi
- Any object that implements org.safs.tools.vars.SimpleVarsInterface used to get and set SAFS variables.java.lang.Exception
- Error messages such as unmatched parentheses or illegal variable namespublic void setVarInterface(SimpleVarsInterface vi)
vi
- Any object that implements org.safs.tools.vars.SimpleVarsInterface used to get and set SAFS variables.public SimpleVarsInterface getVarInterface()
public void setExpression(java.lang.String sText) throws java.lang.Exception
sText
- The SAFS string expressionjava.lang.Exception
- Error messages such as unmatched parentheses or illegal variable namespublic java.lang.String getExpression()
public java.util.Vector getQuoteLocs()
Integer
objects containing locations of the quotes (if any)public java.util.Vector getSubStrings()
String
objects containing quoted substrings of the expression (if any)public java.util.Vector getVariableNames()
String
objects that are the variables found within the expression sExpressionpublic void stripLeadZero(boolean b)
sets option to strip the leading zero from expression results. For example, if the expression evaluates to 0.1 and stripLeadZero is true, the result will be formatted such that the leading zero is not shown, which would be .1
b
- true or false specifying whether or not leading zeros should be strippedpublic boolean stripLeadZero()
public void stripTrailingZeros(boolean b)
sets option to strip the trailing zeros from expression results. For example, if the expression evaluates to 1.100 and stripTrailingZeros is true, the result will be formatted such that the trailing zeros are not shown, which would be 1.1
b
- true or false specifying whether or not trailing zeros should be strippedpublic boolean stripTrailingZeros()
public void stripTrailingDecimalPoint(boolean b)
sets option to strip the trailing decimal points from expression results. For example, if the expression evaluates to 1. and stripTrailingDecimalPoint is true, the result will be formatted such that the trailing zeros are not shown, which would be 1
b
- true or false specifying whether or not trailing decimal points should be strippedpublic boolean stripTrailingDecimalPoint()
public void numDecimalPlaces(int i)
i
- int specifying the number of decimal places to round half up in division operationspublic int numDecimalPlaces()
public java.lang.String evalExpression()
evalExpression(String)
public java.lang.String evalVariables()
protected void initData(java.lang.String sText) throws java.lang.Exception
sText
- The SAFS string expressionjava.lang.Exception
- Error messages such as unmatched parentheses or illegal variable namesprotected boolean isNumericOperator(java.lang.String sOperator)
sOperator
- The first char will be tested against known numeric operatorsprotected void setVariableNames()
protected boolean validateParens()
protected void validateVariableAssignments()
protected void validateVariableNames()
protected void validateQuoteMatches()
protected java.lang.String getVarValue(java.lang.String strVarName)
strVarName
- The name of the variable to retrieveprotected java.lang.String setVarValue(java.lang.String strVarName, java.lang.String strVarValue)
strVarName
- The name of the variable to setstrVarValue
- The value to set the variable toprotected java.lang.String evalExpression(java.lang.String strExpr)
strExpr
- The expression to evaluateevalExpression()
protected java.lang.String substituteVariables(java.lang.String strExpr)
protected java.lang.String extractVariables(java.lang.String strExpr)
protected java.lang.String evalVariables(java.lang.String strExpr)
strExpr
- The expression to evaluateprotected java.lang.String evalSimple(java.lang.String strExpr)
strExpr
- The simple expression to evaluateprotected java.lang.String handleOperators(java.lang.String strExpr, java.lang.String strOps)
strExpr
- The expression to evaluatestrOps
- The operators to handle at the same precedence. For example "+-&" are
evaluated at the same precedence.protected java.lang.String dereferenceVariable(java.lang.String strText)
strText
- The string to check wich may or may not be a variableprotected java.lang.String encodeDereferencedVariable(java.lang.String strText)
strText
- The string to check which may or may not be a variableprotected java.lang.String getRightOperand(java.lang.String strExpr, int iOperatorLoc)
strExpr
- The expressioniOperatorLoc
- The int location of the operator within strExprprotected java.lang.String getLeftOperand(java.lang.String strExpr, int iOperatorLoc)
strExpr
- The expressioniOperatorLoc
- The int location of the operator within strExprprotected java.lang.String evalPrimative(java.lang.String strLeftOperand, java.lang.String strOperator, java.lang.String strRightOperand)
strLeftOperand
- The left operandstrOperator
- The operatorstrRightOperand
- The right operandpublic static void main(java.lang.String[] args)
Copyright © SAS Institute. All Rights Reserved.