1
- package com .efraespada . stringobfuscatorplugin ;
1
+ package com .stringcare ;
2
2
3
3
import java .io .*;
4
- import java .nio .channels .FileChannel ;
5
- import java .nio .file .Files ;
6
- import java .util .HashMap ;
7
- import java .util .Map ;
8
4
9
- import static com .efraespada . stringobfuscatorplugin .PrintUtils .print ;
5
+ import static com .stringcare .PrintUtils .print ;
10
6
11
7
public class FileUtils {
12
8
13
- private static String variant ;
14
9
private static String module ;
15
10
private static String key ;
11
+ private static Config config ;
16
12
17
13
final static int maxToShow = 15 ;
18
14
19
-
20
- private static final Map <String , String > files = new HashMap <>();
21
-
22
15
private FileUtils () {
23
16
// nothing to do here
24
17
}
25
18
26
- public static void init (String key , String module , String variant ) {
27
- files .clear ();
19
+ public static void init (String key , String module , String variant , Config config ) {
28
20
FileUtils .key = key ;
29
21
FileUtils .module = module ;
30
- FileUtils .variant = variant ;
22
+ FileUtils .config = config ;
31
23
}
32
24
33
25
public static String getTextFromFilePath (String path ) {
@@ -37,8 +29,6 @@ public static String getTextFromFilePath(String path) {
37
29
String xml = "" ;
38
30
String inputFilePath = path ;
39
31
40
- if (true ) print ("reading " + inputFilePath );
41
-
42
32
String message = "" ;
43
33
44
34
File file = new File (inputFilePath );
@@ -83,41 +73,58 @@ public static String getString(BufferedReader br) {
83
73
// detect multiple sourceSet res.srcDirs
84
74
public static void backupStringResources () {
85
75
String currentPath = getCurrentPath ();
86
- currentPath += module + File .separator + "src" + File .separator + "main" + File .separator + "res" + File .separator ;
87
- File file = new File (currentPath );
88
- String [] directories = file .list ((current , name ) -> new File (current , name ).isDirectory ());
89
- for (String dir : directories ) {
90
- String pathToCopy = currentPath + dir + File .separator ;
91
- String pathToCheck = getCurrentPath () + module + File .separator + "resbackup" + File .separator + dir + File .separator ;
92
-
93
- try {
94
- File toCopy = new File (pathToCopy + "strings.xml" );
95
- File toCheck = new File (pathToCheck + "strings.xml" );
96
- if (toCheck .exists ()) {
97
- toCheck .delete ();
98
- }
99
- if (toCopy .exists ()) {
100
- copyFile (toCopy , toCheck );
76
+ for (String folder : config .getSrcFolders ()) {
77
+ currentPath += module + File .separator + folder + File .separator + "res" + File .separator ;
78
+ File file = new File (currentPath );
79
+ String [] directories = file .list ((current , name ) -> new File (current , name ).isDirectory ());
80
+ if (directories != null ) {
81
+ for (String dir : directories ) {
82
+ String pathToCopy = currentPath + dir + File .separator ;
83
+ String pathToCheck = getCurrentPath () + module + File .separator + "resbackup" + File .separator + dir + File .separator ;
84
+
85
+ for (String sFile : config .getStringFiles ()) {
86
+ try {
87
+ File toCopy = new File (pathToCopy + sFile );
88
+ File toCheck = new File (pathToCheck + sFile );
89
+ if (toCheck .exists ()) {
90
+ toCheck .delete ();
91
+ }
92
+ if (toCopy .exists ()) {
93
+ print ("- " + toCopy .getParentFile ().getName () + File .separator + toCopy .getName (), true );
94
+ copyFile (toCopy , toCheck );
95
+ }
96
+ } catch (IOException e ) {
97
+ e .printStackTrace ();
98
+ // TODO HANDLE ERROR
99
+ }
100
+ }
101
101
}
102
- } catch (IOException e ) {
103
- e .printStackTrace ();
104
- // TODO HANDLE ERROR
102
+ } else {
103
+ PrintUtils .print ("source folder not found: " + folder , true );
105
104
}
106
105
}
107
106
}
108
107
109
108
public static void encryptStringResources () {
110
109
String currentPath = getCurrentPath ();
111
- currentPath += module + File .separator + "src" + File .separator + "main" + File .separator + "res" + File .separator ;
112
- File file = new File (currentPath );
113
- String [] directories = file .list ((current , name ) -> new File (current , name ).isDirectory ());
114
- for (String dir : directories ) {
115
- String pathToEncrypt = currentPath + dir + File .separator ;
116
-
117
- File toEncrypt = new File (pathToEncrypt + "strings.xml" );
118
- if (toEncrypt .exists ()) {
119
- String encrypted = find (getTextFromFilePath (toEncrypt .getAbsolutePath ()));
120
- writeFile (toEncrypt , encrypted );
110
+ for (String folder : config .getSrcFolders ()) {
111
+ currentPath += module + File .separator + folder + File .separator + "res" + File .separator ;
112
+ File file = new File (currentPath );
113
+ String [] directories = file .list ((current , name ) -> new File (current , name ).isDirectory ());
114
+ if (directories != null ) {
115
+ for (String dir : directories ) {
116
+ String pathToEncrypt = currentPath + dir + File .separator ;
117
+ for (String sFile : config .getStringFiles ()) {
118
+ File toEncrypt = new File (pathToEncrypt + sFile );
119
+ if (toEncrypt .exists ()) {
120
+ PrintUtils .print ("- " + toEncrypt .getParentFile ().getName () + File .separator + toEncrypt .getName (), true );
121
+ String encrypted = find (getTextFromFilePath (toEncrypt .getAbsolutePath ()));
122
+ writeFile (toEncrypt , encrypted );
123
+ }
124
+ }
125
+ }
126
+ } else {
127
+ PrintUtils .print ("source folder not found: " + folder , true );
121
128
}
122
129
}
123
130
}
@@ -126,23 +133,35 @@ public static void restoreStringResources() {
126
133
String currentPath = getCurrentPath () + module + File .separator + "resbackup" + File .separator ;
127
134
File file = new File (currentPath );
128
135
String [] directories = file .list ((current , name ) -> new File (current , name ).isDirectory ());
129
- for (String dir : directories ) {
130
- String pathToEncrypt = currentPath + dir + File .separator ;
131
- String pathRes = getCurrentPath () + module + File .separator + "src" + File .separator + "main" + File .separator + "res" + File .separator + dir + File .separator ;
132
-
133
- File toRestore = new File (pathToEncrypt + "strings.xml" );
134
- File toCheck = new File (pathRes + "strings.xml" );
135
-
136
- try {
137
- copyFile (toRestore , toCheck );
138
- } catch (IOException e ) {
139
- e .printStackTrace ();
136
+ if (directories != null ) {
137
+ File toRestore ;
138
+ for (String dir : directories ) {
139
+ String pathToRestore = currentPath + dir + File .separator ;
140
+ for (String folder : config .getSrcFolders ()) {
141
+ String pathRes = getCurrentPath () + module + File .separator + folder + File .separator + "res" + File .separator + dir + File .separator ;
142
+
143
+ for (String sFile : config .getStringFiles ()) {
144
+ toRestore = new File (pathToRestore + sFile );
145
+ File toCheck = new File (pathRes + sFile );
146
+ if (toRestore .exists ()) {
147
+ try {
148
+ PrintUtils .print ("- " + toCheck .getParentFile ().getName () + File .separator + toCheck .getName (), true );
149
+ copyFile (toRestore , toCheck );
150
+ } catch (IOException e ) {
151
+ e .printStackTrace ();
152
+ }
153
+
154
+ toRestore .delete ();
155
+ toRestore .getParentFile ().delete ();
156
+ }
157
+ }
158
+ }
140
159
}
141
-
142
- toRestore .delete ();
143
- }
144
- if ( file . isDirectory ()) {
145
- file . delete ( );
160
+ if ( file . isDirectory ()) {
161
+ file .delete ();
162
+ }
163
+ } else {
164
+ PrintUtils . print ( "restore folder not found" );
146
165
}
147
166
}
148
167
@@ -192,7 +211,7 @@ public static String find(String xmlO) {
192
211
193
212
toShow = toShow .length () > maxToShow ? toShow .substring (0 , maxToShow ) + ".." : toShow ;
194
213
encrypted = encrypted .length () > maxToShow ? encrypted .substring (0 , maxToShow ) + ".." : encrypted ;
195
- print ("[" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : "" ));
214
+ print ("\t [" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : "" ), true );
196
215
} catch (Exception e ) {
197
216
print ("error on " + result );
198
217
e .printStackTrace ();
@@ -204,8 +223,6 @@ public static String find(String xmlO) {
204
223
if (xml1 .indexOf (toFind1 ) <= 0 ) break ;
205
224
}
206
225
207
- print (content );
208
-
209
226
return content ;
210
227
}
211
228
0 commit comments