package sds;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import sds.assemble.ClassContent;
import sds.decompile.DecompileProcessor;
import sds.util.ClassFilePrinter;
import org.eclipse.collections.impl.list.mutable.FastList;

/**
 * This class is for driving SDS.
 * @author inagaki
 */
public class SDS {
	private FastList<String> argList;
	private JarFile jar;

	/**
	 * constructor.
	 * @param args command line arguments
	 */
	public SDS(String[] args) {
		this.argList = new FastList<>();
		for(String arg : args) {
			if(arg.endsWith(".class")) {
				argList.add(arg);
			} else if(arg.endsWith(".jar")) {
				try {
					this.jar = new JarFile(new File(arg));
				} catch(IOException e) {
					e.printStackTrace();
				}
			} else {
				System.out.println(arg + " is not classfile or jar.");
			}
		}
	}

	/**
	 * starts SDS.
	 */
	public void run() {
		boolean output = true;
		// classfile
		for(String arg : argList) {
			ClassFileReader reader = new ClassFileReader(arg);
			reader.read();
			ClassFile cf = reader.getClassFile();
			printClassFile(cf, output);
			ClassContent cc = new ClassContent(cf);
			DecompileProcessor dp = new DecompileProcessor();
			dp.process(cc);
		}
		
		// jar
		if(jar == null)
			return;
		try {
			for(Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
				JarEntry entry = e.nextElement();
				if(! entry.toString().endsWith(".class")) {
					continue;
				}
				ClassFileReader reader = new ClassFileReader(jar.getInputStream(entry));
				reader.read();
				ClassFile cf = reader.getClassFile();
				printClassFile(cf, output);
				ClassContent cc = new ClassContent(cf);
			}
		} catch(Exception e) {}
	}

	private void printClassFile(ClassFile cf, boolean output) {
		if(! output) {
			return;
		}
		try {
			ClassFilePrinter printer = new ClassFilePrinter(cf.pool);
			printer.printNumber(cf.magicNumber, cf.majorVersion, cf.minorVersion);
			printer.printConstantPool();
			printer.printAccessFlag(cf.accessFlag);
			printer.printThisClass(cf.thisClass);
			printer.printSuperClass(cf.superClass);
			printer.printInterface(cf.interfaces);
			printer.printFields(cf.fields);
			printer.printMethods(cf.methods);
			printer.printAttributes(cf.attr);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
@@ -32,36 +32,8 @@
 le;%0A
-import java.io.InputStream;%0A
 impo
@@ -422,23 +422,26 @@
 String%3E 
-argList
+classFiles
 ;%0A%09priva
@@ -559,23 +559,26 @@
 %0A%09%09this.
-argList
+classFiles
  = new F
@@ -618,16 +618,255 @@
 rgs) %7B%0A%09
+%09%09parseArg(arg);%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * starts SDS.%0A%09 */%0A%09public void run() %7B%0A%09%09// classfile%0A%09%09if(classFiles.size() %3E 0) %7B%0A%09%09%09analyzeClassFile();%0A%09%09%7D%0A%09%09// jar%0A%09%09if(jar != null) %7B%0A%09%09%09analyzeJarFile();%0A%09%09%7D%0A%09%7D%0A%0A%09private void parseArg(String arg) %7B%0A
 %09%09if(arg
@@ -895,16 +895,18 @@
 %0A%09%09%09
-%09argList
+classFiles
 .add
@@ -912,17 +912,16 @@
 d(arg);%0A
-%09
 %09%09%7D else
@@ -951,24 +951,22 @@
 )) %7B%0A%09%09%09
-%09
 try %7B%0A
-%09
 %09%09%09%09this
@@ -1003,17 +1003,16 @@
 g));%0A%09%09%09
-%09
 %7D catch(
@@ -1032,17 +1032,16 @@
 ) %7B%0A%09%09%09%09
-%09
 e.printS
@@ -1053,25 +1053,23 @@
 race();%0A
-%09
 %09%09%09%7D%0A%09%09
-%09
 %7D else %7B
@@ -1069,17 +1069,16 @@
  else %7B%0A
-%09
 %09%09%09Syste
@@ -1131,63 +1131,46 @@
 %22);%0A
-%09
 %09%09%7D%0A%09
-%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * starts SDS.%0A%09 */%0A%09public void run
+%7D%0A%0A%09private void analyzeClassFile
 () %7B
@@ -1195,31 +1195,16 @@
 = true;%0A
-%09%09// classfile%0A
 %09%09for(St
@@ -1218,15 +1218,18 @@
 g : 
-argList
+classFiles
 ) %7B%0A
@@ -1496,46 +1496,67 @@
 %09%7D%0A%09
-%09%0A%09%09// jar%0A%09%09if(jar == null)%0A%09%09%09return
+%7D%0A%0A%09private void analyzeJarFile() %7B%0A%09%09boolean output = true
 ;%0A%09%09
@@ -1679,29 +1679,61 @@
 %09%09%09%09
-if(! entry.toString()
+String fileName = entry.toString();%0A%09%09%09%09if(! fileName
 .end

add method for parsing argument and analyzing classfile and jar file.