1 5 package com.opensymphony.webwork.views.xslt; 6 7 import org.w3c.dom.Node ; 8 9 import java.lang.reflect.Constructor ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.util.Collection ; 12 13 14 19 public class DOMAdapter { 20 22 public short getNodeType() { 23 return Node.PROCESSING_INSTRUCTION_NODE; } 25 26 public String getPropertyName() { 27 return "[Root]"; 28 } 29 30 public AdapterNode adapt(DOMAdapter rootAdapter, Node parent, String propertyName, Object value) { 31 Class klass = value.getClass(); 32 Class adapterClass = findAdapterForClass(klass); 33 34 try { 35 if (adapterClass == null) { 36 if (klass.isArray()) { 37 adapterClass = ArrayAdapter.class; 38 } else if (String .class.isAssignableFrom(klass) || klass.isPrimitive() || Number .class.isAssignableFrom(klass)) { 39 adapterClass = ToStringAdapter.class; 40 } else if (Collection .class.isAssignableFrom(klass)) { 41 adapterClass = CollectionAdapter.class; 42 } else { 43 adapterClass = BeanAdapter.class; 44 } 45 } 46 47 Constructor c = adapterClass.getConstructor(new Class []{ 48 DOMAdapter.class, AdapterNode.class, String .class, 49 Object .class 50 }); 51 AdapterNode adapter = ((AdapterNode) c.newInstance(new Object []{ 52 this, parent, propertyName, value 53 })); 54 55 return adapter; 56 } catch (IllegalAccessException e) { 57 e.printStackTrace(); 58 throw new RuntimeException ("Cannot adapt " + value + " (" + propertyName + ") :" + e.getMessage()); 59 } catch (InstantiationException e) { 60 e.printStackTrace(); 61 throw new RuntimeException ("Cannot adapt " + value + " (" + propertyName + ") :" + e.getMessage()); 62 } catch (NoSuchMethodException e) { 63 e.printStackTrace(); 64 throw new RuntimeException ("Adapter Class " + adapterClass.getName() + " must define the right constructor. :" + e.getMessage()); 65 } catch (InvocationTargetException e) { 66 e.printStackTrace(); 67 throw new RuntimeException ("Cannot adapt " + value + " (" + propertyName + ") :" + e.getMessage()); 68 } 69 } 70 71 public AdapterNode adapt(Object value) throws IllegalAccessException , InstantiationException { 72 return new DocumentAdapter(this, null, "result", value); 73 } 74 75 public AdapterNode adaptNullValue(DOMAdapter rootAdapter, BeanAdapter parent, String propertyName) { 76 return new ToStringAdapter(rootAdapter, parent, propertyName, "null"); 77 } 78 79 private Class findAdapterForClass(Class klass) { 81 return null; 82 } 83 } 84 | Popular Tags |