Java Code Examples for java.beans.PropertyEditor#getAsText()

The following examples show how to use java.beans.PropertyEditor#getAsText() . 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: ValueFormatter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Build the display value of the supplied {@code Object}, HTML escaped
 * as required. If the supplied value is not a {@link String} and the supplied
 * {@link PropertyEditor} is not null then the {@link PropertyEditor} is used
 * to obtain the display value.
 * @see #getDisplayString(Object, boolean)
 */
public static String getDisplayString(
		@Nullable Object value, @Nullable PropertyEditor propertyEditor, boolean htmlEscape) {

	if (propertyEditor != null && !(value instanceof String)) {
		try {
			propertyEditor.setValue(value);
			String text = propertyEditor.getAsText();
			if (text != null) {
				return getDisplayString(text, htmlEscape);
			}
		}
		catch (Throwable ex) {
			// The PropertyEditor might not support this value... pass through.
		}
	}
	return getDisplayString(value, htmlEscape);
}
 
Example 2
Source File: ValueFormatter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Build the display value of the supplied {@code Object}, HTML escaped
 * as required. If the supplied value is not a {@link String} and the supplied
 * {@link PropertyEditor} is not null then the {@link PropertyEditor} is used
 * to obtain the display value.
 * @see #getDisplayString(Object, boolean)
 */
public static String getDisplayString(Object value, PropertyEditor propertyEditor, boolean htmlEscape) {
	if (propertyEditor != null && !(value instanceof String)) {
		try {
			propertyEditor.setValue(value);
			String text = propertyEditor.getAsText();
			if (text != null) {
				return getDisplayString(text, htmlEscape);
			}
		}
		catch (Throwable ex) {
			// The PropertyEditor might not support this value... pass through.
		}
	}
	return getDisplayString(value, htmlEscape);
}
 
Example 3
Source File: ValueFormatter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Build the display value of the supplied {@code Object}, HTML escaped
 * as required. If the supplied value is not a {@link String} and the supplied
 * {@link PropertyEditor} is not null then the {@link PropertyEditor} is used
 * to obtain the display value.
 * @see #getDisplayString(Object, boolean)
 */
public static String getDisplayString(Object value, PropertyEditor propertyEditor, boolean htmlEscape) {
	if (propertyEditor != null && !(value instanceof String)) {
		try {
			propertyEditor.setValue(value);
			String text = propertyEditor.getAsText();
			if (text != null) {
				return getDisplayString(text, htmlEscape);
			}
		}
		catch (Throwable ex) {
			// The PropertyEditor might not support this value... pass through.
		}
	}
	return getDisplayString(value, htmlEscape);
}
 
Example 4
Source File: AbstractPropertyBindingResult.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats the field value based on registered PropertyEditors.
 * @see #getCustomEditor
 */
@Override
protected Object formatFieldValue(String field, Object value) {
	String fixedField = fixedField(field);
	// Try custom editor...
	PropertyEditor customEditor = getCustomEditor(fixedField);
	if (customEditor != null) {
		customEditor.setValue(value);
		String textValue = customEditor.getAsText();
		// If the PropertyEditor returned null, there is no appropriate
		// text representation for this value: only use it if non-null.
		if (textValue != null) {
			return textValue;
		}
	}
	if (this.conversionService != null) {
		// Try custom converter...
		TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
		TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
		if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, strDesc)) {
			return this.conversionService.convert(value, fieldDesc, strDesc);
		}
	}
	return value;
}
 
Example 5
Source File: EditablePropertyDisplayer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Object getEnteredValue() {
    Object result;

    if (getInplaceEditor() != null) {
        result = getInplaceEditor().getValue();
    } else {
        if (cachedInitialValue != NO_VALUE) {
            result = cachedInitialValue;
        } else {
            PropertyEditor ed = PropUtils.getPropertyEditor(getProperty());

            try {
                result = ed.getAsText();
            } catch (ProxyNode.DifferentValuesException dve) {
                result = null;
            }
        }
    }

    return result;
}
 
Example 6
Source File: GetAsTextStringEditor.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
@Override
public void setValue(Object value) {
  if (value == null || value instanceof String) {
    super.setValue(value);
  } else {
    PropertyEditor editor = editorRegistry.findCustomEditor(value.getClass(), null);
    if (editor == null) {
      editor = editorRegistrySupport.getDefaultEditor(value.getClass());
    }
    if (editor != null) {
      editor.setValue(value);
      super.setValue(editor.getAsText());
    } else if (Enum.class.isAssignableFrom(value.getClass())) {
      super.setValue(String.valueOf(value));
    } else {
      throw new IllegalArgumentException("Unable to convert " + value.getClass()
              + " to String. No PropertyEditor found.");
    }
  }
}
 
Example 7
Source File: ValueFormatter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Build the display value of the supplied {@code Object}, HTML escaped
 * as required. If the supplied value is not a {@link String} and the supplied
 * {@link PropertyEditor} is not null then the {@link PropertyEditor} is used
 * to obtain the display value.
 * @see #getDisplayString(Object, boolean)
 */
public static String getDisplayString(
		@Nullable Object value, @Nullable PropertyEditor propertyEditor, boolean htmlEscape) {

	if (propertyEditor != null && !(value instanceof String)) {
		try {
			propertyEditor.setValue(value);
			String text = propertyEditor.getAsText();
			if (text != null) {
				return getDisplayString(text, htmlEscape);
			}
		}
		catch (Throwable ex) {
			// The PropertyEditor might not support this value... pass through.
		}
	}
	return getDisplayString(value, htmlEscape);
}
 
Example 8
Source File: AbstractPropertyBindingResult.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Formats the field value based on registered PropertyEditors.
 * @see #getCustomEditor
 */
@Override
protected Object formatFieldValue(String field, @Nullable Object value) {
	String fixedField = fixedField(field);
	// Try custom editor...
	PropertyEditor customEditor = getCustomEditor(fixedField);
	if (customEditor != null) {
		customEditor.setValue(value);
		String textValue = customEditor.getAsText();
		// If the PropertyEditor returned null, there is no appropriate
		// text representation for this value: only use it if non-null.
		if (textValue != null) {
			return textValue;
		}
	}
	if (this.conversionService != null) {
		// Try custom converter...
		TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
		TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
		if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, strDesc)) {
			return this.conversionService.convert(value, fieldDesc, strDesc);
		}
	}
	return value;
}
 
Example 9
Source File: TransformTag.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected final int doStartTagInternal() throws JspException {
	if (this.value != null) {
		// Find the containing EditorAwareTag (e.g. BindTag), if applicable.
		EditorAwareTag tag = (EditorAwareTag) TagSupport.findAncestorWithClass(this, EditorAwareTag.class);
		if (tag == null) {
			throw new JspException("TransformTag can only be used within EditorAwareTag (e.g. BindTag)");
		}

		// OK, let's obtain the editor...
		String result = null;
		PropertyEditor editor = tag.getEditor();
		if (editor != null) {
			// If an editor was found, edit the value.
			editor.setValue(this.value);
			result = editor.getAsText();
		}
		else {
			// Else, just do a toString.
			result = this.value.toString();
		}
		result = htmlEscape(result);
		if (this.var != null) {
			this.pageContext.setAttribute(this.var, result, TagUtils.getScope(this.scope));
		}
		else {
			try {
				// Else, just print it out.
				this.pageContext.getOut().print(result);
			}
			catch (IOException ex) {
				throw new JspException(ex);
			}
		}
	}

	return SKIP_BODY;
}
 
Example 10
Source File: ObjectPropertyUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Looks up a property value, then convert to text using a registered property editor.
 *
 * @param bean bean instance to look up a property value for
 * @param path property path relative to the bean
 * @return The property value, converted to text using a registered property editor.
 */
public static String getPropertyValueAsText(Object bean, String path) {
    Object propertyValue = getPropertyValue(bean, path);
    PropertyEditor editor = getPropertyEditor(bean, path);

    if (editor == null) {
        return propertyValue == null ? null : propertyValue.toString();
    } else {
        editor.setValue(propertyValue);
        return editor.getAsText();
    }
}
 
Example 11
Source File: ResourceSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String getStringValue(FormProperty prop, Object value) {
    if (value instanceof String)
        return (String) value;

    PropertyEditor prEd = prop.getCurrentEditor();
    prEd.setValue(value);
    return prEd.getAsText(); // [this does not work correctly with IconEditor...]
}
 
Example 12
Source File: StringInplaceEditor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(PropertyEditor p, PropertyEnv env) {
    setActionCommand(COMMAND_SUCCESS);
    this.env = env;
    
    if(PropUtils.supportsValueIncrement( env ) ) {
        PropUtils.wrapUpDownArrowActions( this, this );
    }

    if (editor == p) {
        return;
    }

    editor = p;

    boolean editable = PropUtils.checkEnabled(this, p, env);
    setEnabled(editable);

    //Undocumented, but in NB 3.5 and earlier, getAsText() returning null for
    //paintable editors was yet another way to disable a property editor
    if ((p.getTags() == null) && (p.getAsText() == null) && p.isPaintable()) {
        editable = false;
    }

    setEditable(editable);
    reset();
    added = false;
}
 
Example 13
Source File: IntrospectionSupport.java    From tomee with Apache License 2.0 5 votes vote down vote up
private static String convertToString(final Object value, final Class type)
    throws URISyntaxException {
    final PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        editor.setValue(value);
        return editor.getAsText();
    }
    if (type == URI.class) {
        return ((URI) value).toString();
    }
    return null;
}
 
Example 14
Source File: BeanFactoryTypeConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (targetType.getType() == Void.class || targetType.getType() == Void.TYPE) {
		return null;
	}
	if (conversionService.canConvert(sourceType, targetType)) {
		return conversionService.convert(value, sourceType, targetType);
	}
	if (!String.class.isAssignableFrom(sourceType.getType())) {
		PropertyEditor editor = delegate.findCustomEditor(sourceType.getType(), null);
		editor.setValue(value);
		return editor.getAsText();
	}
	return delegate.convertIfNecessary(value, targetType.getType());
}
 
Example 15
Source File: TransformTag.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected final int doStartTagInternal() throws JspException {
	if (this.value != null) {
		// Find the containing EditorAwareTag (e.g. BindTag), if applicable.
		EditorAwareTag tag = (EditorAwareTag) TagSupport.findAncestorWithClass(this, EditorAwareTag.class);
		if (tag == null) {
			throw new JspException("TransformTag can only be used within EditorAwareTag (e.g. BindTag)");
		}

		// OK, let's obtain the editor...
		String result = null;
		PropertyEditor editor = tag.getEditor();
		if (editor != null) {
			// If an editor was found, edit the value.
			editor.setValue(this.value);
			result = editor.getAsText();
		}
		else {
			// Else, just do a toString.
			result = this.value.toString();
		}
		result = htmlEscape(result);
		if (this.var != null) {
			this.pageContext.setAttribute(this.var, result, TagUtils.getScope(this.scope));
		}
		else {
			try {
				// Else, just print it out.
				this.pageContext.getOut().print(result);
			}
			catch (IOException ex) {
				throw new JspException(ex);
			}
		}
	}

	return SKIP_BODY;
}
 
Example 16
Source File: TransformTag.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected final int doStartTagInternal() throws JspException {
	if (this.value != null) {
		// Find the containing EditorAwareTag (e.g. BindTag), if applicable.
		EditorAwareTag tag = (EditorAwareTag) TagSupport.findAncestorWithClass(this, EditorAwareTag.class);
		if (tag == null) {
			throw new JspException("TransformTag can only be used within EditorAwareTag (e.g. BindTag)");
		}

		// OK, let's obtain the editor...
		String result = null;
		PropertyEditor editor = tag.getEditor();
		if (editor != null) {
			// If an editor was found, edit the value.
			editor.setValue(this.value);
			result = editor.getAsText();
		}
		else {
			// Else, just do a toString.
			result = this.value.toString();
		}
		result = htmlEscape(result);
		if (this.var != null) {
			pageContext.setAttribute(this.var, result, TagUtils.getScope(this.scope));
		}
		else {
			try {
				// Else, just print it out.
				pageContext.getOut().print(result);
			}
			catch (IOException ex) {
				throw new JspException(ex);
			}
		}
	}

	return SKIP_BODY;
}
 
Example 17
Source File: ThreadSafePropertyEditor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public String getAsText(Object object) {
    PropertyEditor editor = fetchFromPool();
    try {
        editor.setValue(object);
        return editor.getAsText();
    } finally {
        pool.putInPool(editor);
    }
}
 
Example 18
Source File: TransformTag.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected final int doStartTagInternal() throws JspException {
	if (this.value != null) {
		// Find the containing EditorAwareTag (e.g. BindTag), if applicable.
		EditorAwareTag tag = (EditorAwareTag) TagSupport.findAncestorWithClass(this, EditorAwareTag.class);
		if (tag == null) {
			throw new JspException("TransformTag can only be used within EditorAwareTag (e.g. BindTag)");
		}

		// OK, let's obtain the editor...
		String result = null;
		PropertyEditor editor = tag.getEditor();
		if (editor != null) {
			// If an editor was found, edit the value.
			editor.setValue(this.value);
			result = editor.getAsText();
		}
		else {
			// Else, just do a toString.
			result = this.value.toString();
		}
		result = htmlEscape(result);
		if (this.var != null) {
			pageContext.setAttribute(this.var, result, TagUtils.getScope(this.scope));
		}
		else {
			try {
				// Else, just print it out.
				pageContext.getOut().print(result);
			}
			catch (IOException ex) {
				throw new JspException(ex);
			}
		}
	}

	return SKIP_BODY;
}
 
Example 19
Source File: BeanFactoryTypeConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (targetType.getType() == Void.class || targetType.getType() == Void.TYPE) {
		return null;
	}
	if (conversionService.canConvert(sourceType, targetType)) {
		return conversionService.convert(value, sourceType, targetType);
	}
	if (!String.class.isAssignableFrom(sourceType.getType())) {
		PropertyEditor editor = delegate.findCustomEditor(sourceType.getType(), null);
		editor.setValue(value);
		return editor.getAsText();
	}
	return delegate.convertIfNecessary(value, targetType.getType());
}
 
Example 20
Source File: XmlDocumentWriter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void writeAttributes( final ReportAttributeMap attributes, ElementType type ) throws IOException {
  if ( type == null ) {
    type = AutoLayoutBoxType.INSTANCE;
  }

  final Set<AttributeMap.DualKey> collection = attributes.keySet();
  final AttributeMap.DualKey[] attributeNames = collection.toArray( new AttributeMap.DualKey[ collection.size() ] );
  Arrays.sort( attributeNames, new DualKeySorter() );
  for ( int j = 0; j < attributeNames.length; j++ ) {

    final String namespace = attributeNames[ j ].namespace;
    if ( AttributeNames.Designtime.NAMESPACE.equals( namespace ) ) {
      continue;
    }

    final String name = attributeNames[ j ].name;
    final Object value = attributes.getAttribute( namespace, name );
    if ( value == null ) {
      continue;
    }

    if ( metaData.isFeatureSupported( XmlTableOutputProcessorMetaData.WRITE_RESOURCEKEYS ) == false
      && value instanceof ResourceKey ) {
      continue;
    }


    final AttributeMetaData attrMeta = type.getMetaData().getAttributeDescription( namespace, name );
    if ( attrMeta == null ) {
      // if you want to use attributes in this output target, declare the attribute's metadata first.
      continue;
    }

    final AttributeList attList = new AttributeList();
    if ( value instanceof String ) {
      final String s = (String) value;
      if ( StringUtils.isEmpty( s ) ) {
        continue;
      }

      if ( xmlWriter.isNamespaceDefined( namespace ) == false
        && attList.isNamespaceUriDefined( namespace ) == false ) {
        attList.addNamespaceDeclaration( "autoGenNs", namespace );
      }

      // preserve strings, but discard anything else. Until a attribute has a definition, we cannot
      // hope to understand the attribute's value. String-attributes can be expressed in XML easily,
      // and string is also how all unknown attributes are stored by the parser.
      attList.setAttribute( namespace, name, s );
      this.xmlWriter.writeTag( XmlDocumentWriter.LAYOUT_OUTPUT_NAMESPACE, "attribute", attList, XmlWriter.CLOSE );
      continue;
    }

    if ( xmlWriter.isNamespaceDefined( namespace ) == false
      && attList.isNamespaceUriDefined( namespace ) == false ) {
      attList.addNamespaceDeclaration( "autoGenNs", namespace );
    }

    try {
      final PropertyEditor propertyEditor = attrMeta.getEditor();
      final String textValue;
      if ( propertyEditor != null ) {
        propertyEditor.setValue( value );
        textValue = propertyEditor.getAsText();
      } else {
        textValue = ConverterRegistry.toAttributeValue( value );
      }

      if ( textValue != null ) {
        if ( "".equals( textValue ) == false ) {
          attList.setAttribute( namespace, name, textValue );
          this.xmlWriter
            .writeTag( XmlDocumentWriter.LAYOUT_OUTPUT_NAMESPACE, "attribute", attList, XmlWriter.CLOSE );
        }
      } else {
        if ( value instanceof ResourceKey ) {
          final ResourceKey reskey = (ResourceKey) value;
          final String identifierAsString = reskey.getIdentifierAsString();
          attList.setAttribute( namespace, name, "resource-key:" + reskey.getSchema() + ":" + identifierAsString );
          this.xmlWriter
            .writeTag( XmlDocumentWriter.LAYOUT_OUTPUT_NAMESPACE, "attribute", attList, XmlWriter.CLOSE );
        } else {
          XmlDocumentWriter.logger.debug(
            "Attribute '" + namespace + '|' + name + "' is not convertible to a text - returned null: " + value );
        }
      }
    } catch ( BeanException e ) {
      if ( attrMeta.isTransient() == false ) {
        XmlDocumentWriter.logger.warn(
          "Attribute '" + namespace + '|' + name + "' is not convertible with the bean-methods" );
      } else {
        XmlDocumentWriter.logger.debug(
          "Attribute '" + namespace + '|' + name + "' is not convertible with the bean-methods" );
      }
    }
  }
}