BrightSide Workbench Full Report + Source Code
CoreProcessor.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.util.Optional;
22 import org.turro.parser.MacroMap;
23 import org.turro.parser.MacroParameter;
24 import org.turro.plugin.contacts.IContact;
25 
30 public class CoreProcessor extends AbstractMacroProcessor {
31 
32  @Override
33  public String getName() {
34  return "Core";
35  }
36 
37  @Override
38  protected void doProcess(MacroProcessorContext context) {
39  switch(context.getMacro().getName()) {
40  case "user-name" -> context.write(
41  Optional.ofNullable(context.getContact())
42  .map(u -> userName(u, context.getMacro().get("mode")))
43  .orElse(context.getMacro().getEmpty()));
44  case "user-mail" -> context.write(
45  Optional.ofNullable(context.getContact())
46  .map(u -> u.getEmail())
47  .orElse(context.getMacro().getEmpty()));
48  case "user-lang" -> context.write(
49  Optional.ofNullable(context.getContact())
50  .map(u -> u.getLocale())
51  .map(l -> l.getLanguage())
52  .orElse(context.getMacro().getEmpty()));
53  };
54  }
55 
56  @Override
57  protected void explainMacros(MacroMap macros) {
58  macros.addMacro("user-name")
59  .addParameter(MacroParameter.strict("mode", "formal", "as-is", "full", "friendly"));
60  macros.addMacro("user-mail");
61  macros.addMacro("user-lang");
62  }
63 
64  /* Utils */
65 
66  private String userName(IContact contact, String mode) {
67  return switch(mode) {
68  case "friendly" -> contact.getFriendly();
69  case "formal" -> contact.getFormal();
70  case "as-is" -> contact.getName();
71  case "full" -> contact.getFullName();
72  default -> "";
73  };
74  }
75 
76 }
void doProcess(MacroProcessorContext context)