Package: TextResourcePropertyResolver
TextResourcePropertyResolver
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TextResourcePropertyResolver(ResourceFile) |
|
|
|
|
|
||||||||||||||||||||
findLocalizedFile(String) |
|
|
|
|
|
||||||||||||||||||||
resolveProperty(Id, Key) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
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.model;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import javax.inject.Provider;
32: import java.util.List;
33: import java.io.IOException;
34: import java.nio.charset.Charset;
35: import org.springframework.beans.factory.annotation.Configurable;
36: import it.tidalwave.util.Id;
37: import it.tidalwave.util.Key;
38: import it.tidalwave.util.NotFoundException;
39: import it.tidalwave.northernwind.core.model.RequestLocaleManager;
40: import it.tidalwave.northernwind.core.model.ResourceFile;
41: import it.tidalwave.northernwind.core.model.ResourceProperties;
42: import lombok.extern.slf4j.Slf4j;
43:
44: /***********************************************************************************************************************
45: *
46: * This class tries to resolve a property by loading it from a text file. The file is searched in the given folder
47: * with the same name of the property, localized.If the searched property is {@code fullText}, and the managed locales
48: * are {@code en} and {@code it}, then the following files are searched for:
49: *
50: * <ul>
51: * <li>{@code fullText.xhtml}</li>
52: * <li>{@code fullText.html}</li>
53: * <li>{@code fullText.xml}</li>
54: * <li>{@code fullText.txt}</li>
55: * <li>{@code fullText_en.xhtml}</li>
56: * <li>{@code fullText_en.html}</li>
57: * <li>{@code fullText_en.xml}</li>
58: * <li>{@code fullText_en.txt}</li>
59: * <li>{@code fullText_it.xhtml}</li>
60: * <li>{@code fullText_it.html}</li>
61: * <li>{@code fullText_it.xml}</li>
62: * <li>{@code fullText_it.txt}</li>
63: * </ul>
64: *
65: * Files are pre-processed through the {@link FilterSetExpander}. If the file MIME type is XHTML, UTF-8 is used for the
66: * encoding, otherwise the default charset is used.
67: *
68: * HTML support is a legacy and will be removed in a future version.
69: *
70: * @author Fabrizio Giudici
71: *
72: **********************************************************************************************************************/
73: @Configurable @Slf4j
74: public class TextResourcePropertyResolver implements ResourceProperties.PropertyResolver
75: {
76: private static final List<String> EXTENSIONS = List.of(".xhtml", ".html", ".xml", ".txt");
77:
78: @Inject
79: private RequestLocaleManager localeRequestManager;
80:
81: @Inject
82: private Provider<FilterSetExpander> filterSetExpander;
83:
84: @Nonnull
85: private final ResourceFile folder;
86:
87: /*******************************************************************************************************************
88: *
89: *
90: *
91: ******************************************************************************************************************/
92: public TextResourcePropertyResolver (@Nonnull final ResourceFile folder)
93:• {
94: this.folder = folder;
95:• }
96:
97: /*******************************************************************************************************************
98: *
99: * {@inheritDoc}
100: *
101: ******************************************************************************************************************/
102: @Nonnull
103: @Override @SuppressWarnings("unchecked")
104: public <Type> Type resolveProperty (@Nonnull final Id propertyGroupId, @Nonnull final Key<Type> propertyName)
105: throws NotFoundException, IOException
106: {
107: log.trace("resolveProperty({})", propertyName);
108:
109: final var propertyFile = findLocalizedFile(propertyName.stringValue());
110: log.trace(">>>> reading from {}", propertyFile.getPath());
111: final var mimeType = propertyFile.getMimeType();
112:• final var charset = "application/xhtml+xml".equals(mimeType) ? "UTF-8" : Charset.defaultCharset().name();
113:
114: try
115: {
116: return (Type)filterSetExpander.get().filter(propertyFile.asText(charset), mimeType);
117: }
118: catch (RuntimeException e) // FIXME: introduce a FilterException
119: {
120: throw new IOException(e);
121: }
122: }
123:
124: /*******************************************************************************************************************
125: *
126: *
127: ******************************************************************************************************************/
128: @Nonnull
129: private ResourceFile findLocalizedFile (@Nonnull final String fileName)
130: throws NotFoundException
131: {
132: log.trace("findLocalizedFile({})", fileName);
133: final var fileNamesNotFound = new StringBuilder();
134: var separator = "";
135:
136:• for (final var localeSuffix : localeRequestManager.getLocaleSuffixes())
137: {
138:• for (final var extension : EXTENSIONS)
139: {
140: final var localizedFileName = fileName + localeSuffix + extension;
141:
142: try
143: {
144: return folder.findChildren().withName(localizedFileName).result();
145: }
146: catch (NotFoundException e)
147: {
148: // continue
149: }
150:
151: fileNamesNotFound.append(separator);
152: fileNamesNotFound.append(localizedFileName);
153: separator = ",";
154: }
155: }
156:
157: throw new NotFoundException(String.format("%s/{%s}", folder.getPath().asString(), fileNamesNotFound));
158: }
159: }