Add a function example of utilizing Bootstrap and the HTML Builder

This commit is contained in:
Aria 2023-06-19 17:32:27 -07:00
parent acbd1e6b7b
commit abd487523e
3 changed files with 87 additions and 47 deletions

View file

@ -1,13 +1,12 @@
package dev.zontreck.ariaslib.html; package dev.zontreck.ariaslib.html;
public class DOM public class DOM {
{
/** /**
* Generates a HTML Header that automatically includes dependencies * Generates a HTML Header that automatically includes dependencies
*
* @return HTML * @return HTML
*/ */
public static String getHTMLHeader(String pageTitle) public static String getHTMLHeader ( String pageTitle ) {
{
return "<!doctype html>\n" + return "<!doctype html>\n" +
"<html lang=\"en\">\n" + "<html lang=\"en\">\n" +
" <head>\n" + " <head>\n" +
@ -32,29 +31,30 @@ public class DOM
} }
public static HTMLElementBuilder beginBootstrapDOM(String pageTitle) public static HTMLElementBuilder beginBootstrapDOM ( String pageTitle ) {
{ var builder = new HTMLElementBuilder ( "!doctype" ).withText ( "html" );
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 );
var html = builder.getOrCreate ( "html" );
head.addChild ( meta ).addChild ( meta2 ).addChild ( linkBootstrap ).addChild ( title ); var head = html.getOrCreate ( "head" );
html.addChild ( head );
head.addChild ( "meta" ).withAttribute ( "charset" , "utf-8" );
head.addChild ( "meta" ).withAttribute ( "name" , "viewport" ).withAttribute ( "content" , "width=device-width, initial-scale=1" );
head.getOrCreate ( "link" ).withAttribute ( "href" , "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" ).withAttribute ( "integrity" , "sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" ).withAttribute ( "crossorigin" , "anonymous" );
head.getOrCreate ( "title" ).withText ( pageTitle );
html.getOrCreate ( "body" ).addChild ( "script" ).withAttribute ( "src" , "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" ).withAttribute ( "integrity" , "sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" ).withAttribute ( "crossorigin" , "anonymous" );
builder.addChild ( html );
return builder; return builder;
} }
public static String closeHTML() public static String closeHTML ( ) {
{
return "</body></html>"; return "</body></html>";
} }
} }

View file

@ -19,13 +19,20 @@ class HTMLElement {
this.isEmptyElement = isEmptyElement; this.isEmptyElement = isEmptyElement;
} }
public String getTagName ( ) {
return tagName;
}
public String generateHTML ( ) { public String generateHTML ( ) {
StringBuilder builder = new StringBuilder ( ); StringBuilder builder = new StringBuilder ( );
boolean addEndingTag = true;
if ( "!doctype".equalsIgnoreCase ( tagName ) ) { if ( "!doctype".equalsIgnoreCase ( tagName ) ) {
builder.append ( "<!" ).append ( tagName ).append ( " " ).append ( text ).append ( ">" ); builder.append ( "<!" ).append ( tagName ).append ( " " ).append ( text ).append ( ">" );
return builder.toString ( ); addEndingTag = false;
text = null;
} }
else {
builder.append ( "<" ).append ( tagName ); builder.append ( "<" ).append ( tagName );
@ -38,13 +45,14 @@ class HTMLElement {
builder.append ( "=\"" ).append ( value ).append ( "\"" ); builder.append ( "=\"" ).append ( value ).append ( "\"" );
} }
} }
if ( isEmptyElement ) { if ( isEmptyElement ) {
builder.append ( " />" ); builder.append ( " />" );
return builder.toString ( ); return builder.toString ( );
} }
builder.append ( ">" ); builder.append ( ">" );
}
if ( text != null ) { if ( text != null ) {
builder.append ( text ); builder.append ( text );
@ -55,6 +63,7 @@ class HTMLElement {
} }
} }
if ( addEndingTag )
builder.append ( "</" ).append ( tagName ).append ( ">" ); builder.append ( "</" ).append ( tagName ).append ( ">" );
return builder.toString ( ); return builder.toString ( );

View file

@ -1,9 +1,7 @@
package dev.zontreck.ariaslib.html; package dev.zontreck.ariaslib.html;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
// Builder class for building HTML elements // Builder class for building HTML elements
class HTMLElementBuilder { class HTMLElementBuilder {
@ -11,13 +9,13 @@ class HTMLElementBuilder {
private String text; private String text;
private List<HTMLAttribute> attributes; private List<HTMLAttribute> attributes;
private boolean isEmptyElement; private boolean isEmptyElement;
private Map<String, HTMLElementBuilder> childElementBuilders; private List<HTMLElementBuilder> childElementBuilders;
public HTMLElementBuilder ( String tagName ) { public HTMLElementBuilder ( String tagName ) {
this.tagName = tagName; this.tagName = tagName;
this.attributes = new ArrayList<> ( ); this.attributes = new ArrayList<> ( );
this.isEmptyElement = false; this.isEmptyElement = false;
this.childElementBuilders = new HashMap<> ( ); this.childElementBuilders = new ArrayList<> ( );
} }
public HTMLElementBuilder withText ( String text ) { public HTMLElementBuilder withText ( String text ) {
@ -42,24 +40,57 @@ class HTMLElementBuilder {
return this; return this;
} }
public HTMLElementBuilder getOrCreate ( String tagName ) { public HTMLElementBuilder addChild ( HTMLElementBuilder childBuilder ) {
HTMLElementBuilder childBuilder = childElementBuilders.get ( tagName ); childElementBuilders.add ( childBuilder );
return this;
if ( childBuilder == null ) {
childBuilder = new HTMLElementBuilder ( tagName );
childElementBuilders.put ( tagName , childBuilder );
} }
public HTMLElementBuilder addChild ( String tagName ) {
HTMLElementBuilder childBuilder = new HTMLElementBuilder ( tagName );
childElementBuilders.add ( childBuilder );
return childBuilder; return childBuilder;
} }
public HTMLElement build ( ) { public HTMLElementBuilder getOrCreate ( String tagName ) {
HTMLElementBuilder childBuilder = getChildByTagName ( tagName );
if ( childBuilder == null ) {
childBuilder = addChild ( tagName );
}
return childBuilder;
}
public HTMLElementBuilder getChildByTagName ( String tagName ) {
return getChildByTagName ( tagName , 0 );
}
public HTMLElementBuilder getChildByTagName ( String tagName , int index ) {
List<HTMLElementBuilder> matchingChildBuilders = new ArrayList<> ( );
for ( HTMLElementBuilder builder : childElementBuilders ) {
if ( builder.tagName.equalsIgnoreCase ( tagName ) ) {
matchingChildBuilders.add ( builder );
}
}
if ( matchingChildBuilders.size ( ) > index ) {
return matchingChildBuilders.get ( index );
}
return null;
}
private List<HTMLElement> buildChildren ( ) {
List<HTMLElement> children = new ArrayList<> ( ); List<HTMLElement> children = new ArrayList<> ( );
for ( HTMLElementBuilder builder : childElementBuilders.values ( ) ) { for ( HTMLElementBuilder builder : childElementBuilders ) {
children.add ( builder.build ( ) ); children.add ( builder.build ( ) );
} }
return children;
}
public HTMLElement build ( ) {
List<HTMLElement> children = buildChildren ( );
return new HTMLElement ( tagName , text , attributes , children , isEmptyElement ); return new HTMLElement ( tagName , text , attributes , children , isEmptyElement );
} }
} }