package com.lbslocal.api.scriptcompact;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import com.lbslocal.api.scriptcompact.JSMin.UnterminatedCommentException;
import com.lbslocal.api.scriptcompact.JSMin.UnterminatedRegExpLiteralException;
import com.lbslocal.api.scriptcompact.JSMin.UnterminatedStringLiteralException;
import com.lbslocal.api.utils.Common;
import com.lbslocal.api.utils.ConfigurationManager;
import com.lbslocal.master.common.Utils;
public class ControlGenerate {
private String pathProject;
private String pathToWrite;
ControlGenerate(String pathProject, String pathToWrite) {
this.pathProject = pathProject;
this.pathToWrite = pathToWrite;
}
void generateAll() {
ConfigurationManager cm = ConfigurationManager.getInstance();
String[] apis = cm.getAppSettings("apis").toString().split(Pattern.quote("|"));
File fileDir = new File(pathToWrite);
if (!fileDir.exists())
fileDir.mkdirs();
String dados;
File file;
for (int i = 0; i < apis.length; i++) {
try {
dados = ToFileString(pathProject, cm.getAppSettings(apis[i]).split(Pattern.quote("|")));
file = new File(pathToWrite + File.separator + apis[i] + "_debug.js");
if (file.exists())
file.delete();
file = new File(pathToWrite + File.separator + apis[i] + ".js");
if (file.exists())
file.delete();
file = new File(pathToWrite + File.separator + apis[i] + ".gz");
if (file.exists())
file.delete();
dados = verifyDataConverter(dados);
Common.writeFile(pathToWrite + File.separator + apis[i] + "_debug.js", dados);
InputStream in = new ByteArrayInputStream(dados.getBytes("UTF-8"));
OutputStream out = new FileOutputStream(pathToWrite + File.separator + apis[i] + ".js");
JSMin js = new JSMin(in, out);
js.jsmin();
out.flush();
out.close();
createGZipFile(pathToWrite + File.separator + apis[i] + ".js", pathToWrite + File.separator + apis[i] + ".gz");
} catch (IOException e) {
} catch (UnterminatedRegExpLiteralException e) {
} catch (UnterminatedCommentException e) {
} catch (UnterminatedStringLiteralException e) {
}
}
}
private String verifyDataConverter(String value) throws IOException {
Common.writeFile(pathToWrite + File.separator + "temp_debug.js", value);
StringBuilder sb = new StringBuilder();
FileReader fr = new FileReader(pathToWrite + File.separator + "temp_debug.js");
int ch;
do {
ch = fr.read();
if (ch != -1 && ch < 256) {
sb.append((char) ch);
}
} while (ch != -1);
fr.close();
return sb.toString();
}
void createGZipFile(String path, String pathToGZip) {
try {
// Create the GZIP output stream
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(pathToGZip));
// Open the input file
FileInputStream in = new FileInputStream(path);
// Transfer bytes from the input file to the GZIP output stream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
// Complete the GZIP file
out.finish();
out.close();
} catch (IOException e) {
}
}
public String ToFileString(String path, String[] sFiles) throws IOException {
StringBuilder sBuilder = new StringBuilder();
try {
for (int i = 0; i < sFiles.length; i++) {
sBuilder.append(ToFileString(path + File.separator + sFiles[i])).append(System.getProperty("line.separator"));
}
return sBuilder.toString();
} catch (IOException e) {
Utils.LogError("MapLinkAPI.ControlGenerate", "String ToFileString(String path, String[] sFiles)", e.getMessage());
throw e;
}
}
public String ToFileString(String sPath) throws IOException {
try {
return Common.readFile(sPath, "utf-8");
} catch (IOException e) {
Utils.LogError("MAPLINKAPI", "ToFileString", e.getMessage());
throw e;
}
}
public File getFlat(String api) throws IOException {
return new File(this.pathToWrite + File.separator + api + ".js");
}
public File getGZip(String api) throws IOException {
return new File(this.pathToWrite + File.separator + api + ".gz");
}
public File getDebug(String api) throws IOException {
return new File(this.pathToWrite + File.separator + api + "_debug.js");
}
byte[] readInBytes(String fl) throws IOException {
File file = new File(fl);
FileInputStream fin = new FileInputStream(file);
byte fileContent[] = new byte[(int) file.length()];
fin.read(fileContent);
return fileContent;
}
}