Update builder pattern to support getting child elements easily

This commit is contained in:
Aria 2023-06-19 17:05:24 -07:00
parent 5b878e52db
commit acbd1e6b7b
3 changed files with 33 additions and 15 deletions

View file

@ -5,16 +5,20 @@ class HTMLAttribute {
private String name;
private String value;
public HTMLAttribute(String name, String value) {
public HTMLAttribute ( String name , String value ) {
this.name = name;
this.value = value;
}
public String getName() {
public HTMLAttribute ( String name ) {
this ( name , null );
}
public String getName ( ) {
return name;
}
public String getValue() {
public String getValue ( ) {
return value;
}
}
}

View file

@ -22,7 +22,6 @@ class HTMLElement {
public String generateHTML ( ) {
StringBuilder builder = new StringBuilder ( );
if ( "!doctype".equalsIgnoreCase ( tagName ) ) {
builder.append ( "<!" ).append ( tagName ).append ( " " ).append ( text ).append ( ">" );
return builder.toString ( );
@ -60,4 +59,4 @@ class HTMLElement {
return builder.toString ( );
}
}
}

View file

@ -1,21 +1,23 @@
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 {
private String tagName;
private String text;
private List<HTMLAttribute> attributes;
private List<HTMLElement> children;
private boolean isEmptyElement;
private Map<String, HTMLElementBuilder> childElementBuilders;
public HTMLElementBuilder ( String tagName ) {
this.tagName = tagName;
this.attributes = new ArrayList<> ( );
this.children = new ArrayList<> ( );
this.isEmptyElement = false;
this.childElementBuilders = new HashMap<> ( );
}
public HTMLElementBuilder withText ( String text ) {
@ -29,13 +31,9 @@ class HTMLElementBuilder {
return this;
}
public HTMLElementBuilder addChild ( HTMLElement child ) {
this.children.add ( child );
return this;
}
public HTMLElementBuilder addChild ( HTMLElementBuilder child ) {
this.children.add ( child.build () );
public HTMLElementBuilder withAttribute ( String name ) {
HTMLAttribute attribute = new HTMLAttribute ( name );
this.attributes.add ( attribute );
return this;
}
@ -44,7 +42,24 @@ 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 );
}
return childBuilder;
}
public HTMLElement build ( ) {
List<HTMLElement> children = new ArrayList<> ( );
for ( HTMLElementBuilder builder : childElementBuilders.values ( ) ) {
children.add ( builder.build ( ) );
}
return new HTMLElement ( tagName , text , attributes , children , isEmptyElement );
}
}