Skip to content

Package: St4TemplateFactory

St4TemplateFactory

nameinstructionbranchcomplexitylinemethod
St4TemplateFactory(Class, Site)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getEmbeddedTemplate(String)
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getTemplate(Optional, String)
M: 0 C: 29
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getTemplate(ResourcePath)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$getTemplate$0(String)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$getTemplate$1(Content)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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.core.impl.text;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Optional;
31: import java.io.IOException;
32: import java.io.InputStreamReader;
33: import java.io.Reader;
34: import org.springframework.core.io.ClassPathResource;
35: import org.springframework.core.io.Resource;
36: import org.stringtemplate.v4.ST;
37: import it.tidalwave.northernwind.core.model.ResourcePath;
38: import it.tidalwave.northernwind.core.model.Site;
39: import it.tidalwave.northernwind.core.model.Template;
40: import lombok.RequiredArgsConstructor;
41: import lombok.extern.slf4j.Slf4j;
42: import static it.tidalwave.northernwind.core.model.Content.*;
43:
44: /***********************************************************************************************************************
45: *
46: * A factory of {@link Template} implementations based on StringTemplate ({@link ST}.
47: *
48: * @author Fabrizio Giudici
49: *
50: **********************************************************************************************************************/
51: @RequiredArgsConstructor @Slf4j
52: public class St4TemplateFactory
53: {
54: @Nonnull
55: private final Class<?> clazz;
56:
57: @Nonnull
58: private final Site site;
59:
60: /*******************************************************************************************************************
61: *
62: *
63: *
64: ******************************************************************************************************************/
65: @Nonnull
66: public Template getTemplate (@Nonnull final Optional<? extends ResourcePath> templatePath,
67: @Nonnull final String embeddedResourceName)
68: {
69: log.debug("getTemplate({}, {})", templatePath, embeddedResourceName);
70: final var text = templatePath.flatMap(this::getTemplate)
71: .orElseGet(() -> getEmbeddedTemplate(embeddedResourceName));
72:• final var delimiter = embeddedResourceName.endsWith(".xslt") ? '%' : '$';
73: // TODO: use a cache. Implement it on Site (as a generic cache), so it gets resetted when the Site is reset.
74: return new St4Template(text, delimiter);
75: }
76:
77: /*******************************************************************************************************************
78: *
79: *
80: *
81: ******************************************************************************************************************/
82: @Nonnull
83: public Optional<String> getTemplate (@Nonnull final ResourcePath templatePath)
84: {
85: log.debug("getTemplate({})", templatePath);
86: return site.find(_Content_).withRelativePath(templatePath).optionalResult().flatMap(c -> c.getProperty(P_TEMPLATE));
87: }
88:
89: /*******************************************************************************************************************
90: *
91: * Gets an embedded, default template with the given name. The template file should be in the classpath, in the same
92: * package as the owner class.
93: *
94: * @param fileName the name of the file
95: * @return the template contents
96: * @throws RuntimeException if the template can't be loaded
97: *
98: ******************************************************************************************************************/
99: @Nonnull
100: /* visible for testing */ String getEmbeddedTemplate (@Nonnull final String fileName)
101: {
102: final var packagePath = clazz.getPackage().getName().replace('.', '/');
103: final Resource resource = new ClassPathResource("/" + packagePath + "/" + fileName);
104:
105: try (final Reader r = new InputStreamReader(resource.getInputStream()))
106: {
107: final var buffer = new char[(int)resource.contentLength()];
108: r.read(buffer);
109: return new String(buffer);
110: }
111: catch (IOException e)
112: {
113: throw new RuntimeException("Missing resource: " + fileName, e);
114: }
115: }
116: }