diff --git a/src/main/java/j2html/TagCreator.java b/src/main/java/j2html/TagCreator.java index 4e20d0c0..31d6b8c4 100644 --- a/src/main/java/j2html/TagCreator.java +++ b/src/main/java/j2html/TagCreator.java @@ -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; @@ -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; @@ -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 area() { + return new EmptyTag<>("area"); } - public static EmptyTag area(Attr.ShortForm shortAttr) { - return Attr.addTo(new EmptyTag("area"), shortAttr); + public static EmptyTag area(Attr.ShortForm shortAttr) { + return Attr.addTo(new EmptyTag("area"), shortAttr); } public static EmptyTag base() { @@ -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 a() { + return new ContainerTag<>("a"); } - public static ContainerTag a(String text) { - return new ContainerTag("a").withText(text); + public static ContainerTag a(String text) { + return new ContainerTag("a").withText(text); } - public static ContainerTag a(DomContent... dc) { - return new ContainerTag("a").with(dc); + public static ContainerTag a(DomContent... dc) { + return new ContainerTag("a").with(dc); } - public static ContainerTag a(Attr.ShortForm shortAttr) { - return Attr.addTo(new ContainerTag("a"), shortAttr); + public static ContainerTag a(Attr.ShortForm shortAttr) { + return Attr.addTo(new ContainerTag("a"), shortAttr); } - public static ContainerTag a(Attr.ShortForm shortAttr, String text) { - return Attr.addTo(new ContainerTag("a").withText(text), shortAttr); + public static ContainerTag a(Attr.ShortForm shortAttr, String text) { + return Attr.addTo(new ContainerTag("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 a(Attr.ShortForm shortAttr, DomContent... dc) { + return Attr.addTo(new ContainerTag("a").with(dc), shortAttr); } public static ContainerTag abbr() { diff --git a/src/main/java/j2html/attributes/AAttribute.java b/src/main/java/j2html/attributes/AAttribute.java new file mode 100644 index 00000000..f0b2a877 --- /dev/null +++ b/src/main/java/j2html/attributes/AAttribute.java @@ -0,0 +1,18 @@ +package j2html.attributes; + +public enum AAttribute { + + CHARSET, + COORDS, + DOWNLOAD, + HREF, + HREFLANG, + MEDIA, + NAME, + PING, + REL, + REV, + SHAPE, + TARGET, + TYPE +} diff --git a/src/main/java/j2html/attributes/AreaAttribute.java b/src/main/java/j2html/attributes/AreaAttribute.java new file mode 100644 index 00000000..f57d1223 --- /dev/null +++ b/src/main/java/j2html/attributes/AreaAttribute.java @@ -0,0 +1,16 @@ +package j2html.attributes; + +public enum AreaAttribute { + + ALT, + COORDS, + DOWNLOAD, + HREF, + HREFLANG, + MEDIA, + NOHREF, + REL, + SHAPE, + TARGET, + TYPE +} diff --git a/src/main/java/j2html/attributes/Attr.java b/src/main/java/j2html/attributes/Attr.java index 17c8af6d..5d35b523 100644 --- a/src/main/java/j2html/attributes/Attr.java +++ b/src/main/java/j2html/attributes/Attr.java @@ -5,6 +5,7 @@ public class Attr { public static class ShortForm { + String id; String classes; @@ -44,7 +45,7 @@ public static ShortForm shortFormFromAttrsString(String attrs) { return new ShortForm(id.trim(), classes.toString().trim()); } - public static > T addTo(T tag, ShortForm shortForm) { + public static , A extends Enum> T addTo(T tag, ShortForm shortForm) { if (shortForm.hasId() && shortForm.hasClasses()) { return tag.withId(shortForm.id).withClass(shortForm.classes); } @@ -57,6 +58,10 @@ public static > T addTo(T tag, ShortForm shortForm) { return tag; } + public static String getName(Enum attribute) { + return attribute.name().replaceAll("_", "-").toLowerCase(); + } + private Attr() { } @@ -170,5 +175,4 @@ private Attr() { public static final String VALUE = "value"; public static final String WIDTH = "width"; public static final String WRAP = "wrap"; - } diff --git a/src/main/java/j2html/tags/ContainerTag.java b/src/main/java/j2html/tags/ContainerTag.java index dbd7a7ae..6e7c88cc 100644 --- a/src/main/java/j2html/tags/ContainerTag.java +++ b/src/main/java/j2html/tags/ContainerTag.java @@ -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 { +public class ContainerTag> extends Tag, A> { private List children; @@ -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 with(DomContent child) { if (this == child) { throw new RuntimeException("Cannot append a tag to itself."); } @@ -32,7 +32,6 @@ public ContainerTag with(DomContent child) { return this; } - /** * Call with-method based on condition * {@link #with(DomContent child)} @@ -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 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 children) { + public ContainerTag with(Iterable children) { if (children != null) { for (DomContent child : children) { this.with(child); @@ -61,7 +59,6 @@ public ContainerTag with(Iterable children) { return this; } - /** * Call with-method based on condition * {@link #with(java.lang.Iterable)} @@ -70,25 +67,23 @@ public ContainerTag with(Iterable children) { * @param children DomContent-objects to be appended if condition met * @return itself for easy chaining */ - public ContainerTag condWith(boolean condition, Iterable children) { + public ContainerTag condWith(boolean condition, Iterable 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 with(DomContent... children) { for (DomContent child : children) { with(child); } return this; } - /** * Call with-method based on condition * {@link #with(DomContent... children)} @@ -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 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 withText(String text) { return with(new Text(text)); } @@ -181,5 +175,4 @@ public void renderModel(Appendable writer, Object model) throws IOException { } renderCloseTag(writer); } - } diff --git a/src/main/java/j2html/tags/EmptyTag.java b/src/main/java/j2html/tags/EmptyTag.java index 7dd63f49..ce82efb9 100644 --- a/src/main/java/j2html/tags/EmptyTag.java +++ b/src/main/java/j2html/tags/EmptyTag.java @@ -4,7 +4,7 @@ import j2html.attributes.Attribute; import java.io.IOException; -public class EmptyTag extends Tag { +public class EmptyTag> extends Tag, A> { public EmptyTag(String tagName) { super(tagName); diff --git a/src/main/java/j2html/tags/Tag.java b/src/main/java/j2html/tags/Tag.java index 888a8dee..baaee406 100644 --- a/src/main/java/j2html/tags/Tag.java +++ b/src/main/java/j2html/tags/Tag.java @@ -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> extends DomContent { +public abstract class Tag, A extends Enum> extends DomContent { + protected String tagName; private ArrayList attributes; @@ -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. * @@ -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)} @@ -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)) { diff --git a/src/main/java/j2html/tags/TagCreatorCodeGenerator.java b/src/main/java/j2html/tags/TagCreatorCodeGenerator.java index e8a04165..ffbffc94 100644 --- a/src/main/java/j2html/tags/TagCreatorCodeGenerator.java +++ b/src/main/java/j2html/tags/TagCreatorCodeGenerator.java @@ -3,16 +3,19 @@ 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)); @@ -20,19 +23,20 @@ public static void main(String[] args) { } 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));