Package: TextHolder
TextHolder
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TextHolder(Id) |
|
|
|
|
|
||||||||||||||||||||
TextHolder(String) |
|
|
|
|
|
||||||||||||||||||||
addAttribute(String, String) |
|
|
|
|
|
||||||||||||||||||||
addComponent(TextHolder) |
|
|
|
|
|
||||||||||||||||||||
asBytes(Charset) |
|
|
|
|
|
||||||||||||||||||||
asString(Charset) |
|
|
|
|
|
||||||||||||||||||||
getMimeType() |
|
|
|
|
|
||||||||||||||||||||
getTemplate() |
|
|
|
|
|
||||||||||||||||||||
loadTemplate() |
|
|
|
|
|
||||||||||||||||||||
setContent(String) |
|
|
|
|
|
||||||||||||||||||||
setMimeType(String) |
|
|
|
|
|
||||||||||||||||||||
setTemplate(String) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.ui.component.htmltemplate;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import javax.inject.Provider;
32: import java.util.ArrayList;
33: import java.util.HashMap;
34: import java.util.List;
35: import java.util.Map;
36: import java.io.FileNotFoundException;
37: import java.io.IOException;
38: import java.io.InputStreamReader;
39: import java.io.Reader;
40: import java.nio.CharBuffer;
41: import java.nio.charset.Charset;
42: import org.springframework.beans.factory.annotation.Configurable;
43: import org.springframework.core.io.ClassPathResource;
44: import org.springframework.core.io.Resource;
45: import org.stringtemplate.v4.ST;
46: import it.tidalwave.util.Id;
47: import it.tidalwave.northernwind.core.model.SiteProvider;
48: import it.tidalwave.northernwind.frontend.ui.SiteViewController;
49: import lombok.Getter;
50: import lombok.Setter;
51: import lombok.ToString;
52: import lombok.extern.slf4j.Slf4j;
53:
54: /***********************************************************************************************************************
55: *
56: * The HtmlTemplate specialization of {@link SiteViewController}.
57: *
58: * @author Fabrizio Giudici
59: *
60: **********************************************************************************************************************/
61: @Configurable @Slf4j @ToString(exclude="siteProvider")
62: public class TextHolder
63: {
64: private final Map<String, String> attributeMap = new HashMap<>();
65:
66: private final List<TextHolder> contents = new ArrayList<>();
67:
68: @Getter @Setter
69: private String template;
70:
71: @Inject
72: private Provider<SiteProvider> siteProvider;
73:
74: @Getter @Setter
75: private String mimeType = "text/plain";
76:
77: /*******************************************************************************************************************
78: *
79: * Creates an instance with the given name.
80: *
81: * @param name the component name
82: *
83: ******************************************************************************************************************/
84: public TextHolder (@Nonnull final Id id)
85:• {
86: attributeMap.put("content", "");
87:
88: try
89: {
90: loadTemplate();
91: }
92: catch (IOException e)
93: {
94: throw new RuntimeException(e);
95: }
96:• }
97:
98: public TextHolder (@Nonnull final String html)
99:• {
100: addAttribute("content", html);
101: template = "$content$";
102:• }
103:
104: // @Override
105: public void setContent (@Nonnull final String content)
106: {
107: addAttribute("content", content);
108: // setValue(html);
109: }
110:
111: public void addComponent (@Nonnull final TextHolder child)
112: {
113: contents.add(child);
114: }
115:
116: public void addAttribute (@Nonnull final String name, @Nonnull final String value)
117: {
118: attributeMap.put(name, value);
119: }
120:
121: // @Nonnull
122: // public byte[] asBytes2 (final @Nonnull Charset charset)
123: // {
124: // return asString2(charset).getBytes(charset);
125: // }
126: //
127: // @Nonnull
128: // public String asString2 (final @Nonnull Charset charset)
129: // {
130: // return template;
131: // }
132:
133: @Nonnull
134: public byte[] asBytes (@Nonnull final Charset charset)
135: {
136: return asString(charset).getBytes(charset);
137: }
138:
139: @Nonnull
140: public String asString (@Nonnull final Charset charset)
141: {
142: var t = new ST(template, '$', '$');
143:
144:• for (final var entry : attributeMap.entrySet())
145: {
146: t = t.add(entry.getKey(), entry.getValue());
147: }
148:
149: final var builder = new StringBuilder();
150:
151:• for (final var child : contents)
152: {
153: builder.append(child.asString(charset)).append("\n");
154: }
155:
156: t = t.add("content", builder.toString());
157: // t = t.add("contextPath", siteProvider.get().getSite().getContextPath());
158: t = t.add("charset", charset.name());
159: // t = t.add("language", "");
160:
161: return t.render();
162: }
163:
164: private void loadTemplate() // FIXME: use TemplateHelper
165: throws IOException
166: {
167: // FIXME: this should be done only once...
168: Resource resource = null;
169:
170:• for (Class<?> clazz = getClass(); clazz.getSuperclass() != null; clazz = clazz.getSuperclass())
171: {
172: final var templateName = clazz.getSimpleName() + ".txt";
173: resource = new ClassPathResource(templateName, clazz);
174:
175:• if (resource.exists())
176: {
177: break;
178: }
179: }
180:
181: try
182: {
183:• if (resource == null)
184: {
185: throw new FileNotFoundException();
186: }
187:
188: try (final Reader r = new InputStreamReader(resource.getInputStream()))
189: {
190: final var charBuffer = CharBuffer.allocate((int)resource.contentLength());
191: final var length = r.read(charBuffer);
192: template = new String(charBuffer.array(), 0, length);
193: }
194: }
195: catch (FileNotFoundException e) // no specific template, fallback
196: {
197: log.warn("No template for {}, using default", getClass().getSimpleName());
198: template = "$content$\n";
199: }
200: }
201: }