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