Package: ResourcePropertiesBinder
ResourcePropertiesBinder
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ResourcePropertiesBinder(ResourceProperties) |
|
|
|
|
|
||||||||||||||||||||
bind(Key, BoundProperty, PropertyBinder.UpdateCallback) |
|
|
|
|
|
||||||||||||||||||||
createBoundDocument(Key, PropertyBinder.UpdateCallback) |
|
|
|
|
|
||||||||||||||||||||
lambda$bind$0(PropertyBinder.UpdateCallback, Key, BoundProperty, PropertyChangeEvent) |
|
|
|
|
|
||||||||||||||||||||
lambda$createBoundDocument$1(PropertyBinder.UpdateCallback, Key, HtmlDocument, String) |
|
|
|
|
|
||||||||||||||||||||
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.rca.ui.contenteditor.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.io.BufferedReader;
31: import java.io.IOException;
32: import java.io.InputStreamReader;
33: import java.nio.charset.StandardCharsets;
34: import it.tidalwave.util.annotation.VisibleForTesting;
35: import org.springframework.core.io.ClassPathResource;
36: import it.tidalwave.util.Key;
37: import it.tidalwave.role.ui.BoundProperty;
38: import it.tidalwave.dci.annotation.DciRole;
39: import it.tidalwave.northernwind.core.model.ResourceProperties;
40: import it.tidalwave.northernwind.rca.embeddedserver.EmbeddedServer;
41: import it.tidalwave.northernwind.rca.ui.contenteditor.spi.PropertyBinder;
42: import lombok.RequiredArgsConstructor;
43:
44: /***********************************************************************************************************************
45: *
46: * @author Fabrizio Giudici
47: *
48: **********************************************************************************************************************/
49:•@DciRole(datumType = ResourceProperties.class) @RequiredArgsConstructor
50: public class ResourcePropertiesBinder implements PropertyBinder
51: {
52: private static final String EDITOR_TEMPLATE =
53: "it/tidalwave/northernwind/rca/ui/contenteditor/spi/EditorTemplate.xhtml";
54:
55: @Nonnull
56: private final ResourceProperties properties;
57:
58: @VisibleForTesting static final String EDITOR_PROLOG;
59:
60: @VisibleForTesting static final String EDITOR_EPILOG;
61:
62: /*******************************************************************************************************************
63: *
64: *
65: *
66: ******************************************************************************************************************/
67: static
68: {
69: try (final BufferedReader r = new BufferedReader(new InputStreamReader(
70: new ClassPathResource(EDITOR_TEMPLATE).getInputStream(), StandardCharsets.UTF_8)))
71: {
72: final StringBuilder prologBuilder = new StringBuilder();
73: final StringBuilder epilogBuilder = new StringBuilder();
74: boolean prolog = true;
75:
76:• for (String line = r.readLine(); line != null; line = r.readLine())
77: {
78:• if ("<!-- split here -->".equals(line.trim()))
79: {
80: prolog = false;
81: continue;
82: }
83:
84:• (prolog ? prologBuilder : epilogBuilder).append(line).append("\n");
85: }
86:
87: EDITOR_PROLOG = prologBuilder.toString();
88: EDITOR_EPILOG = epilogBuilder.toString();
89: }
90: catch (IOException e)
91: {
92: throw new ExceptionInInitializerError(e);
93: }
94: }
95:
96: /*******************************************************************************************************************
97: *
98: * {@inheritDoc}
99: *
100: ******************************************************************************************************************/
101: @Override
102: public <T> void bind (@Nonnull final Key<T> propertyName,
103: @Nonnull final BoundProperty<T> boundProperty,
104: @Nonnull final UpdateCallback callback)
105: {
106: properties.getProperty(propertyName).ifPresent(boundProperty::set);
107: boundProperty.addPropertyChangeListener(
108: event -> callback.notify(properties.withProperty(propertyName, boundProperty.get())));
109: }
110:
111: /*******************************************************************************************************************
112: *
113: * {@inheritDoc}
114: *
115: ******************************************************************************************************************/
116: @Override @Nonnull
117: public EmbeddedServer.Document createBoundDocument (@Nonnull final Key<String> propertyName,
118: @Nonnull final UpdateCallback callback)
119: {
120: final String text = properties.getProperty(propertyName).orElse("");
121: final HtmlDocument originalDocument = HtmlDocument.createFromText(text);
122: final HtmlDocument editableDocument = originalDocument.withProlog(EDITOR_PROLOG)
123: .withEpilog(EDITOR_EPILOG);
124: // FIXME: mime type - XHTML?
125: return new EmbeddedServer.Document().withMimeType("text/html")
126: .withContent(editableDocument.asString())
127: .withUpdateListener(
128: text1 -> callback.notify(properties.withProperty(propertyName,
129: originalDocument.withBody(text1).asString())));
130: }
131: }