1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.util;
20
21 import net.sf.jameleon.bean.FunctionalPoint;
22 import net.sf.jameleon.exception.JameleonScriptException;
23
24 import java.io.*;
25
26 public class JameleonUtility {
27
28 /***
29 * This class isn't meant to be instantiated.
30 */
31 private JameleonUtility(){
32 }
33
34 public static FunctionalPoint loadFunctionalPoint(String className, Object callingClass){
35 FunctionalPoint fpTemp = null;
36 String classNameOrig = className;
37 String className1 = classNameOrig.replace('.',File.separatorChar);
38 className1 = className1+InstanceSerializer.SERIALIZED_EXT;
39 String className2 = classNameOrig.replace('.','/');
40 className2 = className2+InstanceSerializer.SERIALIZED_EXT;
41 InputStream is = null;
42 try{
43 is = getInputStream(className1, callingClass);
44 fpTemp = (FunctionalPoint)InstanceSerializer.deserialize(is);
45 closeInputStream(is);
46 classNameOrig = className1;
47 if (fpTemp == null) {
48 is = getInputStream(className2, callingClass);
49 fpTemp = (FunctionalPoint)InstanceSerializer.deserialize(is);
50 closeInputStream(is);
51 classNameOrig = className2;
52 }
53 }catch (IOException ioe){
54 ioe.printStackTrace();
55 }catch (ClassNotFoundException cnfe){
56 cnfe.printStackTrace();
57 }finally{
58 closeInputStream(is);
59 }
60 if (fpTemp == null) {
61 throw new JameleonScriptException("\nCould not find '"+classNameOrig+"'!\nPlease be sure to execute the Jameleon-provided"+
62 " Ant task for registering custom\ntags and that the files (.dat) generated from "+
63 "this are placed in the CLASSPATH!");
64 }
65 return fpTemp;
66 }
67
68 protected static void closeInputStream(InputStream is){
69 try{
70 if (is != null){
71 is.close();
72 }
73 }catch(IOException e){
74
75
76 }
77 }
78
79 public static InputStream getInputStream(String className, Object callingClass){
80 InputStream input = null;
81 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
82 if (classLoader == null) {
83 classLoader = callingClass.getClass().getClassLoader();
84 input = classLoader.getResourceAsStream( className );
85 } else {
86 input = classLoader.getResourceAsStream( className );
87 if (input == null) {
88 classLoader = callingClass.getClass().getClassLoader();
89 input = classLoader.getResourceAsStream( className );
90 }
91 }
92 return input;
93 }
94
95
96 /***
97 * A helper method to get the stack track of a throwable as a String
98 * @param t The exception to get the stack trace from.
99 * @return The stack trace as a String.
100 */
101 public static String getStack(Throwable t){
102 StringWriter sw = new StringWriter();
103 t.printStackTrace(new PrintWriter(sw));
104 return sw.toString();
105 }
106
107 /***
108 * Formats the error message.
109 * @param msg - The message to format.
110 * @return The formatted <code>msg</code>.
111 */
112 public static String createErrMsg(String msg) {
113 return msg;
114 }
115
116 /***
117 * Write some text to a file who's directory may or may not exist.
118 * @param f - The file to write to.
119 * @param content - The content to write.
120 * @throws IOException - If the directory structure could not be created or if the file could not be written to.
121 */
122 public static void recordResultsToFile(File f, String content) throws IOException{
123 createDirStructure(f.getParentFile());
124 FileWriter fw = new FileWriter(f);
125 BufferedWriter out = new BufferedWriter(fw);
126 try{
127 out.write(content);
128 }finally{
129 out.close();
130 fw.close();
131 }
132 }
133
134 /***
135 * Write some text to a file who's directory may or may not exist.
136 * @param f - The file to write to.
137 * @param content - The content to write.
138 * @param encoding - The encoding type to write to.
139 * @throws IOException - If the directory structure could not be created or if the file could not be written to.
140 */
141 public static void recordResultsToFile(File f, String content, String encoding) throws IOException{
142 createDirStructure(f.getParentFile());
143 BufferedWriter out = null;
144 FileOutputStream fos = new FileOutputStream(f);
145 try{
146 out = new BufferedWriter(new OutputStreamWriter(fos, encoding));
147 }catch(UnsupportedEncodingException ue){
148 ue.printStackTrace();
149
150 }
151 try{
152 if (out != null){
153 out.write(content);
154 }
155 }finally{
156 if (out != null){
157 out.close();
158 }
159 fos.close();
160 }
161 }
162
163 /***
164 * Ensures that a directory structure is created.
165 * @param f The directory to create the structure.
166 */
167 public static void createDirStructure(File f){
168 if (!f.exists() && !f.mkdir()) {
169 createDirStructure(f.getParentFile());
170 f.mkdir();
171 }
172 }
173
174 /***
175 * Deletes the given directory/file and all child files.
176 * @param f The directory/file to delete.
177 */
178 public static void deleteDirStructure(File f){
179 if (f.isFile() || f.list().length == 0){
180 f.delete();
181 }else if (f.isDirectory()){
182 File[] files = f.listFiles();
183 for (int i = 0; i < files.length; i++){
184 deleteDirStructure(files[i]);
185 }
186 deleteDirStructure(f);
187 }
188 }
189
190 public static String decodeXMLToText(String str){
191 str = str.replaceAll("//<","<");
192 str = str.replaceAll("//>",">");
193 str = str.replaceAll("//&","&");
194 return str;
195 }
196
197 public static String decodeTextToXML(String str){
198 str = str.replaceAll("&","//&");
199 str = str.replaceAll("<","//<");
200 str = str.replaceAll(">","//>");
201 return str;
202 }
203
204 public static String executionTimeToString(long time){
205 long hours, mins, secs, tempTime;
206 hours = time / 3600000;
207 tempTime = time % 3600000;
208 mins = tempTime / 60000;
209 tempTime = tempTime % 60000;
210 secs = tempTime / 1000;
211 tempTime = tempTime % 1000;
212 String msS = null;
213 if (tempTime < 10) {
214 msS = "00"+tempTime;
215 }else if (tempTime < 100) {
216 msS = "0"+time;
217 }else{
218 msS = tempTime+"";
219 }
220 String formattedTime = concatNum(hours, "h ");
221 formattedTime += concatNum(mins, "m ");
222 if( time == 0 || secs > 0 || tempTime > 0){
223 formattedTime += secs + "."+msS+"s";
224 }
225
226 return formattedTime.trim();
227
228 }
229
230 private static String concatNum(long num, String postfix){
231 String concat = "";
232 if (num > 0){
233 concat = num + postfix;
234 }
235 return concat;
236 }
237
238
239 public static String stripHtmlComments(String text){
240 return text.replaceAll("(?s)<//!--.*?-->","");
241
242 }
243
244 public static String convertNullToString(String str){
245 String rtrStr = "";
246 if (str != null && str.length() > 0) {
247 rtrStr = str;
248 }
249 return rtrStr;
250 }
251
252 public static String getEndingPath(String path, String file){
253 String endingPath = stripCurrentDirFromPath(fixFileSeparators(file));
254 path = stripCurrentDirFromPath(fixFileSeparators(path));
255 int index = endingPath.indexOf(path);
256 if (index != -1){
257 endingPath = endingPath.substring(index + path.length());
258 if (endingPath.startsWith(File.separator)){
259 endingPath = endingPath.substring(1);
260 }
261 }
262 return endingPath;
263 }
264
265 private static String stripCurrentDirFromPath(String path){
266 String newPath = path;
267 if (newPath.startsWith("."+File.separator)){
268 newPath = newPath.substring(2);
269 }
270 return newPath;
271 }
272
273 public static String getEndingPath(String path, File file){
274 return getEndingPath(path, file.getPath());
275 }
276
277 public static String getEndingPath(File path, String file){
278 return getEndingPath(path.getPath(), new File(file));
279 }
280
281 public static String getFileNameFromPath(String path){
282 String fileName = new File(path).getPath();
283 int index = fileName.lastIndexOf(File.separator);
284 if (index != -1) {
285 fileName = fileName.substring(index+1);
286 }
287 index = fileName.lastIndexOf(".");
288 if (index != -1 ) {
289 fileName = fileName.substring(0,index);
290 }
291 return fileName;
292 }
293
294 public static String getFileNameFromScriptPath(String path){
295 String fileName = path;
296 if (fileName != null){
297 fileName = fixFileSeparators(fileName, "/");
298 int index = fileName.lastIndexOf("/");
299 if (index != -1) {
300 fileName = fileName.substring(index+1);
301 }
302 }
303 return fileName;
304 }
305
306 /***
307 * @param path a path using forward slashes
308 * @return the original path with all forward slashes replaced by unix slash
309 */
310 public static String fixFileSeparators(String path) {
311 return fixFileSeparators(path, File.separator);
312 }
313
314 /***
315 * @param path a path using forward slashes
316 * @param fileSeparator the character to replace the forward slashes
317 * @return the original path with all forward slashes replaced by <code>fileSeparator</code>
318 */
319 public static String fixFileSeparators(String path, String fileSeparator) {
320 String fixedPath = "";
321 if (path != null){
322 String separator = File.separator;
323
324 if (path.indexOf(separator) == -1){
325 if (separator.equals("/")){
326 separator = "//";
327 }else if (separator.equals("//")){
328 separator = "/";
329 }
330 }
331 if (separator.equals("//")){
332 separator = "////";
333 }
334 String[] pathParts = path.split(separator);
335 for (int i = 0; i < pathParts.length; i++) {
336 fixedPath += pathParts[i];
337 if (i != pathParts.length - 1) {
338 fixedPath += fileSeparator;
339 }
340 }
341 }
342 return fixedPath;
343 }
344 }