use of org.mozilla.javascript.Parser in project MtgDesktopCompanion by nicho92.
the class MTGoldFishDashBoard method getPriceVariation.
public Map<Date, Double> getPriceVariation(MagicCard mc, MagicEdition me) throws IOException {
stop = false;
String url = "";
Map<Date, Double> historyPrice = new TreeMap<>();
int index = 0;
if (me == null)
me = mc.getEditions().get(0);
if (mc == null) {
url = getString("URL_EDITIONS") + replace(me.getId(), false) + "#" + getString("FORMAT");
index = 6;
} else {
String cardName = StringUtils.replaceAll(mc.getName(), " ", "+");
cardName = StringUtils.replaceAll(cardName, "'", "");
cardName = StringUtils.replaceAll(cardName, ",", "");
if (cardName.indexOf('/') > -1)
cardName = cardName.substring(0, cardName.indexOf('/')).trim();
String editionName = StringUtils.replaceAll(me.toString(), " ", "+");
editionName = StringUtils.replaceAll(editionName, "'", "");
editionName = StringUtils.replaceAll(editionName, ",", "");
editionName = StringUtils.replaceAll(editionName, ":", "");
url = getString("WEBSITE") + "/price/" + convert(editionName) + "/" + cardName + "#" + getString("FORMAT");
index = 8;
}
try {
logger.debug("get shakes from " + url);
Document d = read(url);
Element js = d.getElementsByTag("script").get(index);
AstNode root = new Parser().parse(js.html(), "", 1);
root.visit(visitedNode -> {
if (!stop && visitedNode.toSource().startsWith("d")) {
String val = visitedNode.toSource();
val = StringUtils.replaceAll(val, "d \\+\\= ", "");
val = StringUtils.replaceAll(val, "\\\\n", "");
val = StringUtils.replaceAll(val, ";", "");
val = StringUtils.replaceAll(val, "\"", "");
String[] res = val.split(",");
try {
Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(res[0] + " 00:00");
if (historyPrice.get(date) == null)
historyPrice.put(date, Double.parseDouble(res[1]));
} catch (Exception e) {
MTGLogger.printStackTrace(e);
}
}
if (visitedNode.toSource().startsWith("g =")) {
stop = true;
}
return true;
});
return historyPrice;
} catch (Exception e) {
logger.error(e);
return historyPrice;
}
}
use of org.mozilla.javascript.Parser in project hackpad by dropbox.
the class ParserTest method parse.
private AstRoot parse(String string, boolean jsdoc) {
CompilerEnvirons environment = new CompilerEnvirons();
TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
environment.setErrorReporter(testErrorReporter);
environment.setRecordingComments(true);
environment.setRecordingLocalJsDocComments(jsdoc);
Parser p = new Parser(environment, testErrorReporter);
AstRoot script = p.parse(string, null, 0);
assertTrue(testErrorReporter.hasEncounteredAllErrors());
assertTrue(testErrorReporter.hasEncounteredAllWarnings());
return script;
}
use of org.mozilla.javascript.Parser in project hackpad by dropbox.
the class ParserTest method parseAsReader.
private AstRoot parseAsReader(String string) throws IOException {
CompilerEnvirons environment = new CompilerEnvirons();
TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
environment.setErrorReporter(testErrorReporter);
environment.setRecordingComments(true);
environment.setRecordingLocalJsDocComments(true);
Parser p = new Parser(environment, testErrorReporter);
AstRoot script = p.parse(new StringReader(string), null, 0);
assertTrue(testErrorReporter.hasEncounteredAllErrors());
assertTrue(testErrorReporter.hasEncounteredAllWarnings());
return script;
}
use of org.mozilla.javascript.Parser in project pentaho-kettle by pentaho.
the class ScriptDialog method parseVariables.
// This could be useful for further improvements
public static ScriptNode parseVariables(Context cx, Scriptable scope, String source, String sourceName, int lineno, Object securityDomain) {
// Interpreter compiler = new Interpreter();
CompilerEnvirons evn = new CompilerEnvirons();
// evn.setLanguageVersion(Context.VERSION_1_5);
evn.setOptimizationLevel(-1);
evn.setGeneratingSource(true);
evn.setGenerateDebugInfo(true);
ErrorReporter errorReporter = new ToolErrorReporter(false);
Parser p = new Parser(evn, errorReporter);
// IOException
ScriptNode tree = p.parse(source, "", 0);
new NodeTransformer().transform(tree);
// Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
return tree;
}
use of org.mozilla.javascript.Parser in project pentaho-kettle by pentaho.
the class ScriptValuesModDialog method parseVariables.
// This could be useful for further improvements
public static ScriptNode parseVariables(Context cx, Scriptable scope, String source, String sourceName, int lineno, Object securityDomain) {
// Interpreter compiler = new Interpreter();
CompilerEnvirons evn = new CompilerEnvirons();
// evn.setLanguageVersion(Context.VERSION_1_5);
evn.setOptimizationLevel(-1);
evn.setGeneratingSource(true);
evn.setGenerateDebugInfo(true);
ErrorReporter errorReporter = new ToolErrorReporter(false);
Parser p = new Parser(evn, errorReporter);
// IOException
ScriptNode tree = p.parse(source, "", 0);
new NodeTransformer().transform(tree);
// Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
return tree;
}
Aggregations