Java Code Examples for javax.el.ValueExpression#getExpressionString()

The following examples show how to use javax.el.ValueExpression#getExpressionString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ELTools.java    From BootsFaces-OSP with Apache License 2.0 6 votes vote down vote up
/**
 * Return the core value expression of a specified component
 * 
 * @param component
 * @return
 */
public static String getCoreValueExpression(UIComponent component) {
	ValueExpression valueExpression = component.getValueExpression("value");
	if (null != valueExpression) {
		String v = valueExpression.getExpressionString();
		if (null != v) {
			Matcher matcher = EL_EXPRESSION.matcher(v);
			if (matcher.find()) {
				String exp = matcher.group();
				return exp.substring(2, exp.length() - 1);
			}
			return v;
		}
	}
	return null;
}
 
Example 2
Source File: DataTableRenderer.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String resolveStaticField(ValueExpression expression) {
	if (expression != null) {
		String expressionString = expression.getExpressionString();
		expressionString = expressionString.substring(2, expressionString.length() - 1); // Remove #{}
		return expressionString.substring(expressionString.indexOf(".") + 1); // Remove var
	} else {
		return null;
	}
}
 
Example 3
Source File: ELTools.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * Which annotations are given to an object displayed by a JSF component?
 *
 * @param p_component
 *            the component
 * @return null if there are no annotations, or if they cannot be accessed
 */
public static Annotation[] readAnnotations(UIComponent p_component) {
	ValueExpression valueExpression = p_component.getValueExpression("value");
	if (valueExpression != null && valueExpression.getExpressionString() != null && valueExpression.getExpressionString().length()>0) {
		return readAnnotations(valueExpression, p_component);
	}
	return null;
}
 
Example 4
Source File: ELTools.java    From BootsFaces-OSP with Apache License 2.0 4 votes vote down vote up
private static Object evaluteBaseForMojarra(ELContext elContext, ValueExpression p_expression) {
	String exp = p_expression.getExpressionString();
	int endOfBaseName = exp.lastIndexOf('.');
	int mapDelimiterPos = exp.lastIndexOf('[');
	if (mapDelimiterPos >= 0) {
		int mapEndDelimiterPos = exp.lastIndexOf(']');
		if (endOfBaseName < mapEndDelimiterPos) {
			endOfBaseName = mapDelimiterPos; // treat the [...] as field
		}
	}

	if (endOfBaseName < 0) {
		LOGGER.log(Level.WARNING, "There's no getter to access: #{" + p_expression + "}");
		return null;
	}
	String basename = exp.substring(2, endOfBaseName);

	Object result = evalAsObject("#{" + basename + "}");
	if (null != result) {
		return result;
	}

	int start = 0;
	int end = basename.indexOf('.', start);
	int end2 = basename.indexOf('[', start);
	if (end2 >= 0 && end2 < end) {
		end = end2;
	}
	if (end < 0) {
		end = basename.length();
	}
	String variableName = basename.substring(start, end);
	FaceletContext faceletContext = (FaceletContext) FacesContext.getCurrentInstance().getAttributes()
			.get(FaceletContext.FACELET_CONTEXT_KEY);
	Object resolvedBase = faceletContext.getAttribute(variableName);
	if (resolvedBase != null) {
		if (endOfBaseName == end + 2) {
			result = resolvedBase;
		} else {
			basename = basename.substring(end + 1, endOfBaseName - 2);
			result = elContext.getELResolver().getValue(elContext, resolvedBase, basename);
		}
	}

	return result;
}
 
Example 5
Source File: RadiobuttonRenderer.java    From BootsFaces-OSP with Apache License 2.0 4 votes vote down vote up
/**
 * This methods generates the HTML code of the current b:radiobutton.
* <code>encodeBegin</code> generates the start of the component. After the, the JSF framework calls <code>encodeChildren()</code>
* to generate the HTML code between the beginning and the end of the component. For instance, in the case of a panel component
* the content of the panel is generated by <code>encodeChildren()</code>. After that, <code>encodeEnd()</code> is called
* to generate the rest of the HTML code.
 * @param context the FacesContext.
 * @param component the current b:radiobutton.
 * @throws IOException thrown if something goes wrong when writing the HTML code.
 */
@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    if (!component.isRendered()) {
        return;
    }
	Radiobutton radiobutton = (Radiobutton) component;
	ResponseWriter rw = context.getResponseWriter();
	String clientId = radiobutton.getClientId();

	ValueExpression valueExpression = radiobutton.getValueExpression("value");
	if (null == valueExpression) {
		throw new FacesException("Radiobuttons always need a value. More precisely, the value attribute must be an EL expression pointing to an attribute of a JSF bean.");
	}
	String propertyName = valueExpression.getExpressionString();
	Object beanValue = ELTools.evalAsObject(propertyName);
	if (propertyName.startsWith("#{") && propertyName.endsWith("}")) {
		propertyName=propertyName.substring(2, propertyName.length()-1).trim();
	} else {
		throw new FacesException("The value attribute of a radiobutton must be an EL expression.");
	}
	UIForm form = findSurroundingForm(component);
	if (null == form) {
		throw new FacesException("Radio buttons must be inside a form.");
	}
	
	String name = radiobutton.getName();
	if (null == name) {
		throw new FacesException("Please specify the 'name' attribute of the radio button.");
	}
	UIComponent radioButtonGroup = findComponentByName(form, name);
	String radiobuttonGroupId = radioButtonGroup.getClientId(context);

	RadioButtonInternalStateBean state = (RadioButtonInternalStateBean) ELTools.evalAsObject("#{radioButtonInternalStateBean}");
	
	String key = "BF_generated_radiobuttonfield_"+radiobuttonGroupId;
	if (!state.inputHasAlreadyBeenRendered(key)) {
		rw.startElement("input", null);
		rw.writeAttribute("id", "input_" + clientId, null);
		rw.writeAttribute("name", name, null);
		rw.writeAttribute("type",  "hidden", null);
		rw.writeAttribute("value", String.valueOf(beanValue), null);
		AJAXRenderer.generateBootsFacesAJAXAndJavaScript(FacesContext.getCurrentInstance(), radiobutton, rw, false);

		rw.endElement("input");

	}

	Converter converter = radiobutton.getConverter();
	List<SelectItemAndComponent> options = SelectItemUtils.collectOptions(context, component, converter);
	if (options.size()>0) {
		// traditional JSF approach using f:selectItem[s]
		int counter=0;
		for (SelectItemAndComponent option:options) {
			generateASingleRadioButton(context, component, radiobutton, rw, propertyName, beanValue,
					option.getSelectItem().getValue(),
					option.getSelectItem().getLabel(), clientId+(counter++), option.getSelectItem().isDisabled());

		}

	} else {
		// BootsFaces approach using b:radioButtons for each radiobutton item
		String itemValue = radiobutton.getItemValue();
		String itemLabel = radiobutton.getItemLabel();
		if (itemLabel == null) {
			itemLabel = radiobutton.getItemValue();
		}
		String itemId = clientId;

		generateASingleRadioButton(context, component, radiobutton, rw, propertyName, beanValue, itemValue,
				itemLabel, itemId, false);
	}
}