1 package net.sourceforge.phpeclipse.wiki.renderer;
 
   3 import java.io.IOException;
 
   5 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
 
   6 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
 
   8 import org.eclipse.core.resources.IProject;
 
   9 import org.eclipse.jface.text.BadLocationException;
 
  10 import org.eclipse.jface.text.IDocument;
 
  12 public abstract class AbstractContentRenderer implements IContentRenderer {
 
  13   protected IProject fProject;
 
  15   public static final String CLASS_MONO_SPACE = "monospace";
 
  17   public static final String CLASS_QUOTE = "quote";
 
  19   public static final String TABLE_DELIMETER = "|";
 
  21   public static final String HR = "hr";
 
  23   public static final String NEW_WIKIDOC_HREF = "?";
 
  25   public static final String WIKI_HREF = "http://--wiki/";
 
  27   public static final String[][] REPLACE_TEXT = new String[][] { { "<", "<" }, { "\"", """ } };
 
  29   private WikiEditor editor;
 
  31   protected StringBuffer buffer;
 
  33   private int currentLine;
 
  35   private int currentListDepth;
 
  37   private boolean inTable;
 
  39   private void setEditor(WikiEditor editor) {
 
  43   protected StringBuffer getBuffer() {
 
  47   protected WikiEditor getEditor() {
 
  51   //  public void render(String content, StringBuffer buf) {
 
  57   //      int startIndex = 0;
 
  59   //      while (index < content.length()) {
 
  60   //        if (content.charAt(index++) == '\n') {
 
  61   //          line = content.substring(startIndex, index);
 
  62   //          startIndex = index;
 
  68   //      if (startIndex < content.length()) {
 
  69   //        line = content.substring(startIndex, content.length());
 
  73   //      // return buffer.toString();
 
  74   //    } catch (Exception e) {
 
  75   //      WikiEditorPlugin.getDefault().log(buffer.toString());
 
  76   //      WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e);
 
  77   //      // return "<html><body><p>" + e.getLocalizedMessage() +
 
  78   //      // "</p></body></html>";
 
  82   //  protected void renderLine(String line) {
 
  83   //    if (isHeader(line)) {
 
  84   //      appendHeader(line);
 
  85   //    } else if (isList(line)) {
 
  86   //      appendListItem(line);
 
  87   //    } else if (tableLine(line)) {
 
  88   //      processTable(line);
 
  89   //    } else if (process(line)) {
 
  92   //      buffer.append(line);
 
  96   //  public final String render(WikiEditor editor) {
 
  97   //    initialise(editor);
 
  99   //      buffer = new StringBuffer();
 
 101   //      buffer.append("<h1>").append(editor.getWikiNameBeingEdited()).append("</h1>");
 
 104   //      return buffer.toString();
 
 105   //    } catch (Exception e) {
 
 106   //      WikiEditorPlugin.getDefault().log(buffer.toString());
 
 107   //      WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e);
 
 108   //      return "<html><body><p>" + e.getLocalizedMessage() + "</p></body></html>";
 
 112   private void initialise(WikiEditor editor) {
 
 114     currentListDepth = 0;
 
 119   protected abstract void initialise();
 
 121   protected void appendHeader(StringBuffer buf) throws IOException {
 
 122     buf.append("<html><head>");
 
 123     //    String basePath = Util.getMiscProjectsPreferenceValue(fProject, WikiConstants.HTML_OUTPUT_PATH);
 
 124     //    buffer.append("<base href=\"" + basePath + "/" + fProject.getName() + "\"/>");
 
 126     //    if (WikiEditorPlugin.getDefault().getPluginPreferences().contains(WikiConstants.BROWSER_CSS_URL)) {
 
 127     //      buffer.append("<link href=\"")
 
 128     //          .append(WikiEditorPlugin.getDefault().getPluginPreferences().getString(WikiConstants.BROWSER_CSS_URL)).append(
 
 129     //              "\" fType=\"text/css\" rel=\"STYLESHEET\">");
 
 133     buf.append("</head><body>\n");
 
 136   private void appendContents() {
 
 137     IDocument document = editor.getDocument();
 
 139     while (currentLine < document.getNumberOfLines()) {
 
 140       String line = getLine(document, currentLine);
 
 146   protected void appendNewLine() {
 
 147     getBuffer().append(System.getProperty("line.separator"));
 
 150   //  protected String getNextLine() {
 
 151   //    if (hasLine(currentLine + 1)) {
 
 153   //      return getLine(editor.getDocument(), currentLine);
 
 155   //    throw new RuntimeException("Should not be called if there is no next line.");
 
 158   //  protected String peekNextLine() {
 
 159   //    if (hasLine(currentLine + 1)) {
 
 160   //      return getLine(editor.getDocument(), currentLine + 1);
 
 165   protected boolean hasNextLine() {
 
 166     return hasLine(currentLine + 1);
 
 169   private boolean hasLine(int lineNumber) {
 
 170     return lineNumber < editor.getDocument().getNumberOfLines();
 
 173   protected void appendFooter(StringBuffer buf) {
 
 174     buf.append("\n</body></html>");
 
 177   //  protected void appendStyle() throws IOException {
 
 178   //    buffer.append("<style fType=\"text/css\"><!--");
 
 179   //    IPath path = new Path("style").append(RendererFactory.getContentRendererName() + ".css");
 
 180   //    buffer.append(WikiEditorPlugin.getDefault().loadTextContents(path));
 
 181   //    buffer.append("--></style>");
 
 184   //  protected void appendLine(String line) {
 
 185   //    if (isHeader(line)) {
 
 186   //      appendHeader(line);
 
 187   //    } else if (isList(line)) {
 
 188   //      appendListItem(line);
 
 189   //    } else if (tableLine(line)) {
 
 190   //      processTable(line);
 
 191   //    } else if (process(line)) {
 
 194   //      buffer.append("<p>");
 
 195   //      append(processTags(line));
 
 196   //      buffer.append("</p>");
 
 200   //  private final void appendListItem(String line) {
 
 201   //    int bullet = getListDepth(line);
 
 202   //    if (bullet > currentListDepth) {
 
 203   //      repeatAppend("<ul>", bullet - currentListDepth);
 
 204   //      currentListDepth = bullet;
 
 205   //    } else if (bullet < currentListDepth) {
 
 206   //      repeatAppend("</ul>", currentListDepth - bullet);
 
 207   //      currentListDepth = bullet;
 
 209   //    getBuffer().append("<li>");
 
 210   //    String content = "";
 
 211   //    if (bullet < line.length() - 1) {
 
 212   //      content = getListtext(line);
 
 214   //    append(processTags(content));
 
 215   //    getBuffer().append("</li>");
 
 216   //    if (!isList(peekNextLine())) {
 
 217   //      repeatAppend("</ul>", currentListDepth);
 
 218   //      currentListDepth = 0;
 
 222   protected abstract String getListtext(String line);
 
 224   protected void repeatAppend(String item, int n) {
 
 225     for (int i = 0; i < n; i++) {
 
 226       getBuffer().append(item);
 
 230   protected abstract int getListDepth(String line);
 
 232   protected abstract String processTags(String line);
 
 234   private String getLine(IDocument document, int n) {
 
 236       String line = document.get(document.getLineOffset(n), document.getLineLength(n));
 
 237       if (document.getLineDelimiter(n) != null) {
 
 238         line = line.substring(0, line.length() - document.getLineDelimiter(n).length());
 
 240       for (int i = 0; i < REPLACE_TEXT.length; i++) {
 
 241         line = line.replaceAll(REPLACE_TEXT[i][0], REPLACE_TEXT[i][1]);
 
 244     } catch (BadLocationException e) {
 
 245       WikiEditorPlugin.getDefault().logAndReport("Error", e.getLocalizedMessage(), e);
 
 246       return e.getLocalizedMessage();
 
 250   //  protected void append(String line) {
 
 251   //    TextRegion[] regions = TextRegionBuilder.getTextRegions(line, editor);
 
 252   //    for (int i = 0; i < regions.length; i++) {
 
 253   //      regions[i].accept(new TextRegionVisitor() {
 
 254   //        public Object visit(UndefinedTextRegion undefinedTextRegion) {
 
 255   //          buffer.append(undefinedTextRegion.getText());
 
 259   //        public Object visit(UrlTextRegion urlTextRegion) {
 
 260   //          appendLink(urlTextRegion.getText(), urlTextRegion.getText());
 
 264   //        public Object visit(WikiNameTextRegion wikiNameTextRegion) {
 
 265   //          if (editor.hasWikiSibling(wikiNameTextRegion)) {
 
 266   //            appendLink(AbstractContentRenderer.WIKI_HREF + wikiNameTextRegion.getText(), wikiNameTextRegion.getText());
 
 268   //            buffer.append(wikiNameTextRegion.getText());
 
 269   //            appendLink(AbstractContentRenderer.WIKI_HREF + wikiNameTextRegion.getText(), AbstractContentRenderer.NEW_WIKIDOC_HREF);
 
 274   //        public Object visit(WikiUrlTextRegion wikiUrlTextRegion) {
 
 276   //          if (wikiUrlTextRegion.getLink().startsWith(WikiConstants.ECLIPSE_PREFIX)) {
 
 277   //            link = AbstractContentRenderer.WIKI_HREF + wikiUrlTextRegion.getLink();
 
 279   //            link = wikiUrlTextRegion.getLink();
 
 281   //          appendLink(link, wikiUrlTextRegion.getText());
 
 285   //        public Object visit(BasicTextRegion basicTextRegion) {
 
 286   //          buffer.append(basicTextRegion.getText());
 
 290   //        public Object visit(EclipseResourceTextRegion eclipseResourceTextRegion) {
 
 291   //          appendLink(AbstractContentRenderer.WIKI_HREF + eclipseResourceTextRegion.getText(), eclipseResourceTextRegion.getText());
 
 295   //        public Object visit(JavaTypeTextRegion region) {
 
 297   //            if (region.getType().getUnderlyingResource() != null) {
 
 298   //              String url = AbstractContentRenderer.WIKI_HREF + WikiConstants.ECLIPSE_PREFIX
 
 299   //                  + region.getType().getUnderlyingResource().getFullPath().toString();
 
 300   //              appendLink(url, region.getText());
 
 302   //              append(region.getText());
 
 304   //          } catch (JavaModelException e) {
 
 305   //            WikiPlugin.getDefault().logAndReport("Error", e.getLocalizedMessage(), e);
 
 314   protected void appendLink(String url, String text) {
 
 315     buffer.append("<a href=\"").append(url).append("\">").append(text).append("</a>");
 
 318   protected abstract boolean isHeader(String line);
 
 320 //  protected abstract void appendHeader(String line);
 
 322   protected abstract boolean isList(String line);
 
 325    * Gives implementors a chance to do processing on this line.
 
 327    * @return if true, the line will not be processed further
 
 329   protected abstract boolean process(String line);
 
 332    * Replace all occurrences of markeup which occurs in pairs with an opening and closing tag in the given line. e.g.
 
 340    *          replacePair("my ''bold'' word", "''", "<b>", ",</b>") returns "my <b>bold</b> word"
 
 348   protected String replacePair(String line, String search, String openingTag, String closingTag) {
 
 349     StringBuffer buffer = new StringBuffer();
 
 350     String[] foo = line.split(search);
 
 351     for (int i = 0; i < foo.length; i++) {
 
 353         buffer.append(openingTag).append(foo[i]).append(closingTag);
 
 355         buffer.append(foo[i]);
 
 358     return buffer.toString();
 
 361   protected void appendHR() {
 
 362     getBuffer().append("<hr>");
 
 365   private boolean tableLine(String line) {
 
 366     return line.startsWith(AbstractContentRenderer.TABLE_DELIMETER);
 
 369   //  private void processTable(String line) {
 
 372   //      getBuffer().append(getTableTag());
 
 374   //    getBuffer().append("<tr>");
 
 375   //    StringTokenizer tokenizer = new StringTokenizer(line, AbstractContentRenderer.TABLE_DELIMETER);
 
 376   //    while (tokenizer.hasMoreTokens()) {
 
 377   //      String cell = tokenizer.nextToken();
 
 378   //      String element = "td";
 
 379   //      if (cell.trim().startsWith("*")) {
 
 381   //        cell = cell.replaceAll("\\*", "");
 
 383   //      getBuffer().append("<").append(element).append(">");
 
 384   //      append(processTags(cell));
 
 385   //      getBuffer().append("</").append(element).append(">");
 
 387   //    getBuffer().append("</tr>");
 
 388   //    if (!tableLine(peekNextLine())) {
 
 389   //      getBuffer().append("</table>");
 
 395    * @return the tag for table (can be overridden to add style)
 
 397   protected String getTableTag() {
 
 401 //  public void forEachHeader(IDocument document, StructureClosure closure) throws BadLocationException {
 
 402 //    for (int i = 0; i < document.getNumberOfLines(); i++) {
 
 403 //      String line = getLine(document, i);
 
 404 //      if (isHeader(line)) {
 
 405 //        String header = getHeaderText(line);
 
 406 //        closure.acceptHeader(header, document.getLineOffset(i));
 
 412    * Get the header from a line with header markup
 
 415    *          guaranteed to be a valid header as defined by b {@link #isHeader(String) isHeader(String)}
 
 417 //  protected abstract String getHeaderText(String line);
 
 421    *          The fProject to set.
 
 423   public void setProject(IProject project) {