Search in sources :

Example 1 with Parser

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;
    }
}
Also used : Element(org.jsoup.nodes.Element) TreeMap(java.util.TreeMap) Document(org.jsoup.nodes.Document) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) Parser(org.mozilla.javascript.Parser) SimpleDateFormat(java.text.SimpleDateFormat) AstNode(org.mozilla.javascript.ast.AstNode)

Example 2 with Parser

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;
}
Also used : TestErrorReporter(org.mozilla.javascript.testing.TestErrorReporter) CompilerEnvirons(org.mozilla.javascript.CompilerEnvirons) Parser(org.mozilla.javascript.Parser)

Example 3 with Parser

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;
}
Also used : TestErrorReporter(org.mozilla.javascript.testing.TestErrorReporter) StringReader(java.io.StringReader) CompilerEnvirons(org.mozilla.javascript.CompilerEnvirons) Parser(org.mozilla.javascript.Parser)

Example 4 with Parser

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;
}
Also used : ToolErrorReporter(org.mozilla.javascript.tools.ToolErrorReporter) ErrorReporter(org.mozilla.javascript.ErrorReporter) NodeTransformer(org.mozilla.javascript.NodeTransformer) CompilerEnvirons(org.mozilla.javascript.CompilerEnvirons) ToolErrorReporter(org.mozilla.javascript.tools.ToolErrorReporter) ScriptNode(org.mozilla.javascript.ast.ScriptNode) Parser(org.mozilla.javascript.Parser)

Example 5 with Parser

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;
}
Also used : ToolErrorReporter(org.mozilla.javascript.tools.ToolErrorReporter) ErrorReporter(org.mozilla.javascript.ErrorReporter) NodeTransformer(org.mozilla.javascript.NodeTransformer) CompilerEnvirons(org.mozilla.javascript.CompilerEnvirons) ToolErrorReporter(org.mozilla.javascript.tools.ToolErrorReporter) ScriptNode(org.mozilla.javascript.ast.ScriptNode) Parser(org.mozilla.javascript.Parser)

Aggregations

Parser (org.mozilla.javascript.Parser)8 CompilerEnvirons (org.mozilla.javascript.CompilerEnvirons)6 ErrorReporter (org.mozilla.javascript.ErrorReporter)2 NodeTransformer (org.mozilla.javascript.NodeTransformer)2 AstNode (org.mozilla.javascript.ast.AstNode)2 ScriptNode (org.mozilla.javascript.ast.ScriptNode)2 TestErrorReporter (org.mozilla.javascript.testing.TestErrorReporter)2 ToolErrorReporter (org.mozilla.javascript.tools.ToolErrorReporter)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 TreeMap (java.util.TreeMap)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1