1 22 package org.enhydra.kelp.common.bridge; 23 24 import org.enhydra.xml.xmlc.dom.XMLCDocument; 26 import org.enhydra.xml.xmlc.XMLCException; 27 28 import java.io.PrintWriter ; 30 import java.lang.reflect.Constructor ; 31 import java.lang.reflect.Method ; 32 33 public class GeneratorV1 implements Generator { 35 36 private final String CODE_GENERATOR_CLASS = 38 "org.enhydra.xml.xmlc.compiler.CodeGenerator"; private final String GENERATE_METHOD = "generateJavaSource"; 41 private Object generator = null; 43 private Class genClass = null; 44 private Object metaData = null; 45 46 public GeneratorV1(MetaDataHandler handler, XMLCDocument xmlcDoc, 47 PrintWriter verboseOutput) throws XMLCException { 48 Constructor [] cons = new Constructor [0]; 49 Constructor constructor = null; 50 Class [] params = null; 51 XMLCException xe = null; 52 Object compileParams = handler.getMetaData(); 53 54 metaData = compileParams; 55 try { 56 genClass = Class.forName(CODE_GENERATOR_CLASS); 57 cons = genClass.getConstructors(); 58 for (int i = 0; i < cons.length; i++) { 59 params = cons[i].getParameterTypes(); 60 61 if (params.length == 3) { 63 constructor = cons[i]; 64 break; 65 } 66 } 67 if (constructor == null) { 68 throw new XMLCException("Constructor not found"); 69 } else { 70 Object [] values = new Object [3]; 71 72 values[0] = metaData; 73 values[1] = xmlcDoc; 74 values[2] = verboseOutput; 75 generator = constructor.newInstance(values); 76 } 77 } catch (java.lang.reflect.InvocationTargetException e) { 78 if (e.getTargetException() instanceof XMLCException) { 79 throw (XMLCException) e.getTargetException(); 80 } else { 81 xe = new XMLCException(e.getTargetException().toString()); 82 e.getTargetException().printStackTrace(); 83 throw xe; 84 } 85 } catch (Exception e) { 86 xe = new XMLCException(e.toString()); 87 e.printStackTrace(); 88 throw xe; 89 } 90 } 91 92 public void generateJavaSource(PrintWriter writer) throws XMLCException { 93 94 Method [] meths = genClass.getMethods(); 96 Method generate = null; 97 Class [] paramTypes = null; 98 XMLCException xe = null; 99 100 for (int i = 0; i < meths.length; i++) { 101 if (meths[i].getName().equals(GENERATE_METHOD)) { 102 generate = meths[i]; 103 break; 104 } 105 } 106 if (generate != null) { 107 paramTypes = generate.getParameterTypes(); 108 Object paramValues[] = new Object [paramTypes.length]; 109 110 paramValues[0] = metaData; 111 paramValues[1] = writer; 112 try { 113 generate.invoke(generator, paramValues); 114 } catch (java.lang.reflect.InvocationTargetException e) { 115 e.printStackTrace(); 116 if (e.getTargetException() instanceof XMLCException) { 117 throw (XMLCException) e.getTargetException(); 118 } else { 119 xe = new XMLCException(e.getTargetException().toString()); 120 throw xe; 121 } 122 } catch (Exception e) { 123 xe = new XMLCException(e.toString()); 124 e.printStackTrace(); 125 throw xe; 126 } 127 } 128 } 129 130 } 131 | Popular Tags |