package com.uzen.slimapk;

import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.FileSystem;
import java.io.IOException;

import com.uzen.slimapk.parser.FileXMLParser;
import com.uzen.slimapk.parser.ApkFileVisitor;
import com.uzen.slimapk.struct.ApkOptions;
import com.uzen.slimapk.struct.AndroidConstants;
import com.uzen.slimapk.struct.ApkMeta;
import com.uzen.slimapk.utils.Utils;

public class SlimParser extends SlimApk {
	
	private FileXMLParser Parser;
	private ApkMeta meta;

	public SlimParser(String input, String output, ApkOptions Options) throws IOException {
		super(input, output, Options);
	}
	
	public void parse() {
		try {
			ApkFileVisitor ApkParser = new ApkFileVisitor() {
				@Override
				public void actionFile(Path apk) {
					if(apk.toString().toLowerCase().endsWith(AndroidConstants.EXTENSION)){
						unzipApk(apk);		
					}
				}
			};
			Parser = new FileXMLParser();
			Files.createDirectories(output);
			Files.walkFileTree(input, ApkParser);
		} catch (IOException e) {
			log.e("Error while searching for files:\n {0}", e);
		} finally {
			try {
				if(temp != null) {
					temp.toFile().deleteOnExit();
				}
				writeFileList();
			} catch (IOException ex) {
				log.e("Error writing to the file list:\n {0}", ex);
			}
		}
	}
	
	private void unzipApk(Path source) {
		log.d("EXTRACTING: {0}", source);
		Path app = null, _app = createTempApp(source);
		Path outdir = null;
		
		try {
			FileSystem ApkFileSystem = Utils.getFileSystem(_app);
			final Path root = ApkFileSystem.getPath("/");
			final Path libdir = root.resolve(AndroidConstants.LIB_PREFIX);
			meta = getMeta(root);
			String label = meta.getName(), version = meta.getVersionName();
			addElementToList(label, version);
			log.d("PackageName: " + label);
			log.d("Version: " + version);
			
			label = label.replaceAll("\\s","");
			
			if (Options.getKeepMode()) {
				outdir = output.resolve(input.relativize(source));
				outdir = outdir.resolveSibling(label);
			} else {
				outdir = output;
				outdir = outdir.resolve(label);
			}
						
			cleaning(outdir);			
			extractLibrary(libdir, outdir);
			ApkFileSystem.close();
			app = outdir.resolve(label + AndroidConstants.EXTENSION);
			Files.move(_app, app);				
			log.d("SAVE: {0}", app);
		} catch (IOException e) {
			log.e("Unpacking the application failed:\n {0}", e);
			try{
				if(outdir != null)
					deleteDirectories(outdir);
			} catch (IOException ex) {
				log.e("Сould not delete the application files:\n {0}", ex);
			}
		}
	}
	
	private void extractLibrary(Path libdir, Path outdir) throws IOException {
			LibraryFilter lib = new LibraryFilter(libdir, meta.getMultiArch());
			log.d("Copying libraries...");
			lib.extract(Options.getABI(), outdir);
			deleteDirectories(libdir);
	}
	
	private ApkMeta getMeta(Path path){
			Parser.setName(path);
			Parser.parseData();
			return Parser.getMeta();
	}
}

@@ -467,17 +467,17 @@
 LParser 
-P
+p
 arser;%0A%09
@@ -497,16 +497,37 @@
 ta meta;
+%09%0A%09private Path temp;
 %0A%0A%09publi
@@ -642,16 +642,91 @@
 tions);%0A
+%09%09if(Options.isCache())%7B%0A%09%09%09temp = Files.createTempDirectory(%22slim_%22);%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09pu
@@ -976,25 +976,30 @@
 %09%7D%0A%09%09%09%7D;%0A%09%09%09
-P
+this.p
 arser = new 
@@ -1499,24 +1499,30 @@
 mpApp(source
+, temp
 );%0A%09%09Path ou
@@ -2179,16 +2179,64 @@
 %0A%09%09%09
-cleaning
+this.deleteDirectory(outdir);%0A%09%09%09Files.createDirectories
 (out
@@ -2242,17 +2242,16 @@
 tdir);%09%09
-%09
 %0A%09%09%09extr
@@ -2549,35 +2549,33 @@
 %09%09deleteDirector
-ies
+y
 (outdir);%0A%09%09%09%7D c
@@ -2916,19 +2916,17 @@
 Director
-ies
+y
 (libdir)
@@ -2968,25 +2968,25 @@
 h path)%7B%0A%09%09%09
-P
+p
 arser.setNam
@@ -2997,17 +2997,17 @@
 th);%0A%09%09%09
-P
+p
 arser.pa
@@ -3027,17 +3027,17 @@
 %09return 
-P
+p
 arser.ge

Remove a function cleaning