1 /* Copyright (c) 2013 Tobias Wolf, All Rights Reserved
2  *
3  * The contents of this file is dual-licensed under 2
4  * alternative Open Source/Free licenses: LGPL 2.1 or later and
5  * Apache License 2.0. (starting with JNA version 4.0.0).
6  *
7  * You can freely decide which license you want to apply to
8  * the project.
9  *
10  * You may obtain a copy of the LGPL License at:
11  *
12  * http://www.gnu.org/licenses/licenses.html
13  *
14  * A copy is also included in the downloadable source code package
15  * containing JNA, in file "LGPL2.1".
16  *
17  * You may obtain a copy of the Apache License at:
18  *
19  * http://www.apache.org/licenses/
20  *
21  * A copy is also included in the downloadable source code package
22  * containing JNA, in file "AL2.0".
23  */
24 package com.sun.jna.platform.win32.COM.tlb;
25 
26 import java.io.BufferedOutputStream;
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 
32 import com.sun.jna.platform.win32.OaIdl.TYPEKIND;
33 import com.sun.jna.platform.win32.COM.TypeLibUtil;
34 import com.sun.jna.platform.win32.COM.tlb.imp.TlbBase;
35 import com.sun.jna.platform.win32.COM.tlb.imp.TlbCmdlineArgs;
36 import com.sun.jna.platform.win32.COM.tlb.imp.TlbCoClass;
37 import com.sun.jna.platform.win32.COM.tlb.imp.TlbConst;
38 import com.sun.jna.platform.win32.COM.tlb.imp.TlbDispInterface;
39 import com.sun.jna.platform.win32.COM.tlb.imp.TlbEnum;
40 import com.sun.jna.platform.win32.COM.tlb.imp.TlbInterface;
41 
42 // TODO: Auto-generated Javadoc
43 /**
44  * The Class TlbImp.
45  *
46  * @author Tobias Wolf, wolf.tobias@gmx.net
47  */
48 public class TlbImp implements TlbConst {
49 
50     /** The type lib util. */
51     private TypeLibUtil typeLibUtil;
52 
53     /** The out. */
54     private File comRootDir;
55 
56     private File outputDir;
57 
58     private TlbCmdlineArgs cmdlineArgs;
59 
60     /**
61      * The main method.
62      *
63      * @param args
64      *            the arguments
65      */
main(String[] args)66     public static void main(String[] args) {
67         new TlbImp(args);
68     }
69 
TlbImp(String[] args)70     public TlbImp(String[] args) {
71         this.cmdlineArgs = new TlbCmdlineArgs(args);
72 
73         if (this.cmdlineArgs.isTlbId()) {
74             String clsid = this.cmdlineArgs.getRequiredParam(CMD_ARG_TYPELIB_ID);
75             int majorVersion = this.cmdlineArgs
76                     .getIntParam(CMD_ARG_TYPELIB_MAJOR_VERSION);
77             int minorVersion = this.cmdlineArgs
78                     .getIntParam(CMD_ARG_TYPELIB_MINOR_VERSION);
79 
80             // initialize typelib
81             // check version numbers with registry entries!!!
82             this.typeLibUtil = new TypeLibUtil(clsid, majorVersion,
83                     minorVersion);
84             this.startCOM2Java();
85         } else if (this.cmdlineArgs.isTlbFile()) {
86             String file = this.cmdlineArgs.getRequiredParam(CMD_ARG_TYPELIB_FILE);
87             // initialize typelib
88             // check version numbers with registry entries!!!
89             this.typeLibUtil = new TypeLibUtil(file);
90             this.startCOM2Java();
91         } else
92             this.cmdlineArgs.showCmdHelp();
93     }
94 
95     /**
96      * Start startCOM2Java.
97      */
startCOM2Java()98     public void startCOM2Java() {
99         try {
100             // create output Dir
101             this.createDir();
102 
103             String bindingMode = this.cmdlineArgs.getBindingMode();
104 
105             int typeInfoCount = typeLibUtil.getTypeInfoCount();
106             for (int i = 0; i < typeInfoCount; ++i) {
107                 TYPEKIND typekind = typeLibUtil.getTypeInfoType(i);
108 
109                 if (typekind.value == TYPEKIND.TKIND_ENUM) {
110                     this.createCOMEnum(i, this.getPackageName(), typeLibUtil);
111                 } else if (typekind.value == TYPEKIND.TKIND_RECORD) {
112                     TlbImp.logInfo("'TKIND_RECORD' objects are currently not supported!");
113                 } else if (typekind.value == TYPEKIND.TKIND_MODULE) {
114                     TlbImp.logInfo("'TKIND_MODULE' objects are currently not supported!");
115                 } else if (typekind.value == TYPEKIND.TKIND_INTERFACE) {
116                     this.createCOMInterface(i, this.getPackageName(),
117                             typeLibUtil);
118                 } else if (typekind.value == TYPEKIND.TKIND_DISPATCH) {
119                     this.createCOMDispInterface(i, this.getPackageName(),
120                             typeLibUtil);
121                 } else if (typekind.value == TYPEKIND.TKIND_COCLASS) {
122                     this.createCOMCoClass(i, this.getPackageName(),
123                             typeLibUtil, bindingMode);
124                 } else if (typekind.value == TYPEKIND.TKIND_ALIAS) {
125                     TlbImp.logInfo("'TKIND_ALIAS' objects are currently not supported!");
126                 } else if (typekind.value == TYPEKIND.TKIND_UNION) {
127                     TlbImp.logInfo("'TKIND_UNION' objects are currently not supported!");
128                 }
129             }
130 
131             logInfo(typeInfoCount + " files sucessfully written to: "
132                     + this.comRootDir.toString());
133         } catch (Exception e) {
134             e.printStackTrace();
135         }
136     }
137 
createDir()138     private void createDir() throws FileNotFoundException {
139         String _outputDir = this.cmdlineArgs.getParam(CMD_ARG_OUTPUT_DIR);
140         String path = "_jnaCOM_" + System.currentTimeMillis() + "\\myPackage\\"
141                 + this.typeLibUtil.getName().toLowerCase() + "\\";
142 
143         if (_outputDir != null) {
144             this.comRootDir = new File(_outputDir + "\\" + path);
145         } else {
146             String tmp = System.getProperty("java.io.tmpdir");
147             this.comRootDir = new File(tmp + "\\" + path);
148         }
149 
150         if (this.comRootDir.exists())
151             this.comRootDir.delete();
152 
153         if (this.comRootDir.mkdirs()) {
154             logInfo("Output directory sucessfully created.");
155         } else {
156             throw new FileNotFoundException(
157                     "Output directory NOT sucessfully created to: "
158                             + this.comRootDir.toString());
159         }
160     }
161 
getPackageName()162     private String getPackageName() {
163         return "myPackage." + this.typeLibUtil.getName().toLowerCase();
164     }
165 
writeTextFile(String filename, String str)166     private void writeTextFile(String filename, String str) throws IOException {
167         String file = this.comRootDir + File.separator + filename;
168         BufferedOutputStream bos = new BufferedOutputStream(
169                 new FileOutputStream(file));
170         bos.write(str.getBytes());
171         bos.close();
172     }
173 
writeTlbClass(TlbBase tlbBase)174     private void writeTlbClass(TlbBase tlbBase) throws IOException {
175         StringBuffer classBuffer = tlbBase.getClassBuffer();
176         this.writeTextFile(tlbBase.getFilename(), classBuffer.toString());
177     }
178 
179     /**
180      * Creates the com enum.
181      *
182      * @param index
183      *            the index
184      * @param typeLibUtil
185      *            the type lib util
186      * @return the string buffer
187      */
createCOMEnum(int index, String packagename, TypeLibUtil typeLibUtil)188     private void createCOMEnum(int index, String packagename,
189             TypeLibUtil typeLibUtil) throws IOException {
190         TlbEnum tlbEnum = new TlbEnum(index, packagename, typeLibUtil);
191         this.writeTlbClass(tlbEnum);
192     }
193 
194     /**
195      * Creates the com interface.
196      *
197      * @param index
198      *            the index
199      * @param typeLibUtil
200      *            the type lib util
201      * @return the string buffer
202      */
createCOMInterface(int index, String packagename, TypeLibUtil typeLibUtil)203     private void createCOMInterface(int index, String packagename,
204             TypeLibUtil typeLibUtil) throws IOException {
205         TlbInterface tlbInterface = new TlbInterface(index, packagename,
206                 typeLibUtil);
207         this.writeTlbClass(tlbInterface);
208     }
209 
210     /**
211      * Creates the com dispatch.
212      *
213      * @param index
214      *            the index
215      * @param typeLibUtil
216      *            the type lib util
217      * @return the string buffer
218      */
createCOMDispInterface(int index, String packagename, TypeLibUtil typeLibUtil)219     private void createCOMDispInterface(int index, String packagename,
220             TypeLibUtil typeLibUtil) throws IOException {
221         TlbDispInterface tlbDispatch = new TlbDispInterface(index, packagename,
222                 typeLibUtil);
223         this.writeTlbClass(tlbDispatch);
224     }
225 
createCOMCoClass(int index, String packagename, TypeLibUtil typeLibUtil, String bindingMode)226     private void createCOMCoClass(int index, String packagename,
227             TypeLibUtil typeLibUtil, String bindingMode) throws IOException {
228         TlbCoClass tlbCoClass = new TlbCoClass(index, this.getPackageName(),
229                 typeLibUtil, bindingMode);
230         this.writeTlbClass(tlbCoClass);
231     }
232 
233     /**
234      * Log info.
235      *
236      * @param msg
237      *            the msg
238      */
logInfo(String msg)239     public static void logInfo(String msg) {
240         System.out.println(msg);
241     }
242 }
243