HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Mini HTML document builder in Java - follow-up

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
builderjavaminidocumentfollowhtml

Problem

(See the previous (and first) iteration.)

Now, I have incorporated all the modifications proposed by janos, and, thus, I have the following:

HtmlViewComponent.java

package net.coderodde.html.view;

/**
 * This abstract class defines the API for logical HTML view components, that 
 * may consist of other view components;
 * 
 * @author Rodion "rodde" Efremov
 * @version 1.6 (Mar 18, 2016)
 */
public interface HtmlViewComponent {

    public String toHtml();
}


HtmlViewContainer.java

package net.coderodde.html.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * This class defines the API for HTML elements that may contain other HTML
 * elements.
 * 
 * @author Rodion "rodde" Efremov
 * @version 1.6 (Mar 18, 2016)
 */
public abstract class HtmlViewContainer implements HtmlViewComponent {

    protected final List components = new ArrayList<>();

    public void addHtmlViewComponent(HtmlViewComponent component) {
        Objects.requireNonNull(component, "The input component is null.");
        components.add(component);
    }

    public boolean containsHtmlViewComponent(HtmlViewComponent component) {
        Objects.requireNonNull(component, "The input component is null.");
        return components.contains(component);
    }

    public void removeHtmlViewComponent(HtmlViewComponent component) {
        Objects.requireNonNull(component, "The input component is null.");
        components.remove(component);
    }
}


HtmlPage.java

```
package net.coderodde.html.view;

/**
* This class is the top-level container of view components.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Mar 18, 2016)
*/
public class HtmlPage extends HtmlViewContainer {

private final String title;

public HtmlPage(String title) {
this.title = title != null ? title : "";
}

@Override
public String toHtml() {
StringBuilder sb = new StringBuilder().append("\n")

Solution

In TableComponent you have the following method :

public void addRow(List row) {
    if (row.size() > columns) {
        table.add(new ArrayList<>(row.subList(0, columns)));
    } else {
        table.add(new ArrayList<>(row));
    }
}


I'd add more specialization here :

public void addRow(TableRow row)


and in the class Row

public void addCell(TableCell cell)


I'd also leave the Component suffix aside and use the same names as in the html terminology (Html, Div, Span, Table, TableRow, TableCell, Anchor).

Also since you the name of your review is "html document builder", why not trying the Builder pattern? Here is a glimpse to the groovy html builder :
https://gist.github.com/kdabir/1885146

You can think also about adding support for :

  • element attributes



  • indentation of the html output

Code Snippets

public void addRow(List<HtmlViewComponent> row) {
    if (row.size() > columns) {
        table.add(new ArrayList<>(row.subList(0, columns)));
    } else {
        table.add(new ArrayList<>(row));
    }
}

Context

StackExchange Code Review Q#123252, answer score: 3

Revisions (0)

No revisions yet.