view src/main/java/org/icedrobot/ika/plugins/build/logtags/Logtags.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.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 */
public class Logtags {

    private String packageName;
    private Map<String, String> constants = new HashMap<String, String>();
    
    void parseLine(String line) {
        StringTokenizer tokens = new StringTokenizer(line);
        while (tokens.hasMoreTokens()) {
            String token = tokens.nextToken(" ");
            if (token.equals("option")) {
                tokens.nextToken();
                packageName = tokens.nextToken();
                if (packageName.endsWith(";")) {
                    // in case there is be more than one ; endind the string
                    packageName = packageName.replace(";", "");
                }
                
            } else if (token.startsWith("#") || token.startsWith("\n")) {
                // ignore this line
                
            } else {
                // the first token is the value, the second the field name
                constants.put(token, tokens.nextToken());
            }
            
            return;
        }
    }

    public String getPackageName() {
        return packageName;
    }

    @Override
    public String toString() {
        return "/* autogenerated by ika logtags plugin, as part of the " +
               "IcedRobot project */" + "\n" +
               "/* --- DO NOT EDIT --- */" + "\n\n" +
               "package " + packageName + ";\n\n" +
               "public class EventLogTags {" + "\n\n" +
                
                   getValues() + "\n" +
                
               "}" +
               "\n";
    }

    private String getValues() {
        StringBuilder output = new StringBuilder();
        for (String constant : constants.keySet()) {
            output.append("    public static final int ");
            output.append(constants.get(constant).toUpperCase());
            output.append(" = ");
            output.append(constant);
            output.append(";\n");
        }
        return output.toString();
    }
}