List of usage examples for org.dom4j.dom DOMElement isTextOnly
public boolean isTextOnly()
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);// w w w . ja v a 2s .c o 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; }
From source file:org.rivetlogic.utils.reflection.ObjectFactory.java
License:Open Source License
private Object getMap(Class<Map> mapClass, DOMElement mapElem) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, ParseException, NoSuchMethodException { Object mapObj = mapClass.newInstance(); DOMElement entriesElem = (DOMElement) mapElem.element(ENTRIES); if (entriesElem != null) { Method method = mapClass.getMethod(PUT_METHOD, Object.class, Object.class); List<DOMElement> entryElems = entriesElem.elements(); for (DOMElement entryElem : entryElems) { String mapKey = entryElem.attributeValue(MAP_KEY); String value;//from w w w. j a va2 s . c o m if (entryElem.isTextOnly()) { value = entryElem.getTextTrim(); method.invoke(mapObj, mapKey, value); } else { Object mapEntry = null; String className = entryElem.attributeValue(CLASS); Class propertyClass = Class.forName(className); if (List.class.isAssignableFrom(propertyClass)) { mapEntry = getList(propertyClass, entryElem); } else { mapEntry = getObject(entryElem); } method.invoke(mapObj, mapKey, mapEntry); } } } return mapObj; }
From source file:org.rivetlogic.utils.reflection.ObjectFactory.java
License:Open Source License
private Object getList(Class<List> listClass, DOMElement listElem) throws IllegalArgumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, ParseException, SecurityException, NoSuchMethodException { Object listObj = listClass.newInstance(); DOMElement entriesElem = (DOMElement) listElem.element(ENTRIES); if (entriesElem != null) { List<DOMElement> entryElems = entriesElem.elements(); Method method = listClass.getMethod(ADD_METHOD, Object.class); for (DOMElement entryElem : entryElems) { String value;/* w w w . j av a 2 s .c om*/ if (entryElem.isTextOnly()) { value = entryElem.getTextTrim(); method.invoke(listObj, value); } else { Object listEntry = getObject(entryElem); method.invoke(listObj, listEntry); } } } return listObj; }