How to obtain the resource defined in the plugin.xml - eclipse-rcp

A bundle , Get the viewpart icon of the org.eclipse.ui.views extension point
Only one string can be obtained through IConfigurationelement
e.g.( icons/full/obj16/julaunch.png )
But I can't get its bundle, and I can't splice a similar URL
e.g.( platform:/plugin/org.eclipse.unittest.ui/icons/full/obj16/julaunch.png )
What should I do?

If you have an IContributionElement and want to get the resource given by an attribute in the element you can do something like:
String pluginId = element.getContributor().getName();
Bundle bundle = Platform.getBundle(pluginId);
String path = element.getAttribute("attribute id");
URL url = FileLocator.find(bundle, new Path(path), null);

Related

Getting filename that was used on setGraphic()

I am currently working on a puzzle type of app on javaFX. I created a 2d array of buttons that I used setGraphic to insert the pictures. I am wondering if there is a way to retrieve the filename that I used on setGraphic so I can compare to pictures together. I know there is a getGraphic method but that returns random numbers.
setGraphic takes a Node, not a Image object; I assume you use ImageViews as graphics.
There is no way to retrieve the file name from an Image object since you need not pass a url to the Image constructor but are also allowed to pass a InputStream. InputStream does not provide any information about it's source and Image also doesn't.
To get the file path from a image you need to store the information yourself, e.g.:
private final Map<Image, String> imageFileNames = new IdentityHashMap<>();
public Image loadImage(String filename) throws MalformedURLException {
File file = new File(filename);
Image image = new Image(file.toURI().toURL().toExternalForm());
imageFileNames.put(image, filename);
return image;
}
public String getImageFileName(Image image) {
return imageFileNames.get(image);
}
With a node containing a ImageView as a graphic, you could do something like this:
ImageView view = (ImageView) node.getGraphic();
Image img = view.getImage();
String fileName = getImageFieldName(img);
Possibly adding null checks, if the graphic and/or the image can be null.
You could also the data in the node's userData or properties if you like:
Storing
File file = new File(filename);
ImageView imageView = new ImageView(file.toURI().toURL().toExternalForm());
imageView.setUserData(filename);
Retrieving
String filename = (String) node.getGraphic().getUserData();

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();

cq5 determine file content type or media type or mimetype

I have following code to save an asset in a servler service
ResourceResolver resourceResolver = this.resolverFactory.getAdministrativeResourceResolver(null);
AssetManager assetMgr = (AssetManager)resourceResolver.adaptTo((Class)AssetManager.class);
String newFile = "/content/dam/travel/" + fileName;
assetMgr.createAsset(newFile, is, "image/jpeg", true);
The following line creates the asset with specified mimetype
assetMgr.createAsset(newFile, is, "image/jpeg", true);
I would like to determine the coming file content type based on stream rather than name.
Thank you,
Sri

Eclipse cdt: IFile/IPath to IDocument

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();

Upload Servlet with custom file keys

I have built a Server that you can upload files to and download, using Eclipse, servlet and jsp, it's all very new to me. (more info).
Currently the upload system works with the file's name. I want to programmatically assign each file a random key. And with that key the user can download the file. That means saving the data in a config file or something like : test.txt(file) fdjrke432(filekey). And when the user inputs the filekey the servlet will pass the file for download.
I have tried using a random string generator and renameTo(), for this. But it doesn't work the first time, only when I upload the same file again does it work. And this system is flawed, the user will receive the file "fdjrke432" instead of test.txt, their content is the same but you can see the problem.
Any thoughts, suggestions or solutions for my problem?
Well Sebek, I'm glad you asked!! This is quite an interesting one, there is no MAGIC way to do this. The answer is indeed to rename the file you uploaded. But I suggest adding the random string before the name of the file; like : fdjrke432test.txt.
Try this:
filekey= RenameRandom();
File renamedUploadFile = new File(uploadFolder + File.separator+ filekey+ fileName);
item.write(renamedUploadFile);
//remember to give the user the filekey
with
public String RenameRandom()
{
final int LENGTH = 8;
StringBuffer sb = new StringBuffer();
for (int x = 0; x < LENGTH; x++)
{
sb.append((char)((int)(Math.random()*26)+97));
}
System.out.println(sb.toString());
return sb.toString();
}
To delete or download the file from the server you will need to locate it, the user will input the key, you just need to search the upload folder for a file that begins with that key:
filekey= request.getParameter("filekey");
File f = new File(getServletContext().getRealPath("") + File.separator+"data");
File[] matchingFiles = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(filekey);
}
});
String newfilename = matchingFiles[0].getName();
// now delete or download newfilename