Portlet Factory, Version 6.1.2


 

A format method implementation

The format method examines the expression it receives and calls the appropriate operation, passing the value as an argument to that operation.

Note: This implementation includes a check to see if the error message is null before it formats the value, allowing you to redisplay the form with the users' inputs as they entered them instead of those inputs being formatted.

public String format(String value, String expression) { String result = null; if(getErrorMessage() == null) { if (expression.equals(PHONEDOTS)) { result = phoneWithDots(value); else if (expression.equals(PHONEDASHES)){ result = phoneWithDashes(value); else { result = value; return result;
}

In this case, the format method checks to see if the expression equals one of the expression constants defined earlier. The following code snippets show the phoneWithDots and phoneWithDashes methods.

String phoneWithDots(String value) { String dotvalue = value.substring(0,3)+ "." + value.substring(3,6) +
"." + value.substring(6,value.length()); return dotvalue; String phoneWithDashes(String value) { String dashvalue = "(" + value.substring(0,3)+ ") " + value.substring(3,6) +
"-" + value.substring(6,value.length()); return dashvalue;
}

Parent topic: About creating a formatter class


Library | Support |