1 package com4j.tlbimp.driver; 2 3 import com4j.COM4J; 4 import com4j.ComException; 5 import com4j.tlbimp.BindingException; 6 import com4j.tlbimp.CodeWriter; 7 import com4j.tlbimp.DumpCodeWriter; 8 import com4j.tlbimp.ErrorListener; 9 import com4j.tlbimp.FileCodeWriter; 10 import com4j.tlbimp.TypeLibInfo; 11 import com4j.tlbimp.def.IWTypeLib; 12 import org.kohsuke.args4j.CmdLineException; 13 import org.kohsuke.args4j.CmdLineOption; 14 import org.kohsuke.args4j.CmdLineParser; 15 import org.kohsuke.args4j.opts.BooleanOption; 16 import org.kohsuke.args4j.opts.FileOption; 17 import org.kohsuke.args4j.opts.StringOption; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Arrays ; 22 import java.util.List ; 23 import java.util.ArrayList ; 24 25 28 public class Main implements ErrorListener { 29 public FileOption outDir = new FileOption("-o",new File ("-")); 30 public StringOption packageName = new StringOption("-p",""); 31 public BooleanOption debug = new BooleanOption("-debug"); 32 public BooleanOption verbose = new BooleanOption("-v"); 33 34 public StringOption libid = new StringOption("-libid"); 35 public StringOption libVersion = new StringOption("-libver"); 36 37 private final List <Ref> refs = new ArrayList <Ref>(); 38 39 public CmdLineOption refOpt = new CmdLineOption() { 40 public boolean accepts(String optionName) { 41 return optionName.equals("-ref"); 42 } 43 44 public int parseArguments(CmdLineParser parser, Parameters params) throws CmdLineException { 45 Ref r = new Ref(); 46 r.setLibid(params.getParameter(0)); 47 r.setPackageName(params.getParameter(1)); 48 refs.add(r); 49 return 2; 50 } 51 }; 52 53 public static void main(String [] args) { 54 System.exit(new Main().doMain(args)); 55 } 56 57 private void usage() { 58 System.err.println(Messages.USAGE); 59 } 60 61 private int doMain(String [] args) { 62 CmdLineParser parser = new CmdLineParser(); 63 parser.addOptionClass(this); 64 65 try { 66 parser.parse(args); 67 } catch (CmdLineException e) { 68 System.err.println(e.getMessage()); 69 usage(); 70 return -1; 71 } 72 73 List <String > files = (List <String >)parser.getArguments(); 74 75 if(libid.value!=null) { 76 if( !parser.getArguments().isEmpty() ) { 77 System.err.println(Messages.CANT_SPECIFY_LIBID_AND_FILENAME); 78 usage(); 79 return -1; 80 } 81 try { 82 TypeLibInfo tli = TypeLibInfo.locate(libid.value,libVersion.value); 83 if(verbose.isOn()) 84 System.err.printf("Found %1s <%2s>\n",tli.libName,tli.version); 85 86 files = Arrays.asList(tli.typeLibrary.toString()); 87 } catch( BindingException e ) { 88 error(e); 89 return -1; 90 } 91 } else { 92 if(files.size()<1) { 94 System.err.println(Messages.NO_FILE_NAME); 95 usage(); 96 return -1; 97 } 98 } 99 100 CodeWriter cw; 101 if(outDir.value.getPath().equals("-")) { 102 if(debug.isOn()) 103 cw = new DumpCodeWriter(); 104 else { 105 System.err.println(Messages.NO_OUTPUT_DIR); 106 usage(); 107 return -1; 108 } 109 } else 110 cw = new FileCodeWriter(outDir.value); 111 112 for( String file : files ) { 113 File typeLibFileName = new File (file); 114 System.err.println("Processing "+typeLibFileName); 115 try { 116 IWTypeLib mainLib = COM4J.loadTypeLibrary(typeLibFileName).queryInterface(IWTypeLib.class); 117 Driver driver = new Driver(); 118 driver.setPackageName(packageName.value); 119 for( Ref r : refs ) 120 driver.addRef(r); 121 driver.run(mainLib,cw,this); 122 mainLib.dispose(); 123 } catch( ComException e ) { 124 return handleException(e); 125 } catch( IOException e ) { 126 return handleException(e); 127 } catch( BindingException e ) { 128 return handleException(e); 129 } 130 } 131 132 return 0; 133 } 134 135 private int handleException( Exception e) { 136 if(debug.isOn()) { 137 e.printStackTrace(System.err); 138 return 1; 139 } else { 140 System.err.println(e.getMessage()); 141 return 1; 142 } 143 } 144 145 public void error(BindingException e) { 146 handleException(e); 147 } 148 149 public void warning(String message) { 150 System.err.println(message); 151 } 152 } 153 | Popular Tags |