下面列出了java.beans.PropertyEditor#getAsText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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);
}
/**
* 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);
}
@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.");
}
}
}
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;
}
/**
* 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;
}
/**
* 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);
}
/**
* 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);
}
@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;
}
@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());
}
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;
}
@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;
}
/**
* 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();
}
}
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...]
}
@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;
}
@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;
}
public String getAsText(Object object) {
PropertyEditor editor = fetchFromPool();
try {
editor.setValue(object);
return editor.getAsText();
} finally {
pool.putInPool(editor);
}
}
@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;
}
@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());
}
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" );
}
}
}
}