List of usage examples for java.beans PropertyEditor getAsText
String getAsText();
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);/* w w w . ja v a 2s.c om*/ } 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.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 ww w .j av a 2s. c o 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.sourceforge.vulcan.spring.SpringBeanXmlEncoder.java
void encodeAsValue(final Element propertyNode, final Object object) { if (object == null) { propertyNode.addContent(new Element("null")); return;/*from www .j a v a 2 s . co m*/ } else if (object instanceof Enum<?>) { encodeEnum(propertyNode, (Enum<?>) object); return; } final Element valueNode = new Element("value"); PropertyEditor editor = PropertyEditorManager.findEditor(object.getClass()); if (editor == null && object instanceof Date) { editor = new DateEditor(); } if (editor != null) { editor.setValue(object); valueNode.setText(editor.getAsText()); } else { valueNode.setText(object.toString()); } propertyNode.addContent(valueNode); }
From source file:net.solarnetwork.support.XmlSupport.java
/** * Turn an object into a simple XML Element, supporting custom property * editors./*from w w w . j a v a 2s.c om*/ * * <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:it.cilea.osd.jdyna.util.AnagraficaUtils.java
private <P extends Property<TP>, TP extends PropertiesDefinition> void toXML(PrintWriter writer, List<P> proprietaList, Class<P> proprietaClass) { for (P proprieta : proprietaList) { writer.print(" <bean id=\"" + proprietaClass.getSimpleName() + proprieta.getId() + "\" class=\"" + proprietaClass.getCanonicalName() + "\">\n"); writer.print(" <property name=\"tipologia\" value=\"" + proprieta.getTypo().getShortName() + "\" />\n"); writer.print(" <property name=\"posizione\" value=\"" + proprieta.getPositionDef() + "\" />\n"); Class valoreClass = proprieta.getTypo().getRendering().getValoreClass(); writer.print(" <property name=\"valore\">\n"); writer.print(" <bean class=\"" + valoreClass.getCanonicalName() + "\">\n"); writer.print(" <property name=\"real\">\n"); PropertyEditor propertyEditor = valorePropertyEditors .get(proprieta.getTypo().getRendering().getValoreClass()); propertyEditor.setValue(proprieta.getValue()); writer.print(/*from w w w. ja v a2 s .c om*/ " <value><![CDATA[" + propertyEditor.getAsText() + "]]></value>\n"); writer.print(" </property>\n"); writer.print(" </bean>\n"); writer.print(" </property>\n"); writer.print(" </bean>\n"); } }
From source file:com.alibaba.citrus.service.form.impl.GroupImpl.java
/** * fields/* ww w.ja v a 2 s . c o 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.node.support.XmlServiceSupport.java
/** * Turn an object into a simple XML Element, supporting custom property * editors.//from www. j av a2s . 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.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;//from w ww. ja v a2s . com } 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(); }
From source file:org.apache.camel.impl.converter.PropertyEditorTypeConverter.java
public <T> T convertTo(Class<T> type, Object value) { // We can't convert null values since we can't figure out a property // editor for it. if (value == null) { return null; }/* w w w . ja v a2 s .c o m*/ if (value.getClass() == String.class) { // No conversion needed. if (type == String.class) { return ObjectHelper.cast(type, value); } Class key = type; PropertyEditor editor = lookupEditor(key); if (editor != null) { editor.setAsText(value.toString()); return ObjectHelper.cast(type, editor.getValue()); } } else if (type == String.class) { Class key = value.getClass(); PropertyEditor editor = lookupEditor(key); if (editor != null) { editor.setValue(value); return ObjectHelper.cast(type, editor.getAsText()); } } return null; }
From source file:org.dspace.app.cris.discovery.CrisItemEnhancerUtility.java
private static <P extends Property<TP>, TP extends PropertiesDefinition, NP extends ANestedProperty<NTP>, NTP extends ANestedPropertiesDefinition, ACNO extends ACrisNestedObject<NP, NTP, P, TP>, ATNO extends ATypeNestedObject<NTP>> List<String[]> getPathAsMetadata( ApplicationService as, ACrisObject<P, TP, NP, NTP, ACNO, ATNO> aCrisObject, String path) { String[] splitted = path.split("\\.", 2); List<String[]> result = new ArrayList<String[]>(); List<P> props = aCrisObject.getAnagrafica4view().get(splitted[0]); if (splitted.length == 2) { for (P prop : props) { if (prop.getObject() instanceof ACrisObject) { result.addAll(getPathAsMetadata(as, (ACrisObject) prop.getObject(), splitted[1])); } else { log.error(//from w ww. j av a 2 s .c o m "Wrong configuration, asked for path " + splitted[1] + " on a not CRIS Object value."); } } } for (P prop : props) { if (prop.getObject() instanceof ACrisObject) { ACrisObject val = (ACrisObject) prop.getObject(); result.add(new String[] { val.getName(), ResearcherPageUtils.getPersistentIdentifier(val) }); } else { PropertyEditor editor = prop.getTypo().getRendering().getPropertyEditor(as); editor.setValue(prop.getObject()); result.add(new String[] { editor.getAsText(), null }); } } return result; }