Skip to content

Generate tags from JSON file #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@
</issueManagement>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -52,12 +67,6 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>junit-benchmarks</artifactId>
Expand Down Expand Up @@ -149,6 +158,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.j2html</groupId>
<artifactId>j2html-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>generate-tags</id>
<goals>
<goal>generate-tags</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/j2html/tags/ContainerTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;
import java.util.stream.Stream;

public class ContainerTag extends Tag<ContainerTag> {
public class ContainerTag<T extends ContainerTag<T>> extends Tag<T> {

private List<DomContent> children;

Expand All @@ -22,15 +22,15 @@ public ContainerTag(String tagName) {
* @param child DomContent-object to be appended
* @return itself for easy chaining
*/
public ContainerTag with(DomContent child) {
public T with(DomContent child) {
if (this == child) {
throw new RuntimeException("Cannot append a tag to itself.");
}
if (child == null) {
return this; // in some cases, like when using iff(), we ignore null children
return (T) this; // in some cases, like when using iff(), we ignore null children
}
children.add(child);
return this;
return (T) this;
}


Expand Down Expand Up @@ -121,7 +121,7 @@ public ContainerTag condWith(boolean condition, DomContent... children) {
* @param text the text to be appended
* @return itself for easy chaining
*/
public ContainerTag withText(String text) {
public T withText(String text) {
return with(new Text(text));
}

Expand Down
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<T extends EmptyTag<T>> extends Tag<T> {

public EmptyTag(String tagName) {
super(tagName);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/j2html/tags/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public T attr(String attribute) {
* @param value the attribute value
* @return itself for easy chaining
*/
public T condAttr(boolean condition, String attribute, String value) {
public T condAttr(boolean condition, String attribute, Object value) {
return (condition ? attr(attribute, value) : (T) this);
}

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/j2html/tags/TagCreatorCodeGeneratorFromJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package j2html.tags;

import com.google.gson.*;
import com.squareup.javapoet.*;
import j2html.attributes.Attr;

import javax.lang.model.element.Modifier;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;

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

public class TagCreatorCodeGeneratorFromJson {

public static void main(String[] args) throws Exception {
StringWriter output = new StringWriter();

try (FileReader fileReader = new FileReader("src/main/resources/tagClasses.json")) {
JsonObject json = new Gson().fromJson(fileReader, JsonObject.class);
ClassName containerTagClassName = ClassName.get(ContainerTag.class);
json.getAsJsonArray("tags").forEach(element -> {
JsonObject tagJson = element.getAsJsonObject();

String tag = tagJson.get("tag").getAsString();
ClassName tagClassName = ClassName.get("j2html.tags", capitalize(tag + "Tag"));
TypeSpec.Builder typeSpec = TypeSpec.classBuilder(tagClassName)
.superclass(ParameterizedTypeName.get(containerTagClassName, tagClassName))
.addMethod(
MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addStatement("super(\"" + tag + "\")")
.build());

JsonArray attrs = tagJson.getAsJsonArray("attrs");
attrs.forEach(attrElement -> {
String attrName = attrElement.isJsonObject() ? attrElement.getAsJsonObject().get("name").getAsString() : attrElement.getAsString();
String attrNameCapitalized = capitalize(attrName);

MethodSpec.Builder methodSpec = MethodSpec.methodBuilder("with" + attrNameCapitalized)
.addModifiers(Modifier.PUBLIC)
.returns(tagClassName);

MethodSpec.Builder condMethodSpec = MethodSpec.methodBuilder("withCond" + attrNameCapitalized)
.addModifiers(Modifier.PUBLIC)
.addParameter(boolean.class, "condition")
.returns(tagClassName);

if (attrElement.isJsonObject() && new JsonPrimitive(true).equals(attrElement.getAsJsonObject().get("empty"))) {
methodSpec.addStatement("return attr(\"" + attrName + "\")");
condMethodSpec.addStatement("return condAttr(condition, \"" + attrName + "\", \"" + attrName + "\")");
} else {
methodSpec
.addParameter(String.class, attrName)
.addStatement("return attr(\"" + attrName + "\", " + attrName + ")");
condMethodSpec
.addParameter(boolean.class, "condition")
.addParameter(String.class, attrName)
.addStatement("return condAttr(condition, \"" + attrName + "\", " + attrName + ")");
}
typeSpec.addMethod(methodSpec.build())
.addMethod(condMethodSpec.build());
});

try {
JavaFile.builder("j2html.tags", typeSpec.build())
.build()
.writeTo(output);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

System.out.println(output);
}
}
7 changes: 7 additions & 0 deletions src/main/resources/globalAttributes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"name": "tabindex",
"type": "Integer"
},
"title"
]
18 changes: 18 additions & 0 deletions src/main/resources/tagClasses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"tags": [
{
"tag": "a",
"attrs": [
"href",
"charset"
]
},
{
"tag": "video",
"attrs": [
{ "name": "autoplay", "empty": true }, "controls", "height", "loop", "muted", "poster", "preload", "src", "width"
]
},
"abbr"
]
}