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 paramObject, Element paramElement) { Map<String, Object> localMap = getProperties(paramElement); Iterator<String> iterator = localMap.keySet().iterator(); Field[] arrayOfField = paramObject.getClass().getFields(); Method[] arrayOfMethod = paramObject.getClass().getMethods(); while (iterator.hasNext()) { String str = iterator.next(); Object localObject = localMap.get(str); try { for (int i = 0; i < arrayOfField.length; ++i) { if ((!arrayOfField[i].getName().equalsIgnoreCase(str)) || (!isTypeMatch(arrayOfField[i].getType(), localObject.getClass()))) continue; arrayOfField[i].set(paramObject, localObject); System.err.println("Set field " + arrayOfField[i].getName() + "=" + localObject); break; } for (int i = 0; i < arrayOfMethod.length; ++i) { if ((!arrayOfMethod[i].getName().equalsIgnoreCase("set" + str)) || (arrayOfMethod[i].getParameterTypes().length != 1) || (!isTypeMatch(arrayOfMethod[i].getParameterTypes()[0], localObject.getClass()))) continue; arrayOfMethod[i].invoke(paramObject, new Object[] { localObject }); System.err.println("Set method " + arrayOfMethod[i].getName() + "=" + localObject); break; } } catch (Exception localException) { System.err.println("Unable to apply property '" + str + "': " + localException.toString()); } } } public static Map<String, Object> getProperties(Element paramElement) { HashMap<String, Object> localHashMap = new HashMap<String, Object>(); Element[] arrayOfElement = getChildrenByName(paramElement, "property"); for (int i = 0; i < arrayOfElement.length; ++i) { String str1 = arrayOfElement[i].getAttribute("name"); String str2 = arrayOfElement[i].getAttribute("type"); String str3 = getText(arrayOfElement[i]); try { Class<?> localClass = Class.forName(str2); Constructor<?> localConstructor = localClass.getConstructor(new Class[] { String.class }); Object localObject = localConstructor.newInstance(new Object[] { str3 }); localHashMap.put(str1, localObject); } catch (Exception localException) { System.err.println( "Unable to parse property '" + str1 + "'='" + str3 + "': " + localException.toString()); } } return localHashMap; } private static boolean isTypeMatch(Class<?> paramClass, Class<? extends Object> paramClass1) { if (paramClass.equals(paramClass1)) return true; if (paramClass.isPrimitive()) { if ((paramClass.getName().equals("int")) && (paramClass1.getName().equals("java.lang.Integer"))) return true; if ((paramClass.getName().equals("long")) && (paramClass1.getName().equals("java.lang.Long"))) return true; if ((paramClass.getName().equals("float")) && (paramClass1.getName().equals("java.lang.Float"))) return true; if ((paramClass.getName().equals("double")) && (paramClass1.getName().equals("java.lang.Double"))) return true; if ((paramClass.getName().equals("char")) && (paramClass1.getName().equals("java.lang.Character"))) return true; if ((paramClass.getName().equals("byte")) && (paramClass1.getName().equals("java.lang.Byte"))) return true; if ((paramClass.getName().equals("short")) && (paramClass1.getName().equals("java.lang.Short"))) return true; if ((paramClass.getName().equals("boolean")) && (paramClass1.getName().equals("java.lang.Boolean"))) return true; } return false; } public static Element[] getChildrenByName(Element element, String paramString) { NodeList localNodeList = element.getChildNodes(); int i = localNodeList.getLength(); LinkedList<Node> nodes = new LinkedList<Node>(); for (int j = 0; j < i; ++j) { Node localNode = localNodeList.item(j); if ((localNode.getNodeType() != 1) || (!localNode.getNodeName().equals(paramString))) continue; nodes.add(localNode); } return (Element[]) nodes.toArray(new Element[nodes.size()]); } public static String getAttribute(Element paramElement, String paramString) { return paramElement.getAttribute(paramString); } public static String getText(Element paramElement) { NodeList localNodeList = paramElement.getChildNodes(); int i = localNodeList.getLength(); for (int j = 0; j < i; ++j) { Node localNode = localNodeList.item(j); if (localNode.getNodeType() == 3) return localNode.getNodeValue(); } return ""; } }