org.metawidget.statically.faces.StaticFacesUtils Maven / Gradle / Ivy
// Metawidget
//
// This file is dual licensed under both the LGPL
// (http://www.gnu.org/licenses/lgpl-2.1.html) and the EPL
// (http://www.eclipse.org/org/documents/epl-v10.php). As a
// recipient of Metawidget, you may choose to receive it under either
// the LGPL or the EPL.
//
// Commercial licenses are also available. See http://metawidget.org
// for details.
package org.metawidget.statically.faces;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Utilities for working with Java Server Faces statically.
*
* @author Richard Kennard
*/
public final class StaticFacesUtils {
//
// Public statics
//
/**
* Return true
if the specified value conforms to the syntax requirements of a
* value binding expression.
*
* This method is a mirror of the one in UIComponentTag.isValueReference
, but that
* one is deprecated so may be removed in the future.
*
* @param value
* The value to evaluate
* @throws NullPointerException
* if value
is null
*/
public static boolean isExpression( String value ) {
return matchExpression( value ).matches();
}
public 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 + unwrapExpression( value ) + EXPRESSION_END;
}
/**
* Match #{...} and ${...}. This mirrors the approach in
* UIComponentTag.isValueReference
, but that one is deprecated so may be removed in
* the future.
*/
private static final Pattern PATTERN_EXPRESSION = Pattern.compile( "(#|\\$)\\{([^\\}]+)\\}" );
private static final String EXPRESSION_START = "#{";
private static final String EXPRESSION_END = "}";
//
// Private constructor
//
private StaticFacesUtils() {
// Can never be called
}
}