joi, 12 februarie 2015

Helper methods for determining if the clientId of a component is auto generated

This post provides two helper methods for determining if the clientId of a component is auto generated by JSF or not (indicated the user via string literal of ValueExpression). As you probably now, OmniFaces has a custom view handler that throw an IllegalStateException whenever an automatically generated JSF component ID (j_id...) is encountered in the rendered output (see NoAutoGeneratedIdViewHandler). Well, based on OmniFaces implementation we wrote the below two simple helpers:

·         pass the UIComponent itself to check its clientId

public boolean isAutoGeneratedId(UIComponent component) {

 String clientId = component.getClientId();

 final String UNIQUE_ID_PREFIX = javax.faces.component.UIViewRoot.UNIQUE_ID_PREFIX;
 final char SEPARATOR_CHAR = getSeparatorChar(Faces.getContext());
 final String INTERMEDIATE_ID_PREFIX = SEPARATOR_CHAR + UNIQUE_ID_PREFIX;

 if (clientId.startsWith(UNIQUE_ID_PREFIX) ||  
     clientId.contains(INTERMEDIATE_ID_PREFIX)) {
     return true;
 }

 return false;
}

·         pass the UIComponent clientId to check

public boolean isAutoGeneratedId(String clientId) {

 final String UNIQUE_ID_PREFIX = javax.faces.component.UIViewRoot.UNIQUE_ID_PREFIX;
 final char SEPARATOR_CHAR = getSeparatorChar(Faces.getContext());
 final String INTERMEDIATE_ID_PREFIX = SEPARATOR_CHAR + UNIQUE_ID_PREFIX;

 if (clientId.startsWith(UNIQUE_ID_PREFIX) ||
       clientId.contains(INTERMEDIATE_ID_PREFIX)) {
     return true;
 }

 return false;
}

Note If you will use manually (or generated somehow) IDs that "looks like" the ones generated by JSF, then you will "fool" these methods. 

Niciun comentariu:

Trimiteți un comentariu