19 package org.turro.parser.processor;
21 import java.io.IOException;
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import java.util.HashSet;
26 import org.turro.elephant.context.IConstructor;
27 import org.turro.log.WebLoggers;
28 import org.turro.parser.Macro;
29 import org.turro.parser.MacroParser;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.reflection.Instances;
39 public static void main(String[] args) {
40 System.out.println(
MacroProcessors.
mail().
process(
"Provem... @{user-name(void=@{user-mail(void=@{user-lang(void=\"Anonymous\")})})}"));
44 this.constructor = constructor;
49 this.contact = contact;
54 this.allowed = allowed;
59 this.forbidden = forbidden;
66 if(text.contains(MACRO_START)) {
67 StringWriter writer =
new StringWriter();
69 return writer.toString();
74 public void process(Writer writer, String text) {
75 if(text.contains(MACRO_START)) {
76 MacroParser.of(MACRO_START, MACRO_END).process(writer, text, content -> {
77 processContent(content, writer);
82 private void processContent(String content, Writer writer) {
83 content = MacroParser.of(MACRO_START, MACRO_END).process(content, nested -> {
84 processContent(nested, writer);
86 String macroName = MacroParser.macroName(content);
87 if(passName(macroName)) {
88 expandMacro(writer, macroName, content);
94 private boolean passName(String macroName) {
95 return !forbidden.contains(macroName) && (allowed.isEmpty() || allowed.contains(macroName));
98 private void expandMacro(Writer writer, String macroName, String content) {
100 for(IMacroProcessor processor : Instances.cached().bySuper(AbstractMacroProcessor.class, IMacroProcessor.class)) {
101 macro = processor.getMacro(macroName);
103 processor.process(
new MacroProcessorContext(constructor, writer, contact,
104 new MacroAdapter(macro, MacroParser.parameterMap(content))));
109 notFound(writer, macroName);
113 private void notFound(Writer writer, String content) {
115 writer.write(
"<span style='color:red'>" + MACRO_START + content + MACRO_END +
"</span>");
116 }
catch (IOException ex) {
117 WebLoggers.info(
this).exception(ex).log();
124 return new MacroProcessors().allowed(Set.of(
"user-name",
"user-mail",
"user-lang"));
131 private static final String MACRO_START =
"@{", MACRO_END =
"}";
133 private Set<String> allowed, forbidden;
139 this.allowed =
new HashSet<>();
140 this.forbidden =
new HashSet<>();
static MacroProcessors mail()
MacroProcessors forbidden(Set< String > forbidden)
MacroProcessors allowed(Set< String > allowed)
void process(Writer writer, String text)
String process(String text)
MacroProcessors constructor(IConstructor constructor)
static MacroProcessors full()
static void main(String[] args)
MacroProcessors contact(IContact contact)