1 package org.simantics.scl.compiler.module;
3 public class ModuleUtils {
4 public static String resolveAbsolutePath(String moduleName, String relativeModuleName) throws InvalidModulePathException {
5 if (relativeModuleName.startsWith(".")) {
6 String originalRelativeModuleName = relativeModuleName;
7 int p = moduleName.lastIndexOf('/');
8 String parentPackage = p < 0 ? "" : moduleName.substring(0, p);
9 while(relativeModuleName.startsWith(".")) {
10 if(relativeModuleName.startsWith("./")) {
11 relativeModuleName = relativeModuleName.substring(2);
13 else if(relativeModuleName.startsWith("../")) {
14 relativeModuleName = relativeModuleName.substring(3);
15 if(parentPackage.isEmpty()) {
16 throw new InvalidModulePathException("Couldn't resolve the relative module name " + originalRelativeModuleName + " when the current module name is " + moduleName + ".");
18 p = parentPackage.lastIndexOf('/');
19 parentPackage = p < 0 ? "" : parentPackage.substring(0, p);
22 throw new InvalidModulePathException("Couldn't resolve the relative module name " + originalRelativeModuleName + ". It has an invalid syntax.");
25 return parentPackage + "/" + relativeModuleName;
27 return relativeModuleName;