1 package net.sourceforge.phpeclipse.phpmanual.views;
3 import java.io.BufferedReader;
4 import java.io.FileNotFoundException;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.io.InputStream;
9 import java.util.ArrayList;
10 import java.util.regex.Matcher;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipFile;
14 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
15 import net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST;
16 import net.sourceforge.phpdt.internal.ui.viewsupport.SelectionListenerWithASTManager;
17 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
20 import net.sourceforge.phpeclipse.phpmanual.PHPManualUIPlugin;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.IRegion;
26 import org.eclipse.jface.text.ITextSelection;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.browser.Browser;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.INullSelectionListener;
34 import org.eclipse.ui.ISelectionListener;
35 import org.eclipse.ui.IWorkbenchPart;
36 import org.eclipse.ui.part.ViewPart;
37 import org.htmlparser.Node;
38 import org.htmlparser.Parser;
39 import org.htmlparser.tags.Div;
40 import org.htmlparser.util.ParserException;
41 import org.htmlparser.visitors.TagFindingVisitor;
42 import org.osgi.framework.Bundle;
45 * This ViewPart is the implementation of the idea of having the
46 * PHP Manual easily accessible while coding. It shows the
47 * under-cursor function's reference inside a browser.
49 * The view listens to selection changes both in the (1)workbench, to
50 * know when the user changes between the instances of the PHPEditor
51 * or when a new instance is created; and in the (2)PHPEditor, to know
52 * when the user changes the cursor position. This explains the need
53 * to implement both ISelectionListener and ISelectionListenerWithAST.
55 * Up to now, the ViewPart show reference pages from HTML stored in the
56 * doc.zip file from the net.sourceforge.phpeclipse.phphelp plugin. It
57 * also depends on net.sourceforge.phpeclipse.phpmanual.htmlparser to
58 * parse these HTML files.
62 public class PHPManualView extends ViewPart implements INullSelectionListener, ISelectionListenerWithAST {
65 * The ViewPart's browser
67 private Browser browser;
70 * A reference to store last active editor to know when we've
71 * got a new instance of the PHPEditor
73 private PHPEditor lastEditor;
76 * String that stores the last selected word
78 private String lastOccurrence = null;
81 * The path to the doc.zip file containing the PHP Manual
84 private final Path docPath = new Path("doc.zip");
89 public PHPManualView() {
93 * This method initializes the ViewPart. It instantiates components
96 * @param parent The parent control
98 public void createPartControl(Composite parent) {
99 browser = new Browser(parent, SWT.NONE);
101 if ((lastEditor = getJavaEditor()) != null) {
102 SelectionListenerWithASTManager.getDefault().addListener(lastEditor, this);
104 getSite().getWorkbenchWindow().getSelectionService()
105 .addPostSelectionListener(PHPeclipsePlugin.EDITOR_ID, this);
109 * Cleanup to remove the selection listener
111 public void dispose() {
112 getSite().getWorkbenchWindow().getSelectionService()
113 .removePostSelectionListener(PHPeclipsePlugin.EDITOR_ID, this);
117 * Passing the focus request to the viewer's control.
119 public void setFocus() {
124 * Treats selection changes from the PHPEditor
126 public void selectionChanged(IEditorPart part, ITextSelection selection) {
127 IDocument document = ((PHPEditor)part).getViewer().getDocument();
128 int offset = selection.getOffset();
129 IRegion iRegion = JavaWordFinder.findWord(document, offset);
130 if (document != null && iRegion != null) {
132 final String wordStr = document.get(iRegion.getOffset(),
133 iRegion.getLength());
134 if (!wordStr.equalsIgnoreCase(lastOccurrence)) {
135 showReference(wordStr);
136 lastOccurrence = wordStr;
138 } catch (Exception e) {
145 * Treats selection changes from the workbench. When part is new
146 * instance of PHPEditor it gets a listener attached
148 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
149 if (part != null && !((PHPEditor)part).equals(lastEditor)) {
150 SelectionListenerWithASTManager.getDefault().addListener((PHPEditor)part, this);
151 lastEditor = (PHPEditor)part;
153 System.out.println(part);
158 * Updates the browser with the reference page for a given function
160 * @param funcName Function name
162 private void showReference(final String funcName) {
163 System.out.println("Show reference for " + funcName);
164 new Thread(new Runnable() {
166 Display.getDefault().asyncExec(new Runnable() {
168 String html = getHtmlSource(funcName);
169 browser.setText(html);
177 * Filters the function's reference page extracting only parts of it
179 * @param source HTML source of the reference page
180 * @return HTML source of reference page
182 private String filterHtmlSource(String source) {
184 Parser parser = new Parser(source);
185 String [] tagsToBeFound = {"DIV"};
186 ArrayList classList = new ArrayList(8);
187 classList.add("refnamediv");
188 classList.add("refsect1 description");
189 classList.add("refsect1 parameters");
190 classList.add("refsect1 returnvalues");
191 classList.add("refsect1 examples");
192 classList.add("refsect1 seealso");
193 classList.add("refsect1 u");
194 TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
195 parser.visitAllNodesWith(visitor);
196 Node [] allPTags = visitor.getTags(0);
197 StringBuffer output = new StringBuffer();
198 for (int i = 0; i < allPTags.length; i++) {
199 String tagClass = ((Div)allPTags[i]).getAttribute("class");
200 if (classList.contains(tagClass)) {
201 output.append(allPTags[i].toHtml());
204 return output.toString().replaceAll("—", "-");
205 //.replace("<h3 class=\"title\">Description</h3>", " ");
206 } catch (ParserException e) {
213 * Reads the template that defines the style of the reference page
214 * shown inside the view's browser
216 * @return HTML source of the template
218 public String getRefPageTemplate() {
219 Bundle bundle = Platform.getBundle(PHPManualUIPlugin.PLUGIN_ID);
220 URL fileURL = Platform.find(bundle, new Path("templates"));
221 StringBuffer contents = new StringBuffer();
222 BufferedReader input = null;
224 URL resolve = Platform.resolve(fileURL);
225 input = new BufferedReader(new FileReader(resolve.getPath()+"/refpage.html"));
227 while ((line = input.readLine()) != null){
228 contents.append(line);
231 catch (FileNotFoundException e) {
233 } catch (IOException e) {
242 catch (IOException ex) {
243 ex.printStackTrace();
246 return contents.toString();
250 * Replaces each substring of source string that matches the
251 * given pattern string with the given replace string
253 * @param source The source string
254 * @param pattern The pattern string
255 * @param replace The replace string
256 * @return The resulting String
258 public static String replace(String source, String pattern, String replace) {
259 if (source != null) {
260 final int len = pattern.length();
261 StringBuffer sb = new StringBuffer();
264 while ((found = source.indexOf(pattern, start)) != -1) {
265 sb.append(source.substring(start, found));
269 sb.append(source.substring(start));
270 return sb.toString();
277 * Looks for the function's reference page inside the doc.zip file and
278 * returns a filtered HTML source of it embedded in the template
282 * @return HTML source of reference page
284 public String getHtmlSource(String funcName) {
285 Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
286 URL fileURL = Platform.find(bundle, docPath);
289 URL resolve = Platform.resolve(fileURL);
290 ZipFile docFile = new ZipFile(resolve.getPath());
291 ZipEntry entry = docFile.getEntry("doc/function."+funcName.replace('_', '-')+".html");
292 InputStream ref = docFile.getInputStream(entry);
293 b = new byte[(int)entry.getSize()];
294 ref.read(b, 0, (int)entry.getSize());
296 String reference = filterHtmlSource(new String(b));
297 String refPageTpl = getRefPageTemplate();
298 refPageTpl = refPageTpl.replaceAll("%title%", funcName);
299 refPageTpl = replace(refPageTpl, "%reference%", reference);
302 } catch (IOException e) {
303 return "<html></html>";
304 } catch (Exception e) {
307 return "<html></html>";
311 * Returns the currently active java editor, or <code>null</code> if it
312 * cannot be determined.
314 * @return the currently active java editor, or <code>null</code>
316 private PHPEditor getJavaEditor() {
318 IEditorPart part = PHPeclipsePlugin.getActivePage().getActiveEditor();
319 if (part instanceof PHPEditor)
320 return (PHPEditor) part;
323 } catch (Exception e) {