1 22 package org.jboss.xb.binding; 23 24 import java.lang.reflect.Method ; 25 import org.jboss.logging.Logger; 26 27 33 public class DelegatingObjectModelProvider 34 implements GenericObjectModelProvider 35 { 36 private static final Logger log = Logger.getLogger(DelegatingObjectModelProvider.class); 37 38 private final ObjectModelProvider provider; 39 private final boolean trace = log.isTraceEnabled(); 40 41 public DelegatingObjectModelProvider(ObjectModelProvider provider) 42 { 43 this.provider = provider; 44 } 45 46 public Object getChildren(Object o, MarshallingContext ctx, String namespaceURI, String localName) 47 { 48 return provideChildren(provider, o, namespaceURI, localName); 49 } 50 51 public Object getElementValue(Object o, MarshallingContext ctx, String namespaceURI, String localName) 52 { 53 return provideValue(provider, o, namespaceURI, localName); 54 } 55 56 public Object getAttributeValue(Object o, MarshallingContext ctx, String namespaceURI, String localName) 57 { 58 return provideAttributeValue(provider, o, namespaceURI, localName); 59 } 60 61 public Object getRoot(Object o, MarshallingContext ctx, String namespaceURI, String localName) 62 { 63 return provider.getRoot(o, null, namespaceURI, localName); 64 } 65 66 68 Object provideChildren(ObjectModelProvider provider, 69 Object parent, 70 String namespaceUri, 71 String name) 72 { 73 Class providerClass = provider.getClass(); 74 Class parentClass = parent.getClass(); 75 String methodName = "getChildren"; 76 77 Object container = null; 78 Method method = getProviderMethod(providerClass, 79 methodName, 80 new Class []{parentClass, String .class, String .class} 81 ); 82 if(method != null) 83 { 84 try 85 { 86 container = method.invoke(provider, new Object []{parent, namespaceUri, name}); 87 } 88 catch(Exception e) 89 { 90 log.error("Failed to invoke method " + methodName, e); 91 throw new IllegalStateException ("Failed to invoke method " + methodName); 92 } 93 } 94 else if(trace) 95 { 96 log.trace("No " + methodName + " for " + name); 97 } 98 return container; 99 } 100 101 Object provideValue(ObjectModelProvider provider, 102 Object parent, 103 String namespaceUri, 104 String name) 105 { 106 Class providerClass = provider.getClass(); 107 Class parentClass = parent.getClass(); 108 String methodName = "getElementValue"; 109 110 Object value = null; 111 Method method = getProviderMethod(providerClass, 112 methodName, 113 new Class []{parentClass, String .class, String .class} 114 ); 115 if(method != null) 116 { 117 try 118 { 119 value = method.invoke(provider, new Object []{parent, namespaceUri, name}); 120 } 121 catch(Exception e) 122 { 123 throw new IllegalStateException ("Failed to invoke method " + methodName); 124 } 125 } 126 else if(trace) 127 { 128 log.trace("No " + methodName + " for " + name); 129 } 130 return value; 131 } 132 133 Object provideAttributeValue(ObjectModelProvider provider, 134 Object object, 135 String namespaceUri, 136 String name) 137 { 138 Class providerClass = provider.getClass(); 139 Class objectClass = object.getClass(); 140 String methodName = "getAttributeValue"; 141 142 Object value = null; 143 Method method = getProviderMethod(providerClass, 144 methodName, 145 new Class []{objectClass, String .class, String .class} 146 ); 147 if(method != null) 148 { 149 try 150 { 151 value = method.invoke(provider, new Object []{object, namespaceUri, name}); 152 } 153 catch(Exception e) 154 { 155 throw new IllegalStateException ("Failed to invoke method " + methodName); 156 } 157 } 158 else if(trace) 159 { 160 log.trace("No " + methodName + " for " + name); 161 } 162 return value; 163 } 164 165 private static Method getProviderMethod(Class providerClass, String methodName, Class [] args) 166 { 167 Method method = null; 168 try 169 { 170 method = providerClass.getMethod(methodName, args); 171 } 172 catch(NoSuchMethodException e) 173 { 174 } 176 return method; 177 } 178 } 179 | Popular Tags |