Update builder pattern to support getting child elements easily
This commit is contained in:
parent
5b878e52db
commit
acbd1e6b7b
3 changed files with 33 additions and 15 deletions
|
@ -10,6 +10,10 @@ class HTMLAttribute {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HTMLAttribute ( String name ) {
|
||||||
|
this ( name , null );
|
||||||
|
}
|
||||||
|
|
||||||
public String getName ( ) {
|
public String getName ( ) {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,6 @@ class HTMLElement {
|
||||||
public String generateHTML ( ) {
|
public String generateHTML ( ) {
|
||||||
StringBuilder builder = new StringBuilder ( );
|
StringBuilder builder = new StringBuilder ( );
|
||||||
|
|
||||||
|
|
||||||
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 ( );
|
return builder.toString ( );
|
||||||
|
|
|
@ -1,21 +1,23 @@
|
||||||
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 {
|
||||||
private String tagName;
|
private String tagName;
|
||||||
private String text;
|
private String text;
|
||||||
private List<HTMLAttribute> attributes;
|
private List<HTMLAttribute> attributes;
|
||||||
private List<HTMLElement> children;
|
|
||||||
private boolean isEmptyElement;
|
private boolean isEmptyElement;
|
||||||
|
private Map<String, 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.children = new ArrayList<> ( );
|
|
||||||
this.isEmptyElement = false;
|
this.isEmptyElement = false;
|
||||||
|
this.childElementBuilders = new HashMap<> ( );
|
||||||
}
|
}
|
||||||
|
|
||||||
public HTMLElementBuilder withText ( String text ) {
|
public HTMLElementBuilder withText ( String text ) {
|
||||||
|
@ -29,13 +31,9 @@ class HTMLElementBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HTMLElementBuilder addChild ( HTMLElement child ) {
|
public HTMLElementBuilder withAttribute ( String name ) {
|
||||||
this.children.add ( child );
|
HTMLAttribute attribute = new HTMLAttribute ( name );
|
||||||
return this;
|
this.attributes.add ( attribute );
|
||||||
}
|
|
||||||
|
|
||||||
public HTMLElementBuilder addChild ( HTMLElementBuilder child ) {
|
|
||||||
this.children.add ( child.build () );
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +42,24 @@ class HTMLElementBuilder {
|
||||||
return this;
|
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 ( ) {
|
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 );
|
return new HTMLElement ( tagName , text , attributes , children , isEmptyElement );
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue