Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -20,13 +21,48 @@ public class ClasUtilsFile {
private static String moduleString = "[ClasUtilsFile] --> ";

public static String getName(){ return moduleString; }

/**
* prints a log message with the module name included.
* @param log
*/
public static void printLog(String log){
System.out.println(ClasUtilsFile.getName() + " " + log);
}

/**
* @param clazz
* @return absolute path of the jar file containing clazz
*/
public static String getJarPath(Class clazz) {
try {
return (new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI())).getAbsolutePath();
} catch (Exception e) {
System.getLogger(ClasUtilsFile.class.getName()).log(System.Logger.Level.ERROR, (String) null, e);
return null;
}
}

/**
* @return absolute path to COATJAVA installation runtime directory
*/
public static String getCoatjavaRuntimeDir() {
String ret = getJarPath(ClasUtilsFile.class);
if (ret != null) {
String[] d = ret.split("/");
if (System.console() == null)
// When run from an IDE, the jar is in the coatjava source tree,
// so assume the "coatjava" installation directory at the top:
ret = "/" + String.join("/", Arrays.copyOfRange(d,0,d.length-4)) + "/coatjava";
else
// When running the JVM directly, the jar is already inside a
// coatjava installation, so just get to the top of it:
ret = "/" + String.join("/", Arrays.copyOfRange(d,0,d.length-3));

}
return ret;
}

/**
* returns package resource directory with given enviromental variable
* and relative path.
Expand All @@ -36,33 +72,37 @@ public static void printLog(String log){
*/
public static String getResourceDir(String env, String rpath){

String envString = System.getenv(env);
if(envString==null){
String value = System.getenv(env);

if(value==null){
ClasUtilsFile.printLog("Environment variable ["+env+"] is not defined");
envString = System.getProperty(env);
value = System.getProperty(env);
}

if(envString == null){
if(value == null){
ClasUtilsFile.printLog("System property ["+env+"] is not defined");
return null;
if (env.equals("COATJAVA") || env.equals("CLAS12DIR")) {
value = getCoatjavaRuntimeDir();
}
}

if (value == null) return null;

StringBuilder str = new StringBuilder();
int index = envString.length()-1;
str.append(envString);
//Char fileSeparator =
if(envString.charAt(index)!='/' && rpath.startsWith("/")==false) str.append('/');
str.append(value);
if (!value.endsWith("/") && !rpath.startsWith("/")) str.append('/');
str.append(rpath);
return str.toString();
}

/**
* returns list of files in the directory. absolute path is given.
* This function will not exclude ".*" and "*~" files.
* @param directory
* @return
*/
public static List<String> getFileList(String directory){
List<String> fileList = new ArrayList<String>();
List<String> fileList = new ArrayList<>();
File[] files = new File(directory).listFiles();
System.out.println("FILE LIST LENGTH = " + files.length);
for (File file : files) {
Expand All @@ -88,7 +128,7 @@ public static List<String> getFileList(String env, String rpath){
String directory = ClasUtilsFile.getResourceDir(env, rpath);
if(directory==null){
ClasUtilsFile.printLog("(error) directory does not exist : " + directory);
return new ArrayList<String>();
return new ArrayList<>();
}
return ClasUtilsFile.getFileList(directory);
}
Expand All @@ -101,10 +141,10 @@ public static List<String> getFileList(String env, String rpath){
*/
public static List<String> getFileList(String env, String rpath, String ext){
String directory = ClasUtilsFile.getResourceDir(env, rpath);
if(directory!=null) return new ArrayList<String>();
if(directory!=null) return new ArrayList<>();

List<String> files = ClasUtilsFile.getFileList(directory);
List<String> selected = new ArrayList<String>();
List<String> selected = new ArrayList<>();
for(String item : files){
if(item.endsWith(ext)==true) selected.add(item);
}
Expand Down Expand Up @@ -135,7 +175,7 @@ public static void writeFile(String filename, List<String> lines){
* @return
*/
public static List<String> readFile(String filename){
List<String> lines = new ArrayList<String>();
List<String> lines = new ArrayList<>();
String line = null;
try {
// FileReader reads text files in the default encoding.
Expand Down Expand Up @@ -177,7 +217,7 @@ public static String readFileString(String filename){
* @return
*/
public static List<String> getFileNamesRelative(List<String> files){
List<String> newList = new ArrayList<String>();
List<String> newList = new ArrayList<>();
for(String file : files){
int index = file.lastIndexOf('/');
if(index>=0&&index<file.length()){
Expand Down Expand Up @@ -209,15 +249,13 @@ public static String createFileName(String filename, String addition, boolean pr

StringBuilder str = new StringBuilder();
int index = inputFile.lastIndexOf(".");
//int index = filename.lastIndexOf(".");
str.append(inputFile.substring(0, index));
str.append(addition);
str.append(inputFile.substring(index, inputFile.length()));
return str.toString();
}

public static void main(String[] args){
String output_file = ClasUtilsFile.createFileName("/Users/gavalian/Work/Software/Release-9.0/COATJAVA/coatjava/../datasets/gemc/eklambda/gemc_eklambda_A0001_gen.evio", "_header", true);
System.out.println(output_file);
System.out.println(getCoatjavaRuntimeDir());
}
}