1 17 package org.apache.ws.jaxme.generator.sg.impl; 18 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import org.apache.ws.jaxme.generator.Generator; 25 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBAppinfoImpl; 26 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBXsObjectFactoryImpl; 27 import org.apache.ws.jaxme.xs.xml.XsEAppinfo; 28 import org.apache.ws.jaxme.xs.xml.XsObject; 29 import org.xml.sax.ContentHandler ; 30 import org.xml.sax.Locator ; 31 import org.xml.sax.SAXException ; 32 33 34 37 public class JaxMeSGFactory extends JAXBSGFactory { 38 public class JaxMeAppinfoImpl extends JAXBAppinfoImpl { 39 JaxMeAppinfoImpl(XsObject pParent) { 40 super(pParent); 41 } 42 43 public ContentHandler getChildHandler(String pQName, String pNamespaceURI, String pLocalName) throws SAXException { 44 List myAnnotationClasses = getAnnotationClasses(); 45 for (int i = 0; i < myAnnotationClasses.size(); i +=3) { 46 if (((String ) myAnnotationClasses.get(i)).equals(pNamespaceURI) && 47 ((String ) myAnnotationClasses.get(i+1)).equals(pLocalName)) { 48 Class c = (Class ) myAnnotationClasses.get(i+2); 49 Object o; 50 try { 51 Constructor con = c.getConstructor(new Class []{JaxMeXsObjectFactory.class, Locator .class}); 52 o = con.newInstance(new Object []{this, getLocator()}); 53 } catch (NoSuchMethodException e) { 54 throw new SAXException ("Invalid appinfo class " + c.getClass().getName() + 55 ": Doesn't have a constructor taking the arguments " + 56 JaxMeXsObjectFactory.class.getName() + " and " + 57 Locator .class.getName()); 58 } catch (InvocationTargetException e) { 59 Throwable t = e.getTargetException(); 60 String msg = "Failed to create appinfo element of class " + c.getClass().getName() + 61 ": " + t.getClass().getName() + ", " + t.getMessage(); 62 if (t instanceof Exception ) { 63 throw new SAXException (msg, (Exception ) t); 64 } else { 65 throw new SAXException (msg, e); 66 } 67 } catch (Exception e) { 68 throw new SAXException ("Failed to create appinfo element of class " + c.getClass().getName() + 69 ": " + e.getClass().getName()); 70 } 71 return getObjectFactory().newXsSAXParser(o); 72 } 73 } 74 return super.getChildHandler(pQName, pNamespaceURI, pLocalName); 75 } 76 } 77 78 public class JaxMeXsObjectFactory extends JAXBXsObjectFactoryImpl { 79 public XsEAppinfo newXsEAppinfo(XsObject pParent) { 80 return new JaxMeAppinfoImpl(pParent); 81 } 82 } 83 84 public JaxMeSGFactory(Generator pGenerator) { 85 super(pGenerator); 86 } 87 88 private List annotationClasses = new ArrayList (); 89 protected List getAnnotationClasses() { 90 return annotationClasses; 91 } 92 93 public void addAnnotationClass(String pNamespaceURI, String pLocalName, Class pClass) { 94 if (pNamespaceURI == null || pNamespaceURI.length() == 0) { 95 throw new NullPointerException ("A nonempty namespace URI must be given."); 96 } 97 if (pLocalName == null || pLocalName.length() == 0) { 98 throw new NullPointerException ("A nonempty local name must be given."); 99 } 100 if (pClass == null) { 101 throw new NullPointerException ("An annotation class must be given."); 102 } 103 annotationClasses.add(pNamespaceURI); 104 annotationClasses.add(pLocalName); 105 annotationClasses.add(pClass); 106 } 107 } 108 | Popular Tags |