initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / renderer / AbstractContentRenderer.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/renderer/AbstractContentRenderer.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/renderer/AbstractContentRenderer.java
new file mode 100644 (file)
index 0000000..4c86952
--- /dev/null
@@ -0,0 +1,430 @@
+package net.sourceforge.phpeclipse.wiki.renderer;
+
+import java.io.IOException;
+import java.util.StringTokenizer;
+
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
+import net.sourceforge.phpeclipse.wiki.preferences.Util;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+
+public abstract class AbstractContentRenderer implements IContentRenderer {
+  protected IProject fProject;
+
+  public static final String CLASS_MONO_SPACE = "monospace";
+
+  public static final String CLASS_QUOTE = "quote";
+
+  public static final String TABLE_DELIMETER = "|";
+
+  public static final String HR = "hr";
+
+  public static final String NEW_WIKIDOC_HREF = "?";
+
+  public static final String WIKI_HREF = "http://--wiki/";
+
+  public static final String[][] REPLACE_TEXT = new String[][] { { "<", "&lt;" }, { "\"", "&quot;" } };
+
+  private WikiEditor editor;
+
+  protected StringBuffer buffer;
+
+  private int currentLine;
+
+  private int currentListDepth;
+
+  private boolean inTable;
+
+  private void setEditor(WikiEditor editor) {
+    this.editor = editor;
+  }
+
+  protected StringBuffer getBuffer() {
+    return buffer;
+  }
+
+  protected WikiEditor getEditor() {
+    return editor;
+  }
+
+  //  public void render(String content, StringBuffer buf) {
+  //    try {
+  //      buffer = buf;
+  //      appendHeader();
+  //
+  //      int index = 0;
+  //      int startIndex = 0;
+  //      String line;
+  //      while (index < content.length()) {
+  //        if (content.charAt(index++) == '\n') {
+  //          line = content.substring(startIndex, index);
+  //          startIndex = index;
+  //          appendLine(line);
+  //
+  //        }
+  //        index++;
+  //      }
+  //      if (startIndex < content.length()) {
+  //        line = content.substring(startIndex, content.length());
+  //        renderLine(line);
+  //      }
+  //      appendFooter();
+  //      // return buffer.toString();
+  //    } catch (Exception e) {
+  //      WikiEditorPlugin.getDefault().log(buffer.toString());
+  //      WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e);
+  //      // return "<html><body><p>" + e.getLocalizedMessage() +
+  //      // "</p></body></html>";
+  //    }
+  //  }
+
+  //  protected void renderLine(String line) {
+  //    if (isHeader(line)) {
+  //      appendHeader(line);
+  //    } else if (isList(line)) {
+  //      appendListItem(line);
+  //    } else if (tableLine(line)) {
+  //      processTable(line);
+  //    } else if (process(line)) {
+  //      return;
+  //    } else {
+  //      buffer.append(line);
+  //    }
+  //  }
+
+  //  public final String render(WikiEditor editor) {
+  //    initialise(editor);
+  //    try {
+  //      buffer = new StringBuffer();
+  //      appendHeader();
+  //      buffer.append("<h1>").append(editor.getWikiNameBeingEdited()).append("</h1>");
+  //      appendContents();
+  //      appendFooter();
+  //      return buffer.toString();
+  //    } catch (Exception e) {
+  //      WikiEditorPlugin.getDefault().log(buffer.toString());
+  //      WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e);
+  //      return "<html><body><p>" + e.getLocalizedMessage() + "</p></body></html>";
+  //    }
+  //  }
+
+  private void initialise(WikiEditor editor) {
+    setEditor(editor);
+    currentListDepth = 0;
+    inTable = false;
+    initialise();
+  }
+
+  protected abstract void initialise();
+
+  protected void appendHeader() throws IOException {
+    buffer.append("<html><head>");
+    //    String basePath = Util.getMiscProjectsPreferenceValue(fProject, WikiConstants.HTML_OUTPUT_PATH);
+    //    buffer.append("<base href=\"" + basePath + "/" + fProject.getName() + "\"/>");
+
+    //    if (WikiEditorPlugin.getDefault().getPluginPreferences().contains(WikiConstants.BROWSER_CSS_URL)) {
+    //      buffer.append("<link href=\"")
+    //          .append(WikiEditorPlugin.getDefault().getPluginPreferences().getString(WikiConstants.BROWSER_CSS_URL)).append(
+    //              "\" fType=\"text/css\" rel=\"STYLESHEET\">");
+    //    }
+    //    appendStyle();
+
+    buffer.append("</head><body>\n");
+  }
+
+  private void appendContents() {
+    IDocument document = editor.getDocument();
+    currentLine = 0;
+    while (currentLine < document.getNumberOfLines()) {
+      String line = getLine(document, currentLine);
+      //      appendLine(line);
+      currentLine++;
+    }
+  }
+
+  protected void appendNewLine() {
+    getBuffer().append(System.getProperty("line.separator"));
+  }
+
+  //  protected String getNextLine() {
+  //    if (hasLine(currentLine + 1)) {
+  //      currentLine++;
+  //      return getLine(editor.getDocument(), currentLine);
+  //    }
+  //    throw new RuntimeException("Should not be called if there is no next line.");
+  //  }
+
+  //  protected String peekNextLine() {
+  //    if (hasLine(currentLine + 1)) {
+  //      return getLine(editor.getDocument(), currentLine + 1);
+  //    }
+  //    return "";
+  //  }
+
+  protected boolean hasNextLine() {
+    return hasLine(currentLine + 1);
+  }
+
+  private boolean hasLine(int lineNumber) {
+    return lineNumber < editor.getDocument().getNumberOfLines();
+  }
+
+  protected void appendFooter() {
+    buffer.append("\n</body></html>");
+  }
+
+  //  protected void appendStyle() throws IOException {
+  //    buffer.append("<style fType=\"text/css\"><!--");
+  //    IPath path = new Path("style").append(RendererFactory.getContentRendererName() + ".css");
+  //    buffer.append(WikiEditorPlugin.getDefault().loadTextContents(path));
+  //    buffer.append("--></style>");
+  //  }
+  //
+  //  protected void appendLine(String line) {
+  //    if (isHeader(line)) {
+  //      appendHeader(line);
+  //    } else if (isList(line)) {
+  //      appendListItem(line);
+  //    } else if (tableLine(line)) {
+  //      processTable(line);
+  //    } else if (process(line)) {
+  //      return;
+  //    } else {
+  //      buffer.append("<p>");
+  //      append(processTags(line));
+  //      buffer.append("</p>");
+  //    }
+  //  }
+
+  //  private final void appendListItem(String line) {
+  //    int bullet = getListDepth(line);
+  //    if (bullet > currentListDepth) {
+  //      repeatAppend("<ul>", bullet - currentListDepth);
+  //      currentListDepth = bullet;
+  //    } else if (bullet < currentListDepth) {
+  //      repeatAppend("</ul>", currentListDepth - bullet);
+  //      currentListDepth = bullet;
+  //    }
+  //    getBuffer().append("<li>");
+  //    String content = "";
+  //    if (bullet < line.length() - 1) {
+  //      content = getListtext(line);
+  //    }
+  //    append(processTags(content));
+  //    getBuffer().append("</li>");
+  //    if (!isList(peekNextLine())) {
+  //      repeatAppend("</ul>", currentListDepth);
+  //      currentListDepth = 0;
+  //    }
+  //  }
+
+  protected abstract String getListtext(String line);
+
+  protected void repeatAppend(String item, int n) {
+    for (int i = 0; i < n; i++) {
+      getBuffer().append(item);
+    }
+  }
+
+  protected abstract int getListDepth(String line);
+
+  protected abstract String processTags(String line);
+
+  private String getLine(IDocument document, int n) {
+    try {
+      String line = document.get(document.getLineOffset(n), document.getLineLength(n));
+      if (document.getLineDelimiter(n) != null) {
+        line = line.substring(0, line.length() - document.getLineDelimiter(n).length());
+      }
+      for (int i = 0; i < REPLACE_TEXT.length; i++) {
+        line = line.replaceAll(REPLACE_TEXT[i][0], REPLACE_TEXT[i][1]);
+      }
+      return line;
+    } catch (BadLocationException e) {
+      WikiEditorPlugin.getDefault().logAndReport("Error", e.getLocalizedMessage(), e);
+      return e.getLocalizedMessage();
+    }
+  }
+
+  //  protected void append(String line) {
+  //    TextRegion[] regions = TextRegionBuilder.getTextRegions(line, editor);
+  //    for (int i = 0; i < regions.length; i++) {
+  //      regions[i].accept(new TextRegionVisitor() {
+  //        public Object visit(UndefinedTextRegion undefinedTextRegion) {
+  //          buffer.append(undefinedTextRegion.getText());
+  //          return null;
+  //        }
+  //
+  //        public Object visit(UrlTextRegion urlTextRegion) {
+  //          appendLink(urlTextRegion.getText(), urlTextRegion.getText());
+  //          return null;
+  //        }
+  //
+  //        public Object visit(WikiNameTextRegion wikiNameTextRegion) {
+  //          if (editor.hasWikiSibling(wikiNameTextRegion)) {
+  //            appendLink(AbstractContentRenderer.WIKI_HREF + wikiNameTextRegion.getText(), wikiNameTextRegion.getText());
+  //          } else {
+  //            buffer.append(wikiNameTextRegion.getText());
+  //            appendLink(AbstractContentRenderer.WIKI_HREF + wikiNameTextRegion.getText(), AbstractContentRenderer.NEW_WIKIDOC_HREF);
+  //          }
+  //          return null;
+  //        }
+  //
+  //        public Object visit(WikiUrlTextRegion wikiUrlTextRegion) {
+  //          String link;
+  //          if (wikiUrlTextRegion.getLink().startsWith(WikiConstants.ECLIPSE_PREFIX)) {
+  //            link = AbstractContentRenderer.WIKI_HREF + wikiUrlTextRegion.getLink();
+  //          } else {
+  //            link = wikiUrlTextRegion.getLink();
+  //          }
+  //          appendLink(link, wikiUrlTextRegion.getText());
+  //          return null;
+  //        }
+  //
+  //        public Object visit(BasicTextRegion basicTextRegion) {
+  //          buffer.append(basicTextRegion.getText());
+  //          return null;
+  //        }
+  //
+  //        public Object visit(EclipseResourceTextRegion eclipseResourceTextRegion) {
+  //          appendLink(AbstractContentRenderer.WIKI_HREF + eclipseResourceTextRegion.getText(), eclipseResourceTextRegion.getText());
+  //          return null;
+  //        }
+  //
+  //        public Object visit(JavaTypeTextRegion region) {
+  //          try {
+  //            if (region.getType().getUnderlyingResource() != null) {
+  //              String url = AbstractContentRenderer.WIKI_HREF + WikiConstants.ECLIPSE_PREFIX
+  //                  + region.getType().getUnderlyingResource().getFullPath().toString();
+  //              appendLink(url, region.getText());
+  //            } else {
+  //              append(region.getText());
+  //            }
+  //          } catch (JavaModelException e) {
+  //            WikiPlugin.getDefault().logAndReport("Error", e.getLocalizedMessage(), e);
+  //          }
+  //          return null;
+  //        }
+  //
+  //      });
+  //    }
+  //  }
+
+  protected void appendLink(String url, String text) {
+    buffer.append("<a href=\"").append(url).append("\">").append(text).append("</a>");
+  }
+
+  protected abstract boolean isHeader(String line);
+
+  protected abstract void appendHeader(String line);
+
+  protected abstract boolean isList(String line);
+
+  /**
+   * Gives implementors a chance to do processing on this line.
+   * 
+   * @return if true, the line will not be processed further
+   */
+  protected abstract boolean process(String line);
+
+  /**
+   * Replace all occurrences of markeup which occurs in pairs with an opening and closing tag in the given line. e.g.
+   * 
+   * <pre>
+   * 
+   *  
+   *   
+   *    
+   *     
+   *          replacePair(&quot;my ''bold'' word&quot;, &quot;''&quot;, &quot;&lt;b&gt;&quot;, &quot;,&lt;/b&gt;&quot;) returns &quot;my &lt;b&gt;bold&lt;/b&gt; word&quot;
+   *      
+   *     
+   *    
+   *   
+   *  
+   * </pre>
+   */
+  protected String replacePair(String line, String search, String openingTag, String closingTag) {
+    StringBuffer buffer = new StringBuffer();
+    String[] foo = line.split(search);
+    for (int i = 0; i < foo.length; i++) {
+      if (i % 2 == 1) {
+        buffer.append(openingTag).append(foo[i]).append(closingTag);
+      } else {
+        buffer.append(foo[i]);
+      }
+    }
+    return buffer.toString();
+  }
+
+  protected void appendHR() {
+    getBuffer().append("<hr>");
+  }
+
+  private boolean tableLine(String line) {
+    return line.startsWith(AbstractContentRenderer.TABLE_DELIMETER);
+  }
+
+  //  private void processTable(String line) {
+  //    if (!inTable) {
+  //      inTable = true;
+  //      getBuffer().append(getTableTag());
+  //    }
+  //    getBuffer().append("<tr>");
+  //    StringTokenizer tokenizer = new StringTokenizer(line, AbstractContentRenderer.TABLE_DELIMETER);
+  //    while (tokenizer.hasMoreTokens()) {
+  //      String cell = tokenizer.nextToken();
+  //      String element = "td";
+  //      if (cell.trim().startsWith("*")) {
+  //        element = "th";
+  //        cell = cell.replaceAll("\\*", "");
+  //      }
+  //      getBuffer().append("<").append(element).append(">");
+  //      append(processTags(cell));
+  //      getBuffer().append("</").append(element).append(">");
+  //    }
+  //    getBuffer().append("</tr>");
+  //    if (!tableLine(peekNextLine())) {
+  //      getBuffer().append("</table>");
+  //      inTable = false;
+  //    }
+  //  }
+
+  /**
+   * @return the tag for table (can be overridden to add style)
+   */
+  protected String getTableTag() {
+    return "<table>";
+  }
+
+  public void forEachHeader(IDocument document, StructureClosure closure) throws BadLocationException {
+    for (int i = 0; i < document.getNumberOfLines(); i++) {
+      String line = getLine(document, i);
+      if (isHeader(line)) {
+        String header = getHeaderText(line);
+        closure.acceptHeader(header, document.getLineOffset(i));
+      }
+    }
+  }
+
+  /**
+   * Get the header from a line with header markup
+   * 
+   * @param line
+   *          guaranteed to be a valid header as defined by b {@link #isHeader(String) isHeader(String)}
+   */
+  protected abstract String getHeaderText(String line);
+
+  /**
+   * @param project
+   *          The fProject to set.
+   */
+  public void setProject(IProject project) {
+    fProject = project;
+  }
+}
\ No newline at end of file