01: /*
02: * Created on 3 Aug 2007
03: */
04: package uk.org.ponder.mapping;
05:
06: import java.beans.PropertyEditor;
07:
08: import uk.org.ponder.beanutil.BeanResolver;
09: import uk.org.ponder.conversion.LeafObjectParser;
10:
11: /** Converts between various forms of data converters **/
12:
13: public class ConverterConverter {
14: public static DARReshaper toReshaper(Object o) {
15: if (o instanceof DARReshaper) {
16: return (DARReshaper) o;
17: } else if (o instanceof LeafObjectParser) {
18: return new LeafObjectDARReshaper((LeafObjectParser) o);
19: }
20: if (o instanceof PropertyEditorFactory) {
21: o = ((PropertyEditorFactory) o).getPropertyEditor();
22: }
23: if (o instanceof PropertyEditor) {
24: final PropertyEditor pe = (PropertyEditor) o;
25: return new DARReshaper() {
26:
27: public DataAlterationRequest reshapeDAR(
28: DataAlterationRequest toshape) {
29: String data = DataAlterationRequest
30: .flattenData(toshape.data);
31: pe.setAsText(data);
32: DataAlterationRequest togo = new DataAlterationRequest(
33: toshape.path, pe.getValue(), toshape.type);
34: return togo;
35: }
36: };
37: } else
38: return null;
39: }
40:
41: public static BeanResolver toResolver(Object o) {
42: if (o instanceof BeanResolver) {
43: return (BeanResolver) o;
44: } else if (o instanceof LeafObjectParser) {
45: final LeafObjectParser parser = (LeafObjectParser) o;
46: return new BeanResolver() {
47: public String resolveBean(Object bean) {
48: return parser.render(bean);
49: }
50: };
51: }
52: if (o instanceof PropertyEditorFactory) {
53: o = ((PropertyEditorFactory) o).getPropertyEditor();
54: }
55: if (o instanceof PropertyEditor) {
56: final PropertyEditor pe = (PropertyEditor) o;
57: return new BeanResolver() {
58: public String resolveBean(Object bean) {
59: pe.setValue(bean);
60: return pe.getAsText();
61: }
62: };
63: } else
64: return null;
65: }
66: }
|