1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse.phpmanual / src / net / sourceforge / phpeclipse / phpmanual / views / PHPManualView.java
index e14af65..f838bc5 100644 (file)
@@ -6,11 +6,13 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Arrays;
 import java.util.ArrayList;
-import java.util.regex.Matcher;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
+//import net.sourceforge.phpeclipse.phpmanual.PHPManualUiMessages;
+//import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
 import net.sourceforge.phpdt.internal.ui.text.JavaWordFinder;
 import net.sourceforge.phpdt.internal.ui.viewsupport.ISelectionListenerWithAST;
 import net.sourceforge.phpdt.internal.ui.viewsupport.SelectionListenerWithASTManager;
@@ -19,6 +21,7 @@ import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
 import net.sourceforge.phpeclipse.phpmanual.PHPManualUIPlugin;
 
+import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.text.IDocument;
@@ -26,12 +29,13 @@ import org.eclipse.jface.text.IRegion;
 import org.eclipse.jface.text.ITextSelection;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.browser.Browser;
+import org.eclipse.swt.browser.LocationAdapter;
+import org.eclipse.swt.browser.LocationEvent;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.browser.Browser;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.INullSelectionListener;
-import org.eclipse.ui.ISelectionListener;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.part.ViewPart;
 import org.htmlparser.Node;
@@ -97,6 +101,21 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
         */
        public void createPartControl(Composite parent) {
                browser = new Browser(parent, SWT.NONE);
+               browser.addLocationListener(new LocationAdapter() {
+                       public void changing(LocationEvent event) {
+                               String loc = event.location.toString();
+                               if(!loc.equalsIgnoreCase("about:blank") && !loc.startsWith("jar:")) {
+                                       String func = loc.replaceAll("file:///", "");
+                                       func = func.replaceAll("#.+$", "");
+                                       String[] afunc = loc.split("\\.");
+                                       if(!afunc[1].equalsIgnoreCase(lastOccurrence)) {
+                                               lastOccurrence = afunc[1];
+                                               showReference(func);
+                                               event.doit = false;
+                                       }
+                               }
+                       }
+               });
                parent.pack();
                if ((lastEditor = getJavaEditor()) != null) {
                        SelectionListenerWithASTManager.getDefault().addListener(lastEditor, this);
@@ -104,7 +123,6 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
                getSite().getWorkbenchWindow().getSelectionService()
                                .addPostSelectionListener(PHPeclipsePlugin.EDITOR_ID, this);
        }
-
        /**
         * Cleanup to remove the selection listener
         */
@@ -149,8 +167,6 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
                if (part != null && !((PHPEditor)part).equals(lastEditor)) {
                        SelectionListenerWithASTManager.getDefault().addListener((PHPEditor)part, this);
                        lastEditor = (PHPEditor)part;
-               } else {
-                       System.out.println(part);
                }
        }
 
@@ -171,7 +187,7 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
                        }
                }).start();
        }
-
+       
        /**
         * Filters the function's reference page extracting only parts of it
         * 
@@ -181,15 +197,14 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
        private String filterHtmlSource(String source) {
                try {
                        Parser parser = new Parser(source);
-                       String [] tagsToBeFound = {"DIV"};
-                       ArrayList classList = new ArrayList(8);
-                       classList.add("refnamediv");
-                       classList.add("refsect1 description");
-                       classList.add("refsect1 parameters");
-                       classList.add("refsect1 returnvalues");
-                       classList.add("refsect1 examples");
-                       classList.add("refsect1 seealso");
-                       classList.add("refsect1 u");
+                       String[] tagsToBeFound = { "DIV" };
+                       // Common classes to be included for all page types
+                       ArrayList classList = new ArrayList(Arrays.asList(new String[] {
+                                       "section", "sect1", "title", "partintro", "refnamediv",
+                                       "refsect1 description", "refsect1 parameters",
+                                       "refsect1 returnvalues", "refsect1 examples",
+                                       "refsect1 seealso", "refsect1 u", "example-contents" }));
+                       // Grab all the tags for processing
                        TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
                        parser.visitAllNodesWith(visitor);
                        Node [] allPTags = visitor.getTags(0);
@@ -207,7 +222,6 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
                }
                return "";
        }
-
        /**
         * Reads the template that defines the style of the reference page
         * shown inside the view's browser
@@ -216,11 +230,11 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
         */
        public String getRefPageTemplate() {
                Bundle bundle = Platform.getBundle(PHPManualUIPlugin.PLUGIN_ID);
-               URL fileURL = Platform.find(bundle, new Path("templates"));
+               URL fileURL = FileLocator.find(bundle, new Path("templates"), null);
                StringBuffer contents = new StringBuffer();
                BufferedReader input = null;
                try {
-                       URL resolve = Platform.resolve(fileURL);
+                       URL resolve = FileLocator.resolve(fileURL);
                        input = new BufferedReader(new FileReader(resolve.getPath()+"/refpage.html"));
                        String line = null;
                        while ((line = input.readLine()) != null){
@@ -281,29 +295,48 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
         * @return HTML source of reference page
         */
        public String getHtmlSource(String funcName) {
+               if (funcName.length() == 0) {
+                       // Don't bother ;-) 
+                       return null;
+               }
                Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
-               URL fileURL = Platform.find(bundle, docPath);
+               URL fileURL = FileLocator.find(bundle, docPath, null);
+               ZipEntry entry = null;
+               // List of prefixes to lookup HTML files by, ordered so that looping
+               // is as minimal as possible.  The empty value matches links passed,
+               // rather than function 
+               String[] prefixes = { "", "function", "control-structures", "ref", "http", "imagick", "ming" };
                byte[] b = null;
+               if (funcName.matches("^[a-z-]+\\.[a-z-0-9]+\\.html$")) {
+                       // funcName is actually a page reference, strip the prefix and suffix
+                       funcName = funcName.substring(0, funcName.lastIndexOf('.'));
+               }
                try {
-                       URL resolve = Platform.resolve(fileURL);
+                       URL resolve = FileLocator.resolve(fileURL);
                        ZipFile docFile = new ZipFile(resolve.getPath());
-                       ZipEntry entry = docFile.getEntry("doc/function."+funcName.replace('_', '-')+".html");
-                       InputStream ref = docFile.getInputStream(entry);
-                       b = new byte[(int)entry.getSize()];
-                       ref.read(b, 0, (int)entry.getSize());
-                       if (b != null) {
-                               String reference = filterHtmlSource(new String(b));
-                               String refPageTpl = getRefPageTemplate();
-                               refPageTpl = refPageTpl.replaceAll("%title%", funcName);
-                               refPageTpl = replace(refPageTpl, "%reference%", reference);
-                               return refPageTpl;
+                       for (int i = 0; i < prefixes.length; i++) {
+                               if ((entry = docFile.getEntry("doc/" + prefixes[i] +
+                                               (prefixes[i].length() == 0 ? "" : ".") +
+                                               funcName.replace('_', '-') + ".html")) != null) {
+                                       // Document was matched
+                                       InputStream ref = docFile.getInputStream(entry);
+                                       b = new byte[(int)entry.getSize()];
+                                       ref.read(b, 0, (int)entry.getSize());
+                                       if (b != null) {
+                                               String reference = filterHtmlSource(new String(b));
+                                               String refPageTpl = getRefPageTemplate();
+                                               refPageTpl = refPageTpl.replaceAll("%title%", funcName);
+                                               refPageTpl = replace(refPageTpl, "%reference%", reference);
+                                               return refPageTpl;
+                                       }
+                               }
                        }
                } catch (IOException e) {
-                       return "<html></html>";
+                       return "<html>" + PHPManualUIPlugin.getString("LookupException") + "</html>";
                } catch (Exception e) {
                        return null;
                }
-               return "<html></html>";
+               return null; // Keeps the last reference
        }
 
        /**