Java tutorial
//package com.java2s; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static void applyProperties(Object o, Element root) { Map<String, Object> map = getProperties(root); Iterator<String> it = map.keySet().iterator(); Field[] fields = o.getClass().getFields(); Method[] methods = o.getClass().getMethods(); while (it.hasNext()) { String name = (String) it.next(); Object value = map.get(name); try { for (int i = 0; i < fields.length; i++) { if (fields[i].getName().equalsIgnoreCase(name) && isTypeMatch(fields[i].getType(), value.getClass())) { fields[i].set(o, value); System.err.println("Set field " + fields [i].getName() + "=" + value); break; } } for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equalsIgnoreCase("set" + name) && methods[i].getParameterTypes ().length == 1 && isTypeMatch(methods [i].getParameterTypes()[0], value.getClass())) { methods[i].invoke(o, new Object[] { value }); System.err.println("Set method " + methods [i].getName() + "=" + value); break; } } } catch (Exception e) { System.err.println("Unable to apply property '" + name + "': " + e.toString()); } } } public static Map<String, Object> getProperties(Element root) { Map<String, Object> map = new HashMap<String, Object>(); Element[] list = getChildrenByName(root, "property"); for (int i = 0; i < list.length; i++) { String name = list[i].getAttribute("name"); String type = list[i].getAttribute("type"); String valueString = getText(list[i]); try { Class<?> cls = Class.forName(type); Constructor<?> con = cls.getConstructor(new Class<?>[] { String.class }); Object value = con.newInstance(new Object[] { valueString }); map.put(name, value); } catch (Exception e) { System.err.println("Unable to parse property '" + name + "'='" + valueString + "': " + e.toString()); } } return map; } private static boolean isTypeMatch(Class<?> one, Class<?> two) { if (one.equals(two)) { return true; } if (one.isPrimitive()) { if (one.getName().equals("int") && two.getName().equals("java.lang.Integer")) { return true; } if (one.getName().equals("long") && two.getName().equals("java.lang.Long")) { return true; } if (one.getName().equals("float") && two.getName().equals("java.lang.Float")) { return true; } if (one.getName().equals("double") && two.getName().equals("java.lang.Double")) { return true; } if (one.getName().equals("char") && two.getName().equals("java.lang.Character")) { return true; } if (one.getName().equals("byte") && two.getName().equals("java.lang.Byte")) { return true; } if (one.getName().equals("short") && two.getName().equals("java.lang.Short")) { return true; } if (one.getName().equals("boolean") && two.getName().equals("java.lang.Boolean")) { return true; } } return false; } public static Element[] getChildrenByName(Element e, String name) { NodeList nl = e.getChildNodes(); int max = nl.getLength(); LinkedList<Node> list = new LinkedList<Node>(); for (int i = 0; i < max; i++) { Node n = nl.item(i); if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) { list.add(n); } } return list.toArray(new Element[list.size()]); } public static String getAttribute(Element e, String name) { return e.getAttribute(name); } public static String getText(Element e) { NodeList nl = e.getChildNodes(); int max = nl.getLength(); for (int i = 0; i < max; i++) { Node n = nl.item(i); if (n.getNodeType() == Node.TEXT_NODE) { return n.getNodeValue(); } } return ""; } }