Package: DefaultInheritanceHelper
DefaultInheritanceHelper
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultInheritanceHelper() |
|
|
|
|
|
||||||||||||||||||||
getFiles(String, String, ResourceFile, List) |
|
|
|
|
|
||||||||||||||||||||
getHierarchyFolders(ResourceFile) |
|
|
|
|
|
||||||||||||||||||||
getInheritedPropertyFiles(ResourceFile, Locale, String) |
|
|
|
|
|
||||||||||||||||||||
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 java.util.ArrayList;
31: import java.util.Collections;
32: import java.util.List;
33: import java.util.Locale;
34: import it.tidalwave.util.NotFoundException;
35: import it.tidalwave.northernwind.core.model.ResourceFile;
36: import lombok.extern.slf4j.Slf4j;
37:
38: /***********************************************************************************************************************
39: *
40: * Implements the default inheritance policy for properties. The sequence of loaded file is:
41: *
42: * <ul>
43: * <li>parent default</li>
44: * <li>parent localized</li>
45: * <li>this level default</li>
46: * <li>this level localized</li>
47: * <li>override default</li>
48: * <li>override localized</li>
49: * </ul>
50: *
51: * @author Fabrizio Giudici
52: *
53: **********************************************************************************************************************/
54: @Slf4j
55: public class DefaultInheritanceHelper implements InheritanceHelper
56: {
57: /*******************************************************************************************************************
58: *
59: ******************************************************************************************************************/
60: @Override @Nonnull
61: public List<ResourceFile> getInheritedPropertyFiles (@Nonnull final ResourceFile folder,
62: @Nonnull final Locale locale,
63: @Nonnull final String propertyFileName)
64: {
65: log.trace("getInheritedPropertyFiles({}, {})", folder.getPath(), propertyFileName);
66:
67: final List<String> suffixes = new ArrayList<>();
68: suffixes.add("");
69: suffixes.add("_" + locale.getLanguage());
70: final List<ResourceFile> files = new ArrayList<>();
71:
72:• for (final var parentFolder : getHierarchyFolders(folder))
73: {
74: files.addAll(getFiles("", propertyFileName, parentFolder, suffixes));
75: }
76:
77: files.addAll(getFiles("Override", propertyFileName, folder, suffixes));
78:
79: log.trace(">>>> property file candidates: {}", files);
80:
81: return files;
82: }
83:
84: /*******************************************************************************************************************
85: *
86: ******************************************************************************************************************/
87: private static List<ResourceFile> getFiles (@Nonnull final String prefix,
88: @Nonnull final String propertyFileName,
89: @Nonnull final ResourceFile folder,
90: @Nonnull final List<String> suffixes)
91: {
92: final List<ResourceFile> files = new ArrayList<>();
93:
94:• for (final var localeSuffix : suffixes)
95: {
96: final var fileName = prefix + propertyFileName + localeSuffix + ".xml";
97: log.trace(">>>> probing {} ...", folder.getPath().asString() + "/" + fileName);
98:
99: try
100: {
101: files.add(folder.findChildren().withName(fileName).result());
102: }
103: catch (NotFoundException e)
104: {
105: // ok. do nothing
106: }
107: }
108:
109: return files;
110: }
111:
112: /*******************************************************************************************************************
113: *
114: ******************************************************************************************************************/
115: @Nonnull
116: private static List<ResourceFile> getHierarchyFolders (@Nonnull final ResourceFile folder)
117: {
118: final List<ResourceFile> folders = new ArrayList<>();
119:
120:• for (var parent = folder; parent.getParent() != null; parent = parent.getParent()) // TODO: refactor with recursion?
121: {
122: folders.add(parent);
123: }
124:
125: Collections.reverse(folders);
126:
127: return folders;
128: }
129: }