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 | int LOC = 0; |
---|
119 | String code = MiscTools.readFile(filename); |
---|
120 | int index = 0; |
---|
121 | while (index != -1) { |
---|
122 | if (index == 0){ |
---|
123 | index = code.indexOf("\n", index); |
---|
124 | } else { |
---|
125 | index = code.indexOf("\n", index + 1); |
---|
126 | } |
---|
127 | LOC++; |
---|
128 | } |
---|
129 | return LOC; |
---|
130 | } |
---|
131 | |
---|
132 | // -------------------------------------------------------------------------- |
---|
133 | public static String doubleToString(double d) { |
---|
134 | DecimalFormat fmt = new DecimalFormat("0.00"); |
---|
135 | String string = fmt.format(d); |
---|
136 | return string; |
---|
137 | } |
---|
138 | |
---|
139 | public static String getSubPath(String inputPath){ |
---|
140 | String subPath = ""; |
---|
141 | File input = new File(getProperInputPath(inputPath)); |
---|
142 | if (input.isFile()){ |
---|
143 | subPath = input.getName().substring(0, input.getName().lastIndexOf(".")); |
---|
144 | } else { |
---|
145 | subPath = input.getName(); |
---|
146 | } |
---|
147 | return subPath; |
---|
148 | } |
---|
149 | |
---|
150 | public static String getProperOutputPath(String outputDirectory) { |
---|
151 | String properOutputPath = ""; |
---|
152 | try { |
---|
153 | //gets the full path if relative path is provided |
---|
154 | properOutputPath = new File(outputDirectory).getCanonicalPath(); |
---|
155 | } catch (IOException e) { |
---|
156 | System.out.println("IO ERROR: "); |
---|
157 | e.printStackTrace(); |
---|
158 | } |
---|
159 | return properOutputPath; |
---|
160 | } |
---|
161 | |
---|
162 | public static String getProperInputPath(String inputPath) { |
---|
163 | // strips slash if present |
---|
164 | //TODO: reorganize and extrarct |
---|
165 | //TODO: Document "feature" -> if multiple input paths are provided, the first one will be used for the sub path calculation |
---|
166 | String properInputPath = new File(inputPath).getPath(); |
---|
167 | |
---|
168 | if (properInputPath.equals(".")) { |
---|
169 | try { |
---|
170 | //gets the full path |
---|
171 | properInputPath = new File(properInputPath).getCanonicalFile() |
---|
172 | .getName(); |
---|
173 | } catch (IOException e) { |
---|
174 | System.out.println("IO ERROR: "); |
---|
175 | e.printStackTrace(); |
---|
176 | } |
---|
177 | } |
---|
178 | return properInputPath; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | } |
---|