⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compiler.java

📁 A translator that converts Qt Designer UI files into SWT java classes. Use the power of Qt Designer
💻 JAVA
字号:
/* This file is part of ui2swt.
 *
 * $Revision: 1.4 $
 * $Date: 2007/02/18 03:24:20 $
 * $Name:  $
 *
 * Copyright (C) 2006-2007 James Forbes, All Rights Reserved.
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software in
 *     a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *  3. This notice may not be removed or altered from any source distribution.
 */

package ui2swt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.Properties;


public class Compiler
{
    private static String LOCAL_PROPERTIES_FILE_NAME = "ui2swt.properties";

    private static String packageName;
    private static String inputFileName;
    private static String outputFileName;
    private static String propertiesFileName;
    private static boolean verbose = false;
    private static boolean debug = false;

    private static Properties properties;

    
    public static void main( String[] args )
    {
        try
        {
            parseArguments(args);
            compile();
        }
        catch ( Throwable ex )
        {
            System.err.println(ex.getMessage());
            
            if ( debug )
            {
                ex.printStackTrace();
            }
        }
    }

    
    //
    //
    //
    
    private static void parseArguments( String[] args )
    {
        int i = 0;
        
        while ( i < args.length )
        {
            String arg = args[i];
            
            if ( arg.equals("-h") || arg.equals("--help") )
            {
                printUsage();
                System.exit(0);
            }
            else if ( arg.equals("-v") || arg.equals("--verbose") )
            {
                verbose = true;
            }
            else if ( arg.equals("-d") || arg.equals("--debug") )
            {
                debug = true;
            }
            else if ( arg.equals("-p") || arg.equals("--package") )
            {
                i++;
                
                if ( i < args.length )
                {
                    packageName = args[i];
                }
                else
                {
                    throw new RuntimeException("ERROR: missing package name after option "+arg+".  Use option -h for usage information.");
                }
            }
            else if ( arg.equals("-o") || arg.equals("--output") )
            {
                i++;
                
                if ( i < args.length )
                {
                    outputFileName = args[i];
                }
                else
                {
                    throw new RuntimeException("ERROR: missing output file name after option "+arg+".  Use option -h for usage information.");
                }
            }
            else if ( arg.equals("-c") || arg.equals("--config") )
            {
                i++;
                
                if ( i < args.length )
                {
                    propertiesFileName = args[i];
                }
                else
                {
                    throw new RuntimeException("ERROR: missing properties file name after option "+arg+".  Use option -h for usage information.");
                }
            }
            else
            {
                inputFileName = arg;
            }
            
            i++;
        }
        
        if ( inputFileName == null )
        {
            throw new RuntimeException("ERROR: missing input file name.  Use option -h for usage information.");
        }
        
        if ( outputFileName == null )
        {
            setOutputFileName();
        }
        
        if ( packageName == null )
        {
            setPackageName();
        }

        
        properties = new Properties();

        try
        {
            properties.load(Compiler.class.getClassLoader().getResourceAsStream(LOCAL_PROPERTIES_FILE_NAME));
        }
        catch ( IOException ex )
        {
            throw new RuntimeException("ERROR: could not load local properties file: "+LOCAL_PROPERTIES_FILE_NAME);
        }

        if ( propertiesFileName != null )
        {
            properties = new Properties(properties);
            
            try
            {
                properties.load(new FileInputStream(propertiesFileName));
            }
            catch ( FileNotFoundException ex )
            {
                throw new RuntimeException("ERROR: could not find properties file: "+propertiesFileName);
            }
            catch ( IOException ex )
            {
                throw new RuntimeException("ERROR: could not load properties file: "+propertiesFileName);
            }
        }
    }
    
    
    private static void compile() throws Exception
    {
        Parser          parser          = new Parser();
        Translator      translator      = new Translator(properties);
        Outputer        outputer        = new Outputer();
        UIDefinition    uiDefinition    = new UIDefinition();
        ClassDefinition classDefinition = new ClassDefinition();

        if ( verbose )
        {
            System.out.println("Translating UI file...");
            System.out.println("  input file:   "+inputFileName);
            System.out.println("  output file:  "+outputFileName);
            System.out.println("  package name: "+((packageName == null) ? "" : packageName));
        }

        setClassHeader(classDefinition);
        classDefinition.setPackageName(packageName);
        parser.parse(inputFileName, uiDefinition);
        translator.translate(uiDefinition, classDefinition);
        outputer.output(classDefinition, outputFileName);

        if ( verbose )
        {
            System.out.println("  UI version:   "+uiDefinition.getVersion());
        }

        if ( debug )
        {
            uiDefinition.dump();
        }
    }
    
    
    //
    //
    //
    
    private static void printUsage()
    {
        System.out.println();
        System.out.println("Usage:");
        System.out.println();
        System.out.println("    ui2swt [options] <input file>");
        System.out.println();
        System.out.println("Options:");
        System.out.println();
        System.out.println("    -h|--help");
        System.out.println("    -v|--verbose");
        System.out.println("    -d|--debug");
        System.out.println("    -p|--package <package name>");
        System.out.println("    -o|--output  <output file>");
        System.out.println("    -c|--config  <properties file>");
        System.out.println();
    }

    
    private static void setOutputFileName()
    {
        String inputName = new File(inputFileName).getName();

        int pos = inputName.lastIndexOf('.');
        
        if ( pos >= 0 )
        {
            outputFileName = inputName.subSequence(0, pos) + ".java";
        }
        else
        {
            outputFileName = inputName + ".java";
        }
    }
    
    
    private static void setPackageName()
    {
        File parent = new File(inputFileName).getParentFile();
        
        if ( parent != null )
        {
            StringBuffer name = new StringBuffer();

            getPackageName(parent, name);

            packageName = name.toString();
        }
    }
    
    
    private static void getPackageName( File file, StringBuffer name )
    {
        File parent = file.getParentFile();
        
        if ( parent != null )
        {
            getPackageName(parent, name);
            name.append(".");
        }

        name.append(file.getName());
    }
    
    
    private static void setClassHeader( ClassDefinition iClassDefinition )
    {
        iClassDefinition.addHeaderLine("//");
        iClassDefinition.addHeaderLine("// This file was generated by the ui2swt tool (http://ui2swt.sourceforge.net/)");
        iClassDefinition.addHeaderLine("//");
        iClassDefinition.addHeaderLine("// Date:");
        iClassDefinition.addHeaderLine("//   "+(new Date()));
        iClassDefinition.addHeaderLine("//");

        try
        {
            String sourceURL = (new File(inputFileName).toURL()).toString();

            iClassDefinition.addHeaderLine("// Source:");
            iClassDefinition.addHeaderLine("//   "+sourceURL);
            iClassDefinition.addHeaderLine("//");
        }
        catch ( MalformedURLException ex )
        {
            if ( debug )
            {
                ex.printStackTrace();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -