How to create a builtin file and folder in custom eclipse plugin - eclipse

I am developing an Eclipse plugin which need to be shipped with builtin ant build file. Its working when I am running the project. However, when I am exporting the plugin and deploying the exported plugin in another eclipse, the ant build file is not getting generated. My suspect is that in the runtime, the source of the ant build file is not accessed. Any pointer how to solve the issue? Here is the code :
private void createAntFile(IProject project, Properties properties) throws CoreException, IOException {
InputStream antFileInputStream =null;
try {
String antFileName = properties.getProperty("name.ant.file");
String antFilePath = properties.getProperty("path.ant.file");
IFile file = project.getFile(antFileName);
antFileInputStream = Activator.getDefault().getBundle().getEntry(antFilePath).openStream();
file.create(antFileInputStream, false, null);
antFileInputStream.close();
}finally{
if(antFileInputStream!=null){
try {
antFileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
name.ant.file=build.xml
path.ant.file=src/weblogic/ant/build.xml
The source build file I am hard coding in the path src/weblogic/ant/build.xml
Edit:
Here is the code to create builtin folders:
private void createWeblogicTemplate(IProject project, Properties properties) throws IOException, CoreException {
String weblogicTemplateSourcePath = properties.getProperty("path.weblogic.template.source");
Path path = new Path(weblogicTemplateSourcePath);
Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
URL fileURL = FileLocator.find(bundle, path, null);
String filePath = FileLocator.resolve(fileURL).getPath();
System.out.println(filePath);
File sourceFile = new File(filePath);
String weblogicTemplateTargetPath = properties.getProperty("path.weblogic.template.target");
IFolder folder = project.getFolder(weblogicTemplateTargetPath);
copyFolder(sourceFile,folder,project,properties);
}
The line System.out.println(filePath) is printing path as
/C:/Users//Desktop/eclipse-rcp-luna-SR2-win32-x86_64/eclipse/../../../workspace-plugin/weblogic/resources/weblogictemplate/
So, locally its working. However, its not working when I deploy the pluin in some other eclipse. Any pointer how to create builtin folders?

You appear to be expecting the src/weblogic/ant/ directory to be included in the exported plugin jar - the src directory is not normally included in the plugin jar.
Put resources you want to include in the plugin in a separate directory (such as resources) and include that directory in the plugin build.properties so that it is included in the exported plugin jar.

Related

How to get path of resource file and return it from jar file in eclipse plugin

Here I am trying to add Help .html files on Cntl+space. Its working on my laptop because I have source code but not on server. No error but not returning path on server.
Here is my code:
Bundle bundle=Platform.getBundle("my plugin ID");
URL url = FileLocator.find(bundle, new Path("/doc/myfile/file/"), null);
//Here in file folder my html files.
String fPath = "";
try {
URL fileURL = FileLocator.toFileURL(url);
fPath=fileURL.getPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String fullPath = filePath + name+"."+ myfileextension;
return fullPath;//returning myfile path
You must give FileLocator.toFileURL the full name of the file you want, you can't use just the folder name.
This is because FileLocator.find may have to extract the file from the plug-in jar to a temporary location to make it available.
So:
URL url = FileLocator.find(bundle, new Path("/doc/myfile/file/" + name + "." + myfileextension), null);
Also check your build.properties file. You must include all the folders you want to use in the plugin in this.

Apache FOP: How to set base URL for accessing external resource using relative path

In my .xsl file I am using external graphics like this
<fo:external-graphic width="90pt" height="29pt" src="url(xsl/logo.jpg)"/>
But image is not getting loaded in the generated PDF and I get this error in console.
[ERROR] Error while creating area : Error with image URL: xsl\logo.jpg (The system cannotfind the path specified) and no base URL is specified
How do I solve this issue? I guess setting the base URL will do. But how to set the base URL? Please help.
I got a solution from this link
http://groups.yahoo.com/group/XSL-FO/message/6116
set base dir using Java code
ServletContext servletContext = getServletConfig().getServletContext();
String appPath = servletContext.getRealPath(""); //root of web app
org.apache.fop.configuration.Configuration.put("baseDir",appPath);
This worked for me.
Please post if you know any better solution.
I am using Apache FOP 1.1 Ver.
fopFactory = FopFactory.newInstance();
// for image base URL : images from Resource path of project
String serverPath = request.getSession().getServletContext().getRealPath("/");
fopFactory.setBaseURL(serverPath);
// for fonts base URL : .ttf from Resource path of project
fopFactory.getFontManager().setFontBaseURL(serverPath);
I added all images and required font font files in resource director of my project.
It is working fine for me.
Thank you
I had the same problem and this only works for me in the version 0.95 of fop.
SetBaseUrl is ignored in version 1.0
Solution for versions 1.0, 1.1 :
In fop 1.0 and 1.1 method setBaseURL() does not work correctly with local files, so you can use method setURIResolveri and write your implementation of interface URIResolver.
1.Add in uses
import javax.xml.transform.URIResolver;
2.Add in mainClass
private static class LocalResolver implements URIResolver {
private String BaseFolder;
#Override
public Source resolve(String href, String base) throws TransformerException {
File f = new File(BaseFolder + "\\" + href);
if (f.exists())
return new StreamSource(f);
else
throw new TransformerException("File " + f.getAbsolutePath() +" not found!");
}
public LocalResolver(String BaseFolder) {
this.BaseFolder = BaseFolder;
}
}
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
3.Add before call transformer.transform(src, res) this:
fop.getUserAgent().setURIResolver(new LocalResolver("C:\\Users\\photon\\Downloads\\fop-1.1-bin\\fop-1.1"));

Can you copy resources from an eclipse project to a UNC path programmatically?

I'm trying to copy resources (IFile) from an eclipse project to another location. The location is a UNC path which I've used before to create IProjects using IProjectDescription. However, when I try to copy the resource using the following code I get a ResourceException:
IResource[] res = project.members();
for (IResource r : res) {
if (r instanceof IFile) {
IFile file = (IFile) r;
file.copy("\\example.com\User\Folder\sj\", true, null);
}
}
The exception goes something like this:
org.eclipse.internal.resources.ResourceException: Resource '/corp.dsd' does not exist.
Anyone have any ideas?
Try using the IFileStore API. http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/filesystem/IFileStore.html
You can use getLocation on your IFile and then obtain the filestore for it.

How to test if a URL from an Eclipse bundle is a directory?

I'm trying to populate a directory from the contents of a bundle built into my plug-in. The following code works when the bundle is a file-system, but fails when the bundle is a JAR.
What is the best way to test if a URL is a directory? Or is there a completely different, better approach for creating a file structure from a resource bundle?
static private void bundleCopy(String dir, String destination) throws IOException {
Bundle bundle = com.mds.apg.Activator.getDefault().getBundle();
for (#SuppressWarnings("unchecked")
Enumeration<URL> en = (Enumeration<URL>) bundle.findEntries(dir, "*", true);
en.hasMoreElements();) {
URL url = en.nextElement();
String toFileName = destination + url.getPath().substring(dir.length());
File toFile = new File(toFileName);
InputStream in;
try {
in = url.openStream();
} catch (FileNotFoundException e) {
// this exception get thrown for file system directories but not for jar file ones
if (!toFile.mkdir()) {
throw new IOException("bundleCopy: " + "directory Creation Failed: "
+ toFileName);
}
continue;
}
FileCopy.coreStreamCopy(in, toFile);
}
}
I found an answer:
The key point is that the Enumeration entries for directories end in a '/'.
The following correctly distinguishes between directories and files for both JARs and file systems:
static private void bundleCopy(String dir, String destination)
throws IOException, URISyntaxException {
Bundle bundle = com.mds.apg.Activator.getDefault().getBundle();
Enumeration<URL> en = bundle.findEntries(dir, "*", true);
while (en.hasMoreElements()) {
URL url = en.nextElement();
String pathFromBase = url.getPath().substring(dir.length()+1);
String toFileName = destination + pathFromBase;
File toFile = new File(toFileName);
if (pathFromBase.lastIndexOf('/') == pathFromBase.length() - 1) {
// This is a directory - create it and recurse
if (!toFile.mkdir()) {
throw new IOException("bundleCopy: " + "directory Creation Failed: " + toFileName);
}
} else {
// This is a file - copy it
FileCopy.coreStreamCopy(url.openStream(), toFile);
}
}
}
You could try something like new File(FileLocator.resolve(url).toUri()) to convert from the Eclipse-specific URL to one using a native Java protocol.

Importing libraries in Eclipse programmatically

Is there a way I could put a library (Jar file) into an Eclipse project programatically? Up to now I've managed to do an external reference to it programatically using
IPath path = new Path("C:\\myfolder\\mylibrary.jar");
libraries.add(JavaCore.newLibraryEntry(path, null, null));
//add libs to project class path
try {
javaProject.setRawClasspath(libraries.toArray(new IClasspathEntry[libraries.size()]), null);
} catch (JavaModelException e1) {
e1.printStackTrace();
}
However I'd like to copy the jtwitter file to the project folder programatically so I could reference it as jtwitter.jar only. Can this be done please?
Thanks a lot and regards,
Krt_Malta
This did the trick. What I wanted exactly is importing the library into the project and then referencing it from the project not using a reference to an external file.
InputStream is = new BufferedInputStream(new FileInputStream("C:\\myfolder\\mylibrary.jar"));
IFile file = project.getFile("mylibrary.jar");
file.create(is, false, null);
IPath path = file.getFullPath();
libraries.add(JavaCore.newLibraryEntry(path, null, null));
//add libs to project class path
try {
javaProject.setRawClasspath(libraries.toArray(new IClasspathEntry[libraries.size()]), null);
} catch (JavaModelException e1) {
e1.printStackTrace();
}
IFile.getRawLocationURI() gets you an absolute path
setRawClasspath() is the right method.
However, you need first to copy your jar to the root directory of your project before adding it (with the new path) to the classpath of the project.
That way, the relative path of the jar will be jtwitter.jar.