package js2as;

import java.util.HashMap;

import libs.MyException;
import libs.Utility;

import org.json.JSONObject;

import androidStringXML.AndroidStringXML;

/**
 * The main class performing the job that reads JSON source files and parse JSON resources and generate Android strings.xml files
 *
 */
public class Js2As {
	
	public static void main(String[] args) {
		
		try {
			// Collect the command args (if fail, then just return.)
			if (!ArgsMgr.collectArgs(args)) {
				return;
			}
			
			System.out.println(">>> Reading the JSON files to get the JSON resources...");
			HashMap<String, JSONObject> jsonResources = Js2As.readJsonFiles(ArgsMgr.ARG_FILE_PATHS);
			if (jsonResources == null) {
				throw (new MyException("Reading the JSON files fails or no valid JSON resources available!!!"));
			}
			
			System.out.println(">>> Parsing the JSON resources into the Android string resources...");
			JsonStringResourcesProvider jp = new JsonStringResourcesProvider(jsonResources);
			
			System.out.println(">>> Generating the strings.xml files...");
			(new AndroidStringXML(ArgsMgr.ARG_RES_DIR_ROOT, jp)).generateXMLs();
			
			// End successfully
			System.out.println(">>> Done on generating: Please check " + ArgsMgr.ARG_RES_DIR_ROOT);
			
		} catch (MyException e) {
			// End erroneously
			System.out.println(">>> Fail on generating: " + e.getMessage());
		}
	}
	
	
	/**
	 * Read .json files and convert the contents into the JSON object resources
	 * 
	 * @param paths
	 * 		The paths of .json files
	 * @return
	 * 		- If OK: One HashMap object which its key is the file path and its value is the JSON object resource parsed from that path
	 * 		<br/>
	 * 		- If NG: null
	 */
	public static HashMap<String, JSONObject> readJsonFiles(String[] paths) {
		
		HashMap<String, JSONObject> jsonResources = null;
		
		if (paths != null && paths.length > 0) {
		
			String jsTxt;
			
			for (String p : paths) {
				
				try {
				
					jsTxt = Utility.Files.readFileAll(p, "UTF-16");
					
					if (jsTxt != null) {
						
						try {
							
							if (jsonResources == null) {
								jsonResources = new HashMap<String, JSONObject>();
							}
							jsonResources.put(p, new JSONObject(jsTxt));
							
						} catch (Exception e) {
							jsTxt = "\r\n        ********** corrupt JSON **********\r\n        " + jsTxt + "\r\n        **********************************\r\n";
							throw new MyException("Unable to read the file: " + p + " because of the corrupt JSON: " + e.getMessage() + jsTxt);	
						}
						
					} else {
						throw new MyException("Nothing can be read from the file: " + p);
					}
					
				} catch (MyException e) {
					e.print1stPoint();
				}
			}
		}
		
		return jsonResources;
	}
}

@@ -687,16 +687,42 @@
 LE_PATHS
+, ArgsMgr.ARG_FILE_CHARSET
 );%0D%0A%09%09%09i
@@ -1586,24 +1586,87 @@
 json files%0D%0A
+%09 * @param charset%0D%0A%09 * %09%09The charset used when reading files%0D%0A
 %09 * @return%0D
@@ -1666,16 +1666,16 @@
 return%0D%0A
-
 %09 * %09%09- 
@@ -1903,24 +1903,40 @@
 ring%5B%5D paths
+, String charset
 ) %7B%0D%0A%09%09%0D%0A%09%09H
@@ -2158,16 +2158,15 @@
 (p, 
-%22UTF-16%22
+charset
 );%0D%0A
@@ -2388,32 +2388,39 @@
 sTxt));%0D%0A%09%09%09%09%09%09%09
+%09%09%09%09%09%09%09
 %0D%0A%09%09%09%09%09%09%7D catch 

Update for the new charset command