1 17 package org.apache.ws.jaxme.xs.parser.impl; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Modifier ; 22 import java.lang.reflect.UndeclaredThrowableException ; 23 24 import org.apache.ws.jaxme.xs.XSParser; 25 import org.apache.ws.jaxme.xs.parser.*; 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.SAXException ; 28 29 30 34 public class ChildSetterImpl implements ChildSetter { 35 private static final Class [] GETCHILDHANDLER_CLASSES = new Class []{String .class, String .class, String .class}; 36 private static final Class [] ZERO_CLASSES = new Class []{}; 37 private static final Object [] ZERO_OBJECTS = new Object []{}; 38 39 private XSContext getData() { 40 return XSParser.getRunningInstance().getContext(); 41 } 42 43 46 protected Object invokeMethod(Object pBean, Method pMethod, String pName, Object [] pArgs) throws SAXException { 47 try { 48 return pMethod.invoke(pBean, pArgs); 49 } catch (InvocationTargetException e) { 50 Throwable t = e.getTargetException(); 51 if (t instanceof RuntimeException ) { 52 throw (RuntimeException ) t; 53 } else if (t instanceof SAXException ) { 54 throw (SAXException ) t; 55 } else { 56 throw new UndeclaredThrowableException (t); 57 } 58 } catch (IllegalAccessException e) { 59 throw new IllegalStateException ("Failed to invoke method " + pMethod.getName() + 60 " of class " + pMethod.getDeclaringClass() + 61 " with argument " + pBean + ": IllegalAccessException, " + 62 e.getMessage()); 63 } 64 } 65 66 67 public ContentHandler getChildHandler(String pQName, 68 String pNamespaceURI, String pLocalName) throws SAXException { 69 final XsSAXParser xsSAXParser = (XsSAXParser) getData().getCurrentContentHandler(); 70 if (xsSAXParser == null) { 71 throw new NullPointerException ("No XsSAXParser registered."); 72 } 73 final Object bean = xsSAXParser.getBean(); 74 final Class beanClass = bean.getClass(); 75 try { 76 Method m = beanClass.getMethod("getChildHandler", GETCHILDHANDLER_CLASSES); 77 if (Modifier.isPublic(m.getModifiers()) && 78 ContentHandler .class.isAssignableFrom(m.getReturnType())) { 79 Object result = (ContentHandler ) invokeMethod(bean, m, pQName, new Object []{pQName, pNamespaceURI, pLocalName}); 80 if (result != null) { 81 return (ContentHandler ) result; 82 } 83 } 84 } catch (NoSuchMethodException e) { 85 } 86 87 final ContentHandler result = getChildHandler(xsSAXParser, pQName, pLocalName); 88 if (result == null) { 89 throw new LocSAXException("Unknown child element '" + pQName + "' for bean " + bean.getClass().getName(), 90 getData().getLocator()); 91 } 92 return result; 93 } 94 95 98 protected ContentHandler newXsSAXParser(XsSAXParser pParent, Object pBean) { 99 return getData().getXsObjectFactory().newXsSAXParser(pBean); 100 } 101 102 protected ContentHandler getChildHandler(ContentHandler pParent, String pQName, String pLocalName) 103 throws SAXException { 104 final XsSAXParser xsSAXParser = (XsSAXParser) pParent; 105 final Object bean = xsSAXParser.getBean(); 106 final Class beanClass = bean.getClass(); 107 final String s = Character.toUpperCase(pLocalName.charAt(0)) + pLocalName.substring(1); 108 try { 109 Method m = beanClass.getMethod("create" + s, ZERO_CLASSES); 110 if (Modifier.isPublic(m.getModifiers()) && 111 !void.class.equals(m.getReturnType())) { 112 Object result = invokeMethod(bean, m, pQName, ZERO_OBJECTS); 113 return newXsSAXParser(xsSAXParser, result); 114 } 115 } catch (NoSuchMethodException e) { 116 } catch (SAXException e) { 117 throw e; 118 } 119 120 return null; 121 } 122 } 123 | Popular Tags |