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