| 1 | |
package org.webslinger.ext.convertors; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.StringWriter; |
| 5 | |
import java.util.Iterator; |
| 6 | |
import java.util.Map; |
| 7 | |
import java.util.regex.Pattern; |
| 8 | |
|
| 9 | |
import org.webslinger.Webslinger; |
| 10 | |
import org.webslinger.WebslingerServletContext; |
| 11 | |
import org.webslinger.commons.vfs.FileResolver; |
| 12 | |
import org.webslinger.ext.wiki.WikiMacroOutput; |
| 13 | |
import org.webslinger.ext.wiki.parser.SimpleNode; |
| 14 | |
import org.webslinger.ext.wiki.parser.WikiNewline; |
| 15 | |
import org.webslinger.ext.wiki.parser.WikiVisitor; |
| 16 | |
import org.webslinger.template.TemplateMacro; |
| 17 | |
import org.webslinger.types.template; |
| 18 | |
|
| 19 | |
public class WikiVelocityMacroHandler extends WikiMacroOutput { |
| 20 | 1 | public static final Factory FACTORY = new Factory() { |
| 21 | |
public WikiVisitor newVisitor(String name, StringWriter writer, FileResolver resolver) { |
| 22 | 3 | return new WikiVelocityMacroHandler(name, writer); |
| 23 | |
} |
| 24 | |
}; |
| 25 | |
protected WikiVelocityHelper helper; |
| 26 | |
|
| 27 | 3 | protected WikiVelocityMacroHandler(String name, StringWriter writer) { |
| 28 | 3 | helper = new WikiVelocityHelper(writer); |
| 29 | 3 | } |
| 30 | |
|
| 31 | |
protected Plugin detectXml(String name, Object data) { |
| 32 | |
try { |
| 33 | 0 | template templateHandler = (template) ((Webslinger) data).getWebslingerServletContext().getTypeHandler("template"); |
| 34 | 0 | TemplateMacro macro = templateHandler.findMacro(name); |
| 35 | 0 | if (macro == null) return Plugin.NOT_SUPPORTED; |
| 36 | 0 | return macro.isBlock() ? Plugin.BLOCK : Plugin.EXISTS; |
| 37 | 0 | } catch (IOException e) { |
| 38 | 0 | throw (IllegalArgumentException) new IllegalArgumentException(e.getMessage()).initCause(e); |
| 39 | |
} |
| 40 | |
} |
| 41 | |
|
| 42 | |
protected void appendText(StringBuilder sb, SimpleNode node) { |
| 43 | 0 | if (node instanceof WikiNewline) sb.append(' '); |
| 44 | 0 | super.appendText(sb, node); |
| 45 | 0 | } |
| 46 | |
|
| 47 | |
protected void acceptMacro(String name, Object[] args) { |
| 48 | 13 | helper.acceptMacro(name, args); |
| 49 | 13 | } |
| 50 | |
|
| 51 | |
protected void endMacro(String name, Object[] args) { |
| 52 | 39 | helper.endMacro(name, args); |
| 53 | 39 | } |
| 54 | |
|
| 55 | |
protected void startMacro(String name, Object[] args) { |
| 56 | 39 | helper.startMacro(name, args); |
| 57 | 39 | } |
| 58 | |
|
| 59 | |
public Object visit(WikiNewline node, Object data) { |
| 60 | 27 | helper.acceptNewline(node.getCount()); |
| 61 | 27 | return super.visit(node, data); |
| 62 | |
} |
| 63 | |
|
| 64 | |
public void acceptText(String text) { |
| 65 | 39 | helper.acceptText(text); |
| 66 | 39 | } |
| 67 | |
|
| 68 | |
protected void acceptXml(String name, Map attributes) { |
| 69 | 0 | helper.acceptXml(name, attributes); |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
protected void startXml(String name, Map attributes) { |
| 73 | 0 | helper.startXml(name, attributes); |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
protected void endXml(String name, Map attributes) { |
| 77 | 0 | helper.endXml(name, attributes); |
| 78 | 0 | } |
| 79 | |
|
| 80 | |
protected void blockNewline() { |
| 81 | 12 | helper.blockNewline(); |
| 82 | 12 | } |
| 83 | |
|
| 84 | |
public void close() throws IOException { |
| 85 | 0 | helper.close(); |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
public void flush() throws IOException { |
| 89 | 0 | } |
| 90 | |
} |