Refactory: remove unused packages.
[phpeclipse.git] / net.sourceforge.phpeclipse.phpmanual / src / net / sourceforge / phpeclipse / phpmanual / views / PHPManualView.java
index 2d637c7..d08944a 100644 (file)
@@ -6,10 +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.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;
@@ -17,6 +20,7 @@ import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
 import net.sourceforge.phpeclipse.phpmanual.PHPManualUIPlugin;
+import net.sourceforge.phpeclipse.ui.WebUI;
 
 import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.Path;
@@ -98,24 +102,19 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
         */
        public void createPartControl(Composite parent) {
                browser = new Browser(parent, SWT.NONE);
-               browser.addLocationListener(new LocationAdapter(){
+               browser.addLocationListener(new LocationAdapter() {
                        public void changing(LocationEvent event) {
                                String loc = event.location.toString();
-                               if(!loc.equalsIgnoreCase("about:blank") && !loc.startsWith("jar:")){
+                               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];
-                                               showLinkReference(func);
+                                               showReference(func);
+                                               event.doit = false;
                                        }
-                               } else if (loc.startsWith("jar:")) {
-                                       // TODO find a better way of not showing the location error page. This is a cheap trick
-                                       // to keep the page from showing.
-                                       // ed_mann
-                                       browser.setText("<html></html>");
                                }
-                               event.doit = false;
                        }
                });
                parent.pack();
@@ -125,7 +124,6 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
                getSite().getWorkbenchWindow().getSelectionService()
                                .addPostSelectionListener(PHPeclipsePlugin.EDITOR_ID, this);
        }
-
        /**
         * Cleanup to remove the selection listener
         */
@@ -170,8 +168,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);
                }
        }
 
@@ -194,133 +190,6 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
        }
        
        /**
-        * Updates the browser with the reference page for a given function
-        * 
-        * @param funcName Function name
-        */
-       private void showLinkReference(final String funcName) {
-               new Thread(new Runnable() {
-                       public void run() {
-                               Display.getDefault().asyncExec(new Runnable() {
-                                       public void run() {
-                                               String html = getLinkHtmlSource(funcName);
-                                               browser.setText(html);
-                                       }
-                               });
-                       }
-               }).start();
-       }
-
-       /**
-        * Filters the function's reference page extracting only parts of it
-        * 
-        * @param source HTML source of the reference page
-        * @return HTML source of reference page
-        */
-       private String filterIniHtmlSource(String source) {
-               try {
-                       Parser parser = new Parser(source);
-                       String [] tagsToBeFound = {"DIV"};
-                       ArrayList classList = new ArrayList(8);
-                       classList.add("section");
-                       classList.add("title");
-                       classList.add("refsect1 parameters");
-                       classList.add("refsect1 returnvalues");
-                       classList.add("refsect1 examples");
-                       classList.add("refsect1 seealso");
-                       classList.add("refsect1 u");
-                       TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
-                       parser.visitAllNodesWith(visitor);
-                       Node [] allPTags = visitor.getTags(0);
-                       StringBuffer output = new StringBuffer();
-                       for (int i = 0; i < allPTags.length; i++) {
-                               String tagClass = ((Div)allPTags[i]).getAttribute("class");
-                               if (classList.contains(tagClass)) {
-                                       output.append(allPTags[i].toHtml());
-                               }
-                       }
-                       return output.toString().replaceAll("—", "-");
-                       //.replace("<h3 class=\"title\">Description</h3>", " ");
-               } catch (ParserException e) {
-                       e.printStackTrace();
-               }
-               return "";
-       }
-       
-       /**
-        * Filters the function's reference page extracting only parts of it
-        * 
-        * @param source HTML source of the reference page
-        * @return HTML source of reference page
-        */
-       private String filterLangHtmlSource(String source) {
-               try {
-                       Parser parser = new Parser(source);
-                       String [] tagsToBeFound = {"DIV"};
-                       ArrayList classList = new ArrayList(8);
-                       classList.add("sect1");
-                       classList.add("title");
-                       classList.add("refsect1 parameters");
-                       classList.add("refsect1 returnvalues");
-                       classList.add("refsect1 examples");
-                       classList.add("refsect1 seealso");
-                       classList.add("refsect1 u");
-                       TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
-                       parser.visitAllNodesWith(visitor);
-                       Node [] allPTags = visitor.getTags(0);
-                       StringBuffer output = new StringBuffer();
-                       for (int i = 0; i < allPTags.length; i++) {
-                               String tagClass = ((Div)allPTags[i]).getAttribute("class");
-                               if (classList.contains(tagClass)) {
-                                       output.append(allPTags[i].toHtml());
-                               }
-                       }
-                       return output.toString().replaceAll("—", "-");
-                       //.replace("<h3 class=\"title\">Description</h3>", " ");
-               } catch (ParserException e) {
-                       e.printStackTrace();
-               }
-               return "";
-       }
-       
-       /**
-        * Filters the function's reference page extracting only parts of it
-        * 
-        * @param source HTML source of the reference page
-        * @return HTML source of reference page
-        */
-       private String filterRefHtmlSource(String source) {
-               try {
-                       Parser parser = new Parser(source);
-                       String [] tagsToBeFound = {"DIV"};
-                       ArrayList classList = new ArrayList(8);
-                       classList.add("partintro");
-                       classList.add("section");
-                       classList.add("title");
-                       classList.add("refsect1 parameters");
-                       classList.add("refsect1 returnvalues");
-                       classList.add("refsect1 examples");
-                       classList.add("refsect1 seealso");
-                       classList.add("refsect1 u");
-                       TagFindingVisitor visitor = new TagFindingVisitor(tagsToBeFound);
-                       parser.visitAllNodesWith(visitor);
-                       Node [] allPTags = visitor.getTags(0);
-                       StringBuffer output = new StringBuffer();
-                       for (int i = 0; i < allPTags.length; i++) {
-                               String tagClass = ((Div)allPTags[i]).getAttribute("class");
-                               if (classList.contains(tagClass)) {
-                                       output.append(allPTags[i].toHtml());
-                               }
-                       }
-                       return output.toString().replaceAll("—", "-");
-                       //.replace("<h3 class=\"title\">Description</h3>", " ");
-               } catch (ParserException e) {
-                       e.printStackTrace();
-               }
-               return "";
-       }
-       
-       /**
         * Filters the function's reference page extracting only parts of it
         * 
         * @param source HTML source of the reference page
@@ -329,15 +198,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);
@@ -428,81 +296,50 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
         * @return HTML source of reference page
         */
        public String getHtmlSource(String funcName) {
-               Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
-               URL fileURL = FileLocator.find(bundle, docPath, null);
-               ZipEntry entry;
-               byte[] b = null;
-               try {
-                       URL resolve = FileLocator.resolve(fileURL);
-                       ZipFile docFile = new ZipFile(resolve.getPath());
-                       entry = docFile.getEntry("doc/function."+funcName.replace('_', '-')+".html");
-                       if(entry == null){
-                               entry = docFile.getEntry("doc/ini."+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;
-                       }
-               } catch (IOException e) {
-                       return "<html></html>";
-               } catch (Exception e) {
+               if (funcName.length() == 0) {
+                       // Don't bother ;-) 
                        return null;
                }
-               return "<html></html>";
-       }
-
-       /**
-        * Looks for the function's reference page inside the doc.zip file and
-        * returns a filtered HTML source of it embedded in the template
-        * 
-        * @param funcName
-        *            Function name
-        * @return HTML source of reference page
-        */
-       public String getLinkHtmlSource(String funcName) {
                Bundle bundle = Platform.getBundle(PHPHelpPlugin.PLUGIN_ID);
                URL fileURL = FileLocator.find(bundle, docPath, null);
-               ZipEntry entry;
+               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 = FileLocator.resolve(fileURL);
                        ZipFile docFile = new ZipFile(resolve.getPath());
-                       entry = docFile.getEntry("doc/"+funcName);
-                       InputStream ref = docFile.getInputStream(entry);
-                       b = new byte[(int)entry.getSize()];
-                       ref.read(b, 0, (int)entry.getSize());
-                       if (b != null) {
-                               String reference = null;
-                               String aFuncName = funcName.toString();
-                               if(aFuncName.startsWith("function")){
-                                       reference = filterHtmlSource(new String(b));
-                               } else if (aFuncName.startsWith("ini")){
-                                       reference = filterIniHtmlSource(new String(b));
-                               } else if (aFuncName.startsWith("install")){
-                                       reference = filterIniHtmlSource(new String(b));
-                               } else if (aFuncName.startsWith("language")){
-                                       reference = filterLangHtmlSource(new String(b));
-                               } else if (aFuncName.startsWith("ref")){
-                                       reference = filterRefHtmlSource(new String(b));
+                       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;
+                                       }
                                }
-                               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
        }
+
        /**
         * Returns the currently active java editor, or <code>null</code> if it
         * cannot be determined.
@@ -511,7 +348,7 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I
         */
        private PHPEditor getJavaEditor() {
                try {
-                       IEditorPart part = PHPeclipsePlugin.getActivePage().getActiveEditor();
+                       IEditorPart part = /*PHPeclipsePlugin*/WebUI.getActivePage().getActiveEditor();
                        if (part instanceof PHPEditor)
                                return (PHPEditor) part;
                        else