Java file -- D:\ToCD\JavaSrc\com\cager\guids\VBProject.java

Produced by JavaCCtoHTML program

package com.cager.guids;

import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.*;

public class VBProject
{
    private File vbp;
    private File projectDir;
    private ComDll outputDll;
    private ComDll compatDll;
    private String compatMode;
    
    public VBProject(File vbp) throws FileNotFoundException, IOException
    {
        this.vbp = vbp;
        this.projectDir = vbp.getParentFile();
        
        BufferedReader br = new BufferedReader(new FileReader(vbp));
        
        try
        {
            String str;
            
            String exeName = null;
            String exePath = null;
            String compatExe = null;
            String compatMode = null;
            
            while ((str = br.readLine()) != null)
            {
                //System.out.println("Read: " + str);
                
                StringTokenizer tokens = new StringTokenizer(str, "=");
                
                if (tokens.countTokens() == 2)
                {
                    // Must be a param=value line
                    
                    String param = tokens.nextToken().toLowerCase();
                    String value = remove(tokens.nextToken(), '"');
                    
                    //System.out.println("Param <" + param + ">");
                    
                    if (param.equals("exename32"))
                    {
                        exeName = value;
                    }
                    else if (param.equals("path32"))
                    {
                        exePath = value;
                    }
                    else if (param.equals("compatibleexe32"))
                    {
                        compatExe = value;
                    }
                    else if (param.equals("compatiblemode"))
                    {
                        compatMode = value;
                    }
                }
                
            }
        
            if (false)
            {
                System.out.println("exeName " + exeName);
                System.out.println("exePath " + exePath);
                System.out.println("compatExe " + compatExe);
                System.out.println("compatMode " + compatMode);
            }
                        
            if (exeName == null || exePath == null || compatExe == null || compatMode == null)
                throw new IOException("Invalid VBP file " + vbp.getCanonicalPath());
                
            File exeDir = new File(exePath);
            if (exeDir.isAbsolute())
                this.outputDll = new ComDll(exeDir, exeName);
            else
                this.outputDll = new ComDll(new File(projectDir, exePath), exeName);

            this.compatDll = new ComDll(projectDir, compatExe);
            
            this.compatMode = compatMode;
        }
        catch (IOException e)
        {
            throw e;
        }
        finally
        {
            br.close();
        }
        
        if (true)
        {
            System.out.println("projectDir " + projectDir.getCanonicalFile());
            System.out.println("outputDll " + outputDll.getCanonicalFile());
            System.out.println("compatDll " + compatDll.getCanonicalFile());
        }
    }
    
    public ComDll getOutputDll()
    {
        return outputDll;
    }
    
    public ComDll getCompatDll()
    {
        return compatDll;
    }
    
    public void compile() throws IOException
    {
        String[] argv = {"vb6", "/m", vbp.getAbsolutePath()};
        Process p = Runtime.getRuntime().exec(argv);
        
        while (true)
        {
            try
            {
                int ret = p.waitFor();
                System.out.println("vb6 " + ret);
                if (ret != 0)
                    throw new IOException("Could not compile " + vbp.getAbsolutePath());
                break;
            }
            catch (InterruptedException e)
            {
            }
        }
    }
    
    public void regenerateCompat() throws IOException
    {
        // De-register the current DLL so that VB will not attempt to create version
        // 2 in the registry (I do not know if this is really necessary,as we
        // will be deleting the registry entries anyway, but it might leave something
        // nasty behind...)
        if (outputDll.exists())
        {
            outputDll.unregister();
            outputDll.delete();
        }
    
        setCompatMode("1");
        
        try
        {
            compile();
            
            // Delete the previously compiled entries from the registry, as they will have
            // new GUIDs that are about to be overwritten
            outputDll.unregister();
            
            outputDll.applyGuidMap(compatDll.getGuidMap());
            
            // Something is leaving the compatDLL open, locking updates (is it the
            // COM TLI reader doing this?) Therefore attempt to delete it first.
            if (!compatDll.delete())
            {
                System.out.println("Could not delete compat");
            }
            
            if (!outputDll.renameTo(compatDll))
                throw new IOException("Could not rename " + outputDll.getAbsoluteFile() + " to " + compatDll.getAbsoluteFile());
        }
        finally
        {
            setCompatMode("2");
        }
    }
    
    public String getCompatMode()
    {
        return compatMode;
    }
    
    public void setCompatMode(String mode) throws IOException
    {
        if (mode.equals(compatMode))
            return;
            
        compatMode = mode;
        
        // Now update VBP file
        File bak = new File(projectDir, vbp.getName() + ".bak");
        bak.delete();
        
        if (!vbp.renameTo(bak))
            throw new IOException("Could not rename " + vbp.getAbsoluteFile() + " to " + bak.getAbsoluteFile());
        
        BufferedReader br = new BufferedReader(new FileReader(bak));
        BufferedWriter bw = new BufferedWriter(new FileWriter(vbp));
        
        String str;
        
        while ((str = br.readLine()) != null)
        {
            //System.out.println("Read: " + str);
            
            StringTokenizer tokens = new StringTokenizer(str, "=");
            
            if (tokens.countTokens() == 2)
            {
                // Must be a param=value line
                
                String param = tokens.nextToken().toLowerCase();
                
                //System.out.println("Param <" + param + ">");
                
                if (param.equals("compatiblemode"))
                {
                    bw.write("CompatibleMode=\"" + mode + "\"");
                    bw.newLine();
                    continue;
                }
            }
            
            // Not CompatobleMode line -- write unchanged
            
            bw.write(str);
            bw.newLine();
        }
        
        br.close();
        bw.close();
    }

    private String remove(String str, char c)
    {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++)
            if (str.charAt(i) != c)
                sb.append(str.charAt(i));
                
        return new String(sb);
    }
}