1 package net.sourceforge.phpeclipse.wiki.renderer;
3 import java.io.IOException;
4 import java.util.StringTokenizer;
6 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
7 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
8 import net.sourceforge.phpeclipse.wiki.preferences.Util;
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.core.runtime.Path;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
16 public abstract class AbstractContentRenderer implements IContentRenderer {
17 protected IProject fProject;
19 public static final String CLASS_MONO_SPACE = "monospace";
21 public static final String CLASS_QUOTE = "quote";
23 public static final String TABLE_DELIMETER = "|";
25 public static final String HR = "hr";
27 public static final String NEW_WIKIDOC_HREF = "?";
29 public static final String WIKI_HREF = "http://--wiki/";
31 public static final String[][] REPLACE_TEXT = new String[][] { { "<", "<" }, { "\"", """ } };
33 private WikiEditor editor;
35 protected StringBuffer buffer;
37 private int currentLine;
39 private int currentListDepth;
41 private boolean inTable;
43 private void setEditor(WikiEditor editor) {
47 protected StringBuffer getBuffer() {
51 protected WikiEditor getEditor() {
55 // public void render(String content, StringBuffer buf) {
61 // int startIndex = 0;
63 // while (index < content.length()) {
64 // if (content.charAt(index++) == '\n') {
65 // line = content.substring(startIndex, index);
66 // startIndex = index;
72 // if (startIndex < content.length()) {
73 // line = content.substring(startIndex, content.length());
77 // // return buffer.toString();
78 // } catch (Exception e) {
79 // WikiEditorPlugin.getDefault().log(buffer.toString());
80 // WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e);
81 // // return "<html><body><p>" + e.getLocalizedMessage() +
82 // // "</p></body></html>";
86 // protected void renderLine(String line) {
87 // if (isHeader(line)) {
88 // appendHeader(line);
89 // } else if (isList(line)) {
90 // appendListItem(line);
91 // } else if (tableLine(line)) {
92 // processTable(line);
93 // } else if (process(line)) {
96 // buffer.append(line);
100 // public final String render(WikiEditor editor) {
101 // initialise(editor);
103 // buffer = new StringBuffer();
105 // buffer.append("<h1>").append(editor.getWikiNameBeingEdited()).append("</h1>");
108 // return buffer.toString();
109 // } catch (Exception e) {
110 // WikiEditorPlugin.getDefault().log(buffer.toString());
111 // WikiEditorPlugin.getDefault().log(e.getLocalizedMessage(), e);
112 // return "<html><body><p>" + e.getLocalizedMessage() + "</p></body></html>";
116 private void initialise(WikiEditor editor) {
118 currentListDepth = 0;
123 protected abstract void initialise();
125 protected void appendHeader() throws IOException {
126 buffer.append("<html><head>");
127 // String basePath = Util.getMiscProjectsPreferenceValue(fProject, WikiConstants.HTML_OUTPUT_PATH);
128 // buffer.append("<base href=\"" + basePath + "/" + fProject.getName() + "\"/>");
130 // if (WikiEditorPlugin.getDefault().getPluginPreferences().contains(WikiConstants.BROWSER_CSS_URL)) {
131 // buffer.append("<link href=\"")
132 // .append(WikiEditorPlugin.getDefault().getPluginPreferences().getString(WikiConstants.BROWSER_CSS_URL)).append(
133 // "\" fType=\"text/css\" rel=\"STYLESHEET\">");
137 buffer.append("</head><body>\n");
140 private void appendContents() {
141 IDocument document = editor.getDocument();
143 while (currentLine < document.getNumberOfLines()) {
144 String line = getLine(document, currentLine);
150 protected void appendNewLine() {
151 getBuffer().append(System.getProperty("line.separator"));
154 // protected String getNextLine() {
155 // if (hasLine(currentLine + 1)) {
157 // return getLine(editor.getDocument(), currentLine);
159 // throw new RuntimeException("Should not be called if there is no next line.");
162 // protected String peekNextLine() {
163 // if (hasLine(currentLine + 1)) {
164 // return getLine(editor.getDocument(), currentLine + 1);
169 protected boolean hasNextLine() {
170 return hasLine(currentLine + 1);
173 private boolean hasLine(int lineNumber) {
174 return lineNumber < editor.getDocument().getNumberOfLines();
177 protected void appendFooter() {
178 buffer.append("\n</body></html>");
181 // protected void appendStyle() throws IOException {
182 // buffer.append("<style fType=\"text/css\"><!--");
183 // IPath path = new Path("style").append(RendererFactory.getContentRendererName() + ".css");
184 // buffer.append(WikiEditorPlugin.getDefault().loadTextContents(path));
185 // buffer.append("--></style>");
188 // protected void appendLine(String line) {
189 // if (isHeader(line)) {
190 // appendHeader(line);
191 // } else if (isList(line)) {
192 // appendListItem(line);
193 // } else if (tableLine(line)) {
194 // processTable(line);
195 // } else if (process(line)) {
198 // buffer.append("<p>");
199 // append(processTags(line));
200 // buffer.append("</p>");
204 // private final void appendListItem(String line) {
205 // int bullet = getListDepth(line);
206 // if (bullet > currentListDepth) {
207 // repeatAppend("<ul>", bullet - currentListDepth);
208 // currentListDepth = bullet;
209 // } else if (bullet < currentListDepth) {
210 // repeatAppend("</ul>", currentListDepth - bullet);
211 // currentListDepth = bullet;
213 // getBuffer().append("<li>");
214 // String content = "";
215 // if (bullet < line.length() - 1) {
216 // content = getListtext(line);
218 // append(processTags(content));
219 // getBuffer().append("</li>");
220 // if (!isList(peekNextLine())) {
221 // repeatAppend("</ul>", currentListDepth);
222 // currentListDepth = 0;
226 protected abstract String getListtext(String line);
228 protected void repeatAppend(String item, int n) {
229 for (int i = 0; i < n; i++) {
230 getBuffer().append(item);
234 protected abstract int getListDepth(String line);
236 protected abstract String processTags(String line);
238 private String getLine(IDocument document, int n) {
240 String line = document.get(document.getLineOffset(n), document.getLineLength(n));
241 if (document.getLineDelimiter(n) != null) {
242 line = line.substring(0, line.length() - document.getLineDelimiter(n).length());
244 for (int i = 0; i < REPLACE_TEXT.length; i++) {
245 line = line.replaceAll(REPLACE_TEXT[i][0], REPLACE_TEXT[i][1]);
248 } catch (BadLocationException e) {
249 WikiEditorPlugin.getDefault().logAndReport("Error", e.getLocalizedMessage(), e);
250 return e.getLocalizedMessage();
254 // protected void append(String line) {
255 // TextRegion[] regions = TextRegionBuilder.getTextRegions(line, editor);
256 // for (int i = 0; i < regions.length; i++) {
257 // regions[i].accept(new TextRegionVisitor() {
258 // public Object visit(UndefinedTextRegion undefinedTextRegion) {
259 // buffer.append(undefinedTextRegion.getText());
263 // public Object visit(UrlTextRegion urlTextRegion) {
264 // appendLink(urlTextRegion.getText(), urlTextRegion.getText());
268 // public Object visit(WikiNameTextRegion wikiNameTextRegion) {
269 // if (editor.hasWikiSibling(wikiNameTextRegion)) {
270 // appendLink(AbstractContentRenderer.WIKI_HREF + wikiNameTextRegion.getText(), wikiNameTextRegion.getText());
272 // buffer.append(wikiNameTextRegion.getText());
273 // appendLink(AbstractContentRenderer.WIKI_HREF + wikiNameTextRegion.getText(), AbstractContentRenderer.NEW_WIKIDOC_HREF);
278 // public Object visit(WikiUrlTextRegion wikiUrlTextRegion) {
280 // if (wikiUrlTextRegion.getLink().startsWith(WikiConstants.ECLIPSE_PREFIX)) {
281 // link = AbstractContentRenderer.WIKI_HREF + wikiUrlTextRegion.getLink();
283 // link = wikiUrlTextRegion.getLink();
285 // appendLink(link, wikiUrlTextRegion.getText());
289 // public Object visit(BasicTextRegion basicTextRegion) {
290 // buffer.append(basicTextRegion.getText());
294 // public Object visit(EclipseResourceTextRegion eclipseResourceTextRegion) {
295 // appendLink(AbstractContentRenderer.WIKI_HREF + eclipseResourceTextRegion.getText(), eclipseResourceTextRegion.getText());
299 // public Object visit(JavaTypeTextRegion region) {
301 // if (region.getType().getUnderlyingResource() != null) {
302 // String url = AbstractContentRenderer.WIKI_HREF + WikiConstants.ECLIPSE_PREFIX
303 // + region.getType().getUnderlyingResource().getFullPath().toString();
304 // appendLink(url, region.getText());
306 // append(region.getText());
308 // } catch (JavaModelException e) {
309 // WikiPlugin.getDefault().logAndReport("Error", e.getLocalizedMessage(), e);
318 protected void appendLink(String url, String text) {
319 buffer.append("<a href=\"").append(url).append("\">").append(text).append("</a>");
322 protected abstract boolean isHeader(String line);
324 protected abstract void appendHeader(String line);
326 protected abstract boolean isList(String line);
329 * Gives implementors a chance to do processing on this line.
331 * @return if true, the line will not be processed further
333 protected abstract boolean process(String line);
336 * Replace all occurrences of markeup which occurs in pairs with an opening and closing tag in the given line. e.g.
344 * replacePair("my ''bold'' word", "''", "<b>", ",</b>") returns "my <b>bold</b> word"
352 protected String replacePair(String line, String search, String openingTag, String closingTag) {
353 StringBuffer buffer = new StringBuffer();
354 String[] foo = line.split(search);
355 for (int i = 0; i < foo.length; i++) {
357 buffer.append(openingTag).append(foo[i]).append(closingTag);
359 buffer.append(foo[i]);
362 return buffer.toString();
365 protected void appendHR() {
366 getBuffer().append("<hr>");
369 private boolean tableLine(String line) {
370 return line.startsWith(AbstractContentRenderer.TABLE_DELIMETER);
373 // private void processTable(String line) {
376 // getBuffer().append(getTableTag());
378 // getBuffer().append("<tr>");
379 // StringTokenizer tokenizer = new StringTokenizer(line, AbstractContentRenderer.TABLE_DELIMETER);
380 // while (tokenizer.hasMoreTokens()) {
381 // String cell = tokenizer.nextToken();
382 // String element = "td";
383 // if (cell.trim().startsWith("*")) {
385 // cell = cell.replaceAll("\\*", "");
387 // getBuffer().append("<").append(element).append(">");
388 // append(processTags(cell));
389 // getBuffer().append("</").append(element).append(">");
391 // getBuffer().append("</tr>");
392 // if (!tableLine(peekNextLine())) {
393 // getBuffer().append("</table>");
399 * @return the tag for table (can be overridden to add style)
401 protected String getTableTag() {
405 public void forEachHeader(IDocument document, StructureClosure closure) throws BadLocationException {
406 for (int i = 0; i < document.getNumberOfLines(); i++) {
407 String line = getLine(document, i);
408 if (isHeader(line)) {
409 String header = getHeaderText(line);
410 closure.acceptHeader(header, document.getLineOffset(i));
416 * Get the header from a line with header markup
419 * guaranteed to be a valid header as defined by b {@link #isHeader(String) isHeader(String)}
421 protected abstract String getHeaderText(String line);
425 * The fProject to set.
427 public void setProject(IProject project) {