|
| 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 | +} |
0 commit comments