All pastes #896198 Raw Edit

dd

public text v1 · immutable
·published 2008-02-08 13:20 UTC
rendered paste body
/*
 * Created on 05.02.2008
 * emonic.test emonic.test MBelIntoFile.java
 */

package emonic.test;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.emonic.base.EMonoPlugin;
import org.emonic.base.codehierarchy.AssemblyParser;
import org.emonic.base.codehierarchy.IAssembly;
import org.emonic.base.codehierarchy.IDotNetElement;
import org.emonic.base.codehierarchy.IField;
import org.emonic.base.codehierarchy.IMethod;
import org.emonic.base.codehierarchy.INamespace;
import org.emonic.base.codehierarchy.IType;
import org.emonic.base.filemanipulators.FileDocument;

import junit.framework.TestCase;

public class MBelIntoFile extends TestCase {

	//FIXME dynamic path introduction
	private static final String dllPathOne = "/home/dertl/ViMEM.dll";
	private static final String dllDirPath = "/home/dertl/vimem/mono/lib/mono/1.0/";
	private static final String assemblyFilePath ="/home/dertl/vimem/assembly.txt";
		
	private String text;
	
	
	public MBelIntoFile(){
		super();
	}
	
	public void testCreation() throws Throwable{
		text = "";
	//FIXME MBEL is unable to work with large files like mscorlib.dll => Remy is on track
		//HINT: it works with the object browser but not with this file => maybe too much info?
		Job job = new Job("Parsing...") {
			public IStatus run(IProgressMonitor monitor) {
				try {
					monitor.beginTask("Analyzing " , 10);
					//Call AssemblyParser of MBel => use dllPathOne

					Vector fileVec = new Vector();
					fileVec.add(new File(dllPathOne));
					
					File dllDir = new File(dllDirPath);
					if(dllDir.isDirectory()){
						String[] files = dllDir.list();
						for(int i=0; i< files.length;i++){
							//if file = dll
							if(files[i].endsWith(".dll") && !files[i].endsWith("mscorlib.dll"))
								fileVec.add(new File(dllDir, files[i]));
						}
					}									
					Map[] maps = new Map[]{ Collections.EMPTY_MAP,
							Collections.EMPTY_MAP };				

					monitor.worked(5);			
					FileOutputStream fos = getFileOutputStreamForDlls();
					
					//create file with file input of MBel
					Iterator iter =  fileVec.iterator();
					while(iter.hasNext()){
						File actualFile = (File) iter.next();
						maps = new Map[]{ Collections.EMPTY_MAP,
								Collections.EMPTY_MAP };
						System.out.print(actualFile.getName());
						Date start = new Date();						
						createFile(AssemblyParser.parseAssembly(new FileInputStream(actualFile.getAbsolutePath()),
													 new Path(actualFile.getAbsolutePath()), maps), fos);
						Date end = new Date();
						System.out.println( " Time: "+(end.getTime()-start.getTime())+"ms");
					}
					 													
					//grep for elements
					Date grepstart = new Date();
					grepElements();
										
			        Date grepend = new Date();
			        System.out.println("[GREP] Time: "+(grepend.getTime()-grepstart.getTime())+"ms");

					return Status.OK_STATUS;
					
				}catch (Exception e) {
					e.printStackTrace();
					return Status.CANCEL_STATUS;
				} finally {
					monitor.done();
				}
			}
		};
		job.schedule();
		while(true){
		 if(job.getState() == job.NONE)
			 break;
		}
		
			
	}

	private void grepElements() {
		//Search file via java-grep => accords to string 
		//bei this => create 
		String re = "IParaFrame";
		String line;
		BufferedReader input =null;
		Matcher matcher;
		Pattern pattern;
		
		try {
			input = new BufferedReader(new FileReader(assemblyFilePath));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		pattern = Pattern.compile(".*"+re+".*");
		
		try{
			while((line = input.readLine()) != null){
				matcher = pattern.matcher(line);
				if(matcher.matches()){
					System.out.println(line);
				}
			}
		}catch(IOException e){
			e.printStackTrace();
		}			
	}
	
	private void createFile(IAssembly assembly, FileOutputStream fos) {
				
		//get namespace[]
		INamespace[] namespaces =  assembly.getNamespaces();
		for(int i=0; i<namespaces.length;i++){
			String namespaceName = namespaces[i].getElementName();
			//namespace itself has to element of file 
			text += namespaceName + "\n";
			
			//IDotNetElement[] dotnetelements = namespaces[i].getChildren();
			IType[] dotnetTypes = namespaces[i].getTypes();												
			
			//get dotnetelements of types like classes
			for(int j=0; j<dotnetTypes.length;j++){
				String dotnettext = namespaceName + "." + dotnetTypes[j].getElementName();
				//type itself has to be element of file
				text+=dotnettext +"\n";
				
				
				IDotNetElement[] dotnetelements = dotnetTypes[j].getChildren();

				
				for(int k=0; k<dotnetelements.length;k++){
					int type = dotnetelements[k].getElementType();
					String additionaltext = "";
					
					/*
					 * TODO: if the following switch(type) block is used, the time for calculation
					 * of ViMEM.dll is doubled from 0,3-0,4 sec to 0,6-0,7sec!!!
					 */
					switch(type){
						case IDotNetElement.METHOD:
							additionaltext = " FLAGS:"+((IMethod)dotnetelements[k]).getFlags() + 
								" RETURN:"+((IMethod)dotnetelements[k]).getReturnType() +
								" SIGNATURE:" + ((IMethod)dotnetelements[k]).getSignature(); 
							break;
						case IDotNetElement.FIELD:
							additionaltext = " FLAGS:"+((IField)dotnetelements[k]).getFlags();
							break;

					}
					
					
					text += dotnettext + "." + dotnetelements[k].getElementName() + additionaltext + "\n";
				}
			}
			
		}
		
		//get Types which are not in a namespace
		//FIXME
		
		int length = text.length();
		for (int i=0; i < length; i++){
		      try {
				fos.write((byte)text.charAt(i));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private FileOutputStream getFileOutputStreamForDlls() {
		//create file for output 
		File assemblyText = new File(assemblyFilePath);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(assemblyText);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fos;
	}
	
}