Xtext get project root directory from validator
Asked Answered
C

1

6

I need to get to validate existence of specific file in Xtext project. The file has similar path as validated object but other root directory, for example:

$projPath/src/dir1/dir2/ValidatedFile.src

$projPath/resources/dir1/dir2/SchoudBeExistFile.src

In validate function I get resource only with relative path (/src/dir1/dir2/ValidatedFile.src). But I don't know the project path, so I can't check existence of the /resources/dir1/dir2/SchoudBeExistFile.src.

Can you help me to find the absolute project path in validation function? @Check def checkExternalFileExistance(MyType my) { val myTypeFullPath = ?? val projectPath = ?? }

Thanks

UPDATE: Solved by adding org.eclipse.core.resources and org.eclipse.core.runtime plugin dependencies to xtext project via plugin.xml and using this solution in grammar style. No absolute path required for this.

@Check
def checkExternalFlowDirExistance(MyType my) {
    val platformString = my.eResource.URI.toPlatformString(true);
    val myFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
    val proj = myFile.getProject();
    val shouldExistsFile = proj.geFile("/resources/dir1/dir2/SchoudBeExistFile.src")
    if (!shouldExistsFile.exists) {
    // error
    }
}
Contrail answered 13/3, 2014 at 11:44 Comment(0)
M
2

You can get the full path from the Resource:

@Check
def checkEventFileNameEqualsEventName(Mytype my){
    val myTypeFullPath=my.eResource.URI.toPlatformString(true)
}
May answered 13/3, 2014 at 20:38 Comment(1)
This expression returns me relative to project root path including the project dir, like "/MyPoj/src/package/MyType.mylang". I need absolute path to use in "new File(absolutePath).exists()"Contrail

© 2022 - 2024 — McMap. All rights reserved.