1 17 package org.eclipse.emf.importer.rose.parser; 18 19 import java.io.File ; 20 import java.util.StringTokenizer ; 21 22 23 25 public class Util 26 { 27 public final static String QUOTE = "\""; 28 public final static int PATH_NOT_VALID = 0; 29 public final static int DIRECTORY_NOT_FOUND = 1; 30 public final static int PATH_OK = 2; 31 public final static int NOT_A_DIRECTORY = 3; 32 33 public static int isValidPath(String pathname) 34 { 35 File testDirectory; 36 try 37 { 38 testDirectory = new File (pathname); 39 } 40 catch (NullPointerException e) 41 { 42 return PATH_NOT_VALID; 43 } 44 boolean exists = false; 45 try 46 { 47 exists = testDirectory.exists(); 48 } 49 catch (SecurityException e) 50 { 51 return PATH_NOT_VALID; 52 } 53 boolean isDirectory = false; 54 try 55 { 56 isDirectory = testDirectory.isDirectory(); 57 } 58 catch (SecurityException e) 59 { 60 return PATH_NOT_VALID; 61 } 62 if (!isDirectory) 63 { 64 if (exists) 65 { 66 return NOT_A_DIRECTORY; 67 } 68 else 69 { 70 return DIRECTORY_NOT_FOUND; 71 } 72 } 73 return PATH_OK; 74 } 75 76 static public boolean createDirectory(String dirName) 77 { 78 try 79 { 80 File newDir = new File (dirName); 81 if ((newDir.mkdirs())) 82 { 83 return true; 84 } 85 else 86 { 87 return false; 88 } 89 } 90 catch (NullPointerException e) 91 { 92 return false; 93 } 94 catch (SecurityException e) 95 { 96 return false; 97 } 98 } 99 100 static public String trimQuotes(String str) 101 { 102 int f_ind = str.indexOf(QUOTE); 103 int l_ind = str.lastIndexOf(QUOTE); 104 if (f_ind == -1) 105 { 106 return str; 107 } 108 else if (f_ind == l_ind) 109 { 110 return str.substring(f_ind + 1); 111 } 112 else 113 { 114 return str.substring(f_ind + 1, l_ind); 115 } 116 } 117 118 static public String getType(String str) 119 { 120 return getWord(str, 1); 121 } 122 123 static public String getName(String str) 124 { 125 int ind_end = str.lastIndexOf(QUOTE); 126 if (ind_end == -1) 127 { 128 return null; 129 } 130 String temp = str.substring(0, ind_end); 131 int ind_start = temp.lastIndexOf(QUOTE); 132 if (ind_start == -1) 133 { 134 return null; 135 } 136 String name = str.substring(ind_start + 1, ind_end); 137 if (name.indexOf("$UNNAMED") != -1) 138 { 139 name = ""; 140 } 141 return name; 142 } 143 144 static public String getWord(String str, int n) 145 { 146 StringTokenizer st = new StringTokenizer (str); 147 int i = 0; 148 while (st.hasMoreTokens()) 149 { 150 i++; 151 String tok = st.nextToken(); 152 if (i == n) 153 { 154 return tok; 155 } 156 } 157 return null; 158 } 159 160 static public String updateFileName(String fileName, String dilimiter) 161 { 162 String name = ""; 163 while (true) 164 { 165 int ind = fileName.indexOf(dilimiter); 166 if (ind == -1) 167 { 168 name += fileName; 169 break; 170 } 171 name += (fileName.substring(0, ind) + File.separator); 172 fileName = fileName.substring(ind + dilimiter.length()); 173 } 174 return name; 175 } 176 } 177 | Popular Tags |