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;
public class DOM
{
public class DOM {
/**
* Generates a HTML Header that automatically includes dependencies
*
* @return HTML
*/
public static String getHTMLHeader(String pageTitle)
{
public static String getHTMLHeader ( String pageTitle ) {
return "<!doctype html>\n" +
"<html lang=\"en\">\n" +
" <head>\n" +
@ -19,7 +18,7 @@ public class DOM
" \n" +
" <link href=\"https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM\" crossorigin=\"anonymous\">\n\n" +
"\n" +
" <title>"+pageTitle+"</title>\n" +
" <title>" + pageTitle + "</title>\n" +
" </head>\n" +
" <body>\n" +
"\n" +
@ -32,29 +31,30 @@ 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 );
public static HTMLElementBuilder beginBootstrapDOM ( String pageTitle ) {
var builder = new HTMLElementBuilder ( "!doctype" ).withText ( "html" );
var html = builder.getOrCreate ( "html" );
head.addChild ( meta ).addChild ( meta2 ).addChild ( linkBootstrap ).addChild ( title );
html.addChild ( head );
var head = html.getOrCreate ( "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;
}
public static String closeHTML()
{
public static String closeHTML ( ) {
return "</body></html>";
}
}

View file

@ -19,32 +19,40 @@ class HTMLElement {
this.isEmptyElement = isEmptyElement;
}
public String getTagName ( ) {
return tagName;
}
public String generateHTML ( ) {
StringBuilder builder = new StringBuilder ( );
boolean addEndingTag = true;
if ( "!doctype".equalsIgnoreCase ( tagName ) ) {
builder.append ( "<!" ).append ( tagName ).append ( " " ).append ( text ).append ( ">" );
return builder.toString ( );
addEndingTag = false;
text = null;
}
else {
builder.append ( "<" ).append ( tagName );
builder.append ( "<" ).append ( tagName );
for ( HTMLAttribute attribute : attributes ) {
builder.append ( " " )
.append ( attribute.getName ( ) );
for ( HTMLAttribute attribute : attributes ) {
builder.append ( " " )
.append ( attribute.getName ( ) );
String value = attribute.getValue ( );
if ( value != null ) {
builder.append ( "=\"" ).append ( value ).append ( "\"" );
String value = attribute.getValue ( );
if ( value != null ) {
builder.append ( "=\"" ).append ( value ).append ( "\"" );
}
}
if ( isEmptyElement ) {
builder.append ( " />" );
return builder.toString ( );
}
builder.append ( ">" );
}
if ( isEmptyElement ) {
builder.append ( " />" );
return builder.toString ( );
}
builder.append ( ">" );
if ( text != null ) {
builder.append ( text );
@ -55,7 +63,8 @@ class HTMLElement {
}
}
builder.append ( "</" ).append ( tagName ).append ( ">" );
if ( addEndingTag )
builder.append ( "</" ).append ( tagName ).append ( ">" );
return builder.toString ( );
}

View file

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