/** * */ package oboannotator.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Hashtable; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author ahmedabdeenhamed * */ public class Util { private static Hashtable fileOrderNamePair = new Hashtable(); /** * A helper method that reads from a certain directory and */ public static Hashtable getFilesByDirectoryName(String directoryFullPath){ //System.out.println(directoryFullPath); File directory = new File(directoryFullPath); File listOfFiles[] = directory.listFiles(); int count = 1; for(int i = 0; i < listOfFiles.length; i++){ if (listOfFiles[i].isFile()){ // only count the files not the directories Integer order = new Integer(count); // make up the value which would be the name of the file as it appear String name = new String(listOfFiles[i].getName()); fileOrderNamePair.put(order.toString(), name); count++; } } return fileOrderNamePair; } public static void writeStringToFile(String stringToBeWritten, String filePath, String fileName) throws IOException{ StringBuilder fileFullName = new StringBuilder(filePath); fileFullName.append(fileName); BufferedWriter out = new BufferedWriter(new FileWriter(fileFullName.toString())); out.write(stringToBeWritten); out.close(); } public static String[] splitOBO_Entry(String OOB_Entry){ //System.out.println("the current term is: "+ OOB_Entry); String[] cononFormTerms = new String[3]; String[] oboEntrySplits = OOB_Entry.split("\\("); // System.out.println("the current term split 1: "+ oboEntrySplits[0]); // System.out.println("the current term split 2: "+ oboEntrySplits[1]); String[] oboNameSplits = oboEntrySplits[1].split("_"); // System.out.println("dict name split 1: "+ oboNameSplits[0]); // System.out.println("the number split 2: "+ oboNameSplits[1]); int oboNumberIndex = oboNameSplits[1].indexOf(")"); String oboNumber = oboNameSplits[1].substring(0, oboNumberIndex); cononFormTerms[0] = (oboEntrySplits[0]); cononFormTerms[1] = (oboNameSplits[0]); cononFormTerms[2] = (oboNumber); // System.out.println(cononFormTerms[0]); // System.out.println(cononFormTerms[1]); // System.out.println(cononFormTerms[2]); return cononFormTerms; } public static String[] splitOBOTerm(String term){ //System.out.println("Current Term " + term); String[] cononFormTerms = new String[3]; Pattern termPattern = Pattern.compile("([A-Z]*?)_(\\d+)"); Matcher ontologyMatcher = termPattern.matcher(term); while(ontologyMatcher.find()){ int index = term.indexOf(ontologyMatcher.group(1).toString()); String oboTermText = term.substring(0, index-1); cononFormTerms[0] = oboTermText; cononFormTerms[1] = ontologyMatcher.group(1).toString(); cononFormTerms[2] = ontologyMatcher.group(2).toString(); } return cononFormTerms; } public static String removeFileExtension(String fileName){ //System.out.println("File Name: " + fileName); String[] fileNameWithoutExtension = fileName.split(".xml"); return fileNameWithoutExtension[0]; } public static String removeString(String targetString, String regex){ //System.out.println("File Name before: " + targetString); int index = targetString.indexOf(regex); String fileNameWithoutExtension = targetString.substring(0, index); //System.out.println("File Name after: " + fileNameWithoutExtension); return fileNameWithoutExtension; } public static ArrayList interpretUserInput(String userInput){ if(userInput.equalsIgnoreCase("") || userInput==null){ return null; } String[] userCommands = userInput.toLowerCase().split(","); ArrayList interepretedCommnds = new ArrayList(); UserCommand uComm = null; for(String command: userCommands){ String tempComm = command.trim(); if(tempComm.trim().startsWith("a")){ uComm = new UserCommand(); uComm.setCommand("ADD_NEW_DICTIONARY"); interepretedCommnds.add(uComm); }else if(command.trim().startsWith("u")){ uComm = new UserCommand(); uComm.setCommand("UPDATE_DICTIONARY"); uComm.setCommandOption(Integer.parseInt(command.trim().substring(1))); interepretedCommnds.add(uComm); }else if(Character.isDigit(tempComm.trim().charAt(0))){ uComm = new UserCommand(); uComm.setCommand("INCLUDE_DICTIONARY"); uComm.setIntCommand(Integer.parseInt(command.trim())); interepretedCommnds.add(uComm); }else{ System.out.println(command.trim() + " is an invalid command, command is ignored..."); continue; } } return interepretedCommnds; } /** * @param args */ public static void main(String[] args) throws IOException { Properties properties = new Properties(); try { properties.load(new FileInputStream("config.properties")); } catch (IOException e) { System.out.println(e.getMessage()); System.exit(-1); //System.err.println(e.getMessage()); } AppConfig.DICTIONARY_AE_DIR = properties.getProperty("DICTIONARY_AE_DIR"); getFilesByDirectoryName(AppConfig.DICTIONARY_AE_DIR ); //Util.writeStringToFile("This is awesome!!!", "/Users/ahmedabdeenhamed/Test/", "awesome.txt"); //String fileNameOnly = Util.removeFileExtension("ahmed-try-check.xml.xml.xml"); //System.out.println("File witouth XML extension is: " + fileNameOnly); //splitOBOTerm("Parco Nazionale dei Monti Sibillini(ENVO_00089869)"); //File f = new File("/Users/ahmedabdeenhamed/Test/hello.xml"); //f.createNewFile(); removeString("ahmedOwlOboAE.xml", "AE.xml"); } }