BrightSide Workbench Full Report + Source Code
ElephantProcessor.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.wiki;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.action.Contacts;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.file.Folder;
31 import org.turro.marker.ElephantMarker;
32 import org.turro.plugin.contacts.IContact;
33 import org.turro.string.Strings;
34 
39 public class ElephantProcessor implements IMacroProcessor {
40 
41  @Override
42  public String expand(IConstructor constructor, IContact contact, String name, Map<String, String> pars) {
43  if(contact == null) contact = constructor.getUser();
44  String template = locateTemplate(constructor, name);
45  if(!Strings.isBlank(template)) {
46  ElephantMarker marker = new ElephantMarker(constructor);
47  marker.put("help", false);
48  marker.put("end", name.startsWith("/"));
49  marker.putAll(pars);
50  marker.put("contact", checkContact(contact, pars));
51  return marker.parse(TMPL_ROOT, template);
52  }
53  return null;
54  }
55 
56  @Override
57  public String help(IConstructor constructor, String name) {
58  String template = locateTemplate(constructor, name);
59  if(!Strings.isBlank(template)) {
60  ElephantMarker marker = new ElephantMarker(constructor);
61  marker.put("help", true);
62  marker.put("end", false);
63  return marker.parse(TMPL_ROOT, template);
64  }
65  return null;
66  }
67 
68  /* Template */
69 
70  private static final String TMPL_ROOT = "widgets/mwiki";
71 
72  @Override
73  public Set<String> locateNames(IConstructor constructor) {
74  Folder root = Folder.from(ElephantMarker.templateRoot(constructor, false) + "/" + TMPL_ROOT);
75  Set<String> names = new TreeSet<>();
76  if(root.exists()) {
77  try {
78  root.documents("*Template.html").forEach(d -> {
79  names.add(d.name().replace("Template.html", ""));
80  });
81  } catch (IOException ex) {
82  Logger.getLogger(ElephantProcessor.class.getName()).log(Level.SEVERE, null, ex);
83  }
84  }
85  return names;
86  }
87 
88  private String locateTemplate(IConstructor constructor, String name) {
89  if(name.startsWith("/")) name = name.substring(1);
90  File file = ElephantMarker.fileTemplate(constructor, false, TMPL_ROOT, name);
91  return file.exists() ? name : null;
92  }
93 
94  private IContact checkContact(IContact contact, Map<String, String> pars) {
95  if(pars.containsKey("contact")) {
96  return Contacts.getContactById(pars.get("contact"));
97  }
98  return contact;
99  }
100 
101 }
static String templateRoot(IConstructor constructor, boolean mail)
static File fileTemplate(IConstructor constructor, boolean mail, String root, String name)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
String expand(IConstructor constructor, IContact contact, String name, Map< String, String > pars)
Set< String > locateNames(IConstructor constructor)
String help(IConstructor constructor, String name)