KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com4j > tlbimp > TypeLibInfo


1 package com4j.tlbimp;
2
3 import com4j.COM4J;
4 import com4j.ComException;
5
6 import java.util.List JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Set JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.io.File JavaDoc;
12
13 /**
14  * Locates
15  *
16  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
17  */

18 public final class TypeLibInfo {
19
20     /**
21      * Human readable library name.
22      */

23     public final String JavaDoc libName;
24
25     /**
26      * Type library file.
27      */

28     public final File JavaDoc typeLibrary;
29
30     /**
31      * Type library version.
32      */

33     public final Version version;
34
35     /**
36      * Locale ID.
37      */

38     public final int lcid;
39
40     public TypeLibInfo(String JavaDoc libName, File JavaDoc typeLibrary, Version version, int lcid) {
41         this.libName = libName;
42         this.typeLibrary = typeLibrary;
43         this.version = version;
44         this.lcid = lcid;
45     }
46
47     /**
48      * Locates the type library file from the LIBID (a GUID) and an optional version number.
49      *
50      * @param libid
51      * String of the form "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
52      * @param version
53      * Optional version number. If null, the function searches for the latest version.
54      *
55      * @throws ComException
56      * If it fails to find the type library.
57      */

58     public static final TypeLibInfo locate( String JavaDoc libid, String JavaDoc version ) throws BindingException {
59         // make sure to load the com4j.dll
60
COM4J.IID_IUnknown.toString();
61
62         // check if libid is correct
63
if(libid==null) throw new IllegalArgumentException JavaDoc();
64         String JavaDoc libKey = "TypeLib\\{"+libid+"}";
65         try {
66             Native.readRegKey(libKey);
67         } catch( ComException e ) {
68             throw new BindingException(Messages.INVALID_LIBID.format(libid),e);
69         }
70
71
72         if(version==null) {
73             // find the latest version
74
List JavaDoc<Version> versions = new ArrayList JavaDoc<Version>();
75             for( String JavaDoc v : Native.enumRegKeys(libKey) ) {
76                 versions.add(new Version(v));
77             }
78             Collections.sort(versions);
79
80             if( versions.size()==0 )
81                 throw new BindingException(Messages.NO_VERSION_AVAILABLE.format());
82
83             version = versions.get(versions.size()-1).toString();
84         }
85
86         String JavaDoc verKey = "TypeLib\\{"+libid+"}\\"+version;
87         String JavaDoc libName;
88         try {
89             libName = Native.readRegKey(verKey);
90         } catch( ComException e ) {
91             throw new BindingException(Messages.INVALID_VERSION.format(version),e);
92         }
93
94         Set JavaDoc<Integer JavaDoc> lcids = new HashSet JavaDoc<Integer JavaDoc>();
95
96         for( String JavaDoc lcid : Native.enumRegKeys(verKey) ) {
97             try {
98                 lcids.add(Integer.valueOf(lcid));
99             } catch( NumberFormatException JavaDoc e ) {
100                 ; // ignore "FLAGS" and "HELPDIR"
101
}
102         }
103
104         int lcid;
105         if( lcids.contains(0) ) lcid=0;
106         else lcid=lcids.iterator().next();
107
108         String JavaDoc fileName;
109         try {
110             fileName = Native.readRegKey(verKey+"\\"+lcid+"\\win32");
111         } catch( ComException e ) {
112             throw new BindingException(Messages.NO_WIN32_TYPELIB.format(libid,version),e);
113         }
114
115         return new TypeLibInfo( libName, new File JavaDoc(fileName), new Version(version), lcid );
116     }
117
118     /**
119      * Represents the version number of the form "x.y"
120      */

121     public static final class Version implements Comparable JavaDoc<Version> {
122         public final int major;
123         public final int minor;
124
125         public Version(String JavaDoc name) {
126             int idx = name.indexOf('.');
127             major = Integer.valueOf(name.substring(0,idx));
128             minor = Integer.valueOf(name.substring(idx+1));
129         }
130
131         public int compareTo(Version rhs) {
132             if(this.major!=rhs.major)
133                 return this.major-rhs.major;
134             else
135                 return this.minor-rhs.minor;
136         }
137
138         public boolean equals(Object JavaDoc o) {
139             if (this == o) return true;
140             if (!(o instanceof Version)) return false;
141
142             final Version version = (Version) o;
143
144             if (major != version.major) return false;
145             if (minor != version.minor) return false;
146
147             return true;
148         }
149
150         public int hashCode() {
151             return 29 * major + minor;
152         }
153
154         public String JavaDoc toString() {
155             return major + "." + minor;
156         }
157     }
158 }
159
Popular Tags