Example usage for org.dom4j.dom DOMElement getAttribute

List of usage examples for org.dom4j.dom DOMElement getAttribute

Introduction

In this page you can find the example usage for org.dom4j.dom DOMElement getAttribute.

Prototype

public String getAttribute(String name) 

Source Link

Usage

From source file:org.rivetlogic.utils.reflection.ObjectFactory.java

License:Open Source License

public Object getObject(DOMElement objectDefinitionElem)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException, ParseException, SecurityException, NoSuchMethodException {
    String objectClass = objectDefinitionElem.attributeValue(CLASS);

    Class cls = Class.forName(objectClass);
    Method[] methods = cls.getMethods();
    HashMap<String, Method> settersMap = new HashMap<String, Method>(methods.length);
    for (Method method : methods) {
        if (method.getName().startsWith("set")) {
            char[] propNameChars = method.getName().substring(3).toCharArray();
            propNameChars[0] = Character.toLowerCase(propNameChars[0]);
            String propName = new String(propNameChars);
            method.setAccessible(true);/* www .  jav  a 2s . co  m*/
            settersMap.put(propName, method);
        }
    }

    Object object = cls.newInstance();

    DOMElement propertiesElem = (DOMElement) objectDefinitionElem.element(PROPERTIES);

    if (propertiesElem != null) {
        List<DOMElement> propertyElems = propertiesElem.elements();

        for (DOMElement propertyElem : propertyElems) {
            String name = propertyElem.getAttribute(NAME);

            if (name != null && name.length() > 0 && settersMap.containsKey(name)) {
                Method method = settersMap.get(name);
                Class[] paramTypes = method.getParameterTypes();
                if (paramTypes.length == 1) {
                    String paramTypeName = paramTypes[0].getName();
                    String paramTypeNameLower = paramTypeName.toLowerCase();
                    Object param = null;

                    /* */
                    String value = null;
                    if (propertyElem.isTextOnly()) {
                        value = propertyElem.getTextTrim();
                    }

                    if (paramTypeNameLower.startsWith("int")) {
                        param = Integer.valueOf(value);
                    } else if (paramTypeNameLower.startsWith("short")) {
                        param = Short.valueOf(value);
                    } else if (paramTypeNameLower.startsWith("double")) {
                        param = Double.valueOf(value);
                    } else if (paramTypeNameLower.startsWith("float")) {
                        param = Float.valueOf(value);
                    } else if (paramTypeNameLower.startsWith("byte")) {
                        param = Byte.valueOf(value);
                    } else if (paramTypeNameLower.startsWith("char")) {
                        param = Character.valueOf(value.charAt(0));
                    } else if (paramTypeName.equals(Date.class.getName())) {
                        param = IntegrationConstants.xmlDateTimeFormat.parse(value);
                    } else if (List.class.isAssignableFrom(Class.forName(paramTypeName))) {
                        String propertyClassName = propertyElem.getAttribute(CLASS);
                        Class propertyClass = Class.forName(propertyClassName);
                        param = getList(propertyClass, propertyElem);
                    } else if (Map.class.isAssignableFrom(Class.forName(paramTypeName))) {
                        String propertyClassName = propertyElem.getAttribute(CLASS);
                        Class propertyClass = Class.forName(propertyClassName);
                        param = getMap(propertyClass, propertyElem);
                    } else {
                        param = value;
                    }

                    log.debug("Setting property '" + name + "' to '" + param + "'");
                    method.invoke(object, param);
                } else {
                    throw new IllegalArgumentException(
                            "Method should take only one parameter. Could not set property " + name
                                    + " of class " + objectClass);
                }
            }
        }
    }

    return object;
}