Begin to add a builder pattern for HTML building

This commit is contained in:
Aria 2023-06-19 05:52:13 -07:00
parent 1980ff5fd8
commit 5b878e52db
4 changed files with 154 additions and 0 deletions

View file

@ -32,6 +32,27 @@ public class DOM
}
public static HTMLElementBuilder beginBootstrapDOM(String pageTitle)
{
var builder = new HTMLElementBuilder ( "!doctype" );
var html = new HTMLElementBuilder ( "html" );
var head = new HTMLElementBuilder ( "head" );
var meta = new HTMLElementBuilder ( "meta" ).withAttribute ( "charset", "utf-8" );
var meta2 = new HTMLElementBuilder ( "meta" ).withAttribute ( "name", "viewport" ).withAttribute ( "content", "width=device-width, initial-scale=1" );
var linkBootstrap = new HTMLElementBuilder ( "link" ).withAttribute ( "href", "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" ).withAttribute ( "integrity", "sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" ).withAttribute ( "crossorigin", "anonymous" );
var title = new HTMLElementBuilder ( "title" ).withText ( pageTitle );
head.addChild ( meta ).addChild ( meta2 ).addChild ( linkBootstrap ).addChild ( title );
html.addChild ( head );
builder.addChild ( html );
return builder;
}
public static String closeHTML()
{
return "</body></html>";

View file

@ -0,0 +1,20 @@
package dev.zontreck.ariaslib.html;
// Attribute class for HTML element attributes
class HTMLAttribute {
private String name;
private String value;
public HTMLAttribute(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
}

View file

@ -0,0 +1,63 @@
package dev.zontreck.ariaslib.html;
import java.util.List;
// HTML element class supporting tag attributes
class HTMLElement {
private String tagName;
private String text;
private List<HTMLAttribute> attributes;
private List<HTMLElement> children;
private boolean isEmptyElement;
public HTMLElement ( String tagName , String text , List<HTMLAttribute> attributes , List<HTMLElement> children , boolean isEmptyElement ) {
this.tagName = tagName;
this.text = text;
this.attributes = attributes;
this.children = children;
this.isEmptyElement = isEmptyElement;
}
public String generateHTML ( ) {
StringBuilder builder = new StringBuilder ( );
if ( "!doctype".equalsIgnoreCase ( tagName ) ) {
builder.append ( "<!" ).append ( tagName ).append ( " " ).append ( text ).append ( ">" );
return builder.toString ( );
}
builder.append ( "<" ).append ( tagName );
for ( HTMLAttribute attribute : attributes ) {
builder.append ( " " )
.append ( attribute.getName ( ) );
String value = attribute.getValue ( );
if ( value != null ) {
builder.append ( "=\"" ).append ( value ).append ( "\"" );
}
}
if ( isEmptyElement ) {
builder.append ( " />" );
return builder.toString ( );
}
builder.append ( ">" );
if ( text != null ) {
builder.append ( text );
}
else {
for ( HTMLElement child : children ) {
builder.append ( child.generateHTML ( ) );
}
}
builder.append ( "</" ).append ( tagName ).append ( ">" );
return builder.toString ( );
}
}

View file

@ -0,0 +1,50 @@
package dev.zontreck.ariaslib.html;
import java.util.ArrayList;
import java.util.List;
// Builder class for building HTML elements
class HTMLElementBuilder {
private String tagName;
private String text;
private List<HTMLAttribute> attributes;
private List<HTMLElement> children;
private boolean isEmptyElement;
public HTMLElementBuilder ( String tagName ) {
this.tagName = tagName;
this.attributes = new ArrayList<> ( );
this.children = new ArrayList<> ( );
this.isEmptyElement = false;
}
public HTMLElementBuilder withText ( String text ) {
this.text = text;
return this;
}
public HTMLElementBuilder withAttribute ( String name , String value ) {
HTMLAttribute attribute = new HTMLAttribute ( name , value );
this.attributes.add ( attribute );
return this;
}
public HTMLElementBuilder addChild ( HTMLElement child ) {
this.children.add ( child );
return this;
}
public HTMLElementBuilder addChild ( HTMLElementBuilder child ) {
this.children.add ( child.build () );
return this;
}
public HTMLElementBuilder setEmptyElement ( ) {
this.isEmptyElement = true;
return this;
}
public HTMLElement build ( ) {
return new HTMLElement ( tagName , text , attributes , children , isEmptyElement );
}
}