/*
 * Copyright (c) 2015-2016 Mozilla Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

package nu.validator.datatype.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.Map;

import javax.script.*;

public class CssParser {

    private static Invocable cssparser;

    static {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    CssParser.class.getClassLoader().getResourceAsStream(
                            "nu/validator/localentities/files/parse-css-js")));
            br.mark(1);
            try {
                ScriptEngine engine = new ScriptEngineManager().getEngineByName(
                        "nashorn");
                engine.eval(br);
                cssparser = (Invocable) engine;
            } catch (ScriptException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
        }
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> tokenize(CharSequence cs) throws ParseException {
        try {
            return (Map<String, Object>) cssparser.invokeFunction("tokenize",
                    cs.toString());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (ScriptException e) {
            throw new ParseException(e.getMessage(), -1);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> parseARule(CharSequence cs)
            throws ParseException {
        try {
            return (Map<String, Object>) cssparser.invokeFunction("parseARule",
                    cs.toString());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (ScriptException e) {
            throw new ParseException(e.getMessage(), -1);
        }
        return null;
    }

}

@@ -1195,36 +1195,8 @@
 er;%0A
-import java.io.IOException;%0A
 impo
@@ -1389,33 +1389,16 @@
 c %7B%0A
-%0A        try %7B%0A  
         
   Bu
@@ -1393,18 +1393,16 @@
         
-  
 Buffered
@@ -1471,20 +1471,16 @@
         
-    
 CssParse
@@ -1549,20 +1549,16 @@
         
-  
-  
 %22nu/vali
@@ -1605,36 +1605,8 @@
 ));%0A
-            br.mark(1);%0A    
     
@@ -1607,36 +1607,32 @@
 ;%0A        try %7B%0A
-    
             Scri
@@ -1673,16 +1673,20 @@
 Manager(
+null
 ).getEng
@@ -1716,20 +1716,16 @@
         
-   
- 
 %22nashorn
@@ -1728,20 +1728,16 @@
 horn%22);%0A
-    
         
@@ -1757,20 +1757,16 @@
 al(br);%0A
-    
         
@@ -1805,28 +1805,24 @@
 ne;%0A        
-    
 %7D catch (Scr
@@ -1831,36 +1831,32 @@
 tException e) %7B%0A
-    
             e.pr
@@ -1876,56 +1876,8 @@
 ();%0A
-            %7D%0A        %7D catch (IOException e) %7B%0A
     

Add null arg to ScriptEngineManager() in CssParser