Skip to content

Commit

Permalink
Attributes per tag PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
mkopylec committed May 18, 2018
1 parent 764b5d7 commit d1998b8
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 52 deletions.
35 changes: 19 additions & 16 deletions src/main/java/j2html/TagCreator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package j2html;

import j2html.attributes.AAttribute;
import j2html.attributes.AreaAttribute;
import j2html.attributes.Attr;
import j2html.tags.ContainerTag;
import j2html.tags.DomContent;
Expand All @@ -8,6 +10,7 @@
import j2html.tags.InlineStaticResource;
import j2html.tags.Text;
import j2html.tags.UnescapedText;

import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -198,12 +201,12 @@ public static DomContent document() {
}

// EmptyTags, generated in class j2html.tags.TagCreatorCodeGenerator
public static EmptyTag area() {
return new EmptyTag("area");
public static EmptyTag<AreaAttribute> area() {
return new EmptyTag<>("area");
}

public static EmptyTag area(Attr.ShortForm shortAttr) {
return Attr.addTo(new EmptyTag("area"), shortAttr);
public static EmptyTag<AreaAttribute> area(Attr.ShortForm shortAttr) {
return Attr.addTo(new EmptyTag<AreaAttribute>("area"), shortAttr);
}

public static EmptyTag base() {
Expand Down Expand Up @@ -319,28 +322,28 @@ public static EmptyTag wbr(Attr.ShortForm shortAttr) {
}

// ContainerTags, generated in class j2html.tags.TagCreatorCodeGenerator
public static ContainerTag a() {
return new ContainerTag("a");
public static ContainerTag<AAttribute> a() {
return new ContainerTag<>("a");
}

public static ContainerTag a(String text) {
return new ContainerTag("a").withText(text);
public static ContainerTag<AAttribute> a(String text) {
return new ContainerTag<AAttribute>("a").withText(text);
}

public static ContainerTag a(DomContent... dc) {
return new ContainerTag("a").with(dc);
public static ContainerTag<AAttribute> a(DomContent... dc) {
return new ContainerTag<AAttribute>("a").with(dc);
}

public static ContainerTag a(Attr.ShortForm shortAttr) {
return Attr.addTo(new ContainerTag("a"), shortAttr);
public static ContainerTag<AAttribute> a(Attr.ShortForm shortAttr) {
return Attr.addTo(new ContainerTag<AAttribute>("a"), shortAttr);
}

public static ContainerTag a(Attr.ShortForm shortAttr, String text) {
return Attr.addTo(new ContainerTag("a").withText(text), shortAttr);
public static ContainerTag<AAttribute> a(Attr.ShortForm shortAttr, String text) {
return Attr.addTo(new ContainerTag<AAttribute>("a").withText(text), shortAttr);
}

public static ContainerTag a(Attr.ShortForm shortAttr, DomContent... dc) {
return Attr.addTo(new ContainerTag("a").with(dc), shortAttr);
public static ContainerTag<AAttribute> a(Attr.ShortForm shortAttr, DomContent... dc) {
return Attr.addTo(new ContainerTag<AAttribute>("a").with(dc), shortAttr);
}

public static ContainerTag abbr() {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/j2html/attributes/AAttribute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package j2html.attributes;

public enum AAttribute {

CHARSET,
COORDS,
DOWNLOAD,
HREF,
HREFLANG,
MEDIA,
NAME,
PING,
REL,
REV,
SHAPE,
TARGET,
TYPE
}
16 changes: 16 additions & 0 deletions src/main/java/j2html/attributes/AreaAttribute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package j2html.attributes;

public enum AreaAttribute {

ALT,
COORDS,
DOWNLOAD,
HREF,
HREFLANG,
MEDIA,
NOHREF,
REL,
SHAPE,
TARGET,
TYPE
}
8 changes: 6 additions & 2 deletions src/main/java/j2html/attributes/Attr.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class Attr {

public static class ShortForm {

String id;
String classes;

Expand Down Expand Up @@ -44,7 +45,7 @@ public static ShortForm shortFormFromAttrsString(String attrs) {
return new ShortForm(id.trim(), classes.toString().trim());
}

public static <T extends Tag<T>> T addTo(T tag, ShortForm shortForm) {
public static <T extends Tag<T, A>, A extends Enum<A>> T addTo(T tag, ShortForm shortForm) {
if (shortForm.hasId() && shortForm.hasClasses()) {
return tag.withId(shortForm.id).withClass(shortForm.classes);
}
Expand All @@ -57,6 +58,10 @@ public static <T extends Tag<T>> T addTo(T tag, ShortForm shortForm) {
return tag;
}

public static String getName(Enum<?> attribute) {
return attribute.name().replaceAll("_", "-").toLowerCase();
}

private Attr() {
}

Expand Down Expand Up @@ -170,5 +175,4 @@ private Attr() {
public static final String VALUE = "value";
public static final String WIDTH = "width";
public static final String WRAP = "wrap";

}
25 changes: 9 additions & 16 deletions src/main/java/j2html/tags/ContainerTag.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package j2html.tags;

import j2html.Config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ContainerTag extends Tag<ContainerTag> {
public class ContainerTag<A extends Enum<A>> extends Tag<ContainerTag<A>, A> {

private List<DomContent> children;

Expand All @@ -14,14 +15,13 @@ public ContainerTag(String tagName) {
this.children = new ArrayList<>();
}


/**
* Appends a DomContent-object to the end of this element
*
* @param child DomContent-object to be appended
* @return itself for easy chaining
*/
public ContainerTag with(DomContent child) {
public ContainerTag<A> with(DomContent child) {
if (this == child) {
throw new RuntimeException("Cannot append a tag to itself.");
}
Expand All @@ -32,7 +32,6 @@ public ContainerTag with(DomContent child) {
return this;
}


/**
* Call with-method based on condition
* {@link #with(DomContent child)}
Expand All @@ -41,18 +40,17 @@ public ContainerTag with(DomContent child) {
* @param child DomContent-object to be appended if condition met
* @return itself for easy chaining
*/
public ContainerTag condWith(boolean condition, DomContent child) {
public ContainerTag<A> condWith(boolean condition, DomContent child) {
return condition ? this.with(child) : this;
}


/**
* Appends a list of DomContent-objects to the end of this element
*
* @param children DomContent-objects to be appended
* @return itself for easy chaining
*/
public ContainerTag with(Iterable<? extends DomContent> children) {
public ContainerTag<A> with(Iterable<? extends DomContent> children) {
if (children != null) {
for (DomContent child : children) {
this.with(child);
Expand All @@ -61,7 +59,6 @@ public ContainerTag with(Iterable<? extends DomContent> children) {
return this;
}


/**
* Call with-method based on condition
* {@link #with(java.lang.Iterable)}
Expand All @@ -70,25 +67,23 @@ public ContainerTag with(Iterable<? extends DomContent> children) {
* @param children DomContent-objects to be appended if condition met
* @return itself for easy chaining
*/
public ContainerTag condWith(boolean condition, Iterable<? extends DomContent> children) {
public ContainerTag<A> condWith(boolean condition, Iterable<? extends DomContent> children) {
return condition ? this.with(children) : this;
}


/**
* Appends the DomContent-objects to the end of this element
*
* @param children DomContent-objects to be appended
* @return itself for easy chaining
*/
public ContainerTag with(DomContent... children) {
public ContainerTag<A> with(DomContent... children) {
for (DomContent child : children) {
with(child);
}
return this;
}


/**
* Call with-method based on condition
* {@link #with(DomContent... children)}
Expand All @@ -97,18 +92,17 @@ public ContainerTag with(DomContent... children) {
* @param children DomContent-objects to be appended if condition met
* @return itself for easy chaining
*/
public ContainerTag condWith(boolean condition, DomContent... children) {
public ContainerTag<A> condWith(boolean condition, DomContent... children) {
return condition ? this.with(children) : this;
}


/**
* Appends a Text-object to this element
*
* @param text the text to be appended
* @return itself for easy chaining
*/
public ContainerTag withText(String text) {
public ContainerTag<A> withText(String text) {
return with(new Text(text));
}

Expand Down Expand Up @@ -181,5 +175,4 @@ public void renderModel(Appendable writer, Object model) throws IOException {
}
renderCloseTag(writer);
}

}
2 changes: 1 addition & 1 deletion src/main/java/j2html/tags/EmptyTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import j2html.attributes.Attribute;
import java.io.IOException;

public class EmptyTag extends Tag<EmptyTag> {
public class EmptyTag<A extends Enum<A>> extends Tag<EmptyTag<A>, A> {

public EmptyTag(String tagName) {
super(tagName);
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/j2html/tags/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import j2html.attributes.Attr;
import j2html.attributes.Attribute;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public abstract class Tag<T extends Tag<T>> extends DomContent {
public abstract class Tag<T extends Tag<T, A>, A extends Enum<A>> extends DomContent {

protected String tagName;
private ArrayList<Attribute> attributes;

Expand Down Expand Up @@ -90,6 +92,10 @@ public T attr(String attribute, Object value) {
return (T) this;
}

public T attr(A attribute, Object value) {
return attr(Attr.getName(attribute), value);
}

/**
* Adds the specified attribute. If the Tag previously contained an attribute with the same name, the old attribute is replaced by the specified attribute.
*
Expand Down Expand Up @@ -123,6 +129,10 @@ public T attr(String attribute) {
return attr(attribute, null);
}

public T attr(A attribute) {
return attr(attribute, null);
}

/**
* Call attr-method based on condition
* {@link #attr(String attribute, Object value)}
Expand All @@ -136,6 +146,10 @@ public T condAttr(boolean condition, String attribute, String value) {
return (condition ? attr(attribute, value) : (T) this);
}

public T condAttr(boolean condition, A attribute, String value) {
return (condition ? attr(attribute, value) : (T) this);
}

@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Tag)) {
Expand Down
36 changes: 20 additions & 16 deletions src/main/java/j2html/tags/TagCreatorCodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,40 @@
import java.util.Arrays;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.capitalize;

class TagCreatorCodeGenerator {

public static void main(String[] args) {
System.out.println("// EmptyTags, generated in " + TagCreatorCodeGenerator.class);
for (String tag : emptyTags()) {
String emptyA1 = "public static EmptyTag " + tag + "()";
String emptyA2 = "{ return new EmptyTag(\"" + tag + "\"); }";
String attribute = capitalize(tag) + "Attribute";
String emptyA1 = "public static EmptyTag<" + attribute + "> " + tag + "()";
String emptyA2 = "{ return new EmptyTag<>(\"" + tag + "\"); }";
// Attr shorthands
String emptyB1 = "public static EmptyTag " + tag + "(Attr.ShortForm shortAttr)";
String emptyB2 = "{ return Attr.addTo(new EmptyTag(\"" + tag + "\"), shortAttr); }";
String emptyB1 = "public static EmptyTag<" + attribute + "> " + tag + "(Attr.ShortForm shortAttr)";
String emptyB2 = "{ return Attr.addTo(new EmptyTag<" + attribute + ">(\"" + tag + "\"), shortAttr); }";
// Print
System.out.println(String.format("%-80s%1s", emptyA1, emptyA2));
System.out.println(String.format("%-80s%1s", emptyB1, emptyB2));
System.out.println("");
}
System.out.println("// ContainerTags, generated in " + TagCreatorCodeGenerator.class);
for (String tag : containerTags()) {
String containerA1 = "public static ContainerTag " + tag + "()";
String containerA2 = "{ return new ContainerTag(\"" + tag + "\"); }";
String containerB1 = "public static ContainerTag " + tag + "(String text)";
String containerB2 = "{ return new ContainerTag(\"" + tag + "\").withText(text); }";
String containerC1 = "public static ContainerTag " + tag + "(DomContent... dc)";
String containerC2 = "{ return new ContainerTag(\"" + tag + "\").with(dc); }";
String attribute = capitalize(tag) + "Attribute";
String containerA1 = "public static ContainerTag<" + attribute + "> " + tag + "()";
String containerA2 = "{ return new ContainerTag<>(\"" + tag + "\"); }";
String containerB1 = "public static ContainerTag<" + attribute + "> " + tag + "(String text)";
String containerB2 = "{ return new ContainerTag<" + attribute + ">(\"" + tag + "\").withText(text); }";
String containerC1 = "public static ContainerTag<" + attribute + "> " + tag + "(DomContent... dc)";
String containerC2 = "{ return new ContainerTag<" + attribute + ">(\"" + tag + "\").with(dc); }";
// Attr shorthands
String containerD1 = "public static ContainerTag " + tag + "(Attr.ShortForm shortAttr)";
String containerD2 = "{ return Attr.addTo(new ContainerTag(\"" + tag + "\"), shortAttr); }";
String containerE1 = "public static ContainerTag " + tag + "(Attr.ShortForm shortAttr, String text)";
String containerE2 = "{ return Attr.addTo(new ContainerTag(\"" + tag + "\").withText(text), shortAttr); }";
String containerF1 = "public static ContainerTag " + tag + "(Attr.ShortForm shortAttr, DomContent... dc)";
String containerF2 = "{ return Attr.addTo(new ContainerTag(\"" + tag + "\").with(dc), shortAttr); }";
String containerD1 = "public static ContainerTag<" + attribute + "> " + tag + "(Attr.ShortForm shortAttr)";
String containerD2 = "{ return Attr.addTo(new ContainerTag<" + attribute + ">(\"" + tag + "\"), shortAttr); }";
String containerE1 = "public static ContainerTag<" + attribute + "> " + tag + "(Attr.ShortForm shortAttr, String text)";
String containerE2 = "{ return Attr.addTo(new ContainerTag<" + attribute + ">(\"" + tag + "\").withText(text), shortAttr); }";
String containerF1 = "public static ContainerTag<" + attribute + "> " + tag + "(Attr.ShortForm shortAttr, DomContent... dc)";
String containerF2 = "{ return Attr.addTo(new <" + attribute + ">ContainerTag(\"" + tag + "\").with(dc), shortAttr); }";
// Print
System.out.println(String.format("%-80s%1s", containerA1, containerA2));
System.out.println(String.format("%-80s%1s", containerB1, containerB2));
Expand Down

0 comments on commit d1998b8

Please sign in to comment.