Package: DefaultContent
DefaultContent
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultContent(Content.Builder) |
|
|
|
|
|
||||||||||||||||||||
deAccent(String) |
|
|
|
|
|
||||||||||||||||||||
findChildren() |
|
|
|
|
|
||||||||||||||||||||
getDefaultExposedUri() |
|
|
|
|
|
||||||||||||||||||||
getExposedUri() |
|
|
|
|
|
||||||||||||||||||||
getProperties() |
|
|
|
|
|
||||||||||||||||||||
lambda$getDefaultExposedUri$0(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.core.impl.model;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import java.text.Normalizer;
32: import java.util.List;
33: import java.util.Optional;
34: import java.util.regex.Pattern;
35: import org.springframework.beans.factory.annotation.Configurable;
36: import it.tidalwave.util.Finder;
37: import it.tidalwave.util.Key;
38: import it.tidalwave.northernwind.core.model.Content;
39: import it.tidalwave.northernwind.core.model.RequestContext;
40: import it.tidalwave.northernwind.core.model.ResourcePath;
41: import it.tidalwave.northernwind.core.model.ResourceProperties;
42: import it.tidalwave.northernwind.core.model.spi.ContentSupport;
43: import lombok.RequiredArgsConstructor;
44: import lombok.ToString;
45: import lombok.experimental.Delegate;
46: import lombok.extern.slf4j.Slf4j;
47:
48: // FIXME: reimplement with an Aspect
49: @RequiredArgsConstructor
50: class ResourcePropertiesDelegate implements ResourceProperties
51: {
52: @Nonnull
53: private final RequestContext requestContext;
54:
55: @Nonnull
56: private final Content content;
57:
58: interface Exclusions
59: {
60: public <T> Optional<T> getProperty (Key<? extends T> key);
61: public <T> Optional<T> getProperty (List<Key<? extends T>> keys);
62: }
63:
64: @Nonnull @Delegate(types=ResourceProperties.class, excludes=Exclusions.class)
65: private final ResourceProperties delegate;
66:
67: @Nonnull
68: @Override
69: public <T> Optional<T> getProperty (@Nonnull final Key<? extends T> key)
70: {
71: try
72: {
73: requestContext.setContent(content);
74: return delegate.getProperty(key);
75: }
76: finally
77: {
78: requestContext.clearContent();
79: }
80: }
81: }
82:
83: /***********************************************************************************************************************
84: *
85: * A piece of content to be composed into a {@code Node}.
86: *
87: * @author Fabrizio Giudici
88: *
89: **********************************************************************************************************************/
90: @Configurable @Slf4j @ToString(callSuper = true, exclude="requestContext")
91: /* package */ class DefaultContent extends ContentSupport
92: {
93: @Inject
94: private RequestContext requestContext;
95:
96: /*******************************************************************************************************************
97: *
98: * Creates a new {@code DefaultContent} with the given {@link Content.Builder}.
99: *
100: * @param builder the builder
101: *
102: ******************************************************************************************************************/
103: public DefaultContent (@Nonnull final Content.Builder builder)
104: {
105:• super(builder);
106:• }
107:
108: /*******************************************************************************************************************
109: *
110: * {@inheritDoc}
111: *
112: ******************************************************************************************************************/
113: @Override @Nonnull
114: public Finder<Content> findChildren()
115: {
116: return new PathFinderSupport<>(this);
117: }
118:
119: /*******************************************************************************************************************
120: *
121: * {@inheritDoc}
122: *
123: ******************************************************************************************************************/
124: @Override @Nonnull
125: public Optional<ResourcePath> getExposedUri()
126: {
127: final var exposedUri = getProperty(P_EXPOSED_URI);
128:• return exposedUri.isPresent() ? exposedUri.map(ResourcePath::of) : getDefaultExposedUri();
129: // return getProperty(P_EXPOSED_URI).map(ResourcePath::of).orElseGet(() -> getDefaultExposedUri()); TODO
130: }
131:
132: /*******************************************************************************************************************
133: *
134: * {@inheritDoc}
135: *
136: ******************************************************************************************************************/
137: @Nonnull
138: private Optional<ResourcePath> getDefaultExposedUri()
139: {
140: return getResource().getProperty(P_TITLE)
141: .map(DefaultContent::deAccent)
142: .map(t -> t.replaceAll(" ", "-")
143: .replaceAll(",", "")
144: .replaceAll("\\.", "")
145: .replaceAll(";", "")
146: .replaceAll("/", "")
147: .replaceAll("!", "")
148: .replaceAll("\\?", "")
149: .replaceAll(":", "")
150: .replaceAll("[^\\w-]*", ""))
151: .map(String::toLowerCase)
152: .map(ResourcePath::of);
153: }
154:
155: /*******************************************************************************************************************
156: *
157: * See http://stackoverflow.com/questions/1008802/converting-symbols-accent-letters-to-english-alphabet
158: *
159: ******************************************************************************************************************/
160: @Nonnull
161: public static String deAccent (@Nonnull final String string)
162: {
163: final var nfdNormalizedString = Normalizer.normalize(string, Normalizer.Form.NFD);
164: final var pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
165: return pattern.matcher(nfdNormalizedString).replaceAll("");
166: }
167:
168: @Override @Nonnull
169: public ResourceProperties getProperties()
170: {
171: return new ResourcePropertiesDelegate(requestContext, this, getResource().getProperties());
172: }
173: }