Skip to content

Commit c8e392e

Browse files
committed
Introduce DefaultHtmlBuilder
to have a central and easy to use entry point for configuring the HTML rendering, and to hide the details of FlatHtml and IndentedHtml. Adds static methods in Indenter for easy changing the indent depth. This commits also adds @deprecated annotations to some methods and fields that should be removed in a future version of the library, namely - some static fields of Config - the public visibility of FlatHtml and IndentedHtml
1 parent 83866be commit c8e392e

File tree

13 files changed

+198
-37
lines changed

13 files changed

+198
-37
lines changed

library/src/main/java/j2html/Config.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import j2html.utils.Minifier;
88
import j2html.utils.TextEscaper;
99

10-
import java.util.Collections;
11-
1210
public class Config {
1311

1412
/**
1513
* Change this to configure text-escaping
1614
* For example, to disable escaping, do <code>{@code Config.textEscaper = text -> text;}</code>
15+
* @deprecated in favor of {@link j2html.rendering.DefaultHtmlBuilder#withTextEscaper(TextEscaper)}
1716
*/
17+
@Deprecated
1818
public static TextEscaper textEscaper = EscapeUtil::escape;
1919
/**
2020
* Change this to configure css-minification.
@@ -29,14 +29,18 @@ public class Config {
2929
/**
3030
* Change this to configure enable/disable closing empty tags
3131
* The default is to NOT close them
32+
* @deprecated in favor of {@link j2html.rendering.DefaultHtmlBuilder#withEmptyTagsClosed(boolean)}
3233
*/
34+
@Deprecated
3335
public static boolean closeEmptyTags = false;
34-
private static String FOUR_SPACES = " ";
36+
3537
/**
3638
* Change this to configure indentation when rendering formatted html
3739
* The default is four spaces
40+
* @deprecated in favor of {@link j2html.rendering.DefaultHtmlBuilder#withIndenter(Indenter)}
3841
*/
39-
public static Indenter indenter = (level, text) -> String.join("", Collections.nCopies(level, FOUR_SPACES)) + text;
42+
@Deprecated
43+
public static Indenter indenter = Indenter.defaults();
4044

4145

4246
private TextEscaper _textEscaper;
@@ -93,6 +97,10 @@ public Indenter indenter() {
9397
return _indenter;
9498
}
9599

100+
/**
101+
* @deprecated in favor of {@link j2html.rendering.DefaultHtmlBuilder#withTextEscaper(TextEscaper)}
102+
*/
103+
@Deprecated
96104
public Config withTextEscaper(TextEscaper textEscaper){
97105
Config copy = new Config(this);
98106
copy._textEscaper = textEscaper;
@@ -111,12 +119,20 @@ public Config withJsMinifier(Minifier jsMinifier){
111119
return copy;
112120
}
113121

122+
/**
123+
* @deprecated in favor of {@link j2html.rendering.DefaultHtmlBuilder#withEmptyTagsClosed(boolean)}
124+
*/
125+
@Deprecated
114126
public Config withEmptyTagsClosed(boolean closeEmptyTags){
115127
Config copy = new Config(this);
116128
copy._closeEmptyTags = closeEmptyTags;
117129
return copy;
118130
}
119131

132+
/**
133+
* @deprecated in favor of {@link j2html.rendering.DefaultHtmlBuilder#withIndenter(Indenter)}
134+
*/
135+
@Deprecated
120136
public Config withIndenter(Indenter indenter){
121137
Config copy = new Config(this);
122138
copy._indenter = indenter;
@@ -128,7 +144,7 @@ public Config withIndenter(Indenter indenter){
128144
CSSMin::compressCss,
129145
JSMin::compressJs,
130146
false,
131-
(level, text) -> String.join("", Collections.nCopies(level, FOUR_SPACES)) + text
147+
Indenter.defaults()
132148
);
133149

134150
public static final Config defaults() {
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package j2html.rendering;
2+
3+
import j2html.Config;
4+
import j2html.utils.Indenter;
5+
import j2html.utils.TextEscaper;
6+
7+
/**
8+
* Default entry point for constructing an {@link HtmlBuilder} instance.
9+
* Examples:
10+
*
11+
* <pre>
12+
* HtmlTag html = ...
13+
* Appendable out = ...
14+
* html.render(DefaultHtmlBuilder.into(out));
15+
* </pre>
16+
*
17+
* will write the HTML into {@code out} without any line breaks or indentation
18+
* using the default settings of {@link Config#defaults()}.
19+
* <p>
20+
*
21+
* <pre>
22+
* html.render(DefaultHtmlBuilder.indented(true).into(out));
23+
* </pre>
24+
*
25+
* will write the HTML into {@code out} with line breaks and indentation using
26+
* the default settings and the indenter {@link Indenter#defaults()}.
27+
* <p>
28+
*
29+
* <pre>
30+
* html.render(DefaultHtmlBuilder.indented(true).withIndenter(Indenter.with("\t")).into(out));
31+
* </pre>
32+
*
33+
* will use the tab character {@code \t} for indentation.
34+
* <p>
35+
* A different {@link TextEscaper} can be provided using
36+
* {@link #withTextEscaper(TextEscaper)}, a different setting for the closing of
37+
* empty tags can be applied with {@link #withEmptyTagsClosed(boolean)}.
38+
* <p>
39+
* There is also a convenience method {@link #inMemory()} for rendering the HTML
40+
* into a String:
41+
*
42+
* <pre>
43+
* String text = html.render(DefaultHtmlBuilder.with...().inMemory()).toString();
44+
* </pre>
45+
*/
46+
public class DefaultHtmlBuilder {
47+
48+
public static <A extends Appendable> HtmlBuilder<A> into(A out) {
49+
return new Configurer().into(out);
50+
}
51+
52+
public static HtmlBuilder<StringBuilder> inMemory() {
53+
return into(new StringBuilder());
54+
}
55+
56+
public static Configurer indented(boolean indented) {
57+
return new Configurer().indented(indented);
58+
}
59+
60+
public static Configurer withEmptyTagsClosed(boolean closeTags) {
61+
return new Configurer().withEmptyTagsClosed(closeTags);
62+
}
63+
64+
public static Configurer withTextEscaper(TextEscaper textEscaper) {
65+
return new Configurer().withTextEscaper(textEscaper);
66+
}
67+
68+
public static Configurer withIndenter(Indenter indenter) {
69+
return new Configurer().withIndenter(indenter);
70+
}
71+
72+
/**
73+
* @deprecated will be removed in a future version
74+
*/
75+
@Deprecated
76+
public static Configurer withConfig(Config config) {
77+
return new Configurer(config);
78+
}
79+
80+
public static class Configurer {
81+
private boolean indented;
82+
private Config config;
83+
84+
private Configurer() {
85+
this(Config.defaults());
86+
}
87+
88+
private Configurer(Config config) {
89+
this.config = config;
90+
this.indented = false;
91+
}
92+
93+
@SuppressWarnings("deprecation")
94+
public <A extends Appendable> HtmlBuilder<A> into(A out) {
95+
return this.indented ? IndentedHtml.into(out, this.config) : FlatHtml.into(out, this.config);
96+
}
97+
98+
public HtmlBuilder<StringBuilder> inMemory() {
99+
return into(new StringBuilder());
100+
}
101+
102+
public Configurer indented(boolean indented) {
103+
this.indented = indented;
104+
return this;
105+
}
106+
107+
@SuppressWarnings("deprecation")
108+
public Configurer withEmptyTagsClosed(boolean closeTags) {
109+
this.config = this.config.withEmptyTagsClosed(closeTags);
110+
return this;
111+
}
112+
113+
@SuppressWarnings("deprecation")
114+
public Configurer withTextEscaper(TextEscaper textEscaper) {
115+
this.config = this.config.withTextEscaper(textEscaper);
116+
return this;
117+
}
118+
119+
@SuppressWarnings("deprecation")
120+
public Configurer withIndenter(Indenter indenter) {
121+
this.config = this.config.withIndenter(indenter);
122+
return this;
123+
}
124+
}
125+
}

library/src/main/java/j2html/rendering/FlatHtml.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
* Composes HTML without any extra line breaks or indentation.
1010
*
1111
* @param <T> The type of the Appendable to which HTML will be appended.
12+
*
13+
* @deprecated in favor of {@link DefaultHtmlBuilder}
1214
*/
15+
// should be made package private in 2.0
1316
public class FlatHtml<T extends Appendable> implements HtmlBuilder<T> {
1417

1518
/**
@@ -20,6 +23,7 @@ public class FlatHtml<T extends Appendable> implements HtmlBuilder<T> {
2023
* @param <T> The type of the Appendable to which HTML will be appended.
2124
* @return An HtmlBuilder for flat HTML.
2225
*/
26+
@Deprecated
2327
public static final <T extends Appendable> FlatHtml<T> into(T out) {
2428
return new FlatHtml<>(out, Config.defaults());
2529
}
@@ -43,6 +47,7 @@ public static final <T extends Appendable> FlatHtml<T> into(T out, Config config
4347
*
4448
* @return An HtmlBuilder for flat HTML.
4549
*/
50+
@Deprecated
4651
public static final FlatHtml<StringBuilder> inMemory() {
4752
return into(new StringBuilder());
4853
}
@@ -53,6 +58,7 @@ public static final FlatHtml<StringBuilder> inMemory() {
5358
* @param config The Config which will specify text escapement, tag closing, etc.
5459
* @return An HtmlBuilder for flat HTML.
5560
*/
61+
@Deprecated
5662
public static final FlatHtml<StringBuilder> inMemory(Config config) {
5763
return into(new StringBuilder(), config);
5864
}

library/src/main/java/j2html/rendering/HtmlBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* Appendable, and support appending HTML-specific character
88
* sequences to that Appendable.
99
* <p>
10+
* This library provides configurable implementations for flat and
11+
* indented HTML, which can be obtained from the {@link DefaultHtmlBuilder}.
12+
* <p>
1013
* Note: HtmlBuilder extends Appendable for compatibility with
1114
* previous version of this library. This extension will probably be
1215
* removed in the future, so avoid relying on the deprecated methods

library/src/main/java/j2html/rendering/IndentedHtml.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
* Composes HTML with lines breaks and indentation between tags and text.
1313
*
1414
* @param <T> The type of the Appendable to which HTML will be appended.
15+
*
16+
* @deprecated in favor of {@link DefaultHtmlBuilder}
1517
*/
18+
// should be made package private in 2.0
1619
public class IndentedHtml<T extends Appendable> implements HtmlBuilder<T> {
1720

1821
/**
@@ -23,6 +26,7 @@ public class IndentedHtml<T extends Appendable> implements HtmlBuilder<T> {
2326
* @param <T> The type of the Appendable to which HTML will be appended.
2427
* @return An HtmlBuilder for indented HTML.
2528
*/
29+
@Deprecated
2630
public static final <T extends Appendable> IndentedHtml<T> into(T out) {
2731
return new IndentedHtml<>(out, Config.defaults());
2832
}
@@ -46,6 +50,7 @@ public static final <T extends Appendable> IndentedHtml<T> into(T out, Config co
4650
*
4751
* @return An HtmlBuilder for indented HTML.
4852
*/
53+
@Deprecated
4954
public static final IndentedHtml<StringBuilder> inMemory() {
5055
return into(new StringBuilder());
5156
}
@@ -57,6 +62,7 @@ public static final IndentedHtml<StringBuilder> inMemory() {
5762
* @param config The Config which will specify indentation, text escapement, tag closing, etc.
5863
* @return An HtmlBuilder for indented HTML.
5964
*/
65+
@Deprecated
6066
public static final IndentedHtml<StringBuilder> inMemory(Config config) {
6167
return into(new StringBuilder(), config);
6268
}

library/src/main/java/j2html/tags/ContainerTag.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
import j2html.Config;
44
import j2html.attributes.Attribute;
5-
import j2html.rendering.TagBuilder;
6-
import j2html.rendering.FlatHtml;
5+
import j2html.rendering.DefaultHtmlBuilder;
76
import j2html.rendering.HtmlBuilder;
8-
import j2html.rendering.IndentedHtml;
9-
7+
import j2html.rendering.TagBuilder;
108
import java.io.IOException;
119
import java.util.ArrayList;
1210
import java.util.List;
@@ -145,9 +143,10 @@ public int getNumChildren() {
145143
*
146144
* @return the rendered and formatted string
147145
*/
146+
@SuppressWarnings("deprecation")
148147
public String renderFormatted() {
149148
try {
150-
return render(IndentedHtml.into(new StringBuilder(), Config.global())).toString();
149+
return render(DefaultHtmlBuilder.withConfig(Config.global()).indented(true).inMemory()).toString();
151150
}catch (IOException e) {
152151
throw new RuntimeException(e.getMessage(), e);
153152
}
@@ -179,7 +178,7 @@ public <A extends Appendable> A render(HtmlBuilder<A> builder, Object model) thr
179178
public void renderModel(Appendable writer, Object model) throws IOException {
180179
HtmlBuilder<?> builder = (writer instanceof HtmlBuilder)
181180
? (HtmlBuilder<?>) writer
182-
: FlatHtml.into(writer, Config.global());
181+
: DefaultHtmlBuilder.withConfig(Config.global()).into(writer);
183182

184183
render(builder, model);
185184
}

library/src/main/java/j2html/tags/EmptyTag.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import j2html.Config;
44
import j2html.attributes.Attribute;
5-
import j2html.rendering.TagBuilder;
6-
import j2html.rendering.FlatHtml;
5+
import j2html.rendering.DefaultHtmlBuilder;
76
import j2html.rendering.HtmlBuilder;
8-
7+
import j2html.rendering.TagBuilder;
98
import java.io.IOException;
109

1110
public class EmptyTag<T extends EmptyTag<T>> extends Tag<T> {
@@ -35,7 +34,7 @@ public <A extends Appendable> A render(HtmlBuilder<A> builder, Object model) thr
3534
public void renderModel(Appendable writer, Object model) throws IOException {
3635
HtmlBuilder<?> builder = (writer instanceof HtmlBuilder)
3736
? (HtmlBuilder<?>) writer
38-
: FlatHtml.into(writer, Config.global());
37+
: DefaultHtmlBuilder.withConfig(Config.global()).into(writer);
3938

4039
render(builder, model);
4140
}

library/src/main/java/j2html/tags/Renderable.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package j2html.tags;
22

33
import j2html.Config;
4-
import j2html.rendering.FlatHtml;
4+
import j2html.rendering.DefaultHtmlBuilder;
55
import j2html.rendering.HtmlBuilder;
6-
76
import java.io.IOException;
87
import java.io.UncheckedIOException;
98

@@ -42,9 +41,10 @@ default <T extends Appendable> T render(HtmlBuilder<T> builder) throws IOExcepti
4241
* Create a StringBuilder and use it to render the Renderable and it's
4342
* children
4443
*/
44+
@SuppressWarnings("deprecation")
4545
default String render() {
4646
try {
47-
return render(FlatHtml.into(new StringBuilder(), Config.global())).toString();
47+
return render(DefaultHtmlBuilder.withConfig(Config.global()).inMemory()).toString();
4848
} catch (IOException e) {
4949
throw new UncheckedIOException(e);
5050
}
@@ -60,7 +60,7 @@ default void render(Appendable writer) throws IOException {
6060
if (writer instanceof HtmlBuilder) {
6161
render((HtmlBuilder<? extends Appendable>) writer);
6262
} else {
63-
render(FlatHtml.into(writer, Config.global()));
63+
render(DefaultHtmlBuilder.withConfig(Config.global()).into(writer));
6464
}
6565
}
6666

library/src/main/java/j2html/tags/Text.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package j2html.tags;
22

33
import j2html.Config;
4-
import j2html.rendering.FlatHtml;
4+
import j2html.rendering.DefaultHtmlBuilder;
55
import j2html.rendering.HtmlBuilder;
6-
76
import java.io.IOException;
87

98
public class Text extends DomContent {
@@ -25,7 +24,7 @@ public <T extends Appendable> T render(HtmlBuilder<T> builder, Object model) thr
2524
public void renderModel(Appendable writer, Object model) throws IOException {
2625
HtmlBuilder<?> builder = (writer instanceof HtmlBuilder)
2726
? (HtmlBuilder<?>) writer
28-
: FlatHtml.into(writer, Config.global());
27+
: DefaultHtmlBuilder.withConfig(Config.global()).into(writer);
2928

3029
render(builder, model);
3130
}

0 commit comments

Comments
 (0)