BrightSide Workbench Full Report + Source Code
MacroProcessorContext.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.Writer;
23 import java.util.Optional;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.elephant.context.IConstructor;
26 import org.turro.log.WebLoggers;
27 import org.turro.marker.ElephantMarker;
28 import org.turro.plugin.contacts.IContact;
29 
34 public class MacroProcessorContext {
35 
36  private final IConstructor constructor;
37  private final Writer writer;
38  private final IContact contact;
39  private final MacroAdapter macro;
40 
41  private ElephantMarker marker;
42 
43  public MacroProcessorContext(IConstructor constructor, Writer writer, IContact contact, MacroAdapter macro) {
44  this.constructor = constructor;
45  this.writer = writer;
46  this.contact = contact;
47  this.macro = macro;
48  }
49 
51  return constructor;
52  }
53 
54  public Writer getWriter() {
55  return writer;
56  }
57 
58  public IContact getContact() {
59  if(contact != null) {
60  return contact;
61  } else if(constructor != null) {
62  return constructor.getUser();
63  } else {
64  return null;
65  }
66  }
67 
69  return macro;
70  }
71 
73  if(marker == null) {
74  marker = new ElephantMarker(constructor);
75  }
76  return marker;
77  }
78 
79  public void write(String value) {
80  try {
81  writer.write(value);
82  } catch (IOException ex) {
83  WebLoggers.info(this).exception(ex).log();
84  }
85  }
86 
87  public String getContextPath(boolean full) {
88  return (full ? ElephantContext.getRootWebPath() : "") +
89  Optional.ofNullable(macro.get("context"))
90  .orElse(getCurrentContextPath());
91  }
92 
93  public String getTemplateRoot() {
94  return macro.get("tmpl-root");
95  }
96 
97  public String getTemplate() {
98  return macro.get("template");
99  }
100 
101  public boolean isNavigation() {
102  return macro.get(Boolean.class, "navigation", true);
103  }
104 
105  public int getPage() {
106  return macro.get(Integer.class, "count");
107  }
108 
109  public boolean isRestricted() {
110  return macro.get(Boolean.class, "restricted", false);
111  }
112 
113  public boolean isRanking() {
114  return macro.get(Boolean.class, "ranking", false);
115  }
116 
117  public boolean isMatching() {
118  return macro.get(Boolean.class, "matching", false);
119  }
120 
121  private String getCurrentContextPath() {
122  return Optional.ofNullable(constructor)
123  .map(c -> c.getCurrentContext())
124  .map(c -> c.getPath())
125  .orElse("");
126  }
127 
128 }
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
static WebLoggers info(Object entity)
Definition: WebLoggers.java:43
MacroProcessorContext(IConstructor constructor, Writer writer, IContact contact, MacroAdapter macro)