1 22 23 package org.enhydra.kelp.common.bridge; 24 25 import org.enhydra.kelp.common.node.OtterXMLCNode; 27 28 import org.enhydra.xml.xmlc.XMLCException; 30 import org.enhydra.xml.xmlc.commands.xmlc.XMLCOptions; 31 import org.enhydra.xml.xmlc.commands.options.OptionsParser; 32 33 import java.io.File ; 35 import java.io.IOException ; 36 import java.io.PrintWriter ; 37 import java.lang.reflect.Constructor ; 38 import java.lang.reflect.Method ; 39 import java.lang.reflect.InvocationTargetException ; 40 import java.lang.reflect.Array ; 41 42 public class MetaDataHandlerV1 implements MetaDataHandler { 44 45 private final String PARSER_CLASS = 47 "org.enhydra.xml.xmlc.commands.options.OptionsParser"; private final String SET_DOCUMENT_OUTPUT_METHOD = "setDocumentOutput"; private final String SET_FOR_RECOMP_METHOD = "setForRecomp"; private final String SET_PRINT_VERSION_METHOD = "setPrintVersion"; private final String GET_DELETE_ELEMENT_CLASSES_METHOD = 52 "getDeleteElementClasses"; private final String GET_URL_EDITS_METHOD = "getURLEdits"; private final String SAVE_METHOD = "save"; private final String PARSE_METHOD = "parse"; private final String BOOLEAN_TYPE = "boolean"; 58 private LocalMetaData metaData = null; 60 private boolean printDocInfo = false; 61 62 public MetaDataHandlerV1() { 63 metaData = new LocalMetaData(); 64 } 65 66 public String getDocumentOutput() { 67 String outPath = null; 68 File outFile = metaData.getDocumentOutput(); 69 70 if (outFile != null) { 71 outPath = outFile.getAbsolutePath(); 72 } 73 return outPath; 74 } 75 76 public void setDocumentOutput(String s) { 77 invokeSetter(SET_DOCUMENT_OUTPUT_METHOD, File .class.getName(), 78 new File (s)); 79 } 80 81 public boolean getCompileSource() { 83 return (!metaData.getNoCompile()); 84 } 85 86 public void setCompileSource(boolean b) { 87 metaData.setNoCompile(!b); 88 } 89 90 public String getInputDocument() { 91 File doc = null; 92 String docPath = new String (); 93 94 doc = metaData.getSourceDocument(); 95 if (doc != null) { 96 docPath = doc.getAbsolutePath(); 97 } 98 return docPath; 99 } 100 101 public void setInputDocument(String s) { 102 File f = new File (s); 103 104 metaData.setSourceDocument(f, true); 105 } 106 107 public boolean getKeepGeneratedSource() { 108 return metaData.getKeep(); 109 } 110 111 public void setKeepGeneratedSource(boolean b) { 112 metaData.setKeep(b); 113 } 114 115 public boolean getPrintAccessorInfo() { 116 return metaData.getPrintAccessMethods(); 117 } 118 119 public void setPrintAccessorInfo(boolean b) { 120 metaData.setPrintAccessMethods(b); 121 } 122 123 public boolean getPrintDocumentInfo() { 124 return printDocInfo; 125 } 126 127 public void setPrintDocumentInfo(boolean b) { 128 printDocInfo = b; 129 } 130 131 public boolean getPrintDOM() { 132 return metaData.getDump(); 133 } 134 135 public void setPrintDOM(boolean b) { 136 metaData.setDump(b); 137 } 138 139 public boolean getPrintParseInfo() { 140 return metaData.getParseInfo(); 141 } 142 143 public void setPrintParseInfo(boolean b) { 144 metaData.setParseInfo(b); 145 } 146 147 public boolean getPrintVersion() { 148 return metaData.getPrintVersion(); 149 } 150 151 public boolean getVerbose() { 152 return metaData.getVerbose(); 153 } 154 155 public void setVerbose(boolean b) { 156 metaData.setVerbose(b); 157 } 158 159 public String getClassName() { 161 return metaData.getClassName(); 162 } 163 164 public void setClassName(String n) { 165 metaData.setClassName(n); 166 } 167 168 public File getJavaClassSource() { 169 return metaData.getJavaClassSource(); 170 } 171 172 public void setJavaClassSource(File f, OtterXMLCNode node) { 173 metaData.setJavaClassSource(f); 174 } 175 176 public File getJavaInterfaceSource() { 177 return metaData.getJavaInterfaceSource(); 178 } 179 180 public void setJavaInterfaceSource(File f, OtterXMLCNode node) { 181 metaData.setJavaInterfaceSource(f); 182 } 183 184 public String getPackageName() { 185 return metaData.getPackageName(); 186 } 187 188 public boolean getRecompilation() { 189 return metaData.getForRecomp(); 190 } 191 192 public void setRecompilation(boolean b) { 193 invokeSetter(SET_FOR_RECOMP_METHOD, BOOLEAN_TYPE, new Boolean (b)); 194 } 195 196 public void setPrintVersion(boolean b) { 197 invokeSetter(SET_PRINT_VERSION_METHOD, BOOLEAN_TYPE, new Boolean (b)); 198 } 199 200 public Object [] getDeleteElements() { 202 Object result = new Object (); 203 204 result = invokeGetter(GET_DELETE_ELEMENT_CLASSES_METHOD); 205 return (Object []) result; 206 } 207 208 public Object [] getURLMappings() { 209 Object result = new Object (); 210 211 result = invokeGetter(GET_URL_EDITS_METHOD); 212 return (Object []) result; 213 } 214 215 public Object getMetaData() { 217 return metaData; 218 } 219 220 public void parse(String [] files, String [] args, PrintWriter writer, 221 OtterXMLCNode node) throws XMLCException, IOException { 222 OptionsParser op = null; 223 224 metaData.complete(); 225 if ((files.length >= 1)) { 226 op = getNewOptionsParser(); 227 callParse(op, files); 228 } 229 if ((args.length >= 1)) { 230 if (op == null) { 231 op = getNewOptionsParser(); 232 } 233 callParse(op, args); 234 } 235 } 236 237 public void save(File op) throws IOException { 238 if (!op.getParentFile().exists()) { 239 op.getParentFile().mkdirs(); 240 } 241 invokeSetter(SAVE_METHOD, File .class.getName(), op); 242 } 243 244 private Object invokeGetter(String methodName) { 246 Class [] types = new Class [0]; 247 Object [] values = new Object [0]; 248 Object result = new Object (); 249 Method meth = null; 250 251 try { 252 meth = metaData.getClass().getMethod(methodName, types); 253 result = meth.invoke(metaData, values); 254 } catch (Exception e) { 255 e.printStackTrace(); 256 } 257 return result; 258 } 259 260 private void invokeSetter(String methodName, String typeName, 261 Object value) { 262 Class [] types = new Class [1]; 263 Object [] values = new Object [1]; 264 Method meth = null; 265 266 try { 267 types[0] = Class.forName(typeName); 268 values[0] = value; 269 meth = metaData.getClass().getMethod(methodName, types); 270 meth.invoke(metaData, values); 271 } catch (Exception e) { 272 e.printStackTrace(); 273 } 274 } 275 276 private OptionsParser getNewOptionsParser() { 277 OptionsParser parser = null; 278 279 try { 280 Class opClass = Class.forName(PARSER_CLASS); 281 Constructor [] opCon = opClass.getConstructors(); 282 Class [] params = opCon[0].getParameterTypes(); 283 Object [] values = new Object [0]; 284 285 if (params.length == 1) { 286 values = new Object [1]; 287 values[0] = metaData; 288 } 289 parser = (OptionsParser) opCon[0].newInstance(values); 290 } catch (Exception e) { 291 parser = null; 292 e.printStackTrace(); 293 } 294 return parser; 295 } 296 297 private void callParse(OptionsParser parser, 298 String [] args) throws IOException , XMLCException { 299 Class opClass = parser.getClass(); 300 Class [] params = new Class [1]; 301 Object [] values = null; 302 Method method = null; 303 304 params[0] = args.getClass(); 305 try { 306 method = opClass.getMethod(PARSE_METHOD, params); 307 values = new Object [1]; 308 values[0] = args; 309 method.invoke(parser, values); 310 } catch (InvocationTargetException e) { 311 if (e.getTargetException() instanceof IOException ) { 312 throw (IOException ) e.getTargetException(); 313 } else if (e.getTargetException() instanceof XMLCException) { 314 throw (XMLCException) e.getTargetException(); 315 } else { 316 e.printStackTrace(); 317 } 318 } catch (Exception e) { 319 e.printStackTrace(); 320 } 321 322 } 325 326 329 public class LocalMetaData extends XMLCOptions { 330 private File javaClassSource = null; 331 private File javaInterfaceSource = null; 332 private File sourceDoc = null; 333 334 340 public void setJavaClassSource(File f) { 341 javaClassSource = f; 342 } 343 344 public void setJavaInterfaceSource(File f) { 345 javaInterfaceSource = f; 346 } 347 348 354 public File getJavaClassSource() { 355 return javaClassSource; 356 } 357 358 public File getJavaInterfaceSource() { 359 return javaInterfaceSource; 360 } 361 362 369 public void setSourceDocument(File f, boolean real) { 370 if (real) { 371 sourceDoc = f; 372 setSourceDocument(sourceDoc); 373 } 374 } 375 376 382 public void setSourceDocument(File f) { 383 super.setSourceDocument(sourceDoc); 384 } 385 386 392 public File getSourceDocument() { 393 return sourceDoc; 394 } 395 396 } 397 } 398 | Popular Tags |