Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

BanderaUtil.java

00001 package edu.ksu.cis.bandera.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 2000  Roby Joehanes (robbyjo@cis.ksu.edu)           *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import java.util.*;
00036 import java.io.*;
00037 
00038 public class BanderaUtil {
00039     private static final int BUFSIZE = 50000;
00040     private static byte[] buffer = new byte[BUFSIZE];
00041     private static Runtime runtime = Runtime.getRuntime();
00042 public static boolean copyFile(File src, File dest)
00043 {
00044     if (!src.exists()) return false;
00045     if (src.isFile() && dest.isDirectory())
00046     {
00047         dest = new File (dest.getAbsolutePath()+File.separator+src.getName());
00048     }
00049     if (dest.exists()) dest.delete();
00050     FileInputStream  in = null;
00051     FileOutputStream out = null;
00052     try {
00053         in  = new FileInputStream(src);
00054         out = new FileOutputStream(dest);
00055         byte[] buf = new byte[64*1024];
00056         int i = 0;
00057         while ((i = in.read(buf)) != -1)
00058             out.write(buf, 0, i);
00059         in.close(); out.close();
00060     } catch (Exception e)
00061     {
00062         try { if (in != null) in.close(); } catch (Exception ex) {}
00063         try { if (out != null) out.close(); } catch (Exception ex) {}
00064         return false;
00065     }
00066     return true;
00067 }
00068 /**
00069  * 
00070  * @return boolean
00071  * @param dir java.io.File
00072  */
00073 public static boolean deleteDir(File dir) {
00074     if (!dir.exists()) return true;
00075 
00076     File[] files = dir.listFiles();
00077     if (files == null) return true;  // robbyjo's bugfix
00078     for (int i = 0; i < files.length; i++) {
00079         if (files[i].isDirectory()) {
00080             deleteDir(files[i]);
00081         } else {
00082             if (!files[i].delete()) {
00083                 return false;
00084             }
00085         }
00086     }
00087     
00088     return dir.delete();
00089 }
00090 public static boolean deleteDir(String s) { return deleteDir(new File(s)); }
00091 public static String exec(String command, StringBuffer output, StringBuffer error, int verbosity)
00092 {
00093     try {
00094         Process p = runtime.exec(command);
00095         InputStream commandErr = p.getErrorStream();
00096         InputStream commandOut = p.getInputStream();
00097         String ln = System.getProperty("line.separator");
00098 
00099         StringBuffer sb = new StringBuffer();
00100         if (verbosity > 5)
00101         {
00102             System.out.println(command);
00103             output.append(command+ln);
00104         }
00105         int charsRead = 0; // chars read on read
00106         int charsAvail = 0; // chars available
00107         while (charsRead != -1) {
00108             if (commandErr.available() > 0) {
00109                 charsRead = commandErr.read(buffer);
00110                 String s = new String(buffer, 0, charsRead);
00111                 error.append(s);
00112                 sb.append(s);
00113             }
00114             charsRead = commandOut.read(buffer);
00115             if (charsRead > 0)
00116             {
00117                 String s = new String(buffer, 0, charsRead);
00118                 output.append(s);
00119                 sb.append(s);
00120             }
00121         }
00122         p.waitFor();
00123         String result = sb.toString();
00124         if (verbosity > 5) System.out.println(result);
00125         return result;
00126     } catch (Exception e) {
00127         throw new RuntimeException("exec of command '" + command + "' was aborted: \n" + e);
00128     }
00129 }
00130 public static String exec(String command, boolean verbose)
00131 {
00132     StringBuffer sb = new StringBuffer();
00133     return exec(command, sb, sb, 9);
00134 }
00135 public static void main(String[] args)
00136 {
00137     // For a test drive
00138     moveFile (new File("Driver.java"), new File("output"));
00139 }
00140 public static boolean mkdirs(String s)
00141 {
00142     File tempDir = new File(s);
00143     String temp = "Error: Can't create temporary directory '" + tempDir.getAbsolutePath() + "'";
00144     //System.out.println("Making directory "+s);
00145     if (tempDir.exists() && !deleteDir(tempDir)) return false;
00146     try {
00147         if (!tempDir.mkdirs()) return false;
00148         if (!tempDir.exists()) System.out.println("Strangely enough, it doesn't exists!");
00149     } catch (Exception e) {
00150         return false;
00151     }
00152     return true;
00153 }
00154 public static void moveFile(File src, File dest)
00155 {
00156     copyFile(src,dest); src.delete();
00157 }
00158 public static void moveFile(String file, String dir) {
00159     File f = new File(file);
00160     if (!f.exists() || dir == null || ("."+File.separator).equals(dir) || ".".equals(dir)) return;
00161     moveFile(f, new File(dir));
00162 }
00163 /**
00164  * 
00165  * @param classpath java.lang.String
00166  * @param filename java.lang.String
00167  */
00168 public static void runJavac(String classpath, String filename) {
00169     String cmdLine = "javac -g -classpath " + classpath + " " + filename;
00170     System.out.println(cmdLine);
00171     sun.tools.javac.Main javac = new sun.tools.javac.Main(System.out, "Bandera");
00172     if (!javac.compile(new String[] { "-g", "-classpath", classpath, filename })) {
00173         System.out.println("Error while compiling the model");
00174     }
00175 }
00176 }

Generated at Thu Feb 7 06:40:31 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001