1 package com4j.tlbimp; 2 3 import com4j.COM4J; 4 import com4j.ComException; 5 6 import java.util.List ; 7 import java.util.ArrayList ; 8 import java.util.Collections ; 9 import java.util.Set ; 10 import java.util.HashSet ; 11 import java.io.File ; 12 13 18 public final class TypeLibInfo { 19 20 23 public final String libName; 24 25 28 public final File typeLibrary; 29 30 33 public final Version version; 34 35 38 public final int lcid; 39 40 public TypeLibInfo(String libName, File typeLibrary, Version version, int lcid) { 41 this.libName = libName; 42 this.typeLibrary = typeLibrary; 43 this.version = version; 44 this.lcid = lcid; 45 } 46 47 58 public static final TypeLibInfo locate( String libid, String version ) throws BindingException { 59 COM4J.IID_IUnknown.toString(); 61 62 if(libid==null) throw new IllegalArgumentException (); 64 String 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 List <Version> versions = new ArrayList <Version>(); 75 for( String 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 verKey = "TypeLib\\{"+libid+"}\\"+version; 87 String 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 <Integer > lcids = new HashSet <Integer >(); 95 96 for( String lcid : Native.enumRegKeys(verKey) ) { 97 try { 98 lcids.add(Integer.valueOf(lcid)); 99 } catch( NumberFormatException e ) { 100 ; } 102 } 103 104 int lcid; 105 if( lcids.contains(0) ) lcid=0; 106 else lcid=lcids.iterator().next(); 107 108 String 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 (fileName), new Version(version), lcid ); 116 } 117 118 121 public static final class Version implements Comparable <Version> { 122 public final int major; 123 public final int minor; 124 125 public Version(String 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 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 toString() { 155 return major + "." + minor; 156 } 157 } 158 } 159 | Popular Tags |