Skip to content

Commit fa14f29

Browse files
committed
feat: use lombok to generate java classes
1 parent 41b21eb commit fa14f29

File tree

6 files changed

+88
-242
lines changed

6 files changed

+88
-242
lines changed

minify-html-java/lombok.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config.stopBubbling = true
2+
lombok.addLombokGeneratedAnnotation = false

minify-html-java/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,34 @@
4646

4747
<properties>
4848
<java.version>1.8</java.version>
49+
<lombok.version>1.18.38</lombok.version>
4950
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5051
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5152
</properties>
5253

54+
<dependencies>
55+
<dependency>
56+
<groupId>org.projectlombok</groupId>
57+
<artifactId>lombok</artifactId>
58+
<version>${lombok.version}</version>
59+
<scope>provided</scope>
60+
</dependency>
61+
</dependencies>
62+
5363
<build>
5464
<plugins>
5565
<plugin>
5666
<groupId>org.apache.maven.plugins</groupId>
5767
<artifactId>maven-compiler-plugin</artifactId>
5868
<version>3.8.1</version>
5969
<configuration>
70+
<annotationProcessorPaths>
71+
<path>
72+
<groupId>org.projectlombok</groupId>
73+
<artifactId>lombok</artifactId>
74+
<version>${lombok.version}</version>
75+
</path>
76+
</annotationProcessorPaths>
6077
<source>${java.version}</source>
6178
<target>${java.version}</target>
6279
</configuration>
Lines changed: 24 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,32 @@
11
package in.wilsonl.minifyhtml;
22

3-
// WARNING: Do not manually edit, use Configuration.java.gen.js.
3+
import lombok.AccessLevel;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
46

57
/**
68
* Class representing minification configuration.
9+
* Use the {@link Builder} to create an instance of this class.
710
*/
11+
@Builder(
12+
setterPrefix = "set",
13+
builderClassName = "Builder"
14+
)
15+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
816
public class Configuration {
9-
public final boolean allow_noncompliant_unquoted_attribute_values;
10-
public final boolean allow_optimal_entities;
11-
public final boolean allow_removing_spaces_between_attributes;
12-
public final boolean keep_closing_tags;
13-
public final boolean keep_comments;
14-
public final boolean keep_html_and_head_opening_tags;
15-
public final boolean keep_input_type_text_attr;
16-
public final boolean keep_ssi_comments;
17-
public final boolean minify_css;
18-
public final boolean minify_doctype;
19-
public final boolean minify_js;
20-
public final boolean preserve_brace_template_syntax;
21-
public final boolean preserve_chevron_percent_template_syntax;
22-
public final boolean remove_bangs;
23-
public final boolean remove_processing_instructions;
24-
25-
private Configuration(
26-
boolean allow_noncompliant_unquoted_attribute_values,
27-
boolean allow_optimal_entities,
28-
boolean allow_removing_spaces_between_attributes,
29-
boolean keep_closing_tags,
30-
boolean keep_comments,
31-
boolean keep_html_and_head_opening_tags,
32-
boolean keep_input_type_text_attr,
33-
boolean keep_ssi_comments,
34-
boolean minify_css,
35-
boolean minify_doctype,
36-
boolean minify_js,
37-
boolean preserve_brace_template_syntax,
38-
boolean preserve_chevron_percent_template_syntax,
39-
boolean remove_bangs,
40-
boolean remove_processing_instructions
41-
) {
42-
this.allow_noncompliant_unquoted_attribute_values = allow_noncompliant_unquoted_attribute_values;
43-
this.allow_optimal_entities = allow_optimal_entities;
44-
this.allow_removing_spaces_between_attributes = allow_removing_spaces_between_attributes;
45-
this.keep_closing_tags = keep_closing_tags;
46-
this.keep_comments = keep_comments;
47-
this.keep_html_and_head_opening_tags = keep_html_and_head_opening_tags;
48-
this.keep_input_type_text_attr = keep_input_type_text_attr;
49-
this.keep_ssi_comments = keep_ssi_comments;
50-
this.minify_css = minify_css;
51-
this.minify_doctype = minify_doctype;
52-
this.minify_js = minify_js;
53-
this.preserve_brace_template_syntax = preserve_brace_template_syntax;
54-
this.preserve_chevron_percent_template_syntax = preserve_chevron_percent_template_syntax;
55-
this.remove_bangs = remove_bangs;
56-
this.remove_processing_instructions = remove_processing_instructions;
57-
}
58-
59-
/**
60-
* Builder to help create configuration.
61-
*/
62-
public static class Builder {
63-
private boolean allow_noncompliant_unquoted_attribute_values = false;
64-
private boolean allow_optimal_entities = false;
65-
private boolean allow_removing_spaces_between_attributes = false;
66-
private boolean keep_closing_tags = false;
67-
private boolean keep_comments = false;
68-
private boolean keep_html_and_head_opening_tags = false;
69-
private boolean keep_input_type_text_attr = false;
70-
private boolean keep_ssi_comments = false;
71-
private boolean minify_css = false;
72-
private boolean minify_doctype = false;
73-
private boolean minify_js = false;
74-
private boolean preserve_brace_template_syntax = false;
75-
private boolean preserve_chevron_percent_template_syntax = false;
76-
private boolean remove_bangs = false;
77-
private boolean remove_processing_instructions = false;
78-
79-
public Builder setAllowNoncompliantUnquotedAttributeValues(boolean v) {
80-
this.allow_noncompliant_unquoted_attribute_values = v;
81-
return this;
82-
}
83-
public Builder setAllowOptimalEntities(boolean v) {
84-
this.allow_optimal_entities = v;
85-
return this;
86-
}
87-
public Builder setAllowRemovingSpacesBetweenAttributes(boolean v) {
88-
this.allow_removing_spaces_between_attributes = v;
89-
return this;
90-
}
91-
public Builder setKeepClosingTags(boolean v) {
92-
this.keep_closing_tags = v;
93-
return this;
94-
}
95-
public Builder setKeepComments(boolean v) {
96-
this.keep_comments = v;
97-
return this;
98-
}
99-
public Builder setKeepHtmlAndHeadOpeningTags(boolean v) {
100-
this.keep_html_and_head_opening_tags = v;
101-
return this;
102-
}
103-
public Builder setKeepInputTypeTextAttr(boolean v) {
104-
this.keep_input_type_text_attr = v;
105-
return this;
106-
}
107-
public Builder setKeepSsiComments(boolean v) {
108-
this.keep_ssi_comments = v;
109-
return this;
110-
}
111-
public Builder setMinifyCss(boolean v) {
112-
this.minify_css = v;
113-
return this;
114-
}
115-
public Builder setMinifyDoctype(boolean v) {
116-
this.minify_doctype = v;
117-
return this;
118-
}
119-
public Builder setMinifyJs(boolean v) {
120-
this.minify_js = v;
121-
return this;
122-
}
123-
public Builder setPreserveBraceTemplateSyntax(boolean v) {
124-
this.preserve_brace_template_syntax = v;
125-
return this;
126-
}
127-
public Builder setPreserveChevronPercentTemplateSyntax(boolean v) {
128-
this.preserve_chevron_percent_template_syntax = v;
129-
return this;
130-
}
131-
public Builder setRemoveBangs(boolean v) {
132-
this.remove_bangs = v;
133-
return this;
134-
}
135-
public Builder setRemoveProcessingInstructions(boolean v) {
136-
this.remove_processing_instructions = v;
137-
return this;
138-
}
139-
140-
public Configuration build() {
141-
return new Configuration(
142-
this.allow_noncompliant_unquoted_attribute_values,
143-
this.allow_optimal_entities,
144-
this.allow_removing_spaces_between_attributes,
145-
this.keep_closing_tags,
146-
this.keep_comments,
147-
this.keep_html_and_head_opening_tags,
148-
this.keep_input_type_text_attr,
149-
this.keep_ssi_comments,
150-
this.minify_css,
151-
this.minify_doctype,
152-
this.minify_js,
153-
this.preserve_brace_template_syntax,
154-
this.preserve_chevron_percent_template_syntax,
155-
this.remove_bangs,
156-
this.remove_processing_instructions
157-
);
158-
}
159-
}
17+
public final boolean allowNoncompliantUnquotedAttributeValues;
18+
public final boolean allowOptimalEntities;
19+
public final boolean allowRemovingSpacesBetweenAttributes;
20+
public final boolean keepClosingTags;
21+
public final boolean keepComments;
22+
public final boolean keepHtmlAndHeadOpeningTags;
23+
public final boolean keepInputTypeTextAttr;
24+
public final boolean keepSsiComments;
25+
public final boolean minifyCss;
26+
public final boolean minifyDoctype;
27+
public final boolean minifyJs;
28+
public final boolean preserveBraceTemplateSyntax;
29+
public final boolean preserveChevronPercentTemplateSyntax;
30+
public final boolean removeBangs;
31+
public final boolean removeProcessingInstructions;
16032
}

minify-html-java/src/main/java/in/wilsonl/minifyhtml/Configuration.java.gen.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

minify-html-java/src/main/java/in/wilsonl/minifyhtml/MinifyHtml.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,30 @@
22

33
import java.io.File;
44
import java.io.InputStream;
5-
import java.nio.ByteBuffer;
65
import java.nio.file.Files;
76
import java.nio.file.StandardCopyOption;
87

9-
import static java.lang.String.format;
10-
118
/**
129
* Class containing only static methods and exception classes. Cannot be instantiated.
1310
* Methods call to native compiled Rust code using JNI.
1411
* When this class is loaded, a static initialiser will attempt to load a prebuilt native library for the running operating system and architecture from the JAR. If it cannot, a {@link RuntimeException} will be thrown.
1512
*/
1613
public class MinifyHtml {
1714
static {
18-
String osName = System.getProperty("os.name").toLowerCase();
19-
String osArch = System.getProperty("os.arch").toLowerCase();
20-
21-
String nativeLibNameOs = osName.startsWith("windows")
22-
? "win"
23-
: osName.startsWith("linux")
24-
? "linux"
25-
: osName.startsWith("mac")
26-
? "mac"
27-
: null;
28-
String nativeLibNameArch =
29-
osArch.equals("amd64") || osArch.equals("x86_64")
30-
? "x64"
31-
: osArch.equals("arm64") || osArch.equals("aarch64")
32-
? "aarch64"
33-
: null;
15+
final String osName = System.getProperty("os.name").toLowerCase();
16+
final String osArch = System.getProperty("os.arch").toLowerCase();
17+
18+
final String nativeLibNameOs = getNativeLibNameOs(osName);
19+
final String nativeLibNameArch = getNativeLibNameArch(osArch);
3420

3521
if (nativeLibNameOs == null || nativeLibNameArch == null) {
36-
throw new RuntimeException(format("Platform not supported (os.name=%s, os.arch=%s)", osName, osArch));
22+
throw new RuntimeException(String.format("Platform not supported (os.name=%s, os.arch=%s)", osName, osArch));
3723
}
3824

39-
String nativeLibFile = format("/%s-%s.nativelib", nativeLibNameOs, nativeLibNameArch);
25+
final String nativeLibFile = String.format("/%s-%s.nativelib", nativeLibNameOs, nativeLibNameArch);
4026

4127
try (InputStream is = MinifyHtml.class.getResourceAsStream(nativeLibFile)) {
42-
File temp = File.createTempFile("minify-html-java-nativelib", nativeLibFile.substring(1));
28+
final File temp = File.createTempFile("minify-html-java-nativelib", nativeLibFile.substring(1));
4329
temp.deleteOnExit();
4430
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
4531
System.load(temp.getAbsolutePath());
@@ -60,4 +46,26 @@ private MinifyHtml() {
6046
* @return minified HTML code
6147
*/
6248
public static native String minify(String code, Configuration cfg);
49+
50+
private static String getNativeLibNameOs(String osName) {
51+
if (osName.startsWith("windows")) {
52+
return "win";
53+
} else if (osName.startsWith("linux")) {
54+
return "linux";
55+
} else if (osName.startsWith("mac")) {
56+
return "mac";
57+
} else {
58+
return null;
59+
}
60+
}
61+
62+
private static String getNativeLibNameArch(String osArch) {
63+
if (osArch.equals("amd64") || osArch.equals("x86_64")) {
64+
return "x64";
65+
} else if (osArch.equals("arm64") || osArch.equals("aarch64")) {
66+
return "aarch64";
67+
} else {
68+
return null;
69+
}
70+
}
6371
}

minify-html-java/src/main/rust/lib.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ fn build_cfg(env: &JNIEnv, obj: &JObject) -> Cfg {
1111
#[rustfmt::skip]
1212
// This is a statement because "attributes on expressions are experimental".
1313
let cfg = Cfg {
14-
allow_noncompliant_unquoted_attribute_values: env.get_field(*obj, "allow_noncompliant_unquoted_attribute_values", "Z").unwrap().z().unwrap(),
15-
allow_optimal_entities: env.get_field(*obj, "allow_optimal_entities", "Z").unwrap().z().unwrap(),
16-
allow_removing_spaces_between_attributes: env.get_field(*obj, "allow_removing_spaces_between_attributes", "Z").unwrap().z().unwrap(),
17-
keep_closing_tags: env.get_field(*obj, "keep_closing_tags", "Z").unwrap().z().unwrap(),
18-
keep_comments: env.get_field(*obj, "keep_comments", "Z").unwrap().z().unwrap(),
19-
keep_html_and_head_opening_tags: env.get_field(*obj, "keep_html_and_head_opening_tags", "Z").unwrap().z().unwrap(),
20-
keep_input_type_text_attr: env.get_field(*obj, "keep_input_type_text_attr", "Z").unwrap().z().unwrap(),
21-
keep_ssi_comments: env.get_field(*obj, "keep_ssi_comments", "Z").unwrap().z().unwrap(),
22-
minify_css: env.get_field(*obj, "minify_css", "Z").unwrap().z().unwrap(),
23-
minify_doctype: env.get_field(*obj, "minify_doctype", "Z").unwrap().z().unwrap(),
24-
minify_js: env.get_field(*obj, "minify_js", "Z").unwrap().z().unwrap(),
25-
preserve_brace_template_syntax: env.get_field(*obj, "preserve_brace_template_syntax", "Z").unwrap().z().unwrap(),
26-
preserve_chevron_percent_template_syntax: env.get_field(*obj, "preserve_chevron_percent_template_syntax", "Z").unwrap().z().unwrap(),
27-
remove_bangs: env.get_field(*obj, "remove_bangs", "Z").unwrap().z().unwrap(),
28-
remove_processing_instructions: env.get_field(*obj, "remove_processing_instructions", "Z").unwrap().z().unwrap(),
14+
allow_noncompliant_unquoted_attribute_values: env.get_field(*obj, "allowNoncompliantUnquotedAttributeValues", "Z").unwrap().z().unwrap(),
15+
allow_optimal_entities: env.get_field(*obj, "allowOptimalEntities", "Z").unwrap().z().unwrap(),
16+
allow_removing_spaces_between_attributes: env.get_field(*obj, "allowRemovingSpacesBetweenAttributes", "Z").unwrap().z().unwrap(),
17+
keep_closing_tags: env.get_field(*obj, "keepClosingTags", "Z").unwrap().z().unwrap(),
18+
keep_comments: env.get_field(*obj, "keepComments", "Z").unwrap().z().unwrap(),
19+
keep_html_and_head_opening_tags: env.get_field(*obj, "keepHtmlAndHeadOpeningTags", "Z").unwrap().z().unwrap(),
20+
keep_input_type_text_attr: env.get_field(*obj, "keepInputTypeTextAttr", "Z").unwrap().z().unwrap(),
21+
keep_ssi_comments: env.get_field(*obj, "keepSsiComments", "Z").unwrap().z().unwrap(),
22+
minify_css: env.get_field(*obj, "minifyCss", "Z").unwrap().z().unwrap(),
23+
minify_doctype: env.get_field(*obj, "minifyDoctype", "Z").unwrap().z().unwrap(),
24+
minify_js: env.get_field(*obj, "minifyJs", "Z").unwrap().z().unwrap(),
25+
preserve_brace_template_syntax: env.get_field(*obj, "preserveBraceTemplateSyntax", "Z").unwrap().z().unwrap(),
26+
preserve_chevron_percent_template_syntax: env.get_field(*obj, "preserveChevronPercentTemplateSyntax", "Z").unwrap().z().unwrap(),
27+
remove_bangs: env.get_field(*obj, "removeBangs", "Z").unwrap().z().unwrap(),
28+
remove_processing_instructions: env.get_field(*obj, "removeProcessingInstructions", "Z").unwrap().z().unwrap(),
2929
};
3030
cfg
3131
}

0 commit comments

Comments
 (0)