org.metawidget.statically.jsp.StaticJspUtils Maven / Gradle / Ivy
// Metawidget (licensed under LGPL)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package org.metawidget.statically.jsp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Utilities for working with Java Server Pages statically.
*
* @author Ryan Bradley
*/
public final class StaticJspUtils {
//
// Public static methods
//
/**
* Return true
if the specified value conforms to the syntax requirements of a
* JSP EL expression.
*
* @param value
* The value to evaluate
* @throws NullPointerException
* if value
is null
*/
private static boolean isExpression( String value ) {
return matchExpression( value ).matches();
}
private static Matcher matchExpression( String value ) {
return PATTERN_EXPRESSION.matcher( value );
}
/**
* @return the original String, not wrapped in ${...}. If the original String was not wrapped,
* returns the original String
*/
public static String unwrapExpression( String value ) {
Matcher matcher = PATTERN_EXPRESSION.matcher( value );
if ( !matcher.matches() ) {
return value;
}
return matcher.group( 2 );
}
/**
* @return the original String, wrapped in ${...}. If the original String was already wrapped,
* returns the original String.
*/
public static String wrapExpression( String value ) {
if ( isExpression( value ) ) {
return value;
}
return EXPRESSION_START + value + EXPRESSION_END;
}
//
// Private statics
//
/**
* Match both #{..} and ${...}
*/
private static final Pattern PATTERN_EXPRESSION = Pattern.compile( "(#|\\$)\\{([^\\}]+)\\}" );
private static final String EXPRESSION_START = "${";
private static final String EXPRESSION_END = "}";
//
// Private constructor
//
private StaticJspUtils() {
// Can never be called.
}
}