changeset 36:627ae8325780

IcedRobot build support: logtags generator.
author Mario Torre <neugens.limasoftware@gmail.com>
date Wed, 31 Aug 2011 22:25:47 +0200
parents cf0089413814
children 5463492cd2c9
files src/main/java/org/icedrobot/ika/plugins/build/logtags/IkaLogtagsGenerator.java src/main/java/org/icedrobot/ika/plugins/build/logtags/Logtags.java
diffstat 2 files changed, 182 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/icedrobot/ika/plugins/build/logtags/IkaLogtagsGenerator.java	Wed Aug 31 22:25:47 2011 +0200
@@ -0,0 +1,97 @@
+/*
+ * 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);
+        }
+        
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/icedrobot/ika/plugins/build/logtags/Logtags.java	Wed Aug 31 22:25:47 2011 +0200
@@ -0,0 +1,85 @@
+/*
+ * 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();
+    }
+}