1 | package org.etsi.common; |
---|
2 | |
---|
3 | import java.io.BufferedWriter; |
---|
4 | |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileInputStream; |
---|
7 | import java.io.FileNotFoundException; |
---|
8 | import java.io.FileOutputStream; |
---|
9 | import java.io.IOException; |
---|
10 | import java.io.OutputStreamWriter; |
---|
11 | import java.io.Writer; |
---|
12 | import java.text.DecimalFormat; |
---|
13 | |
---|
14 | import org.etsi.common.exceptions.TerminationException; |
---|
15 | |
---|
16 | |
---|
17 | public class MiscTools { |
---|
18 | |
---|
19 | //TODO: Add exceptions and throws declarations |
---|
20 | public static String readFile(String filename) { |
---|
21 | FileInputStream fis; |
---|
22 | String content = ""; |
---|
23 | try { |
---|
24 | fis = new FileInputStream(filename); |
---|
25 | int x = fis.available(); |
---|
26 | byte b[] = new byte[x]; |
---|
27 | fis.read(b); |
---|
28 | content = new String(b); |
---|
29 | } catch (FileNotFoundException e) { |
---|
30 | String message = "IO ERROR: Filename "+filename+" cannot be found for reading!"; |
---|
31 | try { |
---|
32 | throw new TerminationException(message); |
---|
33 | } catch (TerminationException e1) { |
---|
34 | } |
---|
35 | } catch (IOException e) { |
---|
36 | e.printStackTrace(); |
---|
37 | } |
---|
38 | |
---|
39 | return content; |
---|
40 | } |
---|
41 | |
---|
42 | public static String getShortFilename(String filename) { |
---|
43 | String shortfilename = new File(filename).getName(); |
---|
44 | return shortfilename; |
---|
45 | } |
---|
46 | |
---|
47 | public static String getMethodName(){ |
---|
48 | return Thread.currentThread().getStackTrace()[2].getMethodName(); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | public static void writeFile(String filename, String content) { |
---|
53 | FileOutputStream fos; |
---|
54 | File targetDir = new File(filename).getAbsoluteFile().getParentFile(); |
---|
55 | try { |
---|
56 | if (!targetDir.exists()) { |
---|
57 | if (!targetDir.mkdirs()) { |
---|
58 | System.out |
---|
59 | .println("IO ERROR: Creating the directory structure for \"" |
---|
60 | + filename + "\" failed!"); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | try { |
---|
65 | fos = new FileOutputStream(filename); |
---|
66 | Writer w = new BufferedWriter(new OutputStreamWriter(fos)); |
---|
67 | w.write(content); |
---|
68 | w.flush(); |
---|
69 | w.close(); |
---|
70 | |
---|
71 | } catch (FileNotFoundException e) { |
---|
72 | e.printStackTrace(); |
---|
73 | } catch (IOException e) { |
---|
74 | e.printStackTrace(); |
---|
75 | } |
---|
76 | } catch (SecurityException se) { |
---|
77 | System.out |
---|
78 | .println("IO ERROR: Could not create ouput files and / or directories!" |
---|
79 | + "Caused by: \"" + filename + "\""); |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | public static void streamCopyFile(String sourceFilename, String targetFilename){ |
---|
85 | try { |
---|
86 | File targetFile = new File(targetFilename); |
---|
87 | if (!targetFile.getParentFile().exists()){ |
---|
88 | if (!targetFile.getParentFile().mkdirs()){ |
---|
89 | System.out |
---|
90 | .println("IO ERROR: Creating the directory structure for \"" |
---|
91 | + targetFilename + "\" failed!"); |
---|
92 | } |
---|
93 | } |
---|
94 | FileOutputStream outStream = new FileOutputStream(targetFile); |
---|
95 | FileInputStream inStream = new FileInputStream(sourceFilename); |
---|
96 | int c; |
---|
97 | try { |
---|
98 | while((c = inStream.read()) != -1){ |
---|
99 | outStream.write(c); |
---|
100 | } |
---|
101 | } catch (IOException e) { |
---|
102 | System.out.println("IO ERROR: Caused while copying: \""+sourceFilename+"\" to \""+targetFilename+"\""); |
---|
103 | } |
---|
104 | } catch (FileNotFoundException e1) { |
---|
105 | System.out.println("IO ERROR: File not found: \""+sourceFilename+" / "+targetFilename+"\""); |
---|
106 | e1.printStackTrace(); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | // public static String getSubPath(String basePath, String resourcePath) { |
---|
111 | // String subPath = resourcePath.substring(basePath.length()); |
---|
112 | // return subPath; |
---|
113 | // } |
---|
114 | |
---|
115 | // -------------------------------------------------------------------------- |
---|
116 | |
---|
117 | public static int getLOC(String filename){ |
---|
118 | String code = MiscTools.readFile(filename); |
---|
119 | int LOC = code.split("\n").length; |
---|
120 | return LOC; |
---|
121 | } |
---|
122 | |
---|
123 | // -------------------------------------------------------------------------- |
---|
124 | public static String doubleToString(double d) { |
---|
125 | DecimalFormat fmt = new DecimalFormat("0.00"); |
---|
126 | String string = fmt.format(d); |
---|
127 | return string; |
---|
128 | } |
---|
129 | |
---|
130 | public static String getSubPath(String inputPath){ |
---|
131 | String subPath = ""; |
---|
132 | File input = new File(getProperInputPath(inputPath)); |
---|
133 | if (input.isFile()){ |
---|
134 | subPath = input.getName().substring(0, input.getName().lastIndexOf(".")); |
---|
135 | } else { |
---|
136 | subPath = input.getName(); |
---|
137 | } |
---|
138 | return subPath; |
---|
139 | } |
---|
140 | |
---|
141 | public static String getProperOutputPath(String outputDirectory) { |
---|
142 | String properOutputPath = ""; |
---|
143 | try { |
---|
144 | //gets the full path if relative path is provided |
---|
145 | properOutputPath = new File(outputDirectory).getCanonicalPath(); |
---|
146 | } catch (IOException e) { |
---|
147 | System.out.println("IO ERROR: "); |
---|
148 | e.printStackTrace(); |
---|
149 | } |
---|
150 | return properOutputPath; |
---|
151 | } |
---|
152 | |
---|
153 | public static String getProperInputPath(String inputPath) { |
---|
154 | // strips slash if present |
---|
155 | //TODO: reorganize and extrarct |
---|
156 | //TODO: Document "feature" -> if multiple input paths are provided, the first one will be used for the sub path calculation |
---|
157 | String properInputPath = new File(inputPath).getPath(); |
---|
158 | |
---|
159 | if (properInputPath.equals(".")) { |
---|
160 | try { |
---|
161 | //gets the full path |
---|
162 | properInputPath = new File(properInputPath).getCanonicalFile() |
---|
163 | .getName(); |
---|
164 | } catch (IOException e) { |
---|
165 | System.out.println("IO ERROR: "); |
---|
166 | e.printStackTrace(); |
---|
167 | } |
---|
168 | } |
---|
169 | return properInputPath; |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | } |
---|