Index: trunk/de.ugoe.cs.swe.bnftools.xtools/.classpath
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.xtools/.classpath	(revision 58)
+++ trunk/de.ugoe.cs.swe.bnftools.xtools/.classpath	(revision 58)
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="lib" path="lib/slf4j-api-1.7.2.jar"/>
+	<classpathentry kind="lib" path="lib/slf4j-ext-1.7.2.jar"/>
+	<classpathentry kind="lib" path="lib/slf4j-simple-1.7.2.jar"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
Index: trunk/de.ugoe.cs.swe.bnftools.xtools/.project
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.xtools/.project	(revision 58)
+++ trunk/de.ugoe.cs.swe.bnftools.xtools/.project	(revision 58)
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>de.ugoe.cs.swe.bnftools.xtools</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.ManifestBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.SchemaBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.pde.PluginNature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
Index: trunk/de.ugoe.cs.swe.bnftools.xtools/.settings/org.eclipse.jdt.core.prefs
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.xtools/.settings/org.eclipse.jdt.core.prefs	(revision 58)
+++ trunk/de.ugoe.cs.swe.bnftools.xtools/.settings/org.eclipse.jdt.core.prefs	(revision 58)
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
+org.eclipse.jdt.core.compiler.compliance=1.7
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.7
Index: trunk/de.ugoe.cs.swe.bnftools.xtools/META-INF/MANIFEST.MF
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.xtools/META-INF/MANIFEST.MF	(revision 58)
+++ trunk/de.ugoe.cs.swe.bnftools.xtools/META-INF/MANIFEST.MF	(revision 58)
@@ -0,0 +1,16 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Xtools
+Bundle-SymbolicName: de.ugoe.cs.swe.bnftools.xtools
+Bundle-Version: 1.0.0.qualifier
+Bundle-RequiredExecutionEnvironment: JavaSE-1.7
+Require-Bundle: org.eclipse.core.runtime,
+ org.eclipse.xtext,
+ org.eclipse.ocl.common,
+ org.eclipse.ocl.ecore,
+ de.ugoe.cs.swe.bnftools.ebnf
+Bundle-RequiredExecutionEnvironment: JavaSE-1.7
+Bundle-ActivationPolicy: lazy
+Import-Package: org.apache.commons.io;version="2.0.1"
+Export-Package: app,
+ resource.tools.xtools
Index: trunk/de.ugoe.cs.swe.bnftools.xtools/build.properties
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.xtools/build.properties	(revision 58)
+++ trunk/de.ugoe.cs.swe.bnftools.xtools/build.properties	(revision 58)
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .
Index: trunk/de.ugoe.cs.swe.bnftools.xtools/src/app/Translator.java
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.xtools/src/app/Translator.java	(revision 58)
+++ trunk/de.ugoe.cs.swe.bnftools.xtools/src/app/Translator.java	(revision 58)
@@ -0,0 +1,40 @@
+package app;
+
+import java.io.File;
+
+import org.eclipse.emf.ecore.resource.Resource;
+
+import resource.tools.xtools.EbnfResourceTool;
+
+
+public class Translator {
+	public Translator(){
+		super();
+	}
+	
+	public static void main(String[] args) {
+		if (args.length != 1) {
+			System.out.println("Usage: Translator <FILE>");
+			return;
+		}
+		Translator app = new Translator();
+		app.process(args[0],"bnf");
+	}
+
+	public void process(String filename, String extension) {
+		System.out.println("Processing: " + filename);
+		if (!filename.endsWith(extension)) {
+			System.out.println("Selected file is not supported!");
+			return;
+		}
+		switch (extension) {
+		case "bnf":
+			EbnfResourceTool tool = new EbnfResourceTool();
+			tool.processFile(new File(filename));
+			break;
+		default:
+			break;
+		}
+	}
+
+}
Index: trunk/de.ugoe.cs.swe.bnftools.xtools/src/resource/tools/xtools/EbnfResourceTool.java
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.xtools/src/resource/tools/xtools/EbnfResourceTool.java	(revision 58)
+++ trunk/de.ugoe.cs.swe.bnftools.xtools/src/resource/tools/xtools/EbnfResourceTool.java	(revision 58)
@@ -0,0 +1,121 @@
+package resource.tools.xtools;
+
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EStructuralFeature.Setting;
+import org.eclipse.emf.ecore.EValidator;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.EObjectValidator;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.xtext.EcoreUtil2;
+import org.eclipse.xtext.nodemodel.ICompositeNode;
+import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
+import org.slf4j.LoggerFactory;
+
+import de.ugoe.cs.swe.bnftools.EbnfStandaloneSetup;
+import de.ugoe.cs.swe.bnftools.ebnf.EbnfPackage;
+import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
+import de.ugoe.cs.swe.bnftools.ebnf.Rule;
+import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
+import de.ugoe.cs.swe.bnftools.ebnf.impl.EbnfPackageImpl;
+import resource.tools.xtools.ResourceTool;
+
+public class EbnfResourceTool extends ResourceTool {
+	
+	public EbnfResourceTool(){
+		super();
+		log = LoggerFactory.getLogger(EbnfResourceTool.class);
+		EbnfPackageImpl.init();
+		initializeValidator();
+		injector = new EbnfStandaloneSetup().createInjectorAndDoEMFRegistration();
+	}
+	
+	@Override
+	protected void initializeValidator(){
+		super.initializeValidator();
+		EObjectValidator validator = new EObjectValidator();
+		EValidator.Registry.INSTANCE.put(EbnfPackage.eINSTANCE, validator);
+	}
+
+	public void processFile(File xtextFile) {
+
+		//TODO: extract as configuration
+		String extension = "bnf";
+		String workspace = xtextFile.getParentFile().getAbsolutePath();
+		String xTextLocation = xtextFile.getAbsolutePath();
+		String outputPath = xtextFile.getAbsolutePath()+".ebnf";
+		String outputPathValidated = xtextFile.getAbsolutePath()+".ebnf";
+		
+		Resource resource = loadResourceFromXtext(workspace,xTextLocation,true);
+		findReferences(resource);
+		storeResourceContents(resource.getContents(), outputPath, extension);
+		Resource fromXMI = loadResourceFromXMI(outputPath, extension);
+//	    validateResource(fromXMI);
+		storeResourceContents(fromXMI.getContents(), outputPathValidated, extension);
+	}
+
+	private void findReferences(Resource resource) {
+		//custom reference finding, specific for BNF rule references 
+		//could be generalized for any references at the Ecore level
+		//but maybe it is fully sufficient
+		EtsiBnf bnf = (EtsiBnf) resource.getContents().get(0);
+
+		//first approach
+		System.out.println("first approach (top down)");
+		List<Rule> rules = EcoreUtil2.getAllContentsOfType(bnf, Rule.class);
+		for (Rule r : rules) {
+			System.out.println("Processing rule "+r.getName());
+			for (RuleReference ref : EcoreUtil2.getAllContentsOfType(r, RuleReference.class)) {
+				EcoreUtil2.getContainerOfType(ref, Rule.class);
+				System.out.println("  " + r.getName() + " references " + ref.getRuleref().getName());
+				System.out.println("    -> reference index shall contain \"" + ref.getRuleref().getName() + " referenced in " + r.getName() + "\"");
+				
+			}
+		}
+		
+		//second approach
+		System.out.println("second approach (bottom up)");
+		for (RuleReference ref : EcoreUtil2.getAllContentsOfType(bnf, RuleReference.class)) {
+			Rule r = EcoreUtil2.getContainerOfType(ref, Rule.class);
+			System.out.println("" + r.getName() + " references " + ref.getRuleref().getName());
+			System.out.println("  -> reference index shall contain \"" + ref.getRuleref().getName() + " referenced in " + r.getName() + "\"");
+			
+		}
+		
+		//third approach
+		System.out.println("thir approach (cross referencing)");
+		for (Rule r : rules) {
+			System.out.println("Processing rule "+r.getName());
+			for (Setting s : EcoreUtil.UsageCrossReferencer.find(r, bnf)) {
+				RuleReference ref = (RuleReference) s.getEObject();
+				Rule referencingRule = EcoreUtil2.getContainerOfType(ref, Rule.class);
+				System.out.println("" + referencingRule.getName() + " references " + r.getName());
+				System.out.println("  -> reference index shall contain \"" + r.getName() + " referenced in " + referencingRule.getName() + "\"");
+			}
+		}
+		
+
+	}
+
+	public void process(String workspace) {
+
+		//TODO: extract as configuration
+		String extension = "tplan2x";
+		String xTextLocation = workspace+"/model."+extension;
+		String outputPath = workspace+"/model.tplan2";
+		String outputPathValidated = workspace+"/model.tplan2";
+		
+		Resource resource = loadResourceFromXtext(workspace,xTextLocation,true);
+		storeResourceContents(resource.getContents(), outputPath, extension);
+		Resource fromXMI = loadResourceFromXMI(outputPath, extension);
+//	    validateResource(fromXMI);
+		storeResourceContents(fromXMI.getContents(), outputPathValidated, extension);
+	}
+	
+}
Index: trunk/de.ugoe.cs.swe.bnftools.xtools/src/resource/tools/xtools/ResourceTool.java
===================================================================
--- trunk/de.ugoe.cs.swe.bnftools.xtools/src/resource/tools/xtools/ResourceTool.java	(revision 58)
+++ trunk/de.ugoe.cs.swe.bnftools.xtools/src/resource/tools/xtools/ResourceTool.java	(revision 58)
@@ -0,0 +1,194 @@
+package resource.tools.xtools;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.io.FileUtils;
+import org.eclipse.emf.common.util.BasicDiagnostic;
+import org.eclipse.emf.common.util.Diagnostic;
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.common.util.TreeIterator;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EOperation;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.EValidator;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.FileURIHandlerImpl;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecore.util.Diagnostician;
+import org.eclipse.emf.ecore.util.EObjectValidator;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
+import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
+import org.eclipse.ocl.common.OCLConstants;
+import org.eclipse.ocl.ecore.delegate.OCLInvocationDelegateFactory;
+import org.eclipse.ocl.ecore.delegate.OCLSettingDelegateFactory;
+import org.eclipse.ocl.ecore.delegate.OCLValidationDelegateFactory;
+import org.eclipse.xtext.resource.SaveOptions;
+import org.eclipse.xtext.resource.XtextResource;
+import org.eclipse.xtext.resource.XtextResourceSet;
+import org.eclipse.xtext.serializer.impl.Serializer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.impl.SimpleLogger;
+
+import com.google.inject.Injector;
+
+public class ResourceTool {
+
+	protected static Logger log;
+	protected Injector injector;
+
+	public ResourceTool() {
+		System.setProperty(Logger.class.getName(),SimpleLogger.class.getName());
+		System.setProperty("org.slf4j.simpleLogger.logFile","validation.log");
+		System.setProperty("org.slf4j.simpleLogger.logFile","System.out");
+		log = LoggerFactory.getLogger(ResourceTool.class);	
+	}
+
+	public Resource loadResourceFromXtext(String workspace, String pathName, boolean resolveAll) {
+		// "workspace" is a string that contains the path to the workspace containing the DSL program.
+		new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri(workspace);
+		
+		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
+
+		//TODO:why is this not needed for FAMIX but needed for DAG?
+		if (resolveAll) {
+			resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
+		}
+
+		Resource resource = resourceSet.getResource(URI.createURI(pathName), true);
+		for (org.eclipse.emf.ecore.resource.Resource.Diagnostic diagnostic : resource.getErrors()) {
+			log.warn("xtext: "+diagnostic.getLine() +" :"+diagnostic.getMessage());
+		}
+		return resource;
+	}
+
+	@SuppressWarnings("restriction")
+	public Resource storeResourceInXtext(String workspace, String pathName, EList<EObject> contents) {
+		// "workspace" is a string that contains the path to the workspace containing the DSL program.
+		new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri(workspace);
+		
+		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
+
+		String s = "";
+		Serializer serializer = injector.getInstance(Serializer.class); 
+
+		try {  
+			EObject tdlPackage = contents.get(0);
+			s += serializer.serialize(tdlPackage);  
+		} catch (Exception ex) { // fall back:  
+			System.out.println("Model could not be serialized"); 
+			ex.printStackTrace();
+		}  
+//		TreeIterator<Object> iterator = EcoreUtil.getAllContents(contents);
+//		while (iterator.hasNext()) {
+//			EObject next = (EObject) iterator.next();
+//			try {  
+//				s += serializer.serialize(next)+"\n";  
+//			} catch (Exception ex) { // fall back:  
+//				System.out.println(next.eClass().getName() +" could not be serialized");  
+//			}  
+//		}
+		System.out.println(s);
+		
+		try {
+			FileUtils.writeStringToFile(new File(pathName), s);
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		Resource resource = null;
+		//Resource resource = loadResourceFromXtext(workspace, pathName, true);
+
+		return resource;
+	}
+
+	
+	@SuppressWarnings({ "rawtypes", "unchecked" })
+	public Resource loadResourceFromXMI(String inputPath, String extension) {
+	    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
+	    Map<String, Object> m = reg.getExtensionToFactoryMap();
+		m.put(extension, new XMIResourceFactoryImpl());
+	    ResourceSet resSetIn = new ResourceSetImpl();
+	    Resource inputResource = resSetIn.createResource(URI.createURI(inputPath));
+	    try {
+	    	Map options = new HashMap<>();
+	    	options.put(XMIResourceImpl.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE);
+//	    	options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD);
+			inputResource.load(options);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return inputResource;
+	}
+
+	protected void initializeValidator() {
+	//		OCL.initialize(null);
+			String oclDelegateURI = OCLConstants.OCL_DELEGATE_URI+"/Pivot";
+			
+		    EOperation.Internal.InvocationDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI,
+		        new OCLInvocationDelegateFactory(oclDelegateURI));
+		    EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI,
+		        new OCLSettingDelegateFactory(oclDelegateURI));
+		    EValidator.ValidationDelegate.Registry.INSTANCE.put(oclDelegateURI,
+		        new OCLValidationDelegateFactory(oclDelegateURI));
+		    
+	//	    EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, 
+	//	    	new OCLSettingDelegateFactory.Global());
+	//	    QueryDelegate.Factory.Registry.INSTANCE.put(oclDelegateURI, new OCLQueryDelegateFactory.Global());
+		    
+		}
+
+	public void validateResource(Resource resource) {
+	    BasicDiagnostic diagnostics = new BasicDiagnostic();
+	    boolean valid = true;
+	    for (EObject eo : resource.getContents())
+	    {
+	    	Map<Object, Object> context = new HashMap<Object, Object>();
+	    	boolean validationResult = Diagnostician.INSTANCE.validate(eo, diagnostics, context);
+	    	showDiagnostics(diagnostics, "");
+			valid &= validationResult;
+	    }
+	    
+	    if (!valid){
+	    	System.out.println("Problem with validation!");
+	    }
+	}
+
+	protected void showDiagnostics(Diagnostic diagnostics, String indent) {
+		indent+="  ";
+		for (Diagnostic d : diagnostics.getChildren()){
+			log.warn(indent+d.getSource());
+			log.warn(indent+"  "+d.getMessage());
+			showDiagnostics(d,indent);
+		}
+	}
+
+	@SuppressWarnings({ "unchecked", "rawtypes" })
+	protected void storeResourceContents(EList<EObject> contents, String outputPath, String extension) {
+		//TODO: duplicated from loadResourceFromXMI => move to a more appropriate location
+		Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
+	    Map<String, Object> m = reg.getExtensionToFactoryMap();
+		m.put(extension, new XMIResourceFactoryImpl());
+		
+	    ResourceSet resSet = new ResourceSetImpl();
+		Resource outputResource = resSet.createResource(URI.createURI(outputPath));
+	    outputResource.getContents().addAll(contents);
+	    try {
+	      Map options = new HashMap<>();
+	      options.put(XMIResourceImpl.OPTION_ENCODING, "UTF-8");
+//	      options.put(XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF, XMIResourceImpl.OPTION_PROCESS_DANGLING_HREF_DISCARD);
+	      outputResource.save(options);
+	    } catch (IOException e) {
+	      e.printStackTrace();
+	    }
+	}
+
+}
