From 4d54c8804f1e3f412dee7db21074a1ca31686cc9 Mon Sep 17 00:00:00 2001 From: mbowie Date: Mon, 11 Feb 2008 18:13:57 +0000 Subject: [PATCH] HTML manual changes per #379. --- .../phpeclipse/phpmanual/PHPManualUIPlugin.java | 10 + .../phpeclipse/phpmanual/PHPManualUiMessages.java | 34 +++ .../phpmanual/PHPManualUiMessages.properties | 7 + .../phpeclipse/phpmanual/views/PHPManualView.java | 239 ++++---------------- 4 files changed, 92 insertions(+), 198 deletions(-) create mode 100644 net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUiMessages.java create mode 100644 net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUiMessages.properties diff --git a/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUIPlugin.java b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUIPlugin.java index 81e4e8b..d867060 100644 --- a/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUIPlugin.java +++ b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUIPlugin.java @@ -1,5 +1,7 @@ package net.sourceforge.phpeclipse.phpmanual; +import java.util.MissingResourceException; + import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; @@ -58,4 +60,12 @@ public class PHPManualUIPlugin extends AbstractUIPlugin { public static ImageDescriptor getImageDescriptor(String path) { return imageDescriptorFromPlugin(PLUGIN_ID, path); } + + public static String getString(String key) { + try { + return PHPManualUiMessages.getString(key); + } catch (MissingResourceException e) { + return '!' + key + '!'; + } + } } diff --git a/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUiMessages.java b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUiMessages.java new file mode 100644 index 0000000..faf7411 --- /dev/null +++ b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUiMessages.java @@ -0,0 +1,34 @@ +package net.sourceforge.phpeclipse.phpmanual; + + +import java.text.MessageFormat; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +public class PHPManualUiMessages { + + private static final String BUNDLE_NAME = PHPManualUiMessages.class + .getName(); + + private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle + .getBundle(BUNDLE_NAME); + + private PHPManualUiMessages() { + } + + public static String getString(String key) { + try { + return RESOURCE_BUNDLE.getString(key); + } catch (MissingResourceException e) { + return '!' + key + '!'; + } + } + + public static String getFormattedString(String key, Object arg) { + return MessageFormat.format(getString(key), new Object[] { arg }); + } + + public static String getFormattedString(String key, Object[] args) { + return MessageFormat.format(getString(key), args); + } +} \ No newline at end of file diff --git a/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUiMessages.properties b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUiMessages.properties new file mode 100644 index 0000000..4cd1c0d --- /dev/null +++ b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/PHPManualUiMessages.properties @@ -0,0 +1,7 @@ + +######################################### +# General view return strings +######################################### + +LookupException=Sorry, an exception occurred while looking up the manual page for the selected term. +LookupMiss=Sorry, the manual page for the selected term was not found. diff --git a/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/views/PHPManualView.java b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/views/PHPManualView.java index 984fcd8..f86e1ca 100644 --- a/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/views/PHPManualView.java +++ b/net.sourceforge.phpeclipse.phpmanual/src/net/sourceforge/phpeclipse/phpmanual/views/PHPManualView.java @@ -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; @@ -107,7 +110,7 @@ public class PHPManualView extends ViewPart implements INullSelectionListener, I String[] afunc = loc.split("\\."); if(!afunc[1].equalsIgnoreCase(lastOccurrence)) { lastOccurrence = afunc[1]; - showLinkReference(func); + showReference(func); event.doit = false; } } @@ -120,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 */ @@ -189,133 +191,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("

Description

", " "); - } 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("

Description

", " "); - } 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("

Description

", " "); - } 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 @@ -324,15 +199,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); @@ -423,81 +297,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 ""; - } catch (Exception e) { + if (funcName.length() == 0) { + // Don't bother ;-) return null; } - return ""; - } - - /** - * 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 ""; + return "" + PHPManualUIPlugin.getString("LookupException") + ""; } catch (Exception e) { return null; } - return ""; + return "" + PHPManualUIPlugin.getString("LookupMiss") + ""; } + /** * Returns the currently active java editor, or null if it * cannot be determined. -- 1.7.1