Commit ad4e480a authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ initial fix for loading EVL files in validator in standalone mode (simplify and cleanup, reuse)

parent dcd24138
Loading
Loading
Loading
Loading
+48 −3
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
@@ -236,6 +237,7 @@ public class Validator {
			e.printStackTrace();
		} catch (Exception e) {
			//TODO: Error message refinements
			e.printStackTrace();
			System.err.println(e.getMessage()
					.replaceAll("\n.+epsilon/", "\n  at (epsilon/")
//					.replaceAll("\\.", "")
@@ -260,9 +262,52 @@ public class Validator {
			uri = url.toURI();
		} else {
			//WS
			String binPath=this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
			String projectPath = new File(binPath).getParent();
			uri = new File(projectPath+"/"+source).toURI();
			//TODO: this does not work with exported JAR
			
//			String binPath=this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
//			String projectPath = new File(binPath).getParent();
//			uri = new File(projectPath+"/"+source).toURI();
			uri = getResourceFile(source);
		}
		return uri;
	}
	
	//TODO: simplify
	public URI getResourceFile(String relativePath)
	{
	    URI uri = null;
	    URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
	    String codeLocation = location.toString();
	    System.out.println("Location: "+codeLocation);
	    try{
	        if (codeLocation.endsWith(".jar")) {
	            //Call from jar
	        	//DONE: this does not work.. -> fixed
	        	//should get file out of jar / bundle instead
	        	//TODO: simplify and merge with above, test other cases
	        	//TODO: generalise for use in other cases, e.g. for docx and others?
	        	System.out.println("In jar");
	        	try {
					Enumeration<URL> resources = ClassLoader.getSystemResources(relativePath.replaceAll("epsilon/", ""));
					URL nextElement = resources.nextElement();
					System.out.println("URL: "+nextElement);
					uri = nextElement.toURI();
					System.out.println("URI: "+uri);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//	            Path path = Paths.get(location.toURI()).resolve("../classes/" + relativePath.replaceAll("epsilon/", "")).normalize();
//	            file = path.toFile();
//	            System.out.println("File: "+file);
	        }else{
	            //Call from IDE
	            URL resource = this.getClass().getClassLoader().getResource(relativePath.replaceAll("epsilon/", ""));
				uri = resource.toURI();
//	            file = new File(resource.getPath());
	        }
	    }catch(URISyntaxException ex){
	        ex.printStackTrace();
	    }
	    return uri;
	}