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.zip.ZipEntry;
11 import java.util.zip.ZipFile;
13 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
14 import net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST;
15 import net.sourceforge.phpdt.internal.ui.viewsupport.SelectionListenerWithASTManager;
16 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
19 import net.sourceforge.phpeclipse.phpmanual.PHPManualUIPlugin;
21 import org.eclipse.core.runtime.FileLocator;
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.browser.Browser;
30 import org.eclipse.swt.browser.LocationAdapter;
31 import org.eclipse.swt.browser.LocationEvent;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.ui.IEditorPart;
35 import org.eclipse.ui.INullSelectionListener;
36 import org.eclipse.ui.IWorkbenchPart;
37 import org.eclipse.ui.part.ViewPart;
38 import org.htmlparser.Node;
39 import org.htmlparser.Parser;
40 import org.htmlparser.tags.Div;
41 import org.htmlparser.util.ParserException;
42 import org.htmlparser.visitors.TagFindingVisitor;
43 import org.osgi.framework.Bundle;
46 * This ViewPart is the implementation of the idea of having the
47 * PHP Manual easily accessible while coding. It shows the
48 * under-cursor function's reference inside a browser.
50 * The view listens to selection changes both in the (1)workbench, to
51 * know when the user changes between the instances of the PHPEditor
52 * or when a new instance is created; and in the (2)PHPEditor, to know
53 * when the user changes the cursor position. This explains the need
54 * to implement both ISelectionListener and ISelectionListenerWithAST.
56 * Up to now, the ViewPart show reference pages from HTML stored in the
57 * doc.zip file from the net.sourceforge.phpeclipse.phphelp plugin. It
58 * also depends on net.sourceforge.phpeclipse.phpmanual.htmlparser to
59 * parse these HTML files.
63 public class PHPManualView extends ViewPart implements INullSelectionListener, ISelectionListenerWithAST {
66 * The ViewPart's browser
68 private Browser browser;
71 * A reference to store last active editor to know when we've
72 * got a new instance of the PHPEditor
74 private PHPEditor lastEditor;
77 * String that stores the last selected word
79 private String lastOccurrence = null;
82 * The path to the doc.zip file containing the PHP Manual
85 private final Path docPath = new Path("doc.zip");
90 public PHPManualView() {
94 * This method initializes the ViewPart. It instantiates components
97 * @param parent The parent control
99 public void createPartControl(Composite parent) {
100 browser = new Browser(parent, SWT.NONE);
101 browser.addLocationListener(new LocationAdapter() {
102 public void changing(LocationEvent event) {
103 String loc = event.location.toString();
104 if(!loc.equalsIgnoreCase("about:blank") && !loc.startsWith("jar:")) {
105 String func = loc.replaceAll("file:///", "");
106 func = func.replaceAll("#.+$", "");
107 String[] afunc = loc.split("\\.");
108 if(!afunc[1].equalsIgnoreCase(lastOccurrence)) {
109 lastOccurrence = afunc[1];
110 showLinkReference(func);
117 if ((lastEditor = getJavaEditor()) != null) {
118 SelectionListenerWithASTManager.getDefault().addListener(lastEditor, this);
120 getSite().getWorkbenchWindow().getSelectionService()
121 .addPostSelectionListener(PHPeclipsePlugin.EDITOR_ID, this);
125 * Cleanup to remove the selection listener
127 public void dispose() {
128 getSite().getWorkbenchWindow().getSelectionService()
129 .removePostSelectionListener(PHPeclipsePlugin.EDITOR_ID, this);
133 * Passing the focus request to the viewer's control.
135 public void setFocus() {
140 * Treats selection changes from the PHPEditor
142 public void selectionChanged(IEditorPart part, ITextSelection selection) {
143 IDocument document = ((PHPEditor)part).getViewer().getDocument();
144 int offset = selection.getOffset();
145 IRegion iRegion = JavaWordFinder.findWord(document, offset);
146 if (document != null && iRegion != null) {
148 final String wordStr = document.get(iRegion.getOffset(),
149 iRegion.getLength());
150 if (!wordStr.equalsIgnoreCase(lastOccurrence)) {
151 showReference(wordStr);
152 lastOccurrence = wordStr;
154 } catch (Exception e) {
161 * Treats selection changes from the workbench. When part is new
162 * instance of PHPEditor it gets a listener attached
164 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
165 if (part != null && !((PHPEditor)part).equals(lastEditor)) {
166 SelectionListenerWithASTManager.getDefault().addListener((PHPEditor)part, this);
167 lastEditor = (PHPEditor)part;
169 System.out.println(part);
174 * Updates the browser with the reference page for a given function
176 * @param funcName Function name
178 private void showReference(final String funcName) {
179 new Thread(new Runnable() {
181 Display.getDefault().asyncExec(new Runnable() {
183 String html = getHtmlSource(funcName);
184 browser.setText(html);
192 * Updates the browser with the reference page for a given function
194 * @param funcName Function name
196 private void showLinkReference(final String funcName) {
197 new Thread(new Runnable() {
199 Display.getDefault().asyncExec(new Runnable() {
201 String html = getLinkHtmlSource(funcName);
202 browser.setText(html);
210 * Filters the function's reference page extracting only parts of it
212 * @param source HTML source of the reference page
213 * @return HTML source of reference page
215 private String filterIniHtmlSource(String source) {
217 Parser parser = new Parser(source);
218 String [] tagsToBeFound = {"DIV"};
219 ArrayList classList = new ArrayList(8);
220 classList.add("section");
221 classList.add("title");
222 classList.add("refsect1 parameters");
223 classList.add("refsect1 returnvalues");
224 classList.add("refsect1 examples");
225 classList.add("refsect1 seealso");
226 classList.add("refsect1 u");
227 TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
228 parser.visitAllNodesWith(visitor);
229 Node [] allPTags = visitor.getTags(0);
230 StringBuffer output = new StringBuffer();
231 for (int i = 0; i < allPTags.length; i++) {
232 String tagClass = ((Div)allPTags[i]).getAttribute("class");
233 if (classList.contains(tagClass)) {
234 output.append(allPTags[i].toHtml());
237 return output.toString().replaceAll("—", "-");
238 //.replace("<h3 class=\"title\">Description</h3>", " ");
239 } catch (ParserException e) {
246 * Filters the function's reference page extracting only parts of it
248 * @param source HTML source of the reference page
249 * @return HTML source of reference page
251 private String filterLangHtmlSource(String source) {
253 Parser parser = new Parser(source);
254 String [] tagsToBeFound = {"DIV"};
255 ArrayList classList = new ArrayList(8);
256 classList.add("sect1");
257 classList.add("title");
258 classList.add("refsect1 parameters");
259 classList.add("refsect1 returnvalues");
260 classList.add("refsect1 examples");
261 classList.add("refsect1 seealso");
262 classList.add("refsect1 u");
263 TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
264 parser.visitAllNodesWith(visitor);
265 Node [] allPTags = visitor.getTags(0);
266 StringBuffer output = new StringBuffer();
267 for (int i = 0; i < allPTags.length; i++) {
268 String tagClass = ((Div)allPTags[i]).getAttribute("class");
269 if (classList.contains(tagClass)) {
270 output.append(allPTags[i].toHtml());
273 return output.toString().replaceAll("—", "-");
274 //.replace("<h3 class=\"title\">Description</h3>", " ");
275 } catch (ParserException e) {
282 * Filters the function's reference page extracting only parts of it
284 * @param source HTML source of the reference page
285 * @return HTML source of reference page
287 private String filterRefHtmlSource(String source) {
289 Parser parser = new Parser(source);
290 String [] tagsToBeFound = {"DIV"};
291 ArrayList classList = new ArrayList(8);
292 classList.add("partintro");
293 classList.add("section");
294 classList.add("title");
295 classList.add("refsect1 parameters");
296 classList.add("refsect1 returnvalues");
297 classList.add("refsect1 examples");
298 classList.add("refsect1 seealso");
299 classList.add("refsect1 u");
300 TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
301 parser.visitAllNodesWith(visitor);
302 Node [] allPTags = visitor.getTags(0);
303 StringBuffer output = new StringBuffer();
304 for (int i = 0; i < allPTags.length; i++) {
305 String tagClass = ((Div)allPTags[i]).getAttribute("class");
306 if (classList.contains(tagClass)) {
307 output.append(allPTags[i].toHtml());
310 return output.toString().replaceAll("—", "-");
311 //.replace("<h3 class=\"title\">Description</h3>", " ");
312 } catch (ParserException e) {
319 * Filters the function's reference page extracting only parts of it
321 * @param source HTML source of the reference page
322 * @return HTML source of reference page
324 private String filterHtmlSource(String source) {
326 Parser parser = new Parser(source);
327 String [] tagsToBeFound = {"DIV"};
328 ArrayList classList = new ArrayList(8);
329 classList.add("refnamediv");
330 classList.add("refsect1 description");
331 classList.add("refsect1 parameters");
332 classList.add("refsect1 returnvalues");
333 classList.add("refsect1 examples");
334 classList.add("refsect1 seealso");
335 classList.add("refsect1 u");
336 TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
337 parser.visitAllNodesWith(visitor);
338 Node [] allPTags = visitor.getTags(0);
339 StringBuffer output = new StringBuffer();
340 for (int i = 0; i < allPTags.length; i++) {
341 String tagClass = ((Div)allPTags[i]).getAttribute("class");
342 if (classList.contains(tagClass)) {
343 output.append(allPTags[i].toHtml());
346 return output.toString().replaceAll("—", "-");
347 //.replace("<h3 class=\"title\">Description</h3>", " ");
348 } catch (ParserException e) {
354 * Reads the template that defines the style of the reference page
355 * shown inside the view's browser
357 * @return HTML source of the template
359 public String getRefPageTemplate() {
360 Bundle bundle = Platform.getBundle(PHPManualUIPlugin.PLUGIN_ID);
361 URL fileURL = FileLocator.find(bundle, new Path("templates"), null);
362 StringBuffer contents = new StringBuffer();
363 BufferedReader input = null;
365 URL resolve = FileLocator.resolve(fileURL);
366 input = new BufferedReader(new FileReader(resolve.getPath()+"/refpage.html"));
368 while ((line = input.readLine()) != null){
369 contents.append(line);
372 catch (FileNotFoundException e) {
374 } catch (IOException e) {
383 catch (IOException ex) {
384 ex.printStackTrace();
387 return contents.toString();
391 * Replaces each substring of source string that matches the
392 * given pattern string with the given replace string
394 * @param source The source string
395 * @param pattern The pattern string
396 * @param replace The replace string
397 * @return The resulting String
399 public static String replace(String source, String pattern, String replace) {
400 if (source != null) {
401 final int len = pattern.length();
402 StringBuffer sb = new StringBuffer();
405 while ((found = source.indexOf(pattern, start)) != -1) {
406 sb.append(source.substring(start, found));
410 sb.append(source.substring(start));
411 return sb.toString();
418 * Looks for the function's reference page inside the doc.zip file and
419 * returns a filtered HTML source of it embedded in the template
423 * @return HTML source of reference page
425 public String getHtmlSource(String funcName) {
426 Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
427 URL fileURL = FileLocator.find(bundle, docPath, null);
431 URL resolve = FileLocator.resolve(fileURL);
432 ZipFile docFile = new ZipFile(resolve.getPath());
433 entry = docFile.getEntry("doc/function."+funcName.replace('_', '-')+".html");
435 entry = docFile.getEntry("doc/ini."+funcName.replace('_', '-')+".html");
437 InputStream ref = docFile.getInputStream(entry);
438 b = new byte[(int)entry.getSize()];
439 ref.read(b, 0, (int)entry.getSize());
441 String reference = filterHtmlSource(new String(b));
442 String refPageTpl = getRefPageTemplate();
443 refPageTpl = refPageTpl.replaceAll("%title%", funcName);
444 refPageTpl = replace(refPageTpl, "%reference%", reference);
447 } catch (IOException e) {
448 return "<html></html>";
449 } catch (Exception e) {
452 return "<html></html>";
456 * Looks for the function's reference page inside the doc.zip file and
457 * returns a filtered HTML source of it embedded in the template
461 * @return HTML source of reference page
463 public String getLinkHtmlSource(String funcName) {
464 Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
465 URL fileURL = FileLocator.find(bundle, docPath, null);
469 URL resolve = FileLocator.resolve(fileURL);
470 ZipFile docFile = new ZipFile(resolve.getPath());
471 entry = docFile.getEntry("doc/"+funcName);
472 InputStream ref = docFile.getInputStream(entry);
473 b = new byte[(int)entry.getSize()];
474 ref.read(b, 0, (int)entry.getSize());
476 String reference = null;
477 String aFuncName = funcName.toString();
478 if(aFuncName.startsWith("function")){
479 reference = filterHtmlSource(new String(b));
480 } else if (aFuncName.startsWith("ini")){
481 reference = filterIniHtmlSource(new String(b));
482 } else if (aFuncName.startsWith("install")){
483 reference = filterIniHtmlSource(new String(b));
484 } else if (aFuncName.startsWith("language")){
485 reference = filterLangHtmlSource(new String(b));
486 } else if (aFuncName.startsWith("ref")){
487 reference = filterRefHtmlSource(new String(b));
489 String refPageTpl = getRefPageTemplate();
490 refPageTpl = refPageTpl.replaceAll("%title%", funcName);
491 refPageTpl = replace(refPageTpl, "%reference%", reference);
494 } catch (IOException e) {
495 return "<html></html>";
496 } catch (Exception e) {
499 return "<html></html>";
502 * Returns the currently active java editor, or <code>null</code> if it
503 * cannot be determined.
505 * @return the currently active java editor, or <code>null</code>
507 private PHPEditor getJavaEditor() {
509 IEditorPart part = PHPeclipsePlugin.getActivePage().getActiveEditor();
510 if (part instanceof PHPEditor)
511 return (PHPEditor) part;
514 } catch (Exception e) {