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[][] { { "<", "<" }, { "\"", """ } }; 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 "

" + e.getLocalizedMessage() + // // "

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

").append(editor.getWikiNameBeingEdited()).append("

"); // appendContents(); // appendFooter(); // return buffer.toString(); // } catch (Exception e) { // WikiEditorPlugin.getDefault().log(buffer.toString()); // WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e); // return "

" + e.getLocalizedMessage() + "

"; // } // } private void initialise(WikiEditor editor) { setEditor(editor); currentListDepth = 0; inTable = false; initialise(); } protected abstract void initialise(); protected void appendHeader() throws IOException { buffer.append(""); // String basePath = Util.getMiscProjectsPreferenceValue(fProject, WikiConstants.HTML_OUTPUT_PATH); // buffer.append(""); // if (WikiEditorPlugin.getDefault().getPluginPreferences().contains(WikiConstants.BROWSER_CSS_URL)) { // buffer.append(""); // } // appendStyle(); buffer.append("\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"); } // protected void appendStyle() throws IOException { // buffer.append(""); // } // // 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("

"); // append(processTags(line)); // buffer.append("

"); // } // } // private final void appendListItem(String line) { // int bullet = getListDepth(line); // if (bullet > currentListDepth) { // repeatAppend("", currentListDepth - bullet); // currentListDepth = bullet; // } // getBuffer().append("
  • "); // String content = ""; // if (bullet < line.length() - 1) { // content = getListtext(line); // } // append(processTags(content)); // getBuffer().append("
  • "); // if (!isList(peekNextLine())) { // repeatAppend("", 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("").append(text).append(""); } 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. * *
       * 
       *  
       *   
       *    
       *     
       *          replacePair("my ''bold'' word", "''", "<b>", ",</b>") returns "my <b>bold</b> word"
       *      
       *     
       *    
       *   
       *  
       * 
    */ 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("
    "); } 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(""); // 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(""); // } // getBuffer().append(""); // if (!tableLine(peekNextLine())) { // getBuffer().append(""); // inTable = false; // } // } /** * @return the tag for table (can be overridden to add style) */ protected String getTableTag() { return ""; } 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; } }