Get module relative path of IFile in Eclipse - eclipse-plugin

I have created an IFile instance as follows:
IPath filePath = new Path(WebContent/folder1/file1.html);
IFile file = project.getFile(filePath);
That file will be deployed on a server and I need to get the module relative path (in this case /folder1/file1.html).
I have tried using the IFlatFile class like this:
IFlatFile flatFile = new FlatFile(file, file.getName(), file.getProjectRelativePath());
IPath modulePath = flatFile.getModuleRelativePath();
But the getModuleRelativePath() seems to be returning the same path I provided to the constructor when the instance was created.
Is there any other way to get the module relative path of a given file?

Related

Can the Image path for the AddImage method in MigraDoc be used for the Visual Studio project subfolders?

The following example shows a very simple construction for creating a PDF with MigraDoc. The most parts of it are taken from the examples provided in the MigraDoc - Wiki documentation.
I have commented all the most few steps for the job. The path for the "AddImage" points to the subfolder "Images" which is a subfolder of the folder "Resources" created in the root of the console application of a Visual Studio (VS) project.
The most recent MigraDoc library is added to the VS - project via "NuGet" just yesterday.
The application creates the PDF output file unfortunately without the provided image file, if it is used in another machine. Because the execution file has then no access to the subfolders created in VS project.
Is there any solution to this problem?
public class Program
{
static void Main(string[] args)
{
// Create a MigraDoc document
var document = new Document();
// Add a section the document
var section = document.AddSection();
// .....
/*
------------------------------- NOTE -------------------------------
The path "../../Resources/Images/MigraDoc.png" is valid only for the developer machine!
A copy of the content of the "Debug" or "Release" folder any where else does not show the image in the output PDF.
Because such a copy has no access to the subfolder "/Resources/Images" of the Visual Studio project on the developer machine.
*/
section.Headers.Primary.AddImage("../../Resources/Images/MigraDoc.png");
// Create a renderer for PDF that uses Unicode font encoding.
var pdfRenderer = new PdfDocumentRenderer(true);
// Set the MigraDoc document.
pdfRenderer.Document = document;
// Create the PDF document.
pdfRenderer.RenderDocument();
// Save the PDF document...
var filename = "Invoice.pdf";
// Create the output directory
Directory.CreateDirectory("PDF");
// Create the ouptput file path
var savePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\PDF\\" + filename;
// Delete the output file if it already exists
if (File.Exists(savePath))
File.Delete(savePath);
// Save the output file
pdfRenderer.Save(savePath);
// Start a the default PDF viewer from the operation system
Process.Start(savePath);
}
}
A path beginning with ..\..\ relies on the current directory. For a portable solution, include the images you need in your deployment.
Recommended usage: The MigraDoc Document class has an ImagePath property. If your installation folder has an Images folder, just locate your .EXE file and set the ImagePath property accordingly.

Get resource from another folder in Java

My project has a folder for resource and a folder for src. All my code are in src and all my property files are in resource (it's set as resource root too)
So I want to read xyz.properties in my code as input stream
I tried
InputStream in = getClass().getResourceAsStream("/resources/xyzz.properties");
and
InputStream in = getClass().getResourceAsStream("xyzz.properties");
and neither worked. They all end up being null - only when I move xyzz.properties to the package that has the class that uses the above code does the second code snippet work. What should I be using for the path if I want to keep the current location of the property file?
Like this
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("xyzz.properties");
Or like this
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("xyzz.properties");

how to get a directory path as an input and read a file from that path in java?

i want to read files by a directory path string,
for example i want this method to return the corresponding file:
File reader(String pathFile){....
return File;}
File reader(String pathFile){
return File;}
in the reader function :
File f = new File(directoryPath,filename);
f.createNewFile(); //to create a new file in the directory.surround with try and catch as it throws IOEXCEPTION
return f;
you can also use f.mkdir() to create new directory if it doesnt exist.

Renaming a file in as3 AIR - how to do it if you have file's native path in a var?

The following code is great for renaming a file if you know the file is in applicationStorageDirectory
var sourceFile:File = File.applicationStorageDirectory;
sourceFile = sourceFile.resolvePath("Kalimba.snd");
var destination:File = File.applicationStorageDirectory;
destination = destination.resolvePath("test.snd");
try
{
sourceFile.moveTo(destination, true);
}
catch (error:Error)
{
trace("Error:" + error.message);
}
How do you set the sourceFile if all you have is the file's native path in a string? Like this:
D:\Software\files\testList.db
This throws errors:
sourceFile = sourceFile.resolvePath("D:\Software\files\testList.db");
The idea is I want to rename a file I had previously loaded into a var. I figured I'd extract the native path to a String var, null the File var (so the OS doesn't tell me it can't be renamed while the file is opened in flash), resolve that nativePath as the sourceFile, and use moveTo to rename the file on the hard drive.
Cheers for taking a look.
EDIT:
I've set up a test AIR app with only the following in it:
import flash.events.*;
import flash.filesystem.*;
var original = File.documentsDirectory;
original = original.resolvePath("D:\\Software\\test\\October.db");
var destination:File = File.documentsDirectory;
destination = destination.resolvePath("copy.db");
original.addEventListener(Event.COMPLETE, fileMoveCompleteHandler);
original.addEventListener(IOErrorEvent.IO_ERROR, fileMoveIOErrorEventHandler);
original.moveToAsync(destination);
function fileMoveCompleteHandler(event:Event):void {
trace(event.target); // [object File]
}
function fileMoveIOErrorEventHandler(event:IOErrorEvent):void {
trace("I/O Error.");
}
This fails, as does using D:\Software\test\October.db
I guess what I want to know it - how do you do the resolvePath thing if you already know the full path?
I guess what I want to know it - how do you do the resolvePath thing if you already know the full path?
You don't AFAIK. If your path is actually d:\software\test\october.db you can set a File ref like:
var original:File = new File();
original.nativePath = "d:\software\test\october.db";

Storing/Saving XML in local asset folder in AIR

AIR in general seems to be storing the xml file in the path where the app is installed.
I am generating an xml and I want to save/store a xml file in local asset folder of AIR application.
Any thoughts on doing this.
The File class has some static variables that point to local directories:
From the docs:
File.applicationStorageDirectory — a storage directory unique to each installed AIR application
File.applicationDirectory — the read-only directory where the application is installed (along with any installed assets)
File.desktopDirectory — the user's desktop directory
File.documentsDirectory — the user's documents directory
File.userDirectory — the user directory
Creating a pointer to the file
Usually you'll want to store files like these in File.applicationStorageDirectory.
So to create the file do:
File.applicationStorageDirectory.resolvePath("my-config.xml");
Alternatively, you can let the user choose where to store the file by using File#browseForSave(), which will display a native 'save' window to choose the location.
Writing the content
Open a FileStream for the File in 'write' mode and write an XML string to the file.
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.writeUTF(myXmlContent);
fs.close();
look at http://www.adobe.com/devnet/air/flex/quickstart/articles/xml_prefs.html
http://www.thetechlabs.com/xml/how-to-build-a-contact-manager-in-air-using-xml-part-2/
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html
var prefsFile:File = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("preferences.xml");
stream = new FileStream();
stream.open(prefsFile, FileMode.WRITE);
stream.writeUTFBytes(outputStringXML);
Or something like:
var saveStr:String = xmlToSave.toXMLString();
var file:File = new File('app-storage:/data.xml');
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.writeUTFBytes(saveStr);
fs.close();