eclipse xtext: How can I know if file contain errors - java

I am implementing an Eclipse plugin in Java. I want to write a function that for a given path to a file (such as file.myDSL) returns whether the file has errors or it's a legal file.

IProject theProject =
ResourcePlugin.getWorkspace().getRoot().getProject("theProject");
theProject.build(...); // or whatever operation to make sure Xtext checked the file
IFile theFile = project.getFile("file.myDsl");
theFile.getMarkers(IMarker.PROBLEM, true, IResource.DEPTH_ZERO);

Related

exists() does not work but getAbsolutePath() does work

I have the below code whereby I create a File type based on a pre-created file "test.brd" and also call the getAbsolutePath() method on this File, this all works correctly. However, when I run the exists() method, this is deemed as not existing.
When I debug, the status of the File is null and the path is also null, yet the getAbsolutePath() method works. I have debugged and it goes to the Security section of the exists() method.
Please see below:
File inputFile = new File("/Users/myname/Desktop/project_name/test.brd");
// The below works and returns the path
System.out.println(inputFile.getAbsolutePath());
if (inputFile.exists()) {
System.out.println("Exists");
}
else {
System.out.println("Invalid");
}
Even when I construct the file without the absolute path and just give the file name as a parameter (stored locally with Java file) the correct absolute path is provided.
Hope this makes sense. All I want to do is read a pre-created file into an Array, each character is an element in the array, I was intending on using scanner to read the file, but inputFile does not exist to be read.
The two methods are about different aspects of the file:
getAbsolutePath() is about file name. In a way, this is a "string manipulation method" completely separated from the actual file system
exists() is about the actual file. It checks whether or not the file is present in the file system at the location identified by the given path.
Note that getAbsolutePath() and other path manipulation methods of File must work even without the file or the folder being present in the actual file system. Otherwise, the API would not be able to support file creation, e.g. through createNewFile().
If you take a look at the javadoc, you can find the following sentence
Instances of this class may or may not denote an actual file-system object such as a file or a directory.
Proving that the instance in memory of a File object is not necessarily a real file or directory existing in the file system.
File inputFile = new File("/Users/myname/Desktop/project_name/test.brd");
The line above doesn't create a new File and hence it doesn't exists.
If you want to create a file you can use method inputFile.createNewFile().
The method getAbsolutePath() works on the inputFile object and is completely different from file creation.

No matter what I do, I can't get Java to recognize the filepath for a text file

I've been trying to set up a Scanner to use a File as an input, but it doesn't seem to recognize the filepath. The file exists in the same folder as my .java files.
File errorList = new File("Errors.txt");
Scanner errorIn = new Scanner(errorList);
This results in a FileNotFoundException.
What am I doing wrong, and how can I fix this?
One other approach you could try is, execute the below code in your eclipse (from any of your class), and see where the hello.txt is created, so you get an idea of where Java is looking for the file.
new File("hello.txt").createNewFile();
Then you could either put your Errors.txt in that location or provide the corresponding relative location.

Extract resource folder from running jar in Java 7

My resources folder inside my jar includes a directory with several binary files. I am attempting to use this code to extract them:
try(InputStream is = ExternalHTMLThumbnail.class.getResourceAsStream("/wkhtmltoimage")) {
Files.copy(is, Paths.get("/home/dan/wkhtmltoimage");
}
This is throwing the error
java.nio.file.NoSuchFileException: /home/dan/wkhtmltoimage
Which comes from
if (errno() == UnixConstants.ENOENT)
return new NoSuchFileException(file, other, null);
in UnixException.java. Even though in Files.java the correct options are passed:
ostream = newOutputStream(target, StandardOpenOption.CREATE_NEW,
StandardOpenOption.WRITE);
from Files.copy. Of course there's not! That's why I'm trying to make it. I don't yet understand Path and Files enough to do this right. What's the best way to extract the directory and all its contents?
Confused because the docs for Files.copy claims
By default, the copy fails if the target file already exists or is a symbolic link
(Apparently it fails if the target file doesn't exist as well?)
And lists the possible exceptions, and NoSuchFileException is not one of them.
If you're using Guava:
URL url = Resources.getResource(ExternalHTMLThumbnail.class, "wkhtmltoimage");
byte[] bytes = Resources.toByteArray(url);
Files.write(bytes, new File("/my/path/myFile"));
You could of course just chain that all into one line; I declared the variables to make it more readable.
The file that does not exist may actually be the directory you're trying to create the file in.
/home/dan/wkhtmltoimage
Does /home/dan exist? Probably not if you're on a Mac.

Relative file path being applied from project directory instead of original directory

My program reads in a document from a location that is not the project root directory. The doc contains a relative path. When the program applies that path, it does start from the project's root directory. How can I make it apply the path from the document's original location?
Here are the details. Kind of long, but pretty straightforward.
I have a Java project in Eclipse located at
C:\one\two\three\four\five
The program runs an XSL transform that takes a Schematron schema as input and produces a new XSLT stylesheet as output. The schema is located at
C:\one\two\three\four\five\six\S\P\schema.sch
It contains this line, and several more like it:
<sch:let name="foo" select="document('../../C/P/bar.xml')"/>
If you start from the location of the schema and apply that relative path, you end up with
C:\one\two\three\four\five\six\C\P\bar.xml
which is the correct location of bar.xml. However, when I run my program, I get a number of errors, which all seem to be similar or related to this one:
Recoverable error on line 1262
FODC0002: I/O error reported by XML parser processing
file:/C:/one/two/three/C/P/bar.xml:
C:\one\two\three\C\P\bar.xml (The system cannot find the path specified)
FODC0002 is the error code for "Error retrieving resource." That makes sense, because this is not the correct location of bar.xml. It seems that the relative path is being applied to the project's root directory. This is the relevant code:
void compileToXslt(byte[] schema) throws Exception {
XsltCompiler comp = Runtime.getSaxonProcessor().newXsltCompiler();
comp.setURIResolver(resolver);
Source source = resolver.resolve("iso_svrl_for_xslt2.xsl", null);
XsltExecutable executable = comp.compile(source);
XsltTransformer transformer = executable.load();
transformer.setSource(new StreamSource(new ByteArrayInputStream(schema)));
Serializer serializer = new Serializer();
serializer.setOutputStream(new ByteArrayOutputStream());
transformer.setDestination(serializer);
transformer.transform(); // Errors appear in logs during this line
// ...
Source is javax.xml.transform.Source. The XSL-related classes are all from SAXON (Javadoc).
What can I do to fix this? Moving bar.xml to the location where the program is looking for it, and editing style.xsl, are not options for me, because both files belong to a third-party library.
UPDATE:
Further research has led me to believe that I need to set the system ID of the StreamSource. I tried replacing the transformer.setSource(... line with this:
StreamSource strSrc = new StreamSource(new ByteArrayInputStream(schema));
strSrc.setSystemId(new
File("C:\\one\\two\\three\\four\\five\\six\\S\\P\\schema.sch").toURI()
.toURL().toExternalForm());
transformer.setSource(strSrc);
but I'm getting the same results. Am I using setSystemId() incorrectly? Am I going down the wrong path entirely?
I don't have java installed but I would assume you you need to change resolver, to find the path you are looking for.
You don't show how you get it. Of course you can do the quick and dirty and just change the working directory in your debug configurations under the arguments tab. But I assume you don't want to do that

Get running file name in Java?

Is it possible to get the file name which is being executed. Like e.g. If i am running a Jar file, and inside i want to add code functionality that can detect the file name so that if this jar is renamed, code should be able to detect that.
Is there any possibility of doing this without scanning the files in current directory?
Try this:
String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
http://www.rgagnon.com/javadetails/java-0300.html
http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getProtectionDomain()
http://docs.oracle.com/javase/7/docs/api/java/security/ProtectionDomain.html
you could check if System.getEnv() has some value indicating which file was invoked

Categories