ActionScript to Java

06 May 2011

One of these days I wanted to convert some AS2/3 to Java. It was obvious that it was simple since ActionScript and Java share lots of language constructs, however it is a tedious task so i created a simple helper application:

package com.jetdrone.as2j; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class AS2J {   private static final Map AS2JTYPES = new HashMap();   public static void main(String[] args) throws Exception {     // e.g.: "~/Flixel2/as/AdamAtomic-flixel-348b55f"     File asSourceDir = new File(args[0]);     String skip = "docs";     // e.g.: "~/src"     File outSourceDir = new File(args[1]);     AS2JTYPES.put("uint", "int");     AS2JTYPES.put("Boolean", "boolean");     AS2JTYPES.put("Number", "float");     AS2JTYPES.put("Array", "java.util.List");      processFile(asSourceDir, asSourceDir.getAbsolutePath(), outSourceDir, skip);   }   private static String getType(String type) {     String t = AS2JTYPES.get(type.trim());     if(t != null) {       return t;     }     return type;   }   private static void processFile(File src, String prefix, File target, String skip)       throws Exception {     for(File f : src.listFiles()) {       if(!f.getName().equals(skip)) {         if(f.isDirectory()) {           processFile(f, prefix, target, skip);         } else {           if(f.getName().endsWith(".as")) {             String fname = f.getAbsolutePath();             String sourceFileName = fname.substring(prefix.length(), fname.length() - 3) + ".java";             convertFile(f, new File(target, sourceFileName));           }         }       }     }   }   private static void convertFile(File src, File dest) throws Exception {     String l;     BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(src)));     PrintWriter out = new PrintWriter(dest);     int i=1;     while((l = in.readLine()) != null) {       try {         l = processPackage(l);         l = processConst(l);         l = processVariable(l);         l = processMethod(l);         l = processConstructor(l);         l = processGetter(l);         l = processSetter(l);         l = processCast(l);       } catch(RuntimeException e) {         System.err.println("ERR (" + i + "): [" + src.getAbsolutePath() + "]");         System.err.println(" --> " + l);       }       out.println(l);       i++;     }     in.close();     out.close();   }   private static final Pattern PACKAGE = Pattern.compile("^\\s*package\\s+(.+)");   private static String processPackage(String line) {     Matcher matcher = PACKAGE.matcher(line);     if (matcher.find()) {       return line.substring(0, matcher.start()) + "package " + matcher.group(1) +           ";" + line.substring(matcher.end());     }     return line;   }   // public var exists:Boolean;     private static final Pattern VARIABLE = Pattern.compile("var\\s+(\\w+?)\\s*:\\s*(.+?);");   private static String processVariable(String line) {     Matcher matcher = VARIABLE.matcher(line);     if (matcher.find()) {       if(matcher.group(2).indexOf('=') != -1) {         String type = matcher.group(2).substring(0, matcher.group(2).indexOf('='));         String defaultValue = matcher.group(2).substring(matcher.group(2).indexOf('='));         return line.substring(0, matcher.start()) + getType(type) + " " + matcher.group(1) +             " " + defaultValue + ";" + line.substring(matcher.end());         } else {         return line.substring(0, matcher.start()) + getType(matcher.group(2)) + " " +             matcher.group(1) + ";" + line.substring(matcher.end());       }     }     return line;   }   // static public const A_LIST:uint = 0;   private static final Pattern CONST = Pattern.compile("const\\s+(\\w+?)\\s*:\\s*(\\w+?)\\s*=\\s*(.+);");   private static String processConst(String line) {     Matcher matcher = CONST.matcher(line);     if (matcher.find()) {     return line.substring(0, matcher.start()) + "final " + getType(matcher.group(2)) +         " " + matcher.group(1) + " = " + matcher.group(3) + ";" + line.substring(matcher.end());     }     return line;   }   // public function destroy():void   private static final Pattern METHOD = Pattern.compile("function\\s+(\\w+?)\\s*\\((.*?)\\)\\s*:(\\w+)");   private static String processMethod(String line) {     Matcher matcher = METHOD.matcher(line);     if (matcher.find()) {       return line.substring(0, matcher.start()) + getType(matcher.group(3)) + " " +           matcher.group(1) + "(" + processArgs(matcher.group(2)) + ")" + line.substring(matcher.end());     }     return line;   }   // public function destroy()   private static final Pattern CONSTRUCTOR = Pattern.compile("function\\s+(\\w+?)\\s*\\((.*?)\\)");   private static String processConstructor(String line) {     Matcher matcher = CONSTRUCTOR.matcher(line);     if (matcher.find()) {       return line.substring(0, matcher.start()) + matcher.group(1) + "(" +           processArgs(matcher.group(2)) + ")" + line.substring(matcher.end());     }     return line;   }   private static final String processArgs(String line) {     StringBuffer sb = new StringBuffer();     for(String p : line.split(",")) {       sb.append(processArg(p));       sb.append(", ");     }     if(sb.length() > 0) {       sb.setLength(sb.length() - 2);     }     return sb.toString();   }   // Fixed:Boolean   private static final Pattern PARAM = Pattern.compile("\\s*(\\w+?)\\s*:\\s*(.+)");   private static final String processArg(String line) {     Matcher matcher = PARAM.matcher(line);     if (matcher.find()) {       if(matcher.group(2).indexOf('=') != -1) {         // java has no default values         String defaultValue = matcher.group(2).substring(matcher.group(2).indexOf('=') + 1);         return getType(matcher.group(2).substring(0, matcher.group(2).indexOf('='))) +             " " + matcher.group(1) + " /*" + defaultValue + "*/";       }       return getType(matcher.group(2)) + " " + matcher.group(1);     }     return line;   }   // public get right():Number   private static final Pattern GETTER = Pattern.compile(       "function\\s+get\\s+(\\w+?)\\s*\\(.*\\)\\s*:\\s*(\\w+)");   private static String processGetter(String line) {     Matcher matcher = GETTER.matcher(line);     if (matcher.find()) {       String methodName = matcher.group(1);       return line.substring(0, matcher.start()) + getType(matcher.group(2)) +           " get" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1) +           "()" + line.substring(matcher.end());     }     return line;   }   // public get right():Number   private static final Pattern SETTER = Pattern.compile(       "function\\s+set\\s+(\\w+?)\\s*\\((.*)\\)\\s*:\\s*(\\w+)");   private static String processSetter(String line) {     Matcher matcher = SETTER.matcher(line);     if (matcher.find()) {       String methodName = matcher.group(1);       return line.substring(0, matcher.start()) + getType(matcher.group(3)) +           " set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1) +           "(" + processArgs(matcher.group(2)) + ")" + line.substring(matcher.end());     }     return line;   }     // var members:Array = (Object as FlxGroup).members;   private static final Pattern CAST = Pattern.compile("\\s*(\\S+?)\\s+as\\s+(\\w+)");   private static String processCast(String line) {     Matcher matcher = CAST.matcher(line);     if (matcher.find()) {       if(matcher.group(1).indexOf('=') != -1) {         String prefix = matcher.group(1).substring(0, matcher.group(1).indexOf('=') + 1);         String value = matcher.group(1).substring(matcher.group(1).indexOf('=') + 1);         return line.substring(0, matcher.start()) + prefix + " (" +             getType(matcher.group(2)) + ")" + value + line.substring(matcher.end());       } else {         if(matcher.group(1).charAt(0) == '(') {           return line.substring(0, matcher.start()) + " ((" + getType(matcher.group(2)) +               ") " + matcher.group(1).substring(1) + line.substring(matcher.end());         } else {           return line.substring(0, matcher.start()) + " (" + getType(matcher.group(2)) +               ") " + matcher.group(1) + line.substring(matcher.end());         }       }     }     return line;   } }

Related tags:

Comments

Post a comment