Example usage for org.apache.commons.beanutils ConvertUtils register

List of usage examples for org.apache.commons.beanutils ConvertUtils register

Introduction

In this page you can find the example usage for org.apache.commons.beanutils ConvertUtils register.

Prototype

public static void register(Converter converter, Class clazz) 

Source Link

Document

Register a custom Converter for the specified destination Class, replacing any previously registered Converter.

For more details see ConvertUtilsBean.

Usage

From source file:hermes.browser.HermesBrowser.java

public static void main(String[] args) {
    log.debug("Hermes Browser " + Hermes.VERSION + " starting...");
    log.debug("working directory: " + new File(".").getAbsolutePath());

    Hermes.events.addConnectionListener(new ConnectionListener() {
        public void onConnectionOpen(Hermes hermes) {
            log.debug("Connection " + hermes.getId() + " opened");
        }/*from   w ww .  jav  a2 s .  co  m*/

        public void onConnectionClosed(Hermes hermes) {
            log.debug("Connection " + hermes.getId() + " closed");
        }
    });

    //
    // Need to bootstrap objects into the singleton manager... hack for now.

    JVMUtils.forceInit(SingletonManager.class);
    JVMUtils.forceInit(ThreadPool.class);
    JVMUtils.forceInit(SimpleClassLoaderManager.class);

    //
    // Commented out as this is for debug use only.
    // RepaintManager.setCurrentManager(new ThreadCheckingRepaintManager());

    //
    // Note this is the license for the JIDE Framework, it is licenced
    // to Colin Crist and the Hermes project and should not be used for any
    // other purpose
    //

    Lm.verifyLicense("Colin Crist", "Hermes", "9vkNAfxF1lvVyW7uZXYjpxFskycSGLw1");

    //
    // See http://www.jidesoft.com for licensing terms.

    //
    // Register a converter from a String to a File with PropertyUtils.

    ConvertUtils.register(new Converter() {
        public Object convert(Class arg0, Object filename) {
            return new File((String) filename);
        }
    }, File.class);

    SplashScreen.create(IconCache.getIcon("hermes.splash"));
    SplashScreen.show();

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                ui = new HermesBrowser(HERMES_TITLE);

                ui.initJIDE();

                try {
                    ui.loadConfig();
                } catch (NamingException ex) {
                    log.fatal("cannot initialise hermes: " + ex.getMessage(), ex);

                    System.exit(1);
                } catch (HeadlessException ex) {
                    log.fatal("cannot initialise hermes browser, no head: " + ex.getMessage(), ex);
                    System.exit(1);
                }

                ui.initUI();
                ui.init();

                ui.getLayoutPersistence().setProfileKey(ui.getUserProfileName());
                ui.getLayoutPersistence().loadLayoutData();

                // This must be done after the layout has been set otherwise
                // the
                // frames are hidden.

                final ArrayList<WatchConfig> tmpList = new ArrayList<WatchConfig>(ui.getConfig().getWatch());

                ui.getLoader().getConfig().getWatch().clear();

                for (WatchConfig wConfig : tmpList) {
                    ui.createWatch(wConfig);
                }

                ui.firstLoad = false;
            } catch (Exception ex) {
                log.fatal("cannot initialise hermes browser: " + ex.getMessage(), ex);
            }
        }
    });
}

From source file:common.ClassUtils.java

public static void copyBean(Object src, Object dest) {
    try {/*from   w ww .  j ava2  s .co m*/
        // ?   frombean-->birthday=""  user-birhday=null
        ConvertUtils.register(new Converter() {
            public Object convert(Class type, Object value) { //198a
                if (value == null) {
                    return null;
                }
                String s = (String) value;
                if (s.trim().equals("")) {
                    return null;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return sdf.parse(s);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }, Date.class);
        //bean
        BeanUtils.copyProperties(dest, src);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.yunmel.syncretic.utils.biz.TreeUtils.java

/**
 * ??List? (listcopy)/*from   w ww.  j  a v a2s .c  o m*/
 * 
 * @param list
 * @return
 */
public static <T extends BaseEntity> List<T> toTreeNodeList(List<T> source, Class<T> bean) {
    final Map<String, T> nodes = Maps.newHashMap();
    ConvertUtils.register(new Converter() {
        @SuppressWarnings("hiding")
        @Override
        public <T> T convert(Class<T> arg0, Object arg1) {
            // TODO Auto-generated method stub
            return null;
        }
    }, java.util.Date.class);
    // copy?list?
    List<T> list = CollectionsUtils.copyTo(source, bean);
    // ?
    for (T node : list) {
        node.put("level", -1);
        node.put("hasChild", false);
        node.put("children", new ArrayList<T>());
        nodes.put(node.getString("id"), node);
    }

    final BaseEntity root = new BaseEntity();
    root.put("level", 0);
    root.put("children", new ArrayList<T>());
    root.put("hasChild", false);
    nodes.put("0", (T) root);

    for (T node : list) {
        final T parent = nodes.get(node.getString("parentId"));
        if (parent == null) {
            ((ArrayList<T>) root.get("children")).add(node);
            continue;
            // throw new RuntimeException("?id?");
        } else {
            // ?
            ((List<T>) parent.get("children")).add(node);
        }
    }

    int max = 0;
    for (T node : list) {
        max = Math.max(resolveLevel(node, nodes), max);
    }

    return (List<T>) root.get("children");
}

From source file:com.plat4u.ghost.person.helper.PersonHelper.java

public static Person toPerson(PersonMsg messageCarrier)
        throws IllegalAccessException, InvocationTargetException {
    Person person = new Person();
    ConvertUtils.register(new DateConverter(), java.util.Date.class);
    BeanUtils.copyProperties(person, messageCarrier);
    return person;

}

From source file:com.plat4u.ghost.person.helper.PersonHelper.java

public static PersonMsg toPersonMsg(Person person) throws IllegalAccessException, InvocationTargetException {
    PersonMsg messageCarrier = new PersonMsg();
    ConvertUtils.register(new DateStringConverter(), java.lang.String.class);
    BeanUtils.copyProperties(messageCarrier, person);
    return messageCarrier;
}

From source file:common.ClassUtils.java

public static void copyProperty(Object dest, String name, Object value) {
    try {//from w  ww.  j  a v a2 s. co m
        // ?Date
        ConvertUtils.register(new Converter() {
            public Object convert(Class type, Object value) { //198a
                if (value == null) {
                    return null;
                }
                String s = (String) value;
                if (s.trim().equals("")) {
                    return null;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return sdf.parse(s);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }, Date.class);
        BeanUtils.copyProperty(dest, name, value);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.datatorrent.stram.StringCodecs.java

public static void loadDefaultConverters() {
    LOG.debug("Loading default converters for BeanUtils");
    ConvertUtils.register(new Converter() {
        @Override//ww w . java 2  s.  com
        @SuppressWarnings("unchecked")
        public Object convert(Class type, Object value) {
            if (value == null) {
                return null;
            }
            for (Class<?> clazz = value.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
                Class<? extends StringCodec> codec = codecs.get(clazz);
                if (codec == null) {
                    continue;
                }

                StringCodec instance;
                try {
                    instance = codec.newInstance();
                } catch (IllegalAccessException ex) {
                    throw new RuntimeException(
                            "Internal Error - it's impossible for this exception to be thrown!", ex);
                } catch (InstantiationException ex) {
                    throw new RuntimeException(
                            "Internal Error - it's impossible for this exception to be thrown!", ex);
                }

                return instance.toString(value);
            }

            return value.toString();
        }

    }, String.class);

    ConvertUtils.register(new Converter() {
        @Override
        public Object convert(Class type, Object value) {
            return value == null ? null : URI.create(value.toString());
        }
    }, URI.class);
}

From source file:cn.hxh.springside.mapper.ObjectMapper.java

/**
 * Apache BeanUtilsConverter?,??,','// w  ww  .  j  a v a2  s. c o m
 */
public static void registerDateConverter(String patterns) {
    DateConverter dc = new DateConverter();
    dc.setUseLocaleFormat(true);
    dc.setPatterns(StringUtils.split(patterns, ","));
    ConvertUtils.register(dc, Date.class);
}

From source file:net.erdfelt.android.sdkfido.sdks.SourceOriginsLoader.java

public static SourceOrigins load(URL url) throws IOException {
    try {/*from  w  ww  .j a  va  2s . co m*/
        ConvertUtils.register(new VersionConverter(), Version.class);
        DigesterLoader loader = DigesterLoaderBuilder.byDefaultFactories();
        Digester digester = loader.createDigester(SourceOrigins.class);
        SourceOrigins origins = (SourceOrigins) digester.parse(url);
        origins.normalize();
        return origins;
    } catch (SAXException e) {
        LOG.log(Level.WARNING, "Unable to load/parse url: " + url, e);
        throw new IOException("Unable to load/parse url: " + url, e);
    }
}

From source file:com.rodaxsoft.mailgun.MailingListManager.java

/**
 * Constructor//from w w w.ja  v a  2 s.  co m
 * @param properties Mailgun account info
 */
protected MailingListManager(MailgunAccount properties) {
    super(properties);

    ConvertUtils.register(new ListInfoConverter(), ListInfo.class);
}