Eclipse cdt: IFile/IPath to IDocument - eclipse

Is it possible to retrieve an IDocument from an IFile or IPath? I have tried this:
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(hFilePath);
TextFileDocumentProvider provider = new TextFileDocumentProvider();
IDocument doc = provider.getDocument(file);
but getDocument seems to return null.
Thanks

I was having this same issue and I found a post here that helped. Give this a shot:
IDocumentProvider provider = new TextFileDocumentProvider();
provider.connect(ifile);
document = provider.getDocument(ifile);

That's not the right kind of argument for getDocument(), but you should instead be using the headless FileBuffers APIs, starting from FileBuffers itself, getting the ITextFileBufferManager, and then using it to open a text file buffer and its IDocument: http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/filebuffers/FileBuffers.html .
Don't forget to disconnect from the file buffer when you're done.

You can do something like this:
IFile file = (IFile) resource;
IPath filePath = file.getLocation();
filePath = FileBuffers.normalizeLocation(filePath);
IDocument document = FileBuffers.getTextFileBufferManager().getTextFileBuffer(filePath , LocationKind.NORMALIZE).getDocument();

Related

Eclipse - Convert IPath to URL

How can I convert an IPath to a URL. In my case, I want to convert the output location of a Java project to a URL, so that I can load classes from there with the URLClassLoader. My code looks like this:
javaProject.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null);
URL url = new File( javaProject.getOutputLocation().toFile().getAbsolutePath() ).toURI().toURL();
URL[] urls = new URL[] { url };
URLClassLoader classLoader = new URLClassLoader(urls);
Class<?> c = classLoader.loadClass(fully-qualified-class-name);
The problem is that the URL is only a relative one, i.e. its string representation is file:/C:/com.florianingerl.regexfindandreplace.matchevaluators/bin but it should be something like file:/C:/Users/Hermann/runtime-EclipseApplication/com.florianingerl.regexfindandreplace.matchevaluators/bin
The URLClassLoader can't find the classes obviously.
Any help is appreciated.
You can use something like:
IPath path = javaProject.getOutputLocation();
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
URI uri = file.getLocationURI();
URL url = uri.toURL();

How to read a directory with using InputStream in eclipse plugin developement

I'm developing an eclipse plug-in and I need to traverse a directory and whole content of the directory. I found the method which reads a file in plug-in (bundleresource) as InputStream.
InputStream stream = Activator.class.getResourceAsStream("/dir1/dir2/file.ext");
this method works for files only. I need a way to read directories, list subdirectories and files like File.io.
Thanks.
Do you want to read a resource directory of your plugin? Otherwise you have to traverse a directory and open one stream per file:
String path = "c:\\temp\\";
File directory = new File(path);
if (directory.isDirectory()) {
String[] list = directory.list();
for (String entry : list) {
String absolutePath = path + entry;
System.out.println("processing " + absolutePath);
File file = new File(absolutePath);
if (file.isFile()) {
FileInputStream stream = new FileInputStream(file);
// use stream
stream.close();
}
}
}
If you want to traverse subdirectories as well you should wrap this into a recursive method, check if file is a directory and call the recursive method in this case.

read index.dat file c#

HI,
I want to read index.dat file of user in c#.
StreamReader objInput = new StreamReader("C:\\values.dat", System.Text.Encoding.Default);
string contents = objInput.ReadToEnd().Trim();

Sending an email attachment in memory using OpenXML

I've got an Excel file that's built using OpenXML 2 and I want to send it as an email attachment. e.g.
System.IO.MemoryStream stream = new System.IO.MemoryStream();
SpreadsheetDocument package = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook))
AddParts(package); //created using document reflector
Saving the spreadsheet to a temp file using
stream.WriteTo(new System.IO.FileStream(#"c:\test.xlsx", System.IO.FileMode.Create));
works fine. But trying to send the stream directly as an email attachment fails - just get an empty file attached to the email when I do
System.Net.Mail.Attachment file = new System.Net.Mail.Attachment(stream, "MobileBill.xlsx", "application/vnd.ms-excel");
Anbody know how to do this?
Ok, I got this working, though through some effort. To create the stream:
MemoryStream stream = new MemoryStream();
using (SpreadsheetDocument package = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook))
{
Excel.CreateSpreadsheet(package, Excel_Methods.CreateSpotQuoteOut(), true);
}
stream.Seek(0, SeekOrigin.Begin);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(stream, "spreadsheet.xlsx");
attach.ContentDisposition.CreationDate = DateTime.Now;
attach.ContentDisposition.ModificationDate = DateTime.Now;
attach.ContentDisposition.Inline = false;
attach.ContentDisposition.Size = stream.Length;
attach.ContentType.MediaType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Also, I found that mine were not being sent right after I created them, and the reason for that is "standalone=yes" was not being added to the xml declaration of all the pages, so in my AddParts function, after adding the parts, I passed them into this function:
private static void AddXMLStandalone(OpenXmlPart part)
{
System.IO.StreamWriter writer = new System.IO.StreamWriter(part.GetStream());
XmlDocument doc = new XmlDocument();
doc.Load(part.GetStream());
doc.InnerXml = doc.InnerXml.Substring(doc.InnerXml.IndexOf("?>") + 2);
doc.InnerXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + doc.InnerXml;
part.GetStream().SetLength(doc.InnerXml.Length);
doc.Save(writer);
writer.Flush();
writer.Close();
}
Good luck!
do this:
System.Net.Mail.Attachment file = new System.Net.Mail.Attachment(new MemoryStream(stream.ToArray()), "MobileBill.xlsx", "application/vnd.ms-excel");
Apparently the memory stream doesn't get flushed or something
For your "content unreadable" problem, make sure to Save() your Workbooks and Worksheets and enclose your SpreadsheetDocument in a using statement to ensure all packages and zipped streams are flushed, closed and so on.
System.IO.MemoryStream stream = new System.IO.MemoryStream();
using (SpreadsheetDocument package = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook)))
{
AddParts(package);
//Save if AddParts hasn't done it
}
System.Net.Mail.Attachment file = ...
Thinking out load: could it be, that the Attachment class expects to read from the current possition in the provided stream? If this is the case, you would probably have to "seek" back to the beginning of the stream, before feeding it to the Attachment constructor:
AddParts(package); //created using document reflector
stream.Seek(0, SeekOrigin.Begin);
System.Net.Mail.Attachment file = new System.Net.Mail.Attachment(stream, "MobileBill.xlsx", "application/vnd.ms-excel");

Eclipse plugin: create a new file

I'm trying to create a new file in an eclipse plugin. It's not necessarily a Java file, it can be an HTML file for example.
Right now I'm doing this:
IProject project = ...;
IFile file = project.getFile("/somepath/somefilename"); // such as file.exists() == false
String contents = "Whatever";
InputStream source = new ByteArrayInputStream(contents.getBytes());
file.create(source, false, null);
The file gets created, but the problem is that it doesn't get recognized as any type; I can't open it in any internal editor. That's until I restart Eclipse (refresh or close then open the project doesn't help). After a restart, the file is perfectly usable and opens in the correct default editor for its type.
Is there any method I need to call to get the file outside of that "limbo" state?
That thread does mention the createFile call, but also refers to a FileEditorInput to open it:
Instead of java.io.File, you should use IFile.create(..) or IFile.createLink(..). You will need to get an IFile handle from the project using IProject.getFile(..) first, then create the file using that handle.
Once the file is created you can create FileEditorInput from it and use IWorkbenchPage.openEditor(..) to open the file in an editor.
Now, would that kind of method (from this AbstractExampleInstallerWizard) be of any help in this case?
protected void openEditor(IFile file, String editorID) throws PartInitException
{
IEditorRegistry editorRegistry = getWorkbench().getEditorRegistry();
if (editorID == null || editorRegistry.findEditor(editorID) == null)
{
editorID = getWorkbench().getEditorRegistry().getDefaultEditor(file.getFullPath().toString()).getId();
}
IWorkbenchPage page = getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.openEditor(new FileEditorInput(file), editorID, true, IWorkbenchPage.MATCH_ID);
}
See also this SDOModelWizard opening an editor on a new IFile:
// Open an editor on the new file.
//
try
{
page.openEditor
(new FileEditorInput(modelFile),
workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId());
}
catch (PartInitException exception)
{
MessageDialog.openError(workbenchWindow.getShell(), SDOEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
return false;
}