@ -1,6 +1,13 @@
package com.fr.design.javascript.beautify ;
package com.fr.design.javascript.beautify ;
import com.fr.general.IOUtils ;
import com.fr.log.FineLoggerFactory ;
import com.fr.stable.EncodeConstants ;
import java.io.InputStream ;
import javax.script.Invocable ;
import javax.script.ScriptEngine ;
import javax.script.ScriptEngineManager ;
public class JavaScriptFormatHelper {
public class JavaScriptFormatHelper {
@ -23,24 +30,16 @@ public class JavaScriptFormatHelper {
* @see < a href = "https://github.com/beautify-web/js-beautify" > JSBeautify < a / >
* @see < a href = "https://github.com/beautify-web/js-beautify" > JSBeautify < a / >
* /
* /
public static String beautify ( String jsCode , BeautifyOption option ) {
public static String beautify ( String jsCode , BeautifyOption option ) {
/ * InputStream resourceAsStream = IOUtils . readResource ( "com/fr/design/javascript/beautify/beautify.js" ) ;
String result = jsCode ;
String result = jsCode ;
V8 v8 = V8 . createV8Runtime ( ) ;
try ( InputStream resourceAsStream = IOUtils . readResource ( "com/fr/design/javascript/beautify/beautify.js" ) ) {
try {
ScriptEngine nashorn = new ScriptEngineManager ( ) . getEngineByName ( "JavaScript" ) ;
v8 . executeVoidScript ( IOUtils . inputStream2String ( resourceAsStream , EncodeConstants . ENCODING_UTF_8 ) ) ;
nashorn . eval ( IOUtils . inputStream2String ( resourceAsStream , EncodeConstants . ENCODING_UTF_8 ) ) ;
V8Array parameters = new V8Array ( v8 ) ;
Invocable invocable = ( Invocable ) nashorn ;
parameters . push ( jsCode ) ;
result = ( String ) invocable . invokeFunction ( "js_beautify_global" , new Object [ ] { jsCode , option . toFormatArgument ( ) } ) ;
V8Object arg = V8ObjectUtils . toV8Object ( v8 , option . toFormatArgument ( ) ) ;
} catch ( Exception e ) {
parameters . push ( arg ) ;
result = v8 . executeStringFunction ( "js_beautify_global" , parameters ) ;
parameters . release ( ) ;
arg . release ( ) ;
} catch ( UnsupportedEncodingException e ) {
FineLoggerFactory . getLogger ( ) . error ( e . getMessage ( ) , e ) ;
FineLoggerFactory . getLogger ( ) . error ( e . getMessage ( ) , e ) ;
} finally {
}
v8 . release ( true ) ;
return result ;
} * /
return jsCode ;
}
}
public static void main ( String [ ] args ) {
public static void main ( String [ ] args ) {