All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metawidget.faces.FacesUtils Maven / Gradle / Ivy

There is a newer version: 4.2
Show newest version
// Metawidget
//
// 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.faces;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

/**
 * Utilities for working with Java Server Faces.
 *
 * @author Richard Kennard
 */

public final class FacesUtils {

	//
	// 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; } public static void render( FacesContext context, UIComponent component ) throws IOException { if ( component == null || !component.isRendered() ) { return; } component.encodeBegin( context ); if ( component.getRendersChildren() ) { component.encodeChildren( context ); } else { renderChildren( context, component ); } component.encodeEnd( context ); } /** * @return true if the JSF version is 2 or above, false otherwise */ public static boolean isJsf2() { return IS_JSF_2; } public static boolean isValidationFailed() { FacesContext context = FacesContext.getCurrentInstance(); if ( isJsf2() ) { return ( FacesContext.getCurrentInstance().isValidationFailed() ); } // Under JSF 1.2, any severity (even SEVERITY_INFO) is considered a validation error. See // http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1035 return ( context.getMaximumSeverity() != null ); } public static boolean isPartialStateSavingDisabled() { FacesContext context = FacesContext.getCurrentInstance(); return ( "false".equals( context.getExternalContext().getInitParameter( "javax.faces.PARTIAL_STATE_SAVING" ) ) ); } /** * Create a component using either the JSF 2.x * createComponent( context, componentType, rendererType ) method, or the JSF 1.x * createComponent( componentType ) method. The former can be important for JSF 2.x * controls that need to dynamically include additional resources (such as JavaScript/CSS * files). See this forum discussion. */ @SuppressWarnings( "unchecked" ) public static T createComponent( String componentType, String rendererType ) { FacesContext context = FacesContext.getCurrentInstance(); Application application = context.getApplication(); // JSF 2.x if ( isJsf2() ) { return (T) application.createComponent( context, componentType, rendererType ); } // JSF 1.x return (T) application.createComponent( componentType ); } /** * Sets the given style and styleClass on the given Component. *

* JSF does not provide a common interface for all UIComponents that support * style and styleClass, so we must interrogate their attributes * directly. */ public static void setStyleAndStyleClass( UIComponent component, String style, String styleClass ) { Map attributes = component.getAttributes(); if ( style != null ) { String existingStyle = (String) attributes.get( "style" ); if ( existingStyle == null || "".equals( existingStyle ) ) { attributes.put( "style", style ); } else { attributes.put( "style", existingStyle + " " + style ); } } if ( styleClass != null ) { String existingStyleClass = (String) attributes.get( "styleClass" ); if ( existingStyleClass == null || "".equals( existingStyleClass ) ) { attributes.put( "styleClass", styleClass ); } else { attributes.put( "styleClass", existingStyleClass + " " + styleClass ); } } } // // Private statics // private static void renderChildren( FacesContext context, UIComponent component ) throws IOException { for ( UIComponent componentChild : component.getChildren() ) { render( context, componentChild ); } } /** * 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 static final boolean IS_JSF_2; static { boolean isJsf2; try { Class.forName( "javax.faces.event.PreRenderViewEvent" ); isJsf2 = true; } catch ( ClassNotFoundException e ) { isJsf2 = false; } IS_JSF_2 = isJsf2; } // // Private constructor // private FacesUtils() { // Can never be called } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy