Ignore:
Timestamp:
08/17/10 19:29:27 (9 years ago)
Author:
curtis
Message:

Initial version of Jar2Lib implementation.

Location:
branches/maven/projects/jar2lib
Files:
112 added
2 edited
1 copied
7 moved

Legend:

Unmodified
Added
Removed
  • branches/maven/projects/jar2lib/src/main/java/loci/jar2lib/ClassList.java

    r6109 r6806  
    11// 
    2 // SourceList.java 
     2// ClassList.java 
    33// 
    44 
    55/* 
    6         LOCI autogen package for programmatically generating source code. 
    7         Copyright (C) 2005-2010 UW-Madison LOCI and Glencoe Software, Inc. 
     6Jar2Lib tool for generating C++ proxy classes for a Java library. 
     7Copyright (C) 2010-@year@ UW-Madison LOCI. 
    88 
    9         This program is free software; you can redistribute it and/or modify 
    10         it under the terms of the GNU General Public License as published by 
    11         the Free Software Foundation; either version 2 of the License, or 
    12         (at your option) any later version. 
     9This program is free software; you can redistribute it and/or modify 
     10it under the terms of the GNU General Public License as published by 
     11the Free Software Foundation; either version 2 of the License, or 
     12(at your option) any later version. 
    1313 
    14         This program is distributed in the hope that it will be useful, 
    15         but WITHOUT ANY WARRANTY; without even the implied warranty of 
    16         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    17         GNU General Public License for more details. 
     14This program is distributed in the hope that it will be useful, 
     15but WITHOUT ANY WARRANTY; without even the implied warranty of 
     16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     17GNU General Public License for more details. 
    1818 
    19         You should have received a copy of the GNU General Public License 
    20         along with this program; if not, write to the Free Software 
    21         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
     19You should have received a copy of the GNU General Public License 
     20along with this program; if not, write to the Free Software 
     21Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    2222*/ 
    2323 
    24 import java.io.File; 
     24package loci.jar2lib; 
     25 
    2526import java.io.IOException; 
     27import java.util.ArrayList; 
    2628import java.util.Collections; 
    27 import java.util.Vector; 
     29import java.util.Enumeration; 
     30import java.util.List; 
     31import java.util.jar.JarEntry; 
    2832import java.util.jar.JarFile; 
    29 import java.util.jar.JarEntry; 
    30 import java.util.Enumeration; 
    31 import org.apache.velocity.VelocityContext; 
    32 import org.apache.velocity.app.VelocityEngine; 
    3333 
    3434/** 
    35 * A SourceList is a list of source files rooted at a particular directory. 
    36 * 
    37 * <dl><dt><b>Source code:</b></dt> 
    38 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/components/bio-formats/auto/SourceList.java">Trac</a>, 
    39 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/components/bio-formats/auto/SourceList.java">SVN</a></dd></dl> 
    40 * 
    41 * @author Curtis Rueden ctrueden at wisc.edu 
    42 */ 
    43 public class SourceList { 
     35 * A ClassList is a list of Java classes from a particular JAR file. 
     36 * Note that unlike usual Java convention, the fully qualified class names 
     37 * will be separated by forward slashes rather than dots. 
     38 * 
     39 * <dl><dt><b>Source code:</b></dt> 
     40 * <dd><a href="http://dev.loci.wisc.edu/trac/java/browser/trunk/projects/jar2lib/src/main/java/loci/jar2lib/ClassList.java">Trac</a>, 
     41 * <a href="http://dev.loci.wisc.edu/svn/java/trunk/projects/jar2lib/src/main/java/loci/jar2lib/ClassList.java">SVN</a></dd></dl> 
     42 * 
     43 * @author Curtis Rueden ctrueden at wisc.edu 
     44 */ 
     45public class ClassList { 
    4446 
    45         // -- Fields -- 
     47  // -- Fields -- 
    4648 
    47         /** Root directory prefix. */ 
    48         private String rootDir; 
     49  /** The list of classes. */ 
     50  private List<String> classes; 
    4951 
    50         /** The list of source files, relative to the root directory. */ 
    51         private Vector<String> files; 
     52  // -- Constructor -- 
    5253 
    53         // -- Constructor -- 
     54  /** 
     55   * Constructs a list of Java classes corresponding 
     56   * to those in the given JAR file. 
     57   */ 
     58  public ClassList(String jarPath) throws IOException { 
     59    classes = new ArrayList<String>(); 
     60    buildList(jarPath); 
     61  } 
    5462 
    55         /** Constructs list of source files rooted at the given directory. */ 
    56         public SourceList(JarFile jarFile) throws IOException { 
    57                 // list Java source files 
    58                 files = new Vector<String>(); 
    59                 buildList(jarFile); 
    60         } 
     63  // -- ClassList API methods -- 
    6164 
    62         /** Constructs list of source files rooted at the given directory. */ 
    63         public SourceList(String rootDir) throws IOException { 
    64                 // list Java source files 
    65                 files = new Vector<String>(); 
    66                 setRoot(rootDir); 
    67         } 
     65  /** Builds the list of Java classes contained in the given JAR file. */ 
     66  public synchronized void buildList(String jarPath) throws IOException { 
     67    classes.clear(); 
     68    listFiles(new JarFile(jarPath)); 
     69    Collections.sort(classes); 
     70  } 
    6871 
    69         // -- SourceList API methods -- 
     72  /** Gets the list of Java classes. */ 
     73  public List<String> classes() { 
     74    return classes; 
     75  } 
    7076 
    71         /** Changes the list to correspond to the given root directory. */ 
    72         public void setRoot(String rootDir) { 
    73                 rootDir = rootDir.replace(File.separatorChar, '/'); // Windows stupidity 
    74                 this.rootDir = rootDir; 
    75                 files.removeAllElements(); 
    76                 listFiles(new File(rootDir)); 
    77                 Collections.sort(files); 
    78         } 
     77  // -- Helper methods -- 
    7978 
    80         /** Changes the list to correspond to the given root directory. */ 
    81         public void buildList(JarFile jarFile) { 
    82                 this.rootDir = rootDir; 
    83                 files.removeAllElements(); 
    84                 listFiles(jarFile); 
    85                 Collections.sort(files); 
    86         } 
     79  /** Finds all Java classes within the given JAR file. */ 
     80  private void listFiles(JarFile jarFile) { 
     81    Enumeration<JarEntry> jarEntries = jarFile.entries(); 
     82    if (jarEntries == null) return; 
    8783 
    88         /** Gets the list of source files, relative to the root directory. */ 
    89                 public Vector<String> files() { 
    90                 return files; 
    91         } 
     84    while (jarEntries.hasMoreElements()) { 
     85      final JarEntry entry = jarEntries.nextElement(); 
     86      final String name = entry.getName(); 
     87      if (name == null) continue; 
     88      final String lName = name.toLowerCase(); 
     89      if (!lName.endsWith(".class")) continue; // skip non-classes 
     90      if (lName.indexOf("$") >= 0) continue; // skip inner classes 
     91      final String className = name.substring(0, name.length() - 6); 
     92      classes.add(className); 
     93    } 
     94  } 
    9295 
    93         // -- Helper methods -- 
     96  // -- Utility methods -- 
    9497 
    95         /** Recursively finds all Java source files. */ 
    96         private void listFiles(JarFile jarFile) { 
    97                 Enumeration<JarEntry> jarEntries = jarFile.entries(); 
    98                 if (jarEntries == null) 
    99                         return; 
     98  /** Gets the package name for the given fully qualified class name. */ 
     99  public static String packageName(String className) { 
     100    int slash = className.lastIndexOf("/"); 
     101    return slash < 0 ? "" : className.substring(0, slash); 
     102  } 
    100103 
    101                 while (jarEntries.hasMoreElements()) { 
    102                         JarEntry entry = jarEntries.nextElement(); 
    103                         String name = entry.getName(); 
    104                         if (name.toLowerCase().endsWith(".class")) { 
    105                                 int index = name.lastIndexOf(".class"); 
    106                                 String javaName = name.substring(0, index) + ".java"; 
    107                                 files.add(javaName); 
    108                         } 
    109                 } 
    110         } 
     104  /** Returns true iff the two classes are from different packages. */ 
     105  public static boolean isNewPackage(String class1, String class2) { 
     106    return !packageName(class1).equals(packageName(class2)); 
     107  } 
    111108 
    112         /** Recursively finds all Java source files. */ 
    113         private void listFiles(File dir) { 
    114                 File[] list = dir.listFiles(); 
    115                 if (list == null) { 
    116                         return; 
    117                 } 
    118                 for (File f : list) { 
    119                         if (f.isDirectory()) { 
    120                                 listFiles(f); 
    121                         } 
    122                         String path = f.getPath(); 
    123                         path = path.replace(File.separatorChar, '/'); // Windows stupidity 
    124                         if (!path.startsWith(rootDir)) { 
    125                                 continue; 
    126                         } 
    127                         if (!path.toLowerCase().endsWith(".java")) { 
    128                                 continue; 
    129                         } 
    130                         path = path.substring(rootDir.length() + 1); // relative to root path 
    131                         files.add(path); 
    132                 } 
    133         } 
     109  /** Gets the C++ header name corresponding to the given class name. */ 
     110  public static String header(String className) { 
     111    return className + ".h"; 
     112  } 
    134113 
    135         // -- Utility methods -- 
     114  /** Gets the C++ namespace fragment corresponding to the given class name. */ 
     115  public static String namespace(String className) { 
     116    return packageName(className).replaceAll("/", "::"); 
     117  } 
    136118 
    137         public static String packageName(String path) { 
    138         int slash = path.lastIndexOf("/"); 
    139                 return slash < 0 ? "" : path.substring(0, slash); 
    140         } 
    141  
    142         /** Returns true iff the two files are from different directories. */ 
    143         public static boolean isNewPackage(String path1, String path2) { 
    144                 return !packageName(path1).equals(packageName(path2)); 
    145         } 
    146  
    147         public static String header(String path) { 
    148                 return path.replace(".java", ".h"); 
    149         } 
    150  
    151         public static String namespace(String path) { 
    152                 return packageName(path).replaceAll("/", "::"); 
    153         } 
    154  
    155         public static void main(String[] args) throws Exception { 
    156                 try { 
    157                         if (args.length < 2) { 
    158                                 System.out.println( 
    159                                         "Usage: java JaceHeaderAutogen component-name source-dir"); 
    160                                 System.out.println("    E.g.: java JaceHeaderAutogen " + 
    161                                         "bio-formats ~/svn/java/components/bio-formats/src"); 
    162                                 System.exit(1); 
    163                         } 
    164                         String component = args[0]; 
    165                         String sourceDir = args[1]; 
    166                  
    167                         String headerFile = component + ".h"; 
    168                         String headerLabel = headerFile.toUpperCase().replaceAll("\\W", "_"); 
    169                  
    170                         // initialize Velocity 
    171                         VelocityEngine ve = VelocityTools.createEngine(); 
    172                         VelocityContext context = VelocityTools.createContext(); 
    173                  
    174                         // parse header file template 
    175                         JarFile jf = new JarFile(sourceDir); 
    176                         SourceList javaList = new SourceList(jf); 
    177                  
    178                         context.put("headerFile", headerFile); 
    179                         context.put("headerLabel", headerLabel); 
    180                         context.put("q", javaList); 
    181                  
    182                         // generate C++ header file 
    183                         VelocityTools.processTemplate(ve, context, "jace/header.vm", headerFile); 
    184                 } catch (Exception e) { 
    185                         System.out.println(e.toString()); 
    186                         e.printStackTrace(); 
    187                 } 
    188         } 
    189119} 
  • branches/maven/projects/jar2lib/src/main/java/loci/jar2lib/FixProxies.java

    r6670 r6806  
    44 
    55/* 
    6 OME Bio-Formats C++ bindings for native access to Bio-Formats Java library. 
    7 Copyright (c) 2008-@year@, UW-Madison LOCI. 
    8 All rights reserved. 
     6Jar2Lib tool for generating C++ proxy classes for a Java library. 
     7Copyright (C) 2010-@year@ UW-Madison LOCI. 
    98 
    10 Redistribution and use in source and binary forms, with or without 
    11 modification, are permitted provided that the following conditions are met: 
    12     * Redistributions of source code must retain the above copyright 
    13       notice, this list of conditions and the following disclaimer. 
    14     * Redistributions in binary form must reproduce the above copyright 
    15       notice, this list of conditions and the following disclaimer in the 
    16       documentation and/or other materials provided with the distribution. 
    17     * Neither the name of the UW-Madison LOCI nor the 
    18       names of its contributors may be used to endorse or promote products 
    19       derived from this software without specific prior written permission. 
     9This program is free software; you can redistribute it and/or modify 
     10it under the terms of the GNU General Public License as published by 
     11the Free Software Foundation; either version 2 of the License, or 
     12(at your option) any later version. 
    2013 
    21 THIS SOFTWARE IS PROVIDED BY UW-MADISON LOCI ''AS IS'' AND ANY 
    22 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
    23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    24 DISCLAIMED. IN NO EVENT SHALL UW-MADISON LOCI BE LIABLE FOR ANY 
    25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
    27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
    28 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
    30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
     14This program is distributed in the hope that it will be useful, 
     15but WITHOUT ANY WARRANTY; without even the implied warranty of 
     16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     17GNU General Public License for more details. 
     18 
     19You should have received a copy of the GNU General Public License 
     20along with this program; if not, write to the Free Software 
     21Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    3122*/ 
    3223 
    33 /* 
    34 IMPORTANT NOTE: Although this software is distributed according to a 
    35 "BSD-style" license, it requires the Bio-Formats Java library to do 
    36 anything useful, which is licensed under the GPL v2 or later. 
    37 As such, if you wish to distribute this software with Bio-Formats itself, 
    38 your combined work must be distributed under the terms of the GPL. 
    39 */ 
     24package loci.jar2lib; 
    4025 
    4126import java.io.BufferedReader; 
     27import java.io.IOException; 
    4228import java.io.InputStreamReader; 
    43 import java.io.IOException; 
    4429import java.util.ArrayList; 
    4530 
    4631/** 
    47  * FixProxies is a program to post-process the Jace generated proxies. 
     32 * FixProxies is a program to post-process Jace-generated proxies. 
    4833 * 
    4934 * This step is necessary to avoid potential global namespace name clashes 
    50  * with various Java constants. See conflicts.txt for a list. 
     35 * with various Java constants. 
    5136 * 
    5237 * <dl><dt><b>Source code:</b></dt> 
    53  * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/components/native/bf-cpp/src/FixProxies.java">Trac</a>, 
    54  * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/components/native/bf-cpp/src/FixProxies.java">SVN</a></dd></dl> 
     38 * <dd><a href="http://dev.loci.wisc.edu/trac/java/browser/trunk/projects/jar2lib/src/main/java/loci/jar2lib/FixProxies.java">Trac</a>, 
     39 * <a href="http://dev.loci.wisc.edu/svn/java/trunk/projects/jar2lib/src/main/java/loci/jar2lib/FixProxies.java">SVN</a></dd></dl> 
    5540 * 
    5641 * @author Curtis Rueden ctrueden at wisc.edu 
     
    5944 
    6045  // -- Constants -- 
    61  
    62   private static final String CONFLICTS_FILE = "conflicts.txt"; 
    6346 
    6447  private static final String PATCH_PREFIX = "JACE"; 
     
    7457  // -- Fields -- 
    7558 
    76   private String pathPrefix; 
    7759  private ArrayList<String> constants; 
    7860 
    7961  // -- Constructor -- 
    8062 
    81   public FixProxies(String pathPrefix) throws IOException { 
    82     this.pathPrefix = pathPrefix; 
    83  
     63  public FixProxies(String conflictsFile) 
     64    throws IOException 
     65  { 
    8466    // parse list of conflicting constants 
    8567    constants = new ArrayList<String>(); 
    8668    BufferedReader in = new BufferedReader(new InputStreamReader( 
    87       FixProxies.class.getResourceAsStream(CONFLICTS_FILE))); 
     69      FixProxies.class.getResourceAsStream(conflictsFile))); 
    8870    while (true) { 
    8971      String line = in.readLine(); 
     
    9981  // -- FixProxies methods -- 
    10082 
    101   public void fixProxies() { 
     83  public void fixProxies(String pathPrefix) { 
    10284    for (String entry : constants) { 
    10385      int dot = entry.lastIndexOf("."); 
     
    126108 
    127109  public static void main(String[] args) throws IOException { 
    128     if (args == null || args.length < 1) { 
    129       System.out.println("Usage: java FixProxies /path/to/proxies"); 
    130       return; 
     110    if (args == null || args.length < 2) { 
     111      System.err.println("Usage: java " + FixProxies.class.getName() + 
     112        " conflicts.txt /path/to/proxies [/path/to/more/proxies ...]"); 
     113      System.exit(1); 
    131114    } 
    132     new FixProxies(args[0]).fixProxies(); 
     115    final String conflictsFile = args[0]; 
     116    final FixProxies fixProxies = new FixProxies(conflictsFile); 
     117    for (int i = 1; i < args.length; i++) fixProxies.fixProxies(args[i]); 
    133118  } 
    134119 
  • branches/maven/projects/jar2lib/src/main/java/loci/jar2lib/StringReplace.java

    r6670 r6806  
    44 
    55/* 
    6 OME Bio-Formats C++ bindings for native access to Bio-Formats Java library. 
    7 Copyright (c) 2008-@year@, UW-Madison LOCI. 
    8 All rights reserved. 
     6Jar2Lib tool for generating C++ proxy classes for a Java library. 
     7Copyright (C) 2010-@year@ UW-Madison LOCI. 
    98 
    10 Redistribution and use in source and binary forms, with or without 
    11 modification, are permitted provided that the following conditions are met: 
    12     * Redistributions of source code must retain the above copyright 
    13       notice, this list of conditions and the following disclaimer. 
    14     * Redistributions in binary form must reproduce the above copyright 
    15       notice, this list of conditions and the following disclaimer in the 
    16       documentation and/or other materials provided with the distribution. 
    17     * Neither the name of the UW-Madison LOCI nor the 
    18       names of its contributors may be used to endorse or promote products 
    19       derived from this software without specific prior written permission. 
     9This program is free software; you can redistribute it and/or modify 
     10it under the terms of the GNU General Public License as published by 
     11the Free Software Foundation; either version 2 of the License, or 
     12(at your option) any later version. 
    2013 
    21 THIS SOFTWARE IS PROVIDED BY UW-MADISON LOCI ''AS IS'' AND ANY 
    22 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
    23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    24 DISCLAIMED. IN NO EVENT SHALL UW-MADISON LOCI BE LIABLE FOR ANY 
    25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
    27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
    28 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
    30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
     14This program is distributed in the hope that it will be useful, 
     15but WITHOUT ANY WARRANTY; without even the implied warranty of 
     16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     17GNU General Public License for more details. 
     18 
     19You should have received a copy of the GNU General Public License 
     20along with this program; if not, write to the Free Software 
     21Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    3122*/ 
    3223 
    33 /* 
    34 IMPORTANT NOTE: Although this software is distributed according to a 
    35 "BSD-style" license, it requires the Bio-Formats Java library to do 
    36 anything useful, which is licensed under the GPL v2 or later. 
    37 As such, if you wish to distribute this software with Bio-Formats itself, 
    38 your combined work must be distributed under the terms of the GPL. 
    39 */ 
     24package loci.jar2lib; 
    4025 
    4126import java.io.BufferedReader; 
     
    4833 
    4934/** 
    50  * A program to filter and replace strings in a file. 
    51  * 
    52  * The main reason this program exists is because sed is 
    53  * not available by default on Windows systems. 
     35 * A program to filter and replace strings in a file&mdash;in 
     36 * other words, a poor man's pure Java version of sed. 
    5437 * 
    5538 * <dl><dt><b>Source code:</b></dt> 
    56  * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/components/native/bf-cpp/src/StringReplace.java">Trac</a>, 
    57  * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/components/native/bf-cpp/src/StringReplace.java">SVN</a></dd></dl> 
     39 * <dd><a href="http://dev.loci.wisc.edu/trac/java/browser/trunk/projects/jar2lib/src/main/java/loci/jar2lib/StringReplace.java">Trac</a>, 
     40 * <a href="http://dev.loci.wisc.edu/svn/java/trunk/projects/jar2lib/src/main/java/loci/jar2lib/StringReplace.java">SVN</a></dd></dl> 
    5841 * 
    5942 * @author Curtis Rueden ctrueden at wisc.edu 
     
    144127  public static void main(String[] args) { 
    145128    if (args == null || args.length < 3) { 
    146       System.out.println("Usage: java StringReplace " + 
    147         "inputPattern outputPattern file [file2 file3 ...]"); 
     129      System.out.println("Usage: java " + StringReplace.class.getName() + 
     130        " inputPattern outputPattern file [file2 file3 ...]"); 
    148131      return; 
    149132    } 
  • branches/maven/projects/jar2lib/src/main/java/loci/jar2lib/VelocityAutogen.java

    r5418 r6806  
    11// 
    2 // JaceHeaderAutogen.java 
     2// VelocityAutogen.java 
    33// 
    44 
    55/* 
    6 LOCI autogen package for programmatically generating source code. 
    7 Copyright (C) 2005-@year@ UW-Madison LOCI and Glencoe Software, Inc. 
     6Jar2Lib tool for generating C++ proxy classes for a Java library. 
     7Copyright (C) 2010-@year@ UW-Madison LOCI. 
    88 
    99This program is free software; you can redistribute it and/or modify 
     
    2222*/ 
    2323 
     24package loci.jar2lib; 
     25 
     26import java.io.BufferedReader; 
     27import java.io.File; 
     28import java.io.FileReader; 
     29import java.io.IOException; 
     30 
    2431import org.apache.velocity.VelocityContext; 
    2532import org.apache.velocity.app.VelocityEngine; 
    2633 
    2734/** 
    28  * Automatically generates a C++ header file for use with Jace, listing all 
    29  * classes within the given component. As of this writing, this functionality 
    30  * is mainly used to generate bio-formats.h for the Bio-Formats C++ bindings, 
    31  * but could in principle be used to generate a Jace-friendly list of classes 
    32  * for any of LOCI's Java components. 
     35 * Automatically generates code for a Jar2Lib project. Specifically: 
     36 * <ol> 
     37 *   <li>C++ header files for use with Jace, listing all 
     38 *     classes within the given JAR files.</li> 
     39 *   <li>A CMake build file for use compiling the project.</li> 
     40 * </ol> 
    3341 * 
    3442 * <dl><dt><b>Source code:</b></dt> 
    35  * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/components/bio-formats/auto/JaceHeaderAutogen.java">Trac</a>, 
    36  * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/components/bio-formats/auto/JaceHeaderAutogen.java">SVN</a></dd></dl> 
     43 * <dd><a href="http://dev.loci.wisc.edu/trac/java/browser/trunk/projects/jar2lib/src/main/java/loci/jar2lib/VelocityAutogen.java">Trac</a>, 
     44 * <a href="http://dev.loci.wisc.edu/svn/java/trunk/projects/jar2lib/src/main/java/loci/jar2lib/VelocityAutogen.java">SVN</a></dd></dl> 
    3745 * 
    3846 * @author Curtis Rueden ctrueden at wisc.edu 
    3947 */ 
    40 public class JaceHeaderAutogen { 
     48public class VelocityAutogen { 
    4149 
    42   // -- Main method -- 
     50  // -- Fields -- 
    4351 
    44   public static void main(String[] args) throws Exception { 
    45     if (args.length < 2) { 
    46       System.out.println( 
    47         "Usage: java JaceHeaderAutogen component-name source-dir"); 
    48       System.out.println("    E.g.: java JaceHeaderAutogen " + 
    49         "bio-formats ~/svn/java/components/bio-formats/src"); 
    50       System.exit(1); 
     52  private String javaHeader, scriptHeader; 
     53 
     54  // -- Constructor -- 
     55 
     56  public VelocityAutogen(String headerPath) throws IOException { 
     57    if (headerPath == null) javaHeader = scriptHeader = ""; 
     58    else { 
     59      final File headerFile = new File(headerPath); 
     60      if (!headerFile.exists()) { 
     61        throw new IllegalArgumentException("Invalid header file: " + 
     62          headerPath); 
     63      } 
     64      final BufferedReader in = new BufferedReader(new FileReader(headerFile)); 
     65      final StringBuilder javaBuilder = new StringBuilder("\n"); 
     66      final StringBuilder scriptBuilder = new StringBuilder("\n"); 
     67      while (true) { 
     68        final String line = in.readLine(); 
     69        if (line == null) break; // EOF 
     70        javaBuilder.append("//"); 
     71        if (!line.equals("")) javaBuilder.append(" "); 
     72        javaBuilder.append(line); 
     73        javaBuilder.append("\n"); 
     74        scriptBuilder.append("#"); 
     75        if (!line.equals("")) scriptBuilder.append(" "); 
     76        scriptBuilder.append(line); 
     77        scriptBuilder.append("\n"); 
     78      } 
     79      in.close(); 
     80      javaHeader = javaBuilder.toString(); 
     81      scriptHeader = scriptBuilder.toString(); 
    5182    } 
    52     String component = args[0]; 
    53     String sourceDir = args[1]; 
     83  } 
    5484 
    55     String headerFile = component + ".h"; 
    56     String headerLabel = headerFile.toUpperCase().replaceAll("\\W", "_"); 
     85  // -- VelocityAutogen methods -- 
     86 
     87  public void createJaceHeader(String jarPath, String outputPath) 
     88    throws VelocityException, IOException 
     89  { 
     90    final String jarName = new File(jarPath).getName(); 
     91    int dot = jarName.lastIndexOf("."); 
     92    if (dot < 0) dot = jarName.length(); 
     93    final String headerName = jarName.substring(0, dot) + ".h"; 
     94    final String headerLabel = headerName.toUpperCase().replaceAll("\\W", "_"); 
     95    final File headerFile = new File(outputPath, headerName); 
     96    final String headerPath = headerFile.getAbsolutePath(); 
     97 
     98    // parse header file template 
     99    final ClassList classList = new ClassList(jarPath); 
    57100 
    58101    // initialize Velocity 
     
    60103    VelocityContext context = VelocityTools.createContext(); 
    61104 
    62     // parse header file template 
    63     SourceList javaList = new SourceList(sourceDir); 
    64  
    65     context.put("headerFile", headerFile); 
     105    context.put("headerBlock", javaHeader); 
    66106    context.put("headerLabel", headerLabel); 
    67     context.put("q", javaList); 
     107    context.put("headerName", headerName); 
     108    context.put("q", classList); 
    68109 
    69110    // generate C++ header file 
    70     VelocityTools.processTemplate(ve, context, "jace/header.vm", headerFile); 
     111    VelocityTools.processTemplate(ve, context, "jace-header.vm", headerPath); 
     112  } 
     113 
     114  public void createCMakeLists(String projectId, String projectName, 
     115    String outputPath) throws VelocityException, IOException 
     116  { 
     117    final File buildFile = new File(outputPath, "CMakeLists.txt"); 
     118    final String buildPath = buildFile.getAbsolutePath(); 
     119 
     120    // initialize Velocity 
     121    VelocityEngine ve = VelocityTools.createEngine(); 
     122    VelocityContext context = VelocityTools.createContext(); 
     123 
     124    context.put("headerBlock", scriptHeader); 
     125    context.put("projectId", projectId); 
     126    context.put("projectName", projectName); 
     127 
     128    // generate CMakeLists.txt file 
     129    VelocityTools.processTemplate(ve, context, "CMakeLists.vm", buildPath); 
    71130  } 
    72131 
  • branches/maven/projects/jar2lib/src/main/java/loci/jar2lib/VelocityTools.java

    r5418 r6806  
    44 
    55/* 
    6 LOCI autogen package for programmatically generating source code. 
    7 Copyright (C) 2005-@year@ UW-Madison LOCI and Glencoe Software, Inc. 
     6Jar2Lib tool for generating C++ proxy classes for a Java library. 
     7Copyright (C) 2010-@year@ UW-Madison LOCI. 
    88 
    99This program is free software; you can redistribute it and/or modify 
     
    2222*/ 
    2323 
     24package loci.jar2lib; 
     25 
    2426import java.io.FileWriter; 
     27import java.io.IOException; 
    2528import java.io.PrintWriter; 
    2629import java.io.StringWriter; 
     
    3841 * 
    3942 * <dl><dt><b>Source code:</b></dt> 
    40  * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/components/bio-formats/auto/VelocityTools.java">Trac</a>, 
    41  * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/components/bio-formats/auto/VelocityTools.java">SVN</a></dd></dl> 
     43 * <dd><a href="http://dev.loci.wisc.edu/trac/java/browser/trunk/projects/jar2lib/src/main/java/loci/jar2lib/VelocityTools.java">Trac</a>, 
     44 * <a href="http://dev.loci.wisc.edu/svn/java/trunk/projects/jar2lib/src/main/java/loci/jar2lib/VelocityTools.java">SVN</a></dd></dl> 
    4245 * 
    4346 * @author Curtis Rueden ctrueden at wisc.edu 
     
    4548public class VelocityTools { 
    4649 
    47   public static VelocityEngine createEngine() 
    48     // NB: No choice, as VelocityEngine.init(Properties) throws Exception 
    49     throws Exception 
    50   { 
     50  public static VelocityEngine createEngine() throws VelocityException { 
    5151    // initialize Velocity engine; enable loading of templates as resources 
    5252    VelocityEngine ve = new VelocityEngine(); 
     
    5555    p.setProperty("class.resource.loader.class", 
    5656      "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 
    57     ve.init(p); 
     57    try { 
     58      ve.init(p); 
     59    } 
     60    // NB: VelocityEngine.init(Properties) throws Exception. 
     61    catch (Exception exc) { 
     62      throw new VelocityException(exc); 
     63    } 
    5864    return ve; 
    5965  } 
     
    7379  public static void processTemplate(VelocityEngine ve, 
    7480    VelocityContext context, String inFile, String outFile) 
    75     // NB: No choice, as VelocityEngine.getTemplate(String) throws Exception 
    76     throws Exception 
     81    throws VelocityException, IOException 
    7782  { 
    7883    System.out.print("Writing " + outFile + ": "); 
    79     Template t = ve.getTemplate(inFile); 
    80     StringWriter writer = new StringWriter(); 
     84    final Template t; 
     85    try { 
     86      t = ve.getTemplate(inFile); 
     87    } 
     88    // NB: VelocityEngine.getTemplate(String) throws Exception. 
     89    catch (Exception exc) { 
     90      throw new VelocityException(exc); 
     91    } 
     92    final StringWriter writer = new StringWriter(); 
    8193    t.merge(context, writer); 
    82     PrintWriter out = new PrintWriter(new FileWriter(outFile)); 
     94    final PrintWriter out = new PrintWriter(new FileWriter(outFile)); 
    8395    out.print(writer.toString()); 
    8496    out.close(); 
  • branches/maven/projects/jar2lib/src/main/resources/jace-header.vm

    r6053 r6806  
    11// 
    2 // $headerFile 
     2// $headerName 
    33// 
    4  
    5 /* 
    6 OME Bio-Formats C++ bindings for native access to Bio-Formats Java library. 
    7 Copyright (c) 2008-@year@, UW-Madison LOCI. 
    8 All rights reserved. 
    9  
    10 Redistribution and use in source and binary forms, with or without 
    11 modification, are permitted provided that the following conditions are met: 
    12     * Redistributions of source code must retain the above copyright 
    13       notice, this list of conditions and the following disclaimer. 
    14     * Redistributions in binary form must reproduce the above copyright 
    15       notice, this list of conditions and the following disclaimer in the 
    16       documentation and/or other materials provided with the distribution. 
    17     * Neither the name of the UW-Madison LOCI nor the 
    18       names of its contributors may be used to endorse or promote products 
    19       derived from this software without specific prior written permission. 
    20  
    21 THIS SOFTWARE IS PROVIDED BY UW-MADISON LOCI ''AS IS'' AND ANY 
    22 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
    23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    24 DISCLAIMED. IN NO EVENT SHALL UW-MADISON LOCI BE LIABLE FOR ANY 
    25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
    27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
    28 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
    30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    31 */ 
    32  
    33 /* 
    34 IMPORTANT NOTE: Although this software is distributed according to a 
    35 "BSD-style" license, it requires the Bio-Formats Java library to do 
    36 anything useful, which is licensed under the GPL v2 or later. 
    37 As such, if you wish to distribute this software with Bio-Formats itself, 
    38 your combined work must be distributed under the terms of the GPL. 
    39 */ 
    40  
     4$headerBlock 
    415/*----------------------------------------------------------------------------- 
    426 * 
     
    5317 
    5418#set ($last = "") 
    55 #foreach ($path in $q.files()) 
    56 #if ($last != "" && $q.isNewPackage($path, $last)) 
     19#foreach ($className in $q.classes()) 
     20#if ($last != "" && $q.isNewPackage($className, $last)) 
    5721//using namespace jace::proxy::${q.namespace($last)}; 
    5822 
    59 #end##if ($last != "" && $q.isNewPackage($path, $last)) 
    60 \#include "jace/proxy/${q.header($path)}" 
    61 #set ($last = $path) 
    62 #end##foreach $path 
     23#end##if ($last != "" && $q.isNewPackage($className, $last)) 
     24\#include "jace/proxy/${q.header($className)}" 
     25#set ($last = $className) 
     26#end##foreach $className 
    6327//using namespace jace::proxy::${q.namespace($last)}; 
    6428 
  • branches/maven/projects/jar2lib/src/main/resources/jace/CMakeLists.txt

    r6670 r6806  
    33# 
    44 
    5 # OME Bio-Formats C++ bindings for native access to Bio-Formats Java library. 
    6 # Copyright (C) 2008-@year@ UW-Madison LOCI and Glencoe Software, Inc. 
    7 # 
    8 # This program is free software; you can redistribute it and/or modify 
    9 # it under the terms of the GNU General Public License as published by 
    10 # the Free Software Foundation; either version 2 of the License, or 
    11 # (at your option) any later version. 
    12 # 
    13 # This program is distributed in the hope that it will be useful, 
    14 # but WITHOUT ANY WARRANTY; without even the implied warranty of 
    15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    16 # GNU General Public License for more details. 
    17 # 
    18 # You should have received a copy of the GNU General Public License 
    19 # along with this program; if not, write to the Free Software 
    20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    21  
    225# CMake build file for Jace C++ code. 
    236 
    24 if(WIN32) 
    25   cmake_minimum_required(VERSION 2.8) 
    26 else(WIN32) 
    27   cmake_minimum_required(VERSION 2.6) 
    28 endif(WIN32) 
     7cmake_minimum_required(VERSION 2.8) 
    298 
    309project(jace) 
  • branches/maven/projects/jar2lib/src/main/resources/jace/Prerequisites.cmake

    r6739 r6806  
    11# 
    2 # BoostThread.cmake 
     2# Prerequisites.cmake 
    33# 
    4  
    5 # OME Bio-Formats C++ bindings for native access to Bio-Formats Java library. 
    6 # Copyright (C) 2008-@year@ UW-Madison LOCI and Glencoe Software, Inc. 
    7 # 
    8 # This program is free software; you can redistribute it and/or modify 
    9 # it under the terms of the GNU General Public License as published by 
    10 # the Free Software Foundation; either version 2 of the License, or 
    11 # (at your option) any later version. 
    12 # 
    13 # This program is distributed in the hope that it will be useful, 
    14 # but WITHOUT ANY WARRANTY; without even the implied warranty of 
    15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    16 # GNU General Public License for more details. 
    17 # 
    18 # You should have received a copy of the GNU General Public License 
    19 # along with this program; if not, write to the Free Software 
    20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    214 
    225# CMake build file for cross-platform location of prerequisite libraries, 
     
    10285# For Jace r46 and later, use instead (& see build.properties): 
    10386#set(JACE_CPP_DIR "${JACE_DIR}/core/cpp") 
    104  
Note: See TracChangeset for help on using the changeset viewer.