view src/main/java/org/icedrobot/ika/plugins/build/logtags/IkaLogtagsGenerator.java @ 36:627ae8325780

IcedRobot build support: logtags generator.
author Mario Torre <neugens.limasoftware@gmail.com>
date Wed, 31 Aug 2011 22:25:47 +0200
parents
children
line wrap: on
line source

/*
 * IKA - IcedRobot Kiosk Application
 * Copyright (C) 2011  IcedRobot team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.icedrobot.ika.plugins.build.logtags;

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.icedrobot.ika.plugins.IkaPlugin;
import org.icedrobot.ika.plugins.IkaPluginResult;

/**
 * Simple plugin to search logtags class definition and generate java class
 * files.
 */
public class IkaLogtagsGenerator implements IkaPlugin {

    @Override
    public String getName() {
        return "logtags";
    }

    @Override
    public String getDescription() {
        return "Parses the given logtags file and creates a Java Class";
    }

    @Override
    public IkaPluginResult execute(String[] args) {
        
        processLogtags(args[1], args[2]);
        
        return IkaPluginResult.OK;
    }

    private void processLogtags(String filename, String destDir) {
        try {
            File input = new File(filename);
            if (!input.exists()) {
                Logger.getLogger(IkaLogtagsGenerator.class.getName()).
                    log(Level.WARNING, "Input file does not exist: " + input);
                return;
            }
            
            Scanner fileScanner = new Scanner(input);
            fileScanner.useDelimiter(System.getProperty("line.separator"));
            Logtags logtags = new Logtags();
            while (fileScanner.hasNext()) {
                String line = fileScanner.next();
                
                logtags.parseLine(line);
            }

            // create the directory if it does not exist
            String packageName = logtags.getPackageName();
            
            // we nee to change the '.' to the file separators
            packageName = packageName.replace('.', File.separatorChar);
            File packageDir = new File(destDir + "" + 
                                       File.separatorChar + "" + packageName +
                                       "" + File.separatorChar);
            System.err.println("copy file to: " + packageDir);
            packageDir.mkdirs();
            
            File logtagsFile = new File(packageDir + "" + File.separatorChar +
                                        "EventLogTags.java");
            logtagsFile.createNewFile();
            
            // write the content
            FileWriter fstream = new FileWriter(logtagsFile);
            fstream.append(logtags.toString());
            fstream.close();
            
        } catch (Exception ex) {
            Logger.getLogger(IkaLogtagsGenerator.class.getName()).
                    log(Level.SEVERE, ex.getMessage(), ex);
        }
        
    }
}