use of java.beans.PropertyEditor in project tomcat by apache.
the class JspRuntimeLibrary method getValueFromBeanInfoPropertyEditor.
// *********************************************************************
// PropertyEditor Support
public static Object getValueFromBeanInfoPropertyEditor(Class<?> attrClass, String attrName, String attrValue, Class<?> propertyEditorClass) throws JasperException {
try {
PropertyEditor pe = (PropertyEditor) propertyEditorClass.getConstructor().newInstance();
pe.setAsText(attrValue);
return pe.getValue();
} catch (Exception ex) {
if (attrValue.length() == 0) {
return null;
} else {
throw new JasperException(Localizer.getMessage("jsp.error.beans.property.conversion", attrValue, attrClass.getName(), attrName, ex.getMessage()));
}
}
}
use of java.beans.PropertyEditor in project Smartcity-Smarthouse by TechnionYP5777.
the class StringConverter method convert.
/**
* Convert a string into any class
*
* @param targetType
* The class
* @param text
* The string
* @return the new converted Object
*
* [[SuppressWarningsSpartan]]
*/
public static Object convert(final Class<?> targetType, final String text) {
if (targetType.isPrimitive()) {
if (text == null)
return targetType.equals(Character.TYPE) ? '\0' : targetType.equals(Boolean.TYPE) ? false : 0;
if (targetType.equals(Character.TYPE))
return text.charAt(0);
}
final PropertyEditor editor = PropertyEditorManager.findEditor(targetType);
if (editor == null) {
log.warn("\n\tConverting an uknown Object to null: [targetType: " + targetType.getCanonicalName() + "]");
return null;
}
editor.setAsText(text);
return editor.getValue();
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class PropertyEditorRegistrySupport method findCustomEditor.
@Override
@Nullable
public PropertyEditor findCustomEditor(@Nullable Class<?> requiredType, @Nullable String propertyPath) {
Class<?> requiredTypeToUse = requiredType;
if (propertyPath != null) {
if (this.customEditorsForPath != null) {
// Check property-specific editor first.
PropertyEditor editor = getCustomEditor(propertyPath, requiredType);
if (editor == null) {
List<String> strippedPaths = new ArrayList<>();
addStrippedPropertyPaths(strippedPaths, "", propertyPath);
for (Iterator<String> it = strippedPaths.iterator(); it.hasNext() && editor == null; ) {
String strippedPath = it.next();
editor = getCustomEditor(strippedPath, requiredType);
}
}
if (editor != null) {
return editor;
}
}
if (requiredType == null) {
requiredTypeToUse = getPropertyType(propertyPath);
}
}
// No property-specific editor -> check type-specific editor.
return getCustomEditor(requiredTypeToUse);
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class PropertyEditorRegistrySupport method getCustomEditor.
/**
* Get custom editor for the given type. If no direct match found,
* try custom editor for superclass (which will in any case be able
* to render a value as String via {@code getAsText}).
* @param requiredType the type to look for
* @return the custom editor, or {@code null} if none found for this type
* @see java.beans.PropertyEditor#getAsText()
*/
@Nullable
private PropertyEditor getCustomEditor(@Nullable Class<?> requiredType) {
if (requiredType == null || this.customEditors == null) {
return null;
}
// Check directly registered editor for type.
PropertyEditor editor = this.customEditors.get(requiredType);
if (editor == null) {
// Check cached editor for type, registered for superclass or interface.
if (this.customEditorCache != null) {
editor = this.customEditorCache.get(requiredType);
}
if (editor == null) {
// Find editor for superclass or interface.
for (Map.Entry<Class<?>, PropertyEditor> entry : this.customEditors.entrySet()) {
Class<?> key = entry.getKey();
if (key.isAssignableFrom(requiredType)) {
editor = entry.getValue();
// of repeated assignable-from checks.
if (this.customEditorCache == null) {
this.customEditorCache = new HashMap<>();
}
this.customEditorCache.put(requiredType, editor);
if (editor != null) {
break;
}
}
}
}
}
return editor;
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class CustomEditorTests method testPatternEditor.
@Test
void testPatternEditor() {
final String REGEX = "a.*";
PropertyEditor patternEditor = new PatternEditor();
patternEditor.setAsText(REGEX);
assertThat(((Pattern) patternEditor.getValue()).pattern()).isEqualTo(Pattern.compile(REGEX).pattern());
assertThat(patternEditor.getAsText()).isEqualTo(REGEX);
patternEditor = new PatternEditor();
assertThat(patternEditor.getAsText()).isEmpty();
patternEditor = new PatternEditor();
patternEditor.setAsText(null);
assertThat(patternEditor.getAsText()).isEmpty();
}
Aggregations