]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.help.base/src/org/simantics/help/base/internal/PDFSearchParticipant.java
merged svn revision 33114 and added desktop and help plugins
[simantics/platform.git] / bundles / org.simantics.help.base / src / org / simantics / help / base / internal / PDFSearchParticipant.java
1 package org.simantics.help.base.internal;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.net.URL;\r
6 import java.net.URLDecoder;\r
7 \r
8 import org.eclipse.core.runtime.FileLocator;\r
9 import org.eclipse.core.runtime.IPath;\r
10 import org.eclipse.core.runtime.IStatus;\r
11 import org.eclipse.core.runtime.Path;\r
12 import org.eclipse.core.runtime.Platform;\r
13 import org.eclipse.core.runtime.Status;\r
14 import org.eclipse.help.search.IHelpSearchIndex;\r
15 import org.eclipse.help.search.ISearchDocument;\r
16 import org.eclipse.help.search.SearchParticipant;\r
17 import org.osgi.framework.Bundle;\r
18 \r
19 /**\r
20  * An abstract search participants for adding XML documents to the search index. Subclass it\r
21  * and implement or override protected methods to handle parsing of the document.\r
22  *\r
23  * @since 1.20.0\r
24  */\r
25 public class PDFSearchParticipant extends SearchParticipant {\r
26 \r
27     @Override\r
28     public IStatus addDocument(IHelpSearchIndex index, String pluginId, String name, URL url, String id, ISearchDocument doc) {\r
29         try {\r
30             //System.out.println("PDFSearchParticipant.addDocument(" + index + ", " + pluginId + ", " + name + ", " + url + ", " + id + ", " + doc + ")");\r
31             Bundle bundle = Platform.getBundle(pluginId);\r
32             if (bundle == null)\r
33                 return new Status(IStatus.ERROR, pluginId, "Failed to find bundle " + bundle + " from the platform.");\r
34 \r
35             // Need to decode path because it will fail to resolve if it contains %20 (=' ').\r
36             IPath path = new Path(URLDecoder.decode(name, "UTF-8"));\r
37             path = path.removeFirstSegments(1);\r
38             URL bundleUrl = FileLocator.find(bundle, path, null);\r
39             if (bundleUrl == null)\r
40                 return new Status(IStatus.ERROR, pluginId, "Failed to find file " + name + " from bundle " + pluginId + ".");\r
41 \r
42             URL fileUrl = FileLocator.toFileURL(bundleUrl);\r
43             if (!fileUrl.getProtocol().equals("file"))\r
44                 return new Status(IStatus.ERROR, pluginId, "Failed to make " + url + " available as a file");\r
45 \r
46             File f = new File(URLDecoder.decode(fileUrl.getPath(), "UTF-8")).getCanonicalFile();\r
47             PDFUtil.stripText(f, doc);\r
48             return Status.OK_STATUS;\r
49         } catch (IOException e) {\r
50             return new Status(IStatus.ERROR, pluginId, "Failed to strip text from PDF document " + url + ".", e);\r
51         }\r
52     }\r
53 \r
54 }\r