Skip to contentMethod: createFromText(String)
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 javax.annotation.concurrent.Immutable;
31: import it.tidalwave.northernwind.rca.ui.contenteditor.spi.XhtmlNormalizer;
32: import lombok.EqualsAndHashCode;
33: import lombok.Getter;
34: import lombok.RequiredArgsConstructor;
35: import lombok.ToString;
36: import lombok.With;
37: import lombok.extern.slf4j.Slf4j;
38: import static lombok.AccessLevel.PACKAGE;
39:
40: /***********************************************************************************************************************
41: *
42: * A container for HTML text that allow substitution of prolog and epilog in order to prepare a HTML document for
43: * editing. It keeps an internal representation where the prolog, the body and the epilog are separately stored.
44: *
45: * @author Fabrizio Giudici
46: *
47: **********************************************************************************************************************/
48: @Immutable
49: @RequiredArgsConstructor(access = PACKAGE)
50: @EqualsAndHashCode @ToString @Slf4j
51: public class HtmlDocument
52: {
53: @Getter @With @Nonnull
54: private final String prolog;
55:
56: @Getter @With @Nonnull
57: private final String body;
58:
59: @Getter @With @Nonnull
60: private final String epilog;
61:
62: @Nonnull
63: private final XhtmlNormalizer normalizer;
64:
65: /*******************************************************************************************************************
66: *
67: *
68: *
69: ******************************************************************************************************************/
70: enum State
71: {
72: PROLOG
73: {
74: @Override
75: State process (@Nonnull final String line,
76: @Nonnull final StringBuilder prologBuilder,
77: @Nonnull final StringBuilder bodyBuilder,
78: @Nonnull final StringBuilder epilogBuilder)
79: {
80: prologBuilder.append(line).append("\n");
81: return line.contains("<body") ? BODY : PROLOG;
82: }
83: },
84:
85: BODY
86: {
87: @Override
88: State process (@Nonnull final String line,
89: @Nonnull final StringBuilder prologBuilder,
90: @Nonnull final StringBuilder bodyBuilder,
91: @Nonnull final StringBuilder epilogBuilder)
92: {
93: final boolean containsEndBody = line.contains("</body");
94: (containsEndBody ? epilogBuilder : bodyBuilder).append(line).append("\n");
95: return containsEndBody ? EPILOG : BODY;
96: }
97: },
98:
99: EPILOG
100: {
101: @Override
102: State process (@Nonnull final String line,
103: @Nonnull final StringBuilder prologBuilder,
104: @Nonnull final StringBuilder bodyBuilder,
105: @Nonnull final StringBuilder epilogBuilder)
106: {
107: epilogBuilder.append(line).append("\n");
108: return EPILOG;
109: }
110: };
111:
112: abstract State process (@Nonnull String line,
113: @Nonnull StringBuilder prologBuilder,
114: @Nonnull StringBuilder bodyBuilder,
115: @Nonnull StringBuilder epilogBuilder);
116: }
117:
118: /*******************************************************************************************************************
119: *
120: * Creates a new document from a string.
121: *
122: * @param text the string
123: * @return the document
124: *
125: ******************************************************************************************************************/
126: @Nonnull
127: public static HtmlDocument createFromText (@Nonnull final String text)
128: {
129: final StringBuilder prologBuilder = new StringBuilder();
130: final StringBuilder bodyBuilder = new StringBuilder();
131: final StringBuilder epilogBuilder = new StringBuilder();
132:
133: State state = State.PROLOG;
134:
135:• for (final String line : text.split("\\n"))
136: {
137: state = state.process(line, prologBuilder, bodyBuilder, epilogBuilder);
138: }
139:
140: return new HtmlDocument(prologBuilder.toString(),
141: bodyBuilder.toString(),
142: epilogBuilder.toString(),
143: new JSoupXhtmlNormalizer());
144: }
145:
146: /*******************************************************************************************************************
147: *
148: * Returns the contents as a string, after having being normalised.
149: *
150: * @return the string
151: *
152: ******************************************************************************************************************/
153: @Nonnull
154: public String asString()
155: {
156: return normalizer.asNormalizedString(prolog + body + epilog);
157: }
158: }
159: