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