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
Changes from 1 commit
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
Next Next commit
Simplify and refactor the toHtmlString method in the Tag class
Refactor the `toHtmlString` method in the `Tag` class to improve readability and maintainability.

* Extract the attribute appending logic into a separate `appendAttributes` method.
* Extract the background color appending logic into a separate `appendBgColor` method.
* Extract the style class appending logic into a separate `appendStyleClass` method.
* Extract the emphasized text appending logic into a separate `appendEmphasizedText` method.
* Extract the inner tags appending logic into a separate `appendInnerTags` method.
* Extract the closing tag appending logic into a separate `appendClosingTag` method.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/schemacrawler/SchemaCrawler?shareId=XXXX-XXXX-XXXX-XXXX).
sualeh committed Aug 11, 2024
commit 3db374f2fba90279559f7aaace000f6cd3faa665
Original file line number Diff line number Diff line change
@@ -161,17 +161,7 @@ private String escapeHtml(final String text) {
return buffer.toString();
}

/**
* Converts the tag to HTML.
*
* @return HTML
*/
private String toHtmlString() {
final StringBuilder buffer = new StringBuilder(1024);
if (indent) {
buffer.append("\t");
}
buffer.append("<").append(getTagName());
private void appendAttributes(StringBuilder buffer) {
for (final Entry<String, String> attribute : attributes.entrySet()) {
buffer
.append(" ")
@@ -180,41 +170,75 @@ private String toHtmlString() {
.append(attribute.getValue())
.append("'");
}
}

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

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

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

if (innerTags.isEmpty()) {
private void appendInnerTags(StringBuilder buffer) {
for (final Tag innerTag : innerTags) {
if (indent) {
buffer.append(System.lineSeparator());
}
buffer.append(escapeText ? escapeHtml(text) : text);
} else {
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("\t");
}
buffer.append("\t").append(innerTag.render(html)).append(System.lineSeparator());
}
}

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

/**
* Converts the tag to HTML.
*
* @return HTML
*/
private String toHtmlString() {
final StringBuilder buffer = new StringBuilder(1024);
if (indent) {
buffer.append("\t");
}
buffer.append("<").append(getTagName());
appendAttributes(buffer);
appendBgColor(buffer);
appendStyleClass(buffer);
buffer.append(">");
appendEmphasizedText(buffer);

if (innerTags.isEmpty()) {
if (indent) {
buffer.append(System.lineSeparator());
}
buffer.append(escapeText ? escapeHtml(text) : text);
} else {
buffer.append(System.lineSeparator());
appendInnerTags(buffer);
}

appendClosingTag(buffer);

return buffer.toString();
}