List of usage examples for org.springframework.beans BeanWrapper findCustomEditor
@Nullable PropertyEditor findCustomEditor(@Nullable Class<?> requiredType, @Nullable String propertyPath);
From source file:net.solarnetwork.web.support.SimpleCsvHttpMessageConverter.java
private Object getRowPropertyValue(Object row, String name, Object val, BeanWrapper wrapper) { if (val != null) { if (getPropertySerializerRegistrar() != null) { val = getPropertySerializerRegistrar().serializeProperty(name, val.getClass(), row, val); } else if (wrapper != null) { // Spring does not apply PropertyEditors on read methods, so manually handle PropertyEditor editor = wrapper.findCustomEditor(null, name); if (editor != null) { editor.setValue(val); val = editor.getAsText(); }/*from w ww. j av a 2 s . com*/ } if (val instanceof Enum<?> || javaBeanTreatAsStringValues != null && javaBeanTreatAsStringValues.contains(val.getClass())) { val = val.toString(); } } return val; }
From source file:com.alibaba.citrus.service.form.impl.GroupImpl.java
/** * fields//from w w w. j a v a 2 s . co m * <p> * <code>isValidated()</code><code>true</code>group * </p> */ public void mapTo(Object object) { if (isValidated() || object == null) { return; } if (log.isDebugEnabled()) { log.debug("Mapping properties to fields: group=\"{}\", object={}", getName(), ObjectUtil.identityToString(object)); } BeanWrapper bean = new BeanWrapperImpl(object); getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean); for (Field field : getFields()) { String propertyName = field.getFieldConfig().getPropertyName(); if (bean.isReadableProperty(propertyName)) { Object propertyValue = bean.getPropertyValue(propertyName); Class<?> propertyType = bean.getPropertyType(propertyName); PropertyEditor editor = bean.findCustomEditor(propertyType, propertyName); if (editor == null) { editor = BeanUtils.findEditorByConvention(propertyType); } if (editor == null) { if (propertyType.isArray() || CollectionFactory.isApproximableCollectionType(propertyType)) { field.setValues((String[]) bean.convertIfNecessary(propertyValue, String[].class)); } else { field.setValue(bean.convertIfNecessary(propertyValue, String.class)); } } else { editor.setValue(propertyValue); field.setValue(editor.getAsText()); } } else { log.debug("No readable property \"{}\" found in type {}", propertyName, object.getClass().getName()); } } }
From source file:net.solarnetwork.web.support.SimpleCsvView.java
private void writeCSV(ICsvMapWriter writer, String[] fields, Object row) throws IOException { if (row instanceof Map) { @SuppressWarnings("unchecked") Map<String, ?> map = (Map<String, ?>) row; writer.write(map, fields);//from w ww . j a v a 2s .com } else if (row != null) { Map<String, Object> map = new HashMap<String, Object>(fields.length); // use bean properties if (getPropertySerializerRegistrar() != null) { // try whole-bean serialization first row = getPropertySerializerRegistrar().serializeProperty("row", row.getClass(), row, row); if (row == null) { return; } } BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(row); for (String name : fields) { Object val = wrapper.getPropertyValue(name); if (val != null) { if (getPropertySerializerRegistrar() != null) { val = getPropertySerializerRegistrar().serializeProperty(name, val.getClass(), row, val); } else { // Spring does not apply PropertyEditors on read methods, so manually handle PropertyEditor editor = wrapper.findCustomEditor(null, name); if (editor != null) { editor.setValue(val); val = editor.getAsText(); } } if (val instanceof Enum<?> || getJavaBeanTreatAsStringValues() != null && getJavaBeanTreatAsStringValues().contains(val.getClass())) { val = val.toString(); } if (val != null) { map.put(name, val); } } } writer.write(map, fields); } }
From source file:net.solarnetwork.support.XmlSupport.java
/** * Turn an object into a simple XML Element, supporting custom property * editors./*from www . j a va 2 s .co m*/ * * <p> * The returned XML will be a single element with all JavaBean properties * turned into attributes. For example: * <p> * * <pre> * <powerDatum * id="123" * pvVolts="123.123" * ... /> * </pre> * * <p> * {@link PropertyEditor} instances can be registered with the supplied * {@link BeanWrapper} for custom handling of properties, e.g. dates. * </p> * * @param bean * the object to turn into XML * @param elementName * the name of the XML element * @return the element, as an XML DOM Element */ public Element getElement(BeanWrapper bean, String elementName, Document dom) { PropertyDescriptor[] props = bean.getPropertyDescriptors(); Element root = null; root = dom.createElement(elementName); for (int i = 0; i < props.length; i++) { PropertyDescriptor prop = props[i]; if (prop.getReadMethod() == null) { continue; } String propName = prop.getName(); if ("class".equals(propName)) { continue; } Object propValue = null; PropertyEditor editor = bean.findCustomEditor(prop.getPropertyType(), prop.getName()); if (editor != null) { editor.setValue(bean.getPropertyValue(propName)); propValue = editor.getAsText(); } else { propValue = bean.getPropertyValue(propName); } if (propValue == null) { continue; } if (log.isTraceEnabled()) { log.trace("attribute name: " + propName + " attribute value: " + propValue); } root.setAttribute(propName, propValue.toString()); } return root; }
From source file:net.solarnetwork.node.support.XmlServiceSupport.java
/** * Turn an object into a simple XML Element, supporting custom property * editors./* ww w.j a v a 2s.c o m*/ * * <p> * The returned XML will be a single element with all JavaBean properties * turned into attributes. For example: * <p> * * <pre> * <powerDatum * id="123" * pvVolts="123.123" * ... /> * </pre> * * <p> * {@link PropertyEditor} instances can be registered with the supplied * {@link BeanWrapper} for custom handling of properties, e.g. dates. * </p> * * @param bean * the object to turn into XML * @param elementName * the name of the XML element * @return the element, as an XML DOM Element */ protected Element getElement(BeanWrapper bean, String elementName, Document dom) { PropertyDescriptor[] props = bean.getPropertyDescriptors(); Element root = null; root = dom.createElement(elementName); for (int i = 0; i < props.length; i++) { PropertyDescriptor prop = props[i]; if (prop.getReadMethod() == null) { continue; } String propName = prop.getName(); if ("class".equals(propName)) { continue; } Object propValue = null; PropertyEditor editor = bean.findCustomEditor(prop.getPropertyType(), prop.getName()); if (editor != null) { editor.setValue(bean.getPropertyValue(propName)); propValue = editor.getAsText(); } else { propValue = bean.getPropertyValue(propName); } if (propValue == null) { continue; } if (log.isTraceEnabled()) { log.trace("attribute name: " + propName + " attribute value: " + propValue); } root.setAttribute(propName, propValue.toString()); } return root; }
From source file:net.solarnetwork.web.support.JSONView.java
private void generateJavaBeanObject(JsonGenerator json, String key, Object bean, PropertyEditorRegistrar registrar) throws JsonGenerationException, IOException { if (key != null) { json.writeFieldName(key);//from w ww . ja v a 2s . co m } if (bean == null) { json.writeNull(); return; } BeanWrapper wrapper = getPropertyAccessor(bean, registrar); PropertyDescriptor[] props = wrapper.getPropertyDescriptors(); json.writeStartObject(); for (PropertyDescriptor prop : props) { String name = prop.getName(); if (this.getJavaBeanIgnoreProperties() != null && this.getJavaBeanIgnoreProperties().contains(name)) { continue; } if (wrapper.isReadableProperty(name)) { Object propVal = wrapper.getPropertyValue(name); if (propVal != null) { // test for SerializeIgnore Method getter = prop.getReadMethod(); if (getter != null && getter.isAnnotationPresent(SerializeIgnore.class)) { continue; } if (getPropertySerializerRegistrar() != null) { propVal = getPropertySerializerRegistrar().serializeProperty(name, propVal.getClass(), bean, propVal); } else { // Spring does not apply PropertyEditors on read methods, so manually handle PropertyEditor editor = wrapper.findCustomEditor(null, name); if (editor != null) { editor.setValue(propVal); propVal = editor.getAsText(); } } if (propVal instanceof Enum<?> || getJavaBeanTreatAsStringValues() != null && getJavaBeanTreatAsStringValues().contains(propVal.getClass())) { propVal = propVal.toString(); } writeJsonValue(json, name, propVal, registrar); } } } json.writeEndObject(); }
From source file:net.solarnetwork.node.support.XmlServiceSupport.java
private void writeURLEncodedBeanProperties(BeanWrapper bean, Map<String, ?> attributes, Writer out) throws IOException { PropertyDescriptor[] props = bean.getPropertyDescriptors(); boolean propsWritten = false; if (attributes != null && attributes.containsKey(ATTR_NODE_ID)) { out.write("nodeId=" + attributes.get(ATTR_NODE_ID)); propsWritten = true;/* ww w. j a v a 2 s. co m*/ } for (int i = 0; i < props.length; i++) { PropertyDescriptor prop = props[i]; if (prop.getReadMethod() == null) { continue; } String propName = prop.getName(); if ("class".equals(propName)) { continue; } Object propValue = null; if (attributes != null && attributes.containsKey(propName)) { propValue = attributes.get(propName); } else { PropertyEditor editor = bean.findCustomEditor(prop.getPropertyType(), prop.getName()); if (editor != null) { editor.setValue(bean.getPropertyValue(propName)); propValue = editor.getAsText(); } else { propValue = bean.getPropertyValue(propName); } } if (propValue == null) { continue; } if (propsWritten) { out.write("&"); } out.write(propName); out.write("="); out.write(URLEncoder.encode(propValue.toString(), "UTF-8")); propsWritten = true; } out.flush(); out.close(); }