1- package com .efraespada . stringobfuscatorplugin ;
1+ package com .stringcare ;
22
33import java .io .*;
4- import java .nio .channels .FileChannel ;
5- import java .nio .file .Files ;
6- import java .util .HashMap ;
7- import java .util .Map ;
84
9- import static com .efraespada . stringobfuscatorplugin .PrintUtils .print ;
5+ import static com .stringcare .PrintUtils .print ;
106
117public class FileUtils {
128
13- private static String variant ;
149 private static String module ;
1510 private static String key ;
11+ private static Config config ;
1612
1713 final static int maxToShow = 15 ;
1814
19-
20- private static final Map <String , String > files = new HashMap <>();
21-
2215 private FileUtils () {
2316 // nothing to do here
2417 }
2518
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 ) {
2820 FileUtils .key = key ;
2921 FileUtils .module = module ;
30- FileUtils .variant = variant ;
22+ FileUtils .config = config ;
3123 }
3224
3325 public static String getTextFromFilePath (String path ) {
@@ -37,8 +29,6 @@ public static String getTextFromFilePath(String path) {
3729 String xml = "" ;
3830 String inputFilePath = path ;
3931
40- if (true ) print ("reading " + inputFilePath );
41-
4232 String message = "" ;
4333
4434 File file = new File (inputFilePath );
@@ -83,41 +73,58 @@ public static String getString(BufferedReader br) {
8373 // detect multiple sourceSet res.srcDirs
8474 public static void backupStringResources () {
8575 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+ }
101101 }
102- } catch (IOException e ) {
103- e .printStackTrace ();
104- // TODO HANDLE ERROR
102+ } else {
103+ PrintUtils .print ("source folder not found: " + folder , true );
105104 }
106105 }
107106 }
108107
109108 public static void encryptStringResources () {
110109 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 );
121128 }
122129 }
123130 }
@@ -126,23 +133,35 @@ public static void restoreStringResources() {
126133 String currentPath = getCurrentPath () + module + File .separator + "resbackup" + File .separator ;
127134 File file = new File (currentPath );
128135 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+ }
140159 }
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" );
146165 }
147166 }
148167
@@ -192,7 +211,7 @@ public static String find(String xmlO) {
192211
193212 toShow = toShow .length () > maxToShow ? toShow .substring (0 , maxToShow ) + ".." : toShow ;
194213 encrypted = encrypted .length () > maxToShow ? encrypted .substring (0 , maxToShow ) + ".." : encrypted ;
195- print ("[" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : "" ));
214+ print ("\t [" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : "" ), true );
196215 } catch (Exception e ) {
197216 print ("error on " + result );
198217 e .printStackTrace ();
@@ -204,8 +223,6 @@ public static String find(String xmlO) {
204223 if (xml1 .indexOf (toFind1 ) <= 0 ) break ;
205224 }
206225
207- print (content );
208-
209226 return content ;
210227 }
211228
0 commit comments