001: package nl.knowlogy.validation.jsf.utils;
002:
003: import java.util.List;
004:
005: import javax.faces.component.NamingContainer;
006: import javax.faces.component.UIComponent;
007: import javax.faces.context.FacesContext;
008: import javax.faces.el.ValueBinding;
009:
010: import org.apache.commons.lang.StringUtils;
011:
012: public class JsfUtils {
013:
014: public static String getObjectPartFromValueExpression(
015: String valueExpression) {
016: int indexOfLastPoint = StringUtils.lastIndexOf(valueExpression,
017: '.');
018: int indexOfLastParenthesis = StringUtils.lastIndexOf(
019: valueExpression, '}');
020: return StringUtils.substring(valueExpression, 0,
021: indexOfLastPoint)
022: + "}";
023: }
024:
025: public static String getObjectPart(UIComponent uiComponent) {
026: String valueExpression = uiComponent.getValueBinding("value")
027: .getExpressionString();
028: return getObjectPartFromValueExpression(valueExpression);
029: }
030:
031: public static String getPropertyNameFromValueExpression(
032: String valueExpression) {
033: int indexOfLastPoint = StringUtils.lastIndexOf(valueExpression,
034: '.');
035: int indexOfLastParenthesis = StringUtils.lastIndexOf(
036: valueExpression, '}');
037:
038: return StringUtils.substring(valueExpression,
039: indexOfLastPoint + 1, indexOfLastParenthesis);
040:
041: }
042:
043: public static String getPropertyNamePart(UIComponent uiComponent) {
044: String valueExpression = uiComponent.getValueBinding("value")
045: .getExpressionString();
046: return getPropertyNameFromValueExpression(valueExpression);
047: }
048:
049: public static Object getBindedObject(String objectExpression) {
050: FacesContext context = FacesContext.getCurrentInstance();
051: return getBindedObject(context, objectExpression);
052: }
053:
054: public static Object getBindedObject(FacesContext context,
055: String objectExpression) {
056: return context.getApplication().createValueBinding(
057: objectExpression).getValue(context);
058: }
059:
060: public static Object getContainingObject(UIComponent uiComponent) {
061: String containingObjectExpression = getObjectPart(uiComponent);
062: FacesContext context = FacesContext.getCurrentInstance();
063: return context.getApplication().createValueBinding(
064: containingObjectExpression).getValue(context);
065: }
066:
067: /**
068: * Locates the component identified by <code>forComponent</code>
069: *
070: * @param forComponent -
071: * the component to search for
072: * @param component -
073: * the starting point in which to begin the search
074: * @return the component with the the <code>id</code that matches
075: * <code>forComponent</code> otheriwse null if no match is found.
076: */
077: public static UIComponent getForComponent(FacesContext context,
078: String forComponent, UIComponent component) {
079: if (null == forComponent || forComponent.length() == 0) {
080: return null;
081: }
082:
083: UIComponent result = null;
084: UIComponent currentParent = component;
085: try {
086: // Check the naming container of the current
087: // component for component identified by
088: // 'forComponent'
089: while (currentParent != null) {
090: // If the current component is a NamingContainer,
091: // see if it contains what we're looking for.
092: result = currentParent.findComponent(forComponent);
093: if (result != null)
094: break;
095: // if not, start checking further up in the view
096: currentParent = currentParent.getParent();
097: }
098:
099: // no hit from above, scan for a NamingContainer
100: // that contains the component we're looking for from the root.
101: if (result == null) {
102: result = findUIComponentBelow(context.getViewRoot(),
103: forComponent);
104: }
105: } catch (Throwable t) {
106: // Object[] params = { forComponent };
107: throw new RuntimeException(""
108: // Util.getExceptionMessageString(Util.COMPONENT_NOT_FOUND_ERROR_MESSAGE_ID,
109: // params)
110: );
111: }
112:
113: return result;
114: }
115:
116: /**
117: * <p>
118: * Recursively searches for {@link NamingContainer}s from the given start
119: * point looking for the component with the <code>id</code> specified by
120: * <code>forComponent</code>.
121: *
122: * @param startPoint -
123: * the starting point in which to begin the search
124: * @param forComponent -
125: * the component to search for
126: * @return the component with the the <code>id</code that matches
127: * <code>forComponent</code> otheriwse null if no match is found.
128: */
129: private static UIComponent findUIComponentBelow(
130: UIComponent startPoint, String forComponent) {
131: UIComponent retComp = null;
132: List children = startPoint.getChildren();
133: for (int i = 0, size = children.size(); i < size; i++) {
134: UIComponent comp = (UIComponent) children.get(i);
135:
136: if (comp instanceof NamingContainer) {
137: retComp = comp.findComponent(forComponent);
138: }
139:
140: if (retComp == null) {
141: if (comp.getChildCount() > 0) {
142: retComp = findUIComponentBelow(comp, forComponent);
143: }
144: }
145:
146: if (retComp != null)
147: break;
148: }
149: return retComp;
150: }
151:
152: public static boolean getBooleanAttribute(UIComponent component,
153: String attrName, boolean defaultValue) {
154: Boolean b = (Boolean) component.getAttributes().get(attrName);
155: return b != null ? b.booleanValue() : defaultValue;
156: }
157:
158: public static String getStringValue(FacesContext context,
159: ValueBinding vb) {
160: Object value = vb.getValue(context);
161: if (value == null) {
162: return null;
163: }
164: return value.toString();
165: }
166:
167: }
|