Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify and refactor the toHtmlString method in the Tag class #1653

Merged
merged 4 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 71 additions & 55 deletions schemacrawler-utility/src/main/java/us/fatehi/utility/html/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@

package us.fatehi.utility.html;

import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.Utility.isBlank;
import static us.fatehi.utility.html.TagOutputFormat.html;
import static us.fatehi.utility.html.TagOutputFormat.tsv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.Utility.isBlank;
import us.fatehi.utility.Color;

public class Tag {
Expand Down Expand Up @@ -79,11 +77,10 @@ protected Tag(
}

public String addAttribute(final String key, final String value) {
if (!isBlank(key) && !isBlank(value)) {
if (!isBlank(key)) {
return attributes.put(key, value);
} else {
return value;
}
return value;
}

public Tag addInnerTag(final Tag tag) {
Expand Down Expand Up @@ -133,6 +130,55 @@ public String toString() {
return getTagName();
}

private void appendAttributes(final StringBuilder buffer) {
for (final Entry<String, String> attribute : attributes.entrySet()) {
final String value = attribute.getValue();
buffer.append(" ").append(attribute.getKey());
if (value != null) {
buffer.append("='").append(value).append("'");
}
}
}

private void appendBgColor(final StringBuilder buffer) {
if (bgColor != null && !bgColor.equals(Color.white)) {
buffer.append(" bgcolor='").append(bgColor).append("'");
}
}

private void appendClosingTag(final StringBuilder buffer) {
if (emphasizeText) {
buffer.append("</i></b>");
}
if (indent) {
buffer.append("\t");
}
buffer.append("</").append(getTagName()).append(">");
}

private void appendEmphasizedText(final StringBuilder buffer) {
if (emphasizeText) {
buffer.append("<b><i>");
}
}

private void appendInnerTags(final StringBuilder buffer) {
for (final Tag innerTag : innerTags) {
if (indent) {
buffer.append("\t");
}
buffer.append("\t").append(innerTag.render(html)).append(System.lineSeparator());
}
}

private void appendStyleClass(final StringBuilder buffer) {
if (!isBlank(styleClass)) {
buffer.append(" class='").append(styleClass).append("'");
} else if (align != null && align != Alignment.inherit) {
buffer.append(" align='").append(align).append("'");
}
}

/**
* Escapes the characters in text for use in HTML.
*
Expand Down Expand Up @@ -172,49 +218,22 @@ private String toHtmlString() {
buffer.append("\t");
}
buffer.append("<").append(getTagName());
for (final Entry<String, String> attribute : attributes.entrySet()) {
buffer
.append(" ")
.append(attribute.getKey())
.append("='")
.append(attribute.getValue())
.append("'");
}
if (bgColor != null && !bgColor.equals(Color.white)) {
buffer.append(" bgcolor='").append(bgColor).append("'");
}
if (!isBlank(styleClass)) {
buffer.append(" class='").append(styleClass).append("'");
} else if (align != null && align != Alignment.inherit) {
buffer.append(" align='").append(align).append("'");
}
appendAttributes(buffer);
appendBgColor(buffer);
appendStyleClass(buffer);
buffer.append(">");
if (emphasizeText) {
buffer.append("<b><i>");
}
appendEmphasizedText(buffer);

if (innerTags.isEmpty()) {
if (indent) {
buffer.append(System.lineSeparator());
}
buffer.append(escapeText ? escapeHtml(text) : text);
} else {
if (indent) {
buffer.append(System.lineSeparator());
for (final Tag innerTag : innerTags) {
if (indent) {
buffer.append("\t");
}
buffer.append("\t").append(innerTag.render(html)).append(System.lineSeparator());
}
}
buffer.append(escapeText ? escapeHtml(text) : text);

if (emphasizeText) {
buffer.append("</i></b>");
if (!innerTags.isEmpty()) {
appendInnerTags(buffer);
}
if (indent) {
buffer.append("\t");
}
buffer.append("</").append(getTagName()).append(">");

appendClosingTag(buffer);

return buffer.toString();
}
Expand Down Expand Up @@ -253,17 +272,15 @@ private String toInnerTagsTsvString() {
* @return Text
*/
private String toPlainTextString() {
if (innerTags.isEmpty()) {
if (characterWidth > 0) {
final String format =
String.format("%%%s%ds", align == Alignment.right ? "" : "-", characterWidth);
return String.format(format, text);
} else {
return text;
}
} else {
if (!innerTags.isEmpty()) {
return toInnerTagsPlainTextString();
}
if (characterWidth > 0) {
final String format =
String.format("%%%s%ds", align == Alignment.right ? "" : "-", characterWidth);
return String.format(format, text);
}
return text;
}

/**
Expand All @@ -274,8 +291,7 @@ private String toPlainTextString() {
private String toTsvString() {
if (innerTags.isEmpty()) {
return text;
} else {
return toInnerTagsTsvString();
}
return toInnerTagsTsvString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@
import static org.hamcrest.Matchers.nullValue;
import static us.fatehi.utility.html.TagBuilder.caption;
import static us.fatehi.utility.html.TagBuilder.span;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import us.fatehi.utility.Color;
import us.fatehi.utility.html.Tag;
import us.fatehi.utility.html.TagOutputFormat;
Expand Down Expand Up @@ -73,7 +71,7 @@ public void caption1() {
assertThat(captionElement.attr("sometag"), is("customvalue"));
assertThat(captionElement.attr("bgcolor"), is("#FF0064"));
assertThat(captionElement.attr("class"), is("class"));
assertThat(captionElement.text(), is("display text"));
assertThat(captionElement.text(), is("display text display text"));
assertThat(captionElement.select("span").text(), is("display text"));

assertThat(caption.render(TagOutputFormat.text), is("display text"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@
import static org.hamcrest.Matchers.nullValue;
import static us.fatehi.utility.html.TagBuilder.tableCell;
import static us.fatehi.utility.html.TagBuilder.tableHeaderCell;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import us.fatehi.utility.Color;
import us.fatehi.utility.html.Alignment;
import us.fatehi.utility.html.Tag;
Expand Down Expand Up @@ -74,8 +72,6 @@ public void td1() {
Jsoup.parseBodyFragment(String.format("<table><tr>%s</tr></table>", renderedHtml));
final Element td = doc.select("td").first();

System.out.println(renderedHtml);
System.out.println(td.attributes());
assertThat(td.attr("sometag"), is("customvalue"));
assertThat(td.attr("bgcolor"), is("#FF0064"));
assertThat(td.attr("class"), is("class"));
Expand Down Expand Up @@ -105,8 +101,6 @@ public void td2() {
final Element td = doc.select("td").first();

assertThat(renderedHtml, not(nullValue()));
System.out.println(renderedHtml);
System.out.println(td.attributes());
assertThat(td.attr("sometag"), is("custom&value"));
assertThat(td.attr("colspan"), is("2"));
assertThat(td.attr("align"), is("right"));
Expand Down Expand Up @@ -142,8 +136,6 @@ public void th1() {
Jsoup.parseBodyFragment(String.format("<table><tr>%s</tr></table>", renderedHtml));
final Element th = doc.select("th").first();

System.out.println(renderedHtml);
System.out.println(th.attributes());
assertThat(th.attr("sometag"), is("customvalue"));
assertThat(th.attr("bgcolor"), is("#FF0064"));
assertThat(th.attr("class"), is("class"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
import static org.hamcrest.Matchers.nullValue;
import static us.fatehi.utility.html.TagBuilder.tableCell;
import static us.fatehi.utility.html.TagBuilder.tableRow;

import org.junit.jupiter.api.Test;

import us.fatehi.utility.html.Tag;
import us.fatehi.utility.html.TagOutputFormat;

Expand Down
Loading
Loading