Update Java library
This commit is contained in:
parent
4a60784bbc
commit
cc33290335
|
@ -4,33 +4,100 @@ package in.wilsonl.minifyhtml;
|
|||
* Class representing minification configuration.
|
||||
*/
|
||||
public class Configuration {
|
||||
private final boolean minifyJs;
|
||||
private final boolean minifyCss;
|
||||
public final boolean keep_closing_tags;
|
||||
public final boolean keep_comments;
|
||||
public final boolean keep_html_and_head_opening_tags;
|
||||
public final boolean keep_spaces_between_attributes;
|
||||
public final boolean minify_css;
|
||||
public final boolean minify_js;
|
||||
public final boolean remove_bangs;
|
||||
public final boolean remove_processing_instructions;
|
||||
|
||||
public Configuration(boolean minifyJs, boolean minifyCss) {
|
||||
this.minifyJs = minifyJs;
|
||||
this.minifyCss = minifyCss;
|
||||
public Configuration(
|
||||
boolean keep_closing_tags,
|
||||
boolean keep_comments,
|
||||
boolean keep_html_and_head_opening_tags,
|
||||
boolean keep_spaces_between_attributes,
|
||||
boolean minify_css,
|
||||
boolean minify_js,
|
||||
boolean remove_bangs,
|
||||
boolean remove_processing_instructions
|
||||
) {
|
||||
this.keep_closing_tags = keep_closing_tags;
|
||||
this.keep_comments = keep_comments;
|
||||
this.keep_html_and_head_opening_tags = keep_html_and_head_opening_tags;
|
||||
this.keep_spaces_between_attributes = keep_spaces_between_attributes;
|
||||
this.minify_css = minify_css;
|
||||
this.minify_js = minify_js;
|
||||
this.remove_bangs = remove_bangs;
|
||||
this.remove_processing_instructions = remove_processing_instructions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder to help create configuration.
|
||||
*/
|
||||
public static class Builder {
|
||||
private boolean minifyJs = false;
|
||||
private boolean minifyCss = false;
|
||||
private boolean keep_closing_tags = false;
|
||||
private boolean keep_comments = false;
|
||||
private boolean keep_html_and_head_opening_tags = false;
|
||||
private boolean keep_spaces_between_attributes = false;
|
||||
private boolean minify_css = false;
|
||||
private boolean minify_js = false;
|
||||
private boolean remove_bangs = false;
|
||||
private boolean remove_processing_instructions = false;
|
||||
|
||||
public Builder setMinifyJs(boolean minifyJs) {
|
||||
this.minifyJs = minifyJs;
|
||||
public Builder setKeepClosingTags(boolean val) {
|
||||
this.keep_closing_tags = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMinifyCss(boolean minifyCss) {
|
||||
this.minifyCss = minifyCss;
|
||||
public Builder setKeepComments(boolean val) {
|
||||
this.keep_comments = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setKeepHtmlAndHeadOpeningTags(boolean val) {
|
||||
this.keep_html_and_head_opening_tags = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setKeepSpacesBetweenAttributes(boolean val) {
|
||||
this.keep_spaces_between_attributes = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMinifyCss(boolean val) {
|
||||
this.minify_css = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMinifyJs(boolean val) {
|
||||
this.minify_js = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setRemoveBangs(boolean val) {
|
||||
this.remove_bangs = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setRemoveProcessingInstructions(boolean val) {
|
||||
this.remove_processing_instructions = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Configuration build() {
|
||||
return new Configuration(this.minifyJs, this.minifyCss);
|
||||
return new Configuration(
|
||||
this.keep_closing_tags,
|
||||
this.keep_comments,
|
||||
this.keep_html_and_head_opening_tags,
|
||||
this.keep_spaces_between_attributes,
|
||||
this.minify_css,
|
||||
this.minify_js,
|
||||
this.remove_bangs,
|
||||
this.remove_processing_instructions
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,22 +46,9 @@ public class MinifyHtml {
|
|||
private MinifyHtml() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Minify UTF-8 HTML code contents of a {@link ByteBuffer} instance in place.
|
||||
* The backing data will be mutated. Returns the length of the minified portion of the ByteBuffer.
|
||||
* The ByteBuffer must be direct, otherwise {@link IllegalArgumentException} will be thrown.
|
||||
* If the code fails to be minified, a {@link SyntaxException} will be thrown with a descriptive English message and position in code where the error occurred.
|
||||
*
|
||||
* @param code {@link ByteBuffer} containing HTML code to minify
|
||||
* @param cfg {@link Configuration} minification settings to use
|
||||
* @return length of the written minified code in the {@link ByteBuffer}
|
||||
*/
|
||||
public static native int minifyInPlace(ByteBuffer code, Configuration cfg);
|
||||
|
||||
/**
|
||||
* Minify HTML code represented as a {@link String}.
|
||||
* The {@link String} will be copied to a UTF-8 byte array in native code, and then copied back into a Java {@link String}.
|
||||
* If the code fails to be minified, a {@link SyntaxException} will be thrown with a descriptive English message and position in code where the error occurred.
|
||||
*
|
||||
* @param code HTML code to minify
|
||||
* @param cfg {@link Configuration} minification settings to use
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
package in.wilsonl.minifyhtml;
|
||||
|
||||
/**
|
||||
* Basic exception class representing minification errors.
|
||||
*/
|
||||
public class SyntaxException extends RuntimeException {
|
||||
private SyntaxException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -1,49 +1,25 @@
|
|||
use minify_html::{in_place as minify_html_native, Cfg, Error};
|
||||
use minify_html::{minify as minify_html_native, Cfg};
|
||||
use jni::JNIEnv;
|
||||
use jni::objects::{JByteBuffer, JClass, JObject, JString};
|
||||
use jni::sys::{jint, jstring};
|
||||
use std::str::from_utf8_unchecked;
|
||||
|
||||
const SYNTAX_EXCEPTION_CLASS: &str = "in/wilsonl/minifyhtml/SyntaxException";
|
||||
use jni::objects::{ JClass, JObject, JString};
|
||||
use jni::sys::{ jstring};
|
||||
use std::str::from_utf8;
|
||||
|
||||
fn build_cfg(
|
||||
env: &JNIEnv,
|
||||
obj: &JObject,
|
||||
) -> Cfg {
|
||||
Cfg {
|
||||
minify_js: env.get_field(*obj, "minifyJs", "Z").unwrap().z().unwrap(),
|
||||
minify_css: env.get_field(*obj, "minifyCss", "Z").unwrap().z().unwrap(),
|
||||
keep_closing_tags: env.get_field(*obj, "keep_closing_tags", "Z").unwrap().z().unwrap(),
|
||||
keep_comments: env.get_field(*obj, "keep_comments", "Z").unwrap().z().unwrap(),
|
||||
keep_html_and_head_opening_tags: env.get_field(*obj, "keep_html_and_head_opening_tags", "Z").unwrap().z().unwrap(),
|
||||
keep_spaces_between_attributes: env.get_field(*obj, "keep_spaces_between_attributes", "Z").unwrap().z().unwrap(),
|
||||
minify_css: env.get_field(*obj, "minify_css", "Z").unwrap().z().unwrap(),
|
||||
minify_js: env.get_field(*obj, "minify_js", "Z").unwrap().z().unwrap(),
|
||||
remove_bangs: env.get_field(*obj, "remove_bangs", "Z").unwrap().z().unwrap(),
|
||||
remove_processing_instructions: env.get_field(*obj, "remove_processing_instructions", "Z").unwrap().z().unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_in_wilsonl_minifyhtml_MinifyHtml_minifyInPlace(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
input: JByteBuffer,
|
||||
cfg: JObject,
|
||||
)
|
||||
-> jint {
|
||||
let source = match env.get_direct_buffer_address(input) {
|
||||
Ok(ptr) => ptr,
|
||||
Err(_) => {
|
||||
env.throw_new("java/lang/IllegalArgumentException", "ByteBuffer is not direct").unwrap();
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
(match minify_html_native(source, &build_cfg(&env, &cfg)) {
|
||||
Ok(out_len) => out_len,
|
||||
Err(Error { error_type, position }) => {
|
||||
env.throw_new(
|
||||
SYNTAX_EXCEPTION_CLASS,
|
||||
format!("{} [Character {}]", error_type.message(), position),
|
||||
).unwrap();
|
||||
0
|
||||
}
|
||||
}) as jint
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_in_wilsonl_minifyhtml_MinifyHtml_minify(
|
||||
env: JNIEnv,
|
||||
|
@ -55,14 +31,7 @@ pub extern "system" fn Java_in_wilsonl_minifyhtml_MinifyHtml_minify(
|
|||
let source: String = env.get_string(input).unwrap().into();
|
||||
let mut code = source.into_bytes();
|
||||
|
||||
match minify_html_native(&mut code, &build_cfg(&env, &cfg)) {
|
||||
Ok(out_len) => env.new_string(unsafe { from_utf8_unchecked(&code[0..out_len]) }).unwrap().into_inner(),
|
||||
Err(Error { error_type, position }) => {
|
||||
env.throw_new(
|
||||
SYNTAX_EXCEPTION_CLASS,
|
||||
format!("{} [Character {}]", error_type.message(), position),
|
||||
).unwrap();
|
||||
JObject::null().into_inner()
|
||||
}
|
||||
}
|
||||
let out_code = minify_html_native(&mut code, &build_cfg(&env, &cfg));
|
||||
let out_code_str = from_utf8(&out_code).unwrap();
|
||||
env.new_string(out_code_str).unwrap().into_inner()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue