BrightSide Workbench Full Report + Source Code
MacroProcessors.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.parser.processor;
20 
21 import java.io.IOException;
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import java.util.HashSet;
25 import java.util.Set;
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;
32 
37 public class MacroProcessors {
38 
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\")})})}"));
41  }
42 
44  this.constructor = constructor;
45  return this;
46  }
47 
48  public MacroProcessors contact(IContact contact) {
49  this.contact = contact;
50  return this;
51  }
52 
53  public MacroProcessors allowed(Set<String> allowed) {
54  this.allowed = allowed;
55  return this;
56  }
57 
58  public MacroProcessors forbidden(Set<String> forbidden) {
59  this.forbidden = forbidden;
60  return this;
61  }
62 
63  /* Process */
64 
65  public String process(String text) {
66  if(text.contains(MACRO_START)) {
67  StringWriter writer = new StringWriter();
68  process(writer, text);
69  return writer.toString();
70  }
71  return text;
72  }
73 
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);
78  });
79  }
80  }
81 
82  private void processContent(String content, Writer writer) {
83  content = MacroParser.of(MACRO_START, MACRO_END).process(content, nested -> {
84  processContent(nested, writer);
85  });
86  String macroName = MacroParser.macroName(content);
87  if(passName(macroName)) {
88  expandMacro(writer, macroName, content);
89  }
90  }
91 
92  /* Utils */
93 
94  private boolean passName(String macroName) {
95  return !forbidden.contains(macroName) && (allowed.isEmpty() || allowed.contains(macroName));
96  }
97 
98  private void expandMacro(Writer writer, String macroName, String content) {
99  Macro macro = null;
100  for(IMacroProcessor processor : Instances.cached().bySuper(AbstractMacroProcessor.class, IMacroProcessor.class)) {
101  macro = processor.getMacro(macroName);
102  if(macro != null) {
103  processor.process(new MacroProcessorContext(constructor, writer, contact,
104  new MacroAdapter(macro, MacroParser.parameterMap(content))));
105  break;
106  }
107  }
108  if(macro == null) {
109  notFound(writer, macroName);
110  }
111  }
112 
113  private void notFound(Writer writer, String content) {
114  try {
115  writer.write("<span style='color:red'>" + MACRO_START + content + MACRO_END + "</span>");
116  } catch (IOException ex) {
117  WebLoggers.info(this).exception(ex).log();
118  }
119  }
120 
121  /* Factory */
122 
123  public static MacroProcessors mail() {
124  return new MacroProcessors().allowed(Set.of("user-name", "user-mail", "user-lang"));
125  }
126 
127  public static MacroProcessors full() {
128  return new MacroProcessors();
129  }
130 
131  private static final String MACRO_START = "@{", MACRO_END = "}";
132 
133  private Set<String> allowed, forbidden;
134 
135  private IConstructor constructor;
136  private IContact contact;
137 
138  public MacroProcessors() {
139  this.allowed = new HashSet<>();
140  this.forbidden = new HashSet<>();
141  }
142 
143 }
MacroProcessors forbidden(Set< String > forbidden)
MacroProcessors allowed(Set< String > allowed)
void process(Writer writer, String text)
MacroProcessors constructor(IConstructor constructor)
MacroProcessors contact(IContact contact)