X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.help.base%2Fsrc%2Forg%2Fsimantics%2Fhelp%2Fbase%2Finternal%2FPDFSearchParticipant.java;fp=bundles%2Forg.simantics.help.base%2Fsrc%2Forg%2Fsimantics%2Fhelp%2Fbase%2Finternal%2FPDFSearchParticipant.java;h=d630e113eec31d0f8603c7fd7ede074c94bedb3e;hb=6a4a43b278d6819c660182eb4954524d1757e077;hp=0000000000000000000000000000000000000000;hpb=4e40f9793cc18f08f1fa6c96d9bb4f42408997b4;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.help.base/src/org/simantics/help/base/internal/PDFSearchParticipant.java b/bundles/org.simantics.help.base/src/org/simantics/help/base/internal/PDFSearchParticipant.java new file mode 100644 index 000000000..d630e113e --- /dev/null +++ b/bundles/org.simantics.help.base/src/org/simantics/help/base/internal/PDFSearchParticipant.java @@ -0,0 +1,54 @@ +package org.simantics.help.base.internal; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLDecoder; + +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.help.search.IHelpSearchIndex; +import org.eclipse.help.search.ISearchDocument; +import org.eclipse.help.search.SearchParticipant; +import org.osgi.framework.Bundle; + +/** + * An abstract search participants for adding XML documents to the search index. Subclass it + * and implement or override protected methods to handle parsing of the document. + * + * @since 1.20.0 + */ +public class PDFSearchParticipant extends SearchParticipant { + + @Override + public IStatus addDocument(IHelpSearchIndex index, String pluginId, String name, URL url, String id, ISearchDocument doc) { + try { + //System.out.println("PDFSearchParticipant.addDocument(" + index + ", " + pluginId + ", " + name + ", " + url + ", " + id + ", " + doc + ")"); + Bundle bundle = Platform.getBundle(pluginId); + if (bundle == null) + return new Status(IStatus.ERROR, pluginId, "Failed to find bundle " + bundle + " from the platform."); + + // Need to decode path because it will fail to resolve if it contains %20 (=' '). + IPath path = new Path(URLDecoder.decode(name, "UTF-8")); + path = path.removeFirstSegments(1); + URL bundleUrl = FileLocator.find(bundle, path, null); + if (bundleUrl == null) + return new Status(IStatus.ERROR, pluginId, "Failed to find file " + name + " from bundle " + pluginId + "."); + + URL fileUrl = FileLocator.toFileURL(bundleUrl); + if (!fileUrl.getProtocol().equals("file")) + return new Status(IStatus.ERROR, pluginId, "Failed to make " + url + " available as a file"); + + File f = new File(URLDecoder.decode(fileUrl.getPath(), "UTF-8")).getCanonicalFile(); + PDFUtil.stripText(f, doc); + return Status.OK_STATUS; + } catch (IOException e) { + return new Status(IStatus.ERROR, pluginId, "Failed to strip text from PDF document " + url + ".", e); + } + } + +}