I am supposed to read the currently open DSL File to create an Auto Populate Feature. For that I am supposed to find the location of the file programmatically and then read it. However there seems to be no alternatives for this purpose. If I use Eclipse Plugin Methods, I get the following error java.lang.ClassCastException: org.eclipse.xtext.xbase.ui.editor.XbaseEditor cannot be cast to org.eclipse.core.resources.IFile
Please Help
The standard way to determine the file that any editor is editing is:
IEditorPart editor = get the editor
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput)
{
IFile file = ((IFileEditorInput)editorInput).getFile();
// TODO handle file
}
Related
I'm developing an Eclipse plugin in which I'm trying to get the text from the current open editor, I can do that using this code:
IFile file = ((FileEditorInput) editorInput).getFile();
InputStream inputStream = file.getContents();
String result = IOUtils.toString(inputStream);
But looks like it only returns the state of the file when it is saved, if I have that file open in the editor and make some changes I want to get this latest state, not the one when it was saved, is there any API I can use for that?
I am developing an Eclipse plug-in that programmatically modifies C++ files from the workspace. I am now trying to save the changes made. I have taken a look at this solution : How can I call save method in eclipse plugin development programmatically but this includes having the files I want to save open in the editor. Since I have
5k+ files to modify and save, I cannot afford to open them all in an editor before saving.
While looking for a solution I found this topic on the official Eclipse forum : https://www.eclipse.org/forums/index.php/t/1070377/ . Unfortunately, no answer has been provided.
I have tried using :
PlatformUI.getWorkbench().getService(IHandlerService.class).executeCommand("org.eclipse.ui.file.saveAll", event)
or
IWorkspace.save(boolean, IProgressMonitor)
or
ITranslationUnit.save(IProgressMonitor, boolean)
or
ICProject.save(IProgressMonitor, boolean)
but none of these solution worked.
My question is therefore :
How to save files programmatically in Eclipse without using an editor ?
Thanks a lot in advance
EDIT :
example of what I'm trying to achieve
deleteAndSave(IFunction functionToDelete) {
boolean forceDeletion = true;
IProgressMonitor progressMonitor = new NullProgressMonitor();
//delete the portion of code
functionToDelete.delete(forceDeletion, progressMonitor);
//we get the file containing the function
IFile file = (IFile) functionToDelete.getUnderlyingResource();
//save the file to the disk (ideally 'file.save()' ?)
file.getWorkspace().save(true, progressMonitor);
}
At the end of this execution, I would expect the new version of the file to be saved on the disk. The function is correctly deleted and the modifications appear in the IDE if I open the file in the editor, but the file is marked as unsaved by Eclipse (showing a star before the file name).
EDIT :
While this doesn't answer the question (without using an editor), I got the correct behaviour with :
deleteAndSave(IFunction functionToDelete) {
IProgressMonitor progressMonitor = new NullProgressMonitor();
IWorkbenchPage activePage = PlatformUI
.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage();
functionToDelete.delete(true, progressMonitor);
IFile file = (IFile) functionToDelete.getUnderlyingResource();
IEditorPart editorPart = IDE.openEditor(activePage, file);
editorPart.doSave(progressMonitor);
activePage.closeEditor(editorPart, false);
}
However this opens an editor page for each file and closes it immediately, so the performance are not satisfying for a large volume of files.
It sounds like the problem might be that your operation is modifying the working copy of a file, and not the underlying file itself.
Changes to the working copy can be synced to the underlying file by calling IWorkingCopy.commit().
Does the following help?
...
functionToDelete.delete(forceDeletion, progressMonitor);
ITranslationUnit tu = functionToDelete.getTranslationUnit();
if (tu.isWorkingCopy()) {
boolean forceCommit = true;
((IWorkingCopy) tu).commit(forceCommit, progressMonitor);
}
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);
When you right-click on any file in Windows you can select "Properties" in the menu.
In the upcoming dialog box you see a tab: "Details". There you can find a lot of properties (Name, Type, Owner, ...)!
My question is:
How can I read this file properties?
I've tried it years ago and it was not possible for me. I think with Java 1.7 it might be possible. I have tried UserDefinedAttributeView but it's not working.
I have tried the following: I see in the Details Tab the Section "File" and the property "Name". But I get an Exception if I execute the below code:
Exception in thread "main" java.nio.file.NoSuchFileException: D:\test.txt:File.Name
And text.txt exists ;)
Path path = new File("D:\\test.txt").toPath();
UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
String name = "File.Name";
ByteBuffer buf = ByteBuffer.allocate(view.size(name));
view.read(name, buf);
buf.flip();
String value = Charset.defaultCharset().decode(buf).toString();
Many Thanks for your support!
I had a similar problem and I solved it just calling the new attribute "user." and getting it with the same string "user.". I'm not sure that "File." works, anyway check the user's defined attributes list by the command "view.list().toString()".
Hoping to have help you (..even if quite late...)
I think your problem was not that it wasn't finding test.txt, but it wasn't finding File.Name. Your code snippet has ....view.size(name), where name = "File.Name" - so it is trying to find the size of a file File.Name and isn't finding that. Which is because you don't intend File.Name to be a filename, you intend it to be the name of the property you're looking for.
(That said, I'm also trying to find a solution that works for getting property values from the Details tab, within a Java program... All I've found so far requires JNA.)
I am a beginner in java and I am building an android app.
I want to have an xml file that has text in it.
Whenever the server sends updates, I want to change some lines in that file (what I mean by update is changing some lines in that file by erasing the some part of the text written already and replace by the update)
I know nothing about creating,writing or reading from files.
When I searched I found out that Internal storage suits me best.
But I do not know if I have to create an xml file manually in any directory or just use the code bellow to create this file automatically?
// If this is the first time run,execute one time code
// create XML Internal store
String FILENAME = "My_XML_file";
try{
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
} catch (final IOException e) {
e.printStackTrace();
}
Thank you in advance!
- First give the External Storage permission in the Manifest.xml file.
- You can use JAXP & JAXB, and even CASTOR to handle XML in a better way, but still DOM and SAX are inbuilt into Android.
You can use something like this
String s = "/sdcard/Myfolder/mytext.txt";
File f = new File(s);
The code you have will create a file in internal storage but you need a bit more to create and maintain an XML file easily.
I suggest you use the Build in Android DOM Parser (Android developers site docs on XML Parse options)
I found this example which explains how to use the dom parser to build a specific (new) XML file from code. In your context where the output stream in created:
StreamResult result = new StreamResult(new File("C:\\file.xml"));
you might want to use the other constructor based on the output stream you created above
StreamResult result = new StreamResult(fos);
In a similar fashion this DOM library allows you to read from an input stream (which you might get from android openFileInput) using DocumentBuilder.parse()