This repository has been archived on 2024-10-31. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
LibZontreck/src/main/java/dev/zontreck/ariaslib/html/HTMLElement.java
2024-10-09 22:21:30 -07:00

72 lines
No EOL
1.7 KiB
Java

package dev.zontreck.ariaslib.html;
import java.util.List;
// HTML element class supporting tag attributes
public 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 getTagName ( ) {
return tagName;
}
public String generateHTML ( ) {
StringBuilder builder = new StringBuilder ( );
if ( "!doctype".equalsIgnoreCase ( tagName ) ) {
builder.append ( "<" ).append ( tagName ).append ( " " ).append ( text ).append ( ">\n" );
for ( HTMLElement child : children ) {
builder.append ( child.generateHTML ( ) );
}
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 ( " />\n" );
return builder.toString ( );
}*/
builder.append ( ">" );
if ( text != null ) {
builder.append ( text );
}
if ( ! isEmptyElement ) {
for ( HTMLElement child : children ) {
builder.append ( child.generateHTML ( ) );
}
}
builder.append ( "</" ).append ( tagName ).append ( ">\n" );
return builder.toString ( );
}
}