Search in sources :

Example 46 with PropertyEditor

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()));
        }
    }
}
Also used : JasperException(org.apache.jasper.JasperException) PropertyEditor(java.beans.PropertyEditor) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) JspException(jakarta.servlet.jsp.JspException) JasperException(org.apache.jasper.JasperException)

Example 47 with PropertyEditor

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();
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Example 48 with PropertyEditor

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);
}
Also used : ArrayList(java.util.ArrayList) CharArrayPropertyEditor(org.springframework.beans.propertyeditors.CharArrayPropertyEditor) ResourceArrayPropertyEditor(org.springframework.core.io.support.ResourceArrayPropertyEditor) StringArrayPropertyEditor(org.springframework.beans.propertyeditors.StringArrayPropertyEditor) ByteArrayPropertyEditor(org.springframework.beans.propertyeditors.ByteArrayPropertyEditor) PropertyEditor(java.beans.PropertyEditor) Nullable(org.springframework.lang.Nullable)

Example 49 with PropertyEditor

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;
}
Also used : CharArrayPropertyEditor(org.springframework.beans.propertyeditors.CharArrayPropertyEditor) ResourceArrayPropertyEditor(org.springframework.core.io.support.ResourceArrayPropertyEditor) StringArrayPropertyEditor(org.springframework.beans.propertyeditors.StringArrayPropertyEditor) ByteArrayPropertyEditor(org.springframework.beans.propertyeditors.ByteArrayPropertyEditor) PropertyEditor(java.beans.PropertyEditor) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Nullable(org.springframework.lang.Nullable)

Example 50 with PropertyEditor

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();
}
Also used : Pattern(java.util.regex.Pattern) PropertyEditor(java.beans.PropertyEditor) Test(org.junit.jupiter.api.Test)

Aggregations

PropertyEditor (java.beans.PropertyEditor)167 Test (org.junit.jupiter.api.Test)59 HashMap (java.util.HashMap)14 Test (org.junit.Test)13 Map (java.util.Map)11 File (java.io.File)10 URI (java.net.URI)9 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 IOException (java.io.IOException)7 Path (java.nio.file.Path)7 Resource (org.springframework.core.io.Resource)7 Method (java.lang.reflect.Method)6 URL (java.net.URL)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 PropertyDescriptor (java.beans.PropertyDescriptor)5 Field (java.lang.reflect.Field)4 ArrayList (java.util.ArrayList)4 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 StringArrayPropertyEditor (org.springframework.beans.propertyeditors.StringArrayPropertyEditor)4