package android.malware.detection.transformations;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.m2m.atl.common.ATLExecutionException;
import org.eclipse.m2m.atl.core.ATLCoreException;
import org.eclipse.m2m.atl.core.IExtractor;
import org.eclipse.m2m.atl.core.IInjector;
import org.eclipse.m2m.atl.core.IModel;
import org.eclipse.m2m.atl.core.IReferenceModel;
import org.eclipse.m2m.atl.core.ModelFactory;
import org.eclipse.m2m.atl.core.emf.EMFExtractor;
import org.eclipse.m2m.atl.core.emf.EMFInjector;
import org.eclipse.m2m.atl.core.emf.EMFModel;
import org.eclipse.m2m.atl.core.emf.EMFModelFactory;
import org.eclipse.m2m.atl.core.emf.EMFReferenceModel;
import org.eclipse.m2m.atl.core.launch.ILauncher;
import org.eclipse.m2m.atl.engine.emfvm.launch.EMFVMLauncher;

import malware.android.analysis.Activator;
import malware.android.analysis.Utility;

public class DetectionByICCPattern {

	public static void runATL(String inputURI1,String inputURI2, String outputURI) {
        try {
        	System.out.println("ATLinitial:\n");
            ILauncher transformationLauncher = new EMFVMLauncher();
            ModelFactory modelFactory = new EMFModelFactory();
            IInjector injector = new EMFInjector();
            IExtractor extractor = new EMFExtractor();
            
            
            System.out.println("recievemodels:\n");
            IReferenceModel androidMetaModel = modelFactory.newReferenceModel();
            injector.inject(androidMetaModel, Utility.ANDROID_SECURITY_METAMODEL_PATH);
            
            IReferenceModel FeatureMetaModel = modelFactory.newReferenceModel();
            injector.inject(FeatureMetaModel, Utility.FEATURE_METAMODEL_PATH);
            
            
            
            IReferenceModel ICCPatternMetamodel = modelFactory.newReferenceModel();
    		injector.inject(ICCPatternMetamodel, Utility.ICC_PATTERN_METAMODEL_PATH);
            

            IModel inModel1 = modelFactory.newModel(androidMetaModel);
            injector.inject(inModel1, inputURI1);
            
            IModel inModel2 = modelFactory.newModel(FeatureMetaModel);
            injector.inject(inModel2, inputURI2);

            IModel outModel = modelFactory.newModel(ICCPatternMetamodel);
            transformationLauncher.initialize(new HashMap());
            transformationLauncher.addInModel(inModel1, "IN", "ASEC");
            transformationLauncher.addInModel(inModel2, "IN1", "FeatureModel");
            transformationLauncher.addOutModel(outModel, "OUT", "ICC");
            transformationLauncher.launch(ILauncher.RUN_MODE, new NullProgressMonitor(), new HashMap(),
                    new FileInputStream(Utility.DETECTION_ICC_PATTERN1));
            System.out.println("executeATL:\n");
            extractor.extract(outModel,  outputURI);
            System.out.println("savemodel:\n");
           
            EMFModelFactory emfModelFactory = (EMFModelFactory) modelFactory;
            emfModelFactory.unload((EMFModel) inModel1);
            emfModelFactory.unload((EMFModel) inModel2);
            emfModelFactory.unload((EMFModel) outModel);
            emfModelFactory.unload((EMFReferenceModel) androidMetaModel);
            emfModelFactory.unload((EMFReferenceModel) FeatureMetaModel);
            emfModelFactory.unload((EMFReferenceModel) ICCPatternMetamodel);
        } catch (ATLCoreException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }	

	
}
