opt(ui): js beautifier supports formatting xml
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.stardust.autojs.engine;
|
||||
|
||||
import android.content.res.AssetManager;
|
||||
import android.net.Uri;
|
||||
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
@@ -7,6 +8,7 @@ import org.mozilla.javascript.commonjs.module.provider.ModuleSource;
|
||||
import org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
@@ -21,19 +23,17 @@ import java.util.List;
|
||||
|
||||
public class AssetAndUrlModuleSourceProvider extends UrlModuleSourceProvider {
|
||||
|
||||
private static final String MODULES_PATH = "modules";
|
||||
private android.content.Context mContext;
|
||||
private List<String> mModules;
|
||||
private final URI mBaseURI = URI.create("file:///android_asset/modules");
|
||||
private final URI mBaseURI;
|
||||
private final String mAssetDirPath;
|
||||
private final AssetManager mAssetManager;
|
||||
|
||||
public AssetAndUrlModuleSourceProvider(android.content.Context context, List<URI> list) {
|
||||
public AssetAndUrlModuleSourceProvider(android.content.Context context, String assetDirPath, List<URI> list) {
|
||||
super(list, null);
|
||||
mContext = context;
|
||||
try {
|
||||
mModules = Arrays.asList(mContext.getAssets().list(MODULES_PATH));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
mAssetDirPath = assetDirPath;
|
||||
mBaseURI = URI.create("file:///android_asset/" + assetDirPath);
|
||||
mAssetManager = mContext.getAssets();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -42,10 +42,11 @@ public class AssetAndUrlModuleSourceProvider extends UrlModuleSourceProvider {
|
||||
if (!moduleIdWithExtension.endsWith(".js")) {
|
||||
moduleIdWithExtension += ".js";
|
||||
}
|
||||
if (mModules.contains(moduleIdWithExtension)) {
|
||||
return new ModuleSource(new InputStreamReader(mContext.getAssets().open(MODULES_PATH + "/" + moduleIdWithExtension)), null,
|
||||
URI.create(moduleIdWithExtension), mBaseURI, validator);
|
||||
try {
|
||||
return new ModuleSource(new InputStreamReader(mAssetManager.open(mAssetDirPath + "/" + moduleIdWithExtension)), null,
|
||||
new URI(mBaseURI.toString() + "/" + moduleIdWithExtension), mBaseURI, validator);
|
||||
} catch (FileNotFoundException e) {
|
||||
return super.loadFromPrivilegedLocations(moduleId, validator);
|
||||
}
|
||||
return super.loadFromPrivilegedLocations(moduleId, validator);
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
|
||||
private static final String LOG_TAG = "RhinoJavaScriptEngine";
|
||||
|
||||
private static final String MODULES_PATH = "modules";
|
||||
private static int contextCount = 0;
|
||||
private static StringScriptSource sInitScript;
|
||||
private static final ConcurrentHashMap<Context, RhinoJavaScriptEngine> sContextEngineMap = new ConcurrentHashMap<>();
|
||||
@@ -129,7 +130,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
}
|
||||
|
||||
void initRequireBuilder(Context context, Scriptable scope) {
|
||||
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(mAndroidContext,
|
||||
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(mAndroidContext, MODULES_PATH,
|
||||
Collections.singletonList(new File("/").toURI()));
|
||||
new RequireBuilder()
|
||||
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(provider))
|
||||
|
||||
@@ -4,14 +4,20 @@ import android.content.Context;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import com.stardust.autojs.engine.AssetAndUrlModuleSourceProvider;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import org.mozilla.javascript.Function;
|
||||
import org.mozilla.javascript.ImporterTopLevel;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.mozilla.javascript.commonjs.module.RequireBuilder;
|
||||
import org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@@ -21,6 +27,7 @@ import java.util.concurrent.Executors;
|
||||
|
||||
public class JsBeautifier {
|
||||
|
||||
|
||||
public interface Callback {
|
||||
|
||||
void onSuccess(String beautifiedCode);
|
||||
@@ -34,12 +41,14 @@ public class JsBeautifier {
|
||||
private org.mozilla.javascript.Context mScriptContext;
|
||||
private Scriptable mScriptable;
|
||||
private final String mBeautifyJsPath;
|
||||
private final String mBeautifyJsDir;
|
||||
private View mView;
|
||||
|
||||
public JsBeautifier(View view, String beautifyJsPath) {
|
||||
public JsBeautifier(View view, String beautifyJsDirPath) {
|
||||
mContext = view.getContext();
|
||||
mView = view;
|
||||
mBeautifyJsPath = beautifyJsPath;
|
||||
mBeautifyJsDir = beautifyJsDirPath;
|
||||
mBeautifyJsPath = PFiles.join(beautifyJsDirPath, "beautify.js");
|
||||
}
|
||||
|
||||
public void beautify(final String code, final Callback callback) {
|
||||
@@ -65,11 +74,24 @@ public class JsBeautifier {
|
||||
}
|
||||
|
||||
private void enterContext() {
|
||||
if (mScriptContext == null) {
|
||||
mScriptContext = org.mozilla.javascript.Context.enter();
|
||||
mScriptContext.setLanguageVersion(org.mozilla.javascript.Context.VERSION_1_8);
|
||||
mScriptContext.setOptimizationLevel(-1);
|
||||
if (mScriptContext != null) {
|
||||
return;
|
||||
}
|
||||
mScriptContext = org.mozilla.javascript.Context.enter();
|
||||
mScriptContext.setLanguageVersion(org.mozilla.javascript.Context.VERSION_1_8);
|
||||
mScriptContext.setOptimizationLevel(-1);
|
||||
if (mScriptable == null) {
|
||||
ImporterTopLevel importerTopLevel = new ImporterTopLevel();
|
||||
importerTopLevel.initStandardObjects(mScriptContext, false);
|
||||
mScriptable = importerTopLevel;
|
||||
}
|
||||
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(mContext, mBeautifyJsDir,
|
||||
Collections.singletonList(new File("/").toURI()));
|
||||
new RequireBuilder()
|
||||
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(provider))
|
||||
.setSandboxed(false)
|
||||
.createRequire(mScriptContext, mScriptable)
|
||||
.install(mScriptable);
|
||||
}
|
||||
|
||||
public void prepare() {
|
||||
@@ -93,8 +115,6 @@ public class JsBeautifier {
|
||||
try {
|
||||
enterContext();
|
||||
InputStream is = mContext.getAssets().open(mBeautifyJsPath);
|
||||
if (mScriptable == null)
|
||||
mScriptable = mScriptContext.initSafeStandardObjects();
|
||||
mJsBeautifyFunction = (Function) mScriptContext.evaluateString(mScriptable, PFiles.read(is), "<js_beautify>", 1, null);
|
||||
} catch (IOException e) {
|
||||
exitContext();
|
||||
|
||||
Reference in New Issue
Block a user