1 16 package org.outerj.daisy.books.publisher.impl.util; 17 18 import org.apache.xmlbeans.XmlObject; 19 import org.apache.xmlbeans.XmlCursor; 20 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.lang.reflect.Constructor ; 24 import java.lang.reflect.InvocationTargetException ; 25 26 public class CustomImplementationHelper { 27 31 public static Object instantiateComponent(Class clazz, XmlObject xmlObject) throws IllegalAccessException , InvocationTargetException , InstantiationException { 32 Constructor constructor = null; 34 try { 35 constructor = clazz.getConstructor(new Class [] { XmlObject.class }); 36 } catch (NoSuchMethodException e) { 37 } 39 if (constructor != null) { 40 return constructor.newInstance(new Object [] { xmlObject }); 41 } 42 43 try { 45 constructor = clazz.getConstructor(new Class [] { Map .class }); 46 } catch (NoSuchMethodException e) { 47 } 49 if (constructor != null) { 50 Map attributes = getAttributes(xmlObject); 51 return constructor.newInstance(new Object [] { attributes }); 52 } 53 54 return clazz.newInstance(); 56 57 } 58 59 private static Map getAttributes(XmlObject xmlObject) { 60 Map attributes = new HashMap (); 61 XmlCursor cursor = xmlObject.newCursor(); 62 cursor.toFirstAttribute(); 63 do { 64 attributes.put(cursor.getName().getLocalPart(), cursor.getTextValue()); 65 } while (cursor.toNextAttribute()); 66 cursor.dispose(); 67 return attributes; 68 } 69 } 70 | Popular Tags |