gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r2958 - in freeway/src/org/gnu/freeway: cwrappers/util serv


From: mdonoughe
Subject: [GNUnet-SVN] r2958 - in freeway/src/org/gnu/freeway: cwrappers/util services/impl
Date: Sun, 4 Jun 2006 14:40:57 -0700 (PDT)

Author: mdonoughe
Date: 2006-06-04 14:40:53 -0700 (Sun, 04 Jun 2006)
New Revision: 2958

Modified:
   freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
   freeway/src/org/gnu/freeway/services/impl/StatsService.java
Log:
cleanClassName now functions simmilarly to stripPackage when it is given a null
importMap
implementClass now requires a Writer as its second argument
main now parses command line arguments and implements every interface, outputing
the new source code into files with the correct indentation


Modified: freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java        
2006-06-04 16:33:36 UTC (rev 2957)
+++ freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java        
2006-06-04 21:40:53 UTC (rev 2958)
@@ -25,6 +25,12 @@
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.ArrayList;
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.BufferedWriter;
+import java.io.FileWriter;
+import java.io.Writer;
 
 /**
  * @file SwitchTableGenerator.java
@@ -50,21 +56,159 @@
         * @param args
         */
        public static void main(String[] args) {
-               // for now, start with this
-               Class c = String.class;
-               try {
-                       c = 
Class.forName("org.gnu.freeway.services.StatsService");
-               } catch(ClassNotFoundException e) {
-                       e.printStackTrace();
+               StringBuffer clsPrefix = new StringBuffer();
+               StringBuffer srcPrefix = new StringBuffer();
+               StringBuffer natPrefix = new StringBuffer();
+               
+               int i;
+               StringBuffer currentArg = null;
+               for(i = 0; i < args.length; i++)
+                       if(args[i].startsWith("-"))
+                               if(currentArg == null)
+                                       if(args[i].equals("-c") && 
clsPrefix.length() == 0)
+                                               currentArg = clsPrefix;
+                                       else if(args[i].equals("-s") && 
srcPrefix.length() == 0)
+                                               currentArg = srcPrefix;
+                                       else if(args[i].equals("-n") && 
natPrefix.length() == 0)
+                                               currentArg = natPrefix;
+                                       else
+                                               break;
+                               else
+                                       break;
+                       else if(currentArg != null) {
+                               currentArg.append(args[i]);
+                               currentArg = null;
+                       } else
+                               break;
+               if(clsPrefix.length() == 0)
+                       clsPrefix.append("build");
+               if(srcPrefix.length() == 0)
+                       srcPrefix.append("src");
+               if(natPrefix.length() == 0)
+                       natPrefix.append("native");
+               if(i < args.length || currentArg != null) {
+                       System.err.println("SwitchTableGenerator");
+                       System.err.println("a source code generator for 
Freeway");
+                       System.err.println();
+                       System.err.println("usage: SwitchTableGenerator [-c 
<directory>] [-s <directory>] [-n <directory>]");
+                       System.err.println(" -c    path to class files 
(default: build)");
+                       System.err.println(" -s    path to java source files 
(default: src)");
+                       System.err.println(" -n    path to c source files 
(default: native)");
                        return;
                }
-               implementClass(c);
+               
+               if(clsPrefix.length() > 0 && 
!clsPrefix.toString().endsWith(File.separator))
+                       clsPrefix.append(File.separatorChar);
+               if(srcPrefix.length() > 0 && 
!srcPrefix.toString().endsWith(File.separator))
+                       srcPrefix.append(File.separatorChar);
+               if(natPrefix.length() > 0 && 
!natPrefix.toString().endsWith(File.separator))
+                       natPrefix.append(File.separatorChar);
+               
+               String servicesSubpath = "org" + File.separator + "gnu" + 
File.separator + "freeway" + File.separator + "services";
+               File classesDir = new File(clsPrefix + servicesSubpath);
+               File sourcesDir = new File(srcPrefix + servicesSubpath + 
File.separator + "impl");
+               File nativeCDir = new File(natPrefix.toString());
+               
+               if(!sourcesDir.exists())
+                       if(!sourcesDir.mkdirs()) {
+                               try {
+                                       System.err.println("Could not create 
nonexistant sources folder \"" + sourcesDir.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not create 
nonexistant sources folder \"" + sourcesDir.getAbsolutePath() + "\".");
+                               }
+                       }
+               
+               File inputClasses[] = classesDir.listFiles(new 
ClassFilesFilter());
+               
+               for(i = 0; i < inputClasses.length; i++) {
+                       Class inputClass = null;
+                       try {
+                               inputClass = 
Class.forName("org.gnu.freeway.services." + 
inputClasses[i].getName().substring(0, inputClasses[i].getName().length() - 6));
+                       } catch(Exception e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not create a 
class from \"" + inputClasses[i].getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not create a 
class from \"" + inputClasses[i].getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       }
+                       
+                       File outputSource = new File(sourcesDir, 
stripPackage(inputClass.getName()) + ".java");
+                       try {
+                               if(!outputSource.createNewFile()) {
+                                       if(!outputSource.delete()) {
+                                               try {
+                                                       
System.err.println("Could not delete existing source file \"" + 
outputSource.getCanonicalPath() + "\".");
+                                               } catch(IOException ee) {
+                                                       
System.err.println("Could not delete existing source file \"" + 
outputSource.getAbsolutePath() + "\".");
+                                               }
+                                               continue;
+                                       }
+                                       if(!outputSource.createNewFile()) {
+                                               try {
+                                                       
System.err.println("Could not create source file \"" + 
outputSource.getCanonicalPath() + "\".");
+                                               } catch(IOException ee) {
+                                                       
System.err.println("Could not create source file \"" + 
outputSource.getAbsolutePath() + "\".");
+                                               }
+                                               continue;
+                                       }
+                               }
+                       } catch(Exception e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not create 
source file \"" + outputSource.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not create 
source file \"" + outputSource.getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       }
+                       
+                       BufferedWriter outputSourceWriter = null;
+                       try {
+                               outputSourceWriter = new BufferedWriter(new 
FileWriter(outputSource));
+                       } catch(IOException e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not open 
source file \"" + outputSource.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not open 
source file \"" + outputSource.getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       }
+                       
+                       try {
+                               implementClass(inputClass, outputSourceWriter);
+                       } catch(IOException e) {
+                               e.printStackTrace();
+                               try {
+                                       System.err.println("Could not write 
source file \"" + outputSource.getCanonicalPath() + "\".");
+                               } catch(IOException ee) {
+                                       System.err.println("Could not write 
source file \"" + outputSource.getAbsolutePath() + "\".");
+                               }
+                               continue;
+                       } catch(IllegalArgumentException e) {
+                               System.err.println(inputClass.getName() + " is 
not an interface.");
+                       } finally {
+                               try {
+                                       outputSourceWriter.flush();
+                               } catch(IOException e) {
+                                       e.printStackTrace();
+                               }
+                               try {
+                                       outputSourceWriter.close();
+                               } catch(IOException e) {
+                                       e.printStackTrace();
+                               }
+                       }
+               }
+               
+               //TODO: generate code for the native portion
        }
        
-       public static void implementClass(Class c) {
+       public static void implementClass(Class c, Writer writer) throws 
IOException, IllegalArgumentException {
                if(!c.isInterface()) {
-                       System.err.println(c.getName() + " is not an 
interface.");
-                       return;
+                       throw new IllegalArgumentException();
                }
                System.err.println("Implementing " + c.getName());
                
@@ -78,7 +222,7 @@
                for(int i = 0; i < methods.length; i++) {
                        StringBuffer buffer = new StringBuffer();
                        StringBuffer arrayBuffer = new StringBuffer();
-                       buffer.append("public " + 
cleanClassName(methods[i].getReturnType().getName(), imports) + " " + 
methods[i].getName() + "(");
+                       buffer.append(" public " + 
cleanClassName(methods[i].getReturnType().getName(), imports) + " " + 
methods[i].getName() + "(");
                        Class[] argTypes = methods[i].getParameterTypes();
                        int j;
                        // add every argument to the arguments list and the 
Object[]
@@ -92,37 +236,37 @@
                                buffer.append(paramClassName + " " + paramName);
                                arrayBuffer.append(paramName);
                        }
-                       buffer.append(") {\n");
+                       buffer.append(") {\n            ");
                        if(!"void".equals(methods[i].getReturnType().getName()))
                                buffer.append("return (" + 
cleanClassName(methods[i].getReturnType().getName(), imports) + ") ");
                        buffer.append("loader.callC(handle, \"" + 
methods[i].getName() + "\", this, new Object[] {" + arrayBuffer + "});\n");
-                       buffer.append("}");
+                       buffer.append(" }");
                        methodList.add(buffer.toString());
                }
                
-               System.out.println("// This class was autogenerated by 
SwitchTableGenerator");
-               System.out.println("package org.gnu.freeway.services.impl;");
-               System.out.println();
+               writer.write("// This class was autogenerated by 
SwitchTableGenerator\n");
+               writer.write("package org.gnu.freeway.services.impl;\n");
+               writer.write("\n");
                Collection importSet = imports.values();
                for(Iterator i = importSet.iterator(); i.hasNext(); )
-                       System.out.println("import " + (String) i.next() + ";");
-               System.out.println();
-               System.out.println("public class " + stripPackage(c.getName()) 
+ " implements " + c.getName() + " {");
-               System.out.println("private CPluginLoader loader;");
-               System.out.println("private CPluginLoader.Handle handle;");
-               System.out.println();
-               System.out.println("public " + stripPackage(c.getName()) + 
"(CPluginLoader loader) {");
-               System.out.println("this.loader = loader;");
-               System.out.println("handle = loader.loadService(\"" + 
classToLibraryName(c.getName()) + "\");");
-               System.out.println("}");
-               System.out.println();
-               System.out.println("protected void finalize() {");
-               System.out.println("loader.unloadService(handle);");
-               System.out.println("}");
-               System.out.println();
+                       writer.write("import " + (String) i.next() + ";\n");
+               writer.write("\n");
+               writer.write("public class " + stripPackage(c.getName()) + " 
implements " + c.getName() + " {\n");
+               writer.write("  private CPluginLoader loader;\n");
+               writer.write("  private CPluginLoader.Handle handle;\n");
+               writer.write("\n");
+               writer.write("  public " + stripPackage(c.getName()) + 
"(CPluginLoader loader) {\n");
+               writer.write("          this.loader = loader;\n");
+               writer.write("          handle = loader.loadService(\"" + 
classToLibraryName(c.getName()) + "\");\n");
+               writer.write("  }\n");
+               writer.write("\n");
+               writer.write("  protected void finalize() {\n");
+               writer.write("          loader.unloadService(handle);\n");
+               writer.write("  }\n");
+               writer.write("\n");
                for(Iterator i = methodList.iterator(); i.hasNext(); )
-                       System.out.println((String) i.next() + "\n");
-               System.out.println("}");
+                       writer.write((String) i.next() + "\n\n");
+               writer.write("}\n");
        }
        
        public static String classToLibraryName(String className) {
@@ -140,9 +284,9 @@
                String nameEnd = input.substring(lastDot + 1);
                if("java.lang".equals(nameStart)) 
                        return nameEnd; // already included
-               if(importMap.containsKey(nameEnd)) {
-                       if(input.equals((String) importMap.get(nameEnd)))
-                               return nameEnd; // already imported
+               if(importMap == null || importMap.containsKey(nameEnd)) {
+                       if(importMap == null || input.equals((String) 
importMap.get(nameEnd)))
+                               return nameEnd; // already imported or we have 
no list of imported classes
                        else
                                return input; // another class with the same 
name has already been imported
                } else {
@@ -192,4 +336,15 @@
                                return fields[i];
                throw new NoSuchFieldException(name);
        }
+       
+       private static class ClassFilesFilter implements FilenameFilter {
+               public boolean accept(File dir, String name) {
+                       if(name == null || !name.endsWith(".class"))
+                               return false;
+                       return true;
+               }
+               public ClassFilesFilter() {
+                       
+               }
+       }
 }

Modified: freeway/src/org/gnu/freeway/services/impl/StatsService.java
===================================================================
--- freeway/src/org/gnu/freeway/services/impl/StatsService.java 2006-06-04 
16:33:36 UTC (rev 2957)
+++ freeway/src/org/gnu/freeway/services/impl/StatsService.java 2006-06-04 
21:40:53 UTC (rev 2958)
@@ -9,32 +9,32 @@
 import org.gnu.freeway.server.CPluginLoader;
 
 public class StatsService implements org.gnu.freeway.services.StatsService {
-private CPluginLoader loader;
-private CPluginLoader.Handle handle;
+       private CPluginLoader loader;
+       private CPluginLoader.Handle handle;
 
-public StatsService(CPluginLoader loader) {
-this.loader = loader;
-handle = loader.loadService("module_stats");
-}
+       public StatsService(CPluginLoader loader) {
+               this.loader = loader;
+               handle = loader.loadService("module_stats");
+       }
 
-protected void finalize() {
-loader.unloadService(handle);
-}
+       protected void finalize() {
+               loader.unloadService(handle);
+       }
 
-public void change(ConstCInt arg0, ConstCInt arg1) {
-loader.callC(handle, "change", this, new Object[] {arg0, arg1});
-}
+       public void change(ConstCInt arg0, ConstCInt arg1) {
+               loader.callC(handle, "change", this, new Object[] {arg0, arg1});
+       }
 
-public CLong get(ConstCInt arg0) {
-return (CLong) loader.callC(handle, "get", this, new Object[] {arg0});
-}
+       public CLong get(ConstCInt arg0) {
+               return (CLong) loader.callC(handle, "get", this, new Object[] 
{arg0});
+       }
 
-public void set(ConstCInt arg0, ConstCLong arg1) {
-loader.callC(handle, "set", this, new Object[] {arg0, arg1});
-}
+       public void set(ConstCInt arg0, ConstCLong arg1) {
+               loader.callC(handle, "set", this, new Object[] {arg0, arg1});
+       }
 
-public CInt create(ConstCString arg0) {
-return (CInt) loader.callC(handle, "create", this, new Object[] {arg0});
-}
+       public CInt create(ConstCString arg0) {
+               return (CInt) loader.callC(handle, "create", this, new Object[] 
{arg0});
+       }
 
 }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]