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