Java Code Examples for org.springframework.util.ObjectUtils#getDisplayString()

The following examples show how to use org.springframework.util.ObjectUtils#getDisplayString() . 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: SelectedValueComparator.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static boolean exhaustiveCompare(@Nullable Object boundValue, @Nullable Object candidate,
		@Nullable PropertyEditor editor, @Nullable Map<PropertyEditor, Object> convertedValueCache) {

	String candidateDisplayString = ValueFormatter.getDisplayString(candidate, editor, false);
	if (boundValue != null && boundValue.getClass().isEnum()) {
		Enum<?> boundEnum = (Enum<?>) boundValue;
		String enumCodeAsString = ObjectUtils.getDisplayString(boundEnum.name());
		if (enumCodeAsString.equals(candidateDisplayString)) {
			return true;
		}
		String enumLabelAsString = ObjectUtils.getDisplayString(boundEnum.toString());
		if (enumLabelAsString.equals(candidateDisplayString)) {
			return true;
		}
	}
	else if (ObjectUtils.getDisplayString(boundValue).equals(candidateDisplayString)) {
		return true;
	}

	if (editor != null && candidate instanceof String) {
		// Try PE-based comparison (PE should *not* be allowed to escape creating thread)
		String candidateAsString = (String) candidate;
		Object candidateAsValue;
		if (convertedValueCache != null && convertedValueCache.containsKey(editor)) {
			candidateAsValue = convertedValueCache.get(editor);
		}
		else {
			editor.setAsText(candidateAsString);
			candidateAsValue = editor.getValue();
			if (convertedValueCache != null) {
				convertedValueCache.put(editor, candidateAsValue);
			}
		}
		if (ObjectUtils.nullSafeEquals(boundValue, candidateAsValue)) {
			return true;
		}
	}
	return false;
}
 
Example 2
Source File: OptionsTag.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
	SelectTag selectTag = getSelectTag();
	Object items = getItems();
	Object itemsObject = null;
	if (items != null) {
		itemsObject = (items instanceof String ? evaluate("items", items) : items);
	}
	else {
		Class<?> selectTagBoundType = selectTag.getBindStatus().getValueType();
		if (selectTagBoundType != null && selectTagBoundType.isEnum()) {
			itemsObject = selectTagBoundType.getEnumConstants();
		}
	}
	if (itemsObject != null) {
		String selectName = selectTag.getName();
		String itemValue = getItemValue();
		String itemLabel = getItemLabel();
		String valueProperty =
				(itemValue != null ? ObjectUtils.getDisplayString(evaluate("itemValue", itemValue)) : null);
		String labelProperty =
				(itemLabel != null ? ObjectUtils.getDisplayString(evaluate("itemLabel", itemLabel)) : null);
		OptionsWriter optionWriter = new OptionsWriter(selectName, itemsObject, valueProperty, labelProperty);
		optionWriter.writeOptions(tagWriter);
	}
	return SKIP_BODY;
}
 
Example 3
Source File: ErrorsTag.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void renderDefaultContent(TagWriter tagWriter) throws JspException {
	tagWriter.startTag(getElement());
	writeDefaultAttributes(tagWriter);
	String delimiter = ObjectUtils.getDisplayString(evaluate("delimiter", getDelimiter()));
	String[] errorMessages = getBindStatus().getErrorMessages();
	for (int i = 0; i < errorMessages.length; i++) {
		String errorMessage = errorMessages[i];
		if (i > 0) {
			tagWriter.appendValue(delimiter);
		}
		tagWriter.appendValue(getDisplayString(errorMessage));
	}
	tagWriter.endTag();
}
 
Example 4
Source File: SelectedValueComparator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static boolean exhaustiveCompare(Object boundValue, Object candidate,
		PropertyEditor editor, Map<PropertyEditor, Object> convertedValueCache) {

	String candidateDisplayString = ValueFormatter.getDisplayString(candidate, editor, false);
	if (boundValue != null && boundValue.getClass().isEnum()) {
		Enum<?> boundEnum = (Enum<?>) boundValue;
		String enumCodeAsString = ObjectUtils.getDisplayString(boundEnum.name());
		if (enumCodeAsString.equals(candidateDisplayString)) {
			return true;
		}
		String enumLabelAsString = ObjectUtils.getDisplayString(boundEnum.toString());
		if (enumLabelAsString.equals(candidateDisplayString)) {
			return true;
		}
	}
	else if (ObjectUtils.getDisplayString(boundValue).equals(candidateDisplayString)) {
		return true;
	}
	else if (editor != null && candidate instanceof String) {
		// Try PE-based comparison (PE should *not* be allowed to escape creating thread)
		String candidateAsString = (String) candidate;
		Object candidateAsValue;
		if (convertedValueCache != null && convertedValueCache.containsKey(editor)) {
			candidateAsValue = convertedValueCache.get(editor);
		}
		else {
			editor.setAsText(candidateAsString);
			candidateAsValue = editor.getValue();
			if (convertedValueCache != null) {
				convertedValueCache.put(editor, candidateAsValue);
			}
		}
		if (ObjectUtils.nullSafeEquals(boundValue, candidateAsValue)) {
			return true;
		}
	}
	return false;
}
 
Example 5
Source File: OptionsTag.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
	SelectTag selectTag = getSelectTag();
	Object items = getItems();
	Object itemsObject = null;
	if (items != null) {
		itemsObject = (items instanceof String ? evaluate("items", items) : items);
	}
	else {
		Class<?> selectTagBoundType = selectTag.getBindStatus().getValueType();
		if (selectTagBoundType != null && selectTagBoundType.isEnum()) {
			itemsObject = selectTagBoundType.getEnumConstants();
		}
	}
	if (itemsObject != null) {
		String selectName = selectTag.getName();
		String itemValue = getItemValue();
		String itemLabel = getItemLabel();
		String valueProperty =
				(itemValue != null ? ObjectUtils.getDisplayString(evaluate("itemValue", itemValue)) : null);
		String labelProperty =
				(itemLabel != null ? ObjectUtils.getDisplayString(evaluate("itemLabel", itemLabel)) : null);
		OptionsWriter optionWriter = new OptionsWriter(selectName, itemsObject, valueProperty, labelProperty);
		optionWriter.writeOptions(tagWriter);
	}
	return SKIP_BODY;
}
 
Example 6
Source File: ErrorsTag.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void renderDefaultContent(TagWriter tagWriter) throws JspException {
	tagWriter.startTag(getElement());
	writeDefaultAttributes(tagWriter);
	String delimiter = ObjectUtils.getDisplayString(evaluate("delimiter", getDelimiter()));
	String[] errorMessages = getBindStatus().getErrorMessages();
	for (int i = 0; i < errorMessages.length; i++) {
		String errorMessage = errorMessages[i];
		if (i > 0) {
			tagWriter.appendValue(delimiter);
		}
		tagWriter.appendValue(getDisplayString(errorMessage));
	}
	tagWriter.endTag();
}
 
Example 7
Source File: OptionsTag.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
	SelectTag selectTag = getSelectTag();
	Object items = getItems();
	Object itemsObject = null;
	if (items != null) {
		itemsObject = (items instanceof String ? evaluate("items", items) : items);
	}
	else {
		Class<?> selectTagBoundType = selectTag.getBindStatus().getValueType();
		if (selectTagBoundType != null && selectTagBoundType.isEnum()) {
			itemsObject = selectTagBoundType.getEnumConstants();
		}
	}
	if (itemsObject != null) {
		String selectName = selectTag.getName();
		String itemValue = getItemValue();
		String itemLabel = getItemLabel();
		String valueProperty =
				(itemValue != null ? ObjectUtils.getDisplayString(evaluate("itemValue", itemValue)) : null);
		String labelProperty =
				(itemLabel != null ? ObjectUtils.getDisplayString(evaluate("itemLabel", itemLabel)) : null);
		OptionsWriter optionWriter = new OptionsWriter(selectName, itemsObject, valueProperty, labelProperty);
		optionWriter.writeOptions(tagWriter);
	}
	return SKIP_BODY;
}
 
Example 8
Source File: AbstractHtmlElementTag.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Gets the appropriate CSS class to use based on the state of the current
 * {@link org.springframework.web.servlet.support.BindStatus} object.
 */
protected String resolveCssClass() throws JspException {
	if (getBindStatus().isError() && StringUtils.hasText(getCssErrorClass())) {
		return ObjectUtils.getDisplayString(evaluate("cssErrorClass", getCssErrorClass()));
	}
	else {
		return ObjectUtils.getDisplayString(evaluate("cssClass", getCssClass()));
	}
}
 
Example 9
Source File: ErrorsTag.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void renderDefaultContent(TagWriter tagWriter) throws JspException {
	tagWriter.startTag(getElement());
	writeDefaultAttributes(tagWriter);
	String delimiter = ObjectUtils.getDisplayString(evaluate("delimiter", getDelimiter()));
	String[] errorMessages = getBindStatus().getErrorMessages();
	for (int i = 0; i < errorMessages.length; i++) {
		String errorMessage = errorMessages[i];
		if (i > 0) {
			tagWriter.appendValue(delimiter);
		}
		tagWriter.appendValue(getDisplayString(errorMessage));
	}
	tagWriter.endTag();
}
 
Example 10
Source File: SelectedValueComparator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static boolean exhaustiveCompare(@Nullable Object boundValue, @Nullable Object candidate,
		@Nullable PropertyEditor editor, @Nullable Map<PropertyEditor, Object> convertedValueCache) {

	String candidateDisplayString = ValueFormatter.getDisplayString(candidate, editor, false);
	if (boundValue != null && boundValue.getClass().isEnum()) {
		Enum<?> boundEnum = (Enum<?>) boundValue;
		String enumCodeAsString = ObjectUtils.getDisplayString(boundEnum.name());
		if (enumCodeAsString.equals(candidateDisplayString)) {
			return true;
		}
		String enumLabelAsString = ObjectUtils.getDisplayString(boundEnum.toString());
		if (enumLabelAsString.equals(candidateDisplayString)) {
			return true;
		}
	}
	else if (ObjectUtils.getDisplayString(boundValue).equals(candidateDisplayString)) {
		return true;
	}

	if (editor != null && candidate instanceof String) {
		// Try PE-based comparison (PE should *not* be allowed to escape creating thread)
		String candidateAsString = (String) candidate;
		Object candidateAsValue;
		if (convertedValueCache != null && convertedValueCache.containsKey(editor)) {
			candidateAsValue = convertedValueCache.get(editor);
		}
		else {
			editor.setAsText(candidateAsString);
			candidateAsValue = editor.getValue();
			if (convertedValueCache != null) {
				convertedValueCache.put(editor, candidateAsValue);
			}
		}
		if (ObjectUtils.nullSafeEquals(boundValue, candidateAsValue)) {
			return true;
		}
	}
	return false;
}
 
Example 11
Source File: SelectedValueComparator.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private static boolean exhaustiveCompare(Object boundValue, Object candidate,
		PropertyEditor editor, Map<PropertyEditor, Object> convertedValueCache) {

	String candidateDisplayString = ValueFormatter.getDisplayString(candidate, editor, false);
	if (boundValue != null && boundValue.getClass().isEnum()) {
		Enum<?> boundEnum = (Enum<?>) boundValue;
		String enumCodeAsString = ObjectUtils.getDisplayString(boundEnum.name());
		if (enumCodeAsString.equals(candidateDisplayString)) {
			return true;
		}
		String enumLabelAsString = ObjectUtils.getDisplayString(boundEnum.toString());
		if (enumLabelAsString.equals(candidateDisplayString)) {
			return true;
		}
	}
	else if (ObjectUtils.getDisplayString(boundValue).equals(candidateDisplayString)) {
		return true;
	}
	else if (editor != null && candidate instanceof String) {
		// Try PE-based comparison (PE should *not* be allowed to escape creating thread)
		String candidateAsString = (String) candidate;
		Object candidateAsValue;
		if (convertedValueCache != null && convertedValueCache.containsKey(editor)) {
			candidateAsValue = convertedValueCache.get(editor);
		}
		else {
			editor.setAsText(candidateAsString);
			candidateAsValue = editor.getValue();
			if (convertedValueCache != null) {
				convertedValueCache.put(editor, candidateAsValue);
			}
		}
		if (ObjectUtils.nullSafeEquals(boundValue, candidateAsValue)) {
			return true;
		}
	}
	return false;
}
 
Example 12
Source File: ValueFormatter.java    From es with Apache License 2.0 4 votes vote down vote up
public static String getDisplayString(Object value, boolean htmlEscape) {
    String displayValue = ObjectUtils.getDisplayString(value);
    return (htmlEscape ? HtmlUtils.htmlEscape(displayValue) : displayValue);
}
 
Example 13
Source File: SelectTag.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Renders the HTML '{@code select}' tag to the supplied
 * {@link TagWriter}.
 * <p>Renders nested '{@code option}' tags if the
 * {@link #setItems items} property is set, otherwise exposes the
 * bound value for the nested {@link OptionTag OptionTags}.
 */
@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
	tagWriter.startTag("select");
	writeDefaultAttributes(tagWriter);
	if (isMultiple()) {
		tagWriter.writeAttribute("multiple", "multiple");
	}
	tagWriter.writeOptionalAttributeValue("size", getDisplayString(evaluate("size", getSize())));

	Object items = getItems();
	if (items != null) {
		// Items specified, but might still be empty...
		if (items != EMPTY) {
			Object itemsObject = evaluate("items", items);
			if (itemsObject != null) {
				final String selectName = getName();
				String valueProperty = (getItemValue() != null ?
						ObjectUtils.getDisplayString(evaluate("itemValue", getItemValue())) : null);
				String labelProperty = (getItemLabel() != null ?
						ObjectUtils.getDisplayString(evaluate("itemLabel", getItemLabel())) : null);
				OptionWriter optionWriter =
						new OptionWriter(itemsObject, getBindStatus(), valueProperty, labelProperty, isHtmlEscape()) {
							@Override
							protected String processOptionValue(String resolvedValue) {
								return processFieldValue(selectName, resolvedValue, "option");
							}
						};
				optionWriter.writeOptions(tagWriter);
			}
		}
		tagWriter.endTag(true);
		writeHiddenTagIfNecessary(tagWriter);
		return SKIP_BODY;
	}
	else {
		// Using nested <form:option/> tags, so just expose the value in the PageContext...
		tagWriter.forceBlock();
		this.tagWriter = tagWriter;
		this.pageContext.setAttribute(LIST_VALUE_PAGE_ATTRIBUTE, getBindStatus());
		return EVAL_BODY_INCLUDE;
	}
}
 
Example 14
Source File: SelectTag.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Renders the HTML '{@code select}' tag to the supplied
 * {@link TagWriter}.
 * <p>Renders nested '{@code option}' tags if the
 * {@link #setItems items} property is set, otherwise exposes the
 * bound value for the nested {@link OptionTag OptionTags}.
 */
@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
	tagWriter.startTag("select");
	writeDefaultAttributes(tagWriter);
	if (isMultiple()) {
		tagWriter.writeAttribute("multiple", "multiple");
	}
	tagWriter.writeOptionalAttributeValue("size", getDisplayString(evaluate("size", getSize())));

	Object items = getItems();
	if (items != null) {
		// Items specified, but might still be empty...
		if (items != EMPTY) {
			Object itemsObject = evaluate("items", items);
			if (itemsObject != null) {
				final String selectName = getName();
				String valueProperty = (getItemValue() != null ?
						ObjectUtils.getDisplayString(evaluate("itemValue", getItemValue())) : null);
				String labelProperty = (getItemLabel() != null ?
						ObjectUtils.getDisplayString(evaluate("itemLabel", getItemLabel())) : null);
				OptionWriter optionWriter =
						new OptionWriter(itemsObject, getBindStatus(), valueProperty, labelProperty, isHtmlEscape()) {
							@Override
							protected String processOptionValue(String resolvedValue) {
								return processFieldValue(selectName, resolvedValue, "option");
							}
						};
				optionWriter.writeOptions(tagWriter);
			}
		}
		tagWriter.endTag(true);
		writeHiddenTagIfNecessary(tagWriter);
		return SKIP_BODY;
	}
	else {
		// Using nested <form:option/> tags, so just expose the value in the PageContext...
		tagWriter.forceBlock();
		this.tagWriter = tagWriter;
		this.pageContext.setAttribute(LIST_VALUE_PAGE_ATTRIBUTE, getBindStatus());
		return EVAL_BODY_INCLUDE;
	}
}
 
Example 15
Source File: FormTag.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Override resolve CSS class since error class is not supported.
 */
@Override
protected String resolveCssClass() throws JspException {
	return ObjectUtils.getDisplayString(evaluate("cssClass", getCssClass()));
}
 
Example 16
Source File: FormTag.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Override resolve CSS class since error class is not supported.
 */
@Override
protected String resolveCssClass() throws JspException {
	return ObjectUtils.getDisplayString(evaluate("cssClass", getCssClass()));
}
 
Example 17
Source File: FormTag.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Override resolve CSS class since error class is not supported.
 */
@Override
protected String resolveCssClass() throws JspException {
	return ObjectUtils.getDisplayString(evaluate("cssClass", getCssClass()));
}
 
Example 18
Source File: SelectTag.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Renders the HTML '{@code select}' tag to the supplied
 * {@link TagWriter}.
 * <p>Renders nested '{@code option}' tags if the
 * {@link #setItems items} property is set, otherwise exposes the
 * bound value for the nested {@link OptionTag OptionTags}.
 */
@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {
	tagWriter.startTag("select");
	writeDefaultAttributes(tagWriter);
	if (isMultiple()) {
		tagWriter.writeAttribute("multiple", "multiple");
	}
	tagWriter.writeOptionalAttributeValue("size", getDisplayString(evaluate("size", getSize())));

	Object items = getItems();
	if (items != null) {
		// Items specified, but might still be empty...
		if (items != EMPTY) {
			Object itemsObject = evaluate("items", items);
			if (itemsObject != null) {
				final String selectName = getName();
				String valueProperty = (getItemValue() != null ?
						ObjectUtils.getDisplayString(evaluate("itemValue", getItemValue())) : null);
				String labelProperty = (getItemLabel() != null ?
						ObjectUtils.getDisplayString(evaluate("itemLabel", getItemLabel())) : null);
				OptionWriter optionWriter =
						new OptionWriter(itemsObject, getBindStatus(), valueProperty, labelProperty, isHtmlEscape()) {
							@Override
							protected String processOptionValue(String resolvedValue) {
								return processFieldValue(selectName, resolvedValue, "option");
							}
						};
				optionWriter.writeOptions(tagWriter);
			}
		}
		tagWriter.endTag(true);
		writeHiddenTagIfNecessary(tagWriter);
		return SKIP_BODY;
	}
	else {
		// Using nested <form:option/> tags, so just expose the value in the PageContext...
		tagWriter.forceBlock();
		this.tagWriter = tagWriter;
		this.pageContext.setAttribute(LIST_VALUE_PAGE_ATTRIBUTE, getBindStatus());
		return EVAL_BODY_INCLUDE;
	}
}
 
Example 19
Source File: ValueFormatter.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Build the display value of the supplied {@code Object}, HTML escaped
 * as required. This version is <strong>not</strong> {@link PropertyEditor}-aware.
 * @see #getDisplayString(Object, java.beans.PropertyEditor, boolean)
 */
public static String getDisplayString(Object value, boolean htmlEscape) {
	String displayValue = ObjectUtils.getDisplayString(value);
	return (htmlEscape ? HtmlUtils.htmlEscape(displayValue) : displayValue);
}
 
Example 20
Source File: ValueFormatter.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Build the display value of the supplied {@code Object}, HTML escaped
 * as required. This version is <strong>not</strong> {@link PropertyEditor}-aware.
 * @see #getDisplayString(Object, java.beans.PropertyEditor, boolean)
 */
public static String getDisplayString(Object value, boolean htmlEscape) {
	String displayValue = ObjectUtils.getDisplayString(value);
	return (htmlEscape ? HtmlUtils.htmlEscape(displayValue) : displayValue);
}