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