Java Script getBallerinaFromJsonModel(String jsonModel)

Here you can find the source of getBallerinaFromJsonModel(String jsonModel)

Description

Generate ballerina source from json model using nashorn

License

Open Source License

Parameter

Parameter Description
jsonModel ballerina source in json

Exception

Parameter Description
FileNotFoundException throws if js source couldn't is not found
ScriptException throws if scripting related exception occured

Return

ballerina source

Declaration

public static String getBallerinaFromJsonModel(String jsonModel) throws FileNotFoundException, ScriptException 

Method Source Code

//package com.java2s;
/*//from   ww  w  .ja v  a 2  s. c  o  m
 * Copyright (c) 2017, WSO2 Inc. (http://wso2.com) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileReader;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import java.io.FileNotFoundException;

import java.util.ArrayList;

public class Main {
    /**
     * Generate ballerina source from json model using nashorn
     *
     * @param jsonModel ballerina source in json
     * @return ballerina source
     * @throws FileNotFoundException throws if js source couldn't is not found
     * @throws ScriptException       throws if scripting related exception occured
     */
    public static String getBallerinaFromJsonModel(String jsonModel) throws FileNotFoundException, ScriptException {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine engine = engineManager.getEngineByName("nashorn");
        ArrayList<String> paths = new ArrayList<>();
        String balHome = System.getProperty("bal.composer.home");
        String basePath = balHome + File.separator + "resources" + File.separator + "composer" + File.separator
                + "web";
        paths.add(balHome + File.separator + "resources" + File.separator + "composer" + File.separator + "web"
                + File.separator + "js" + File.separator + "source-gen-script" + File.separator + "readfully.js");
        paths.add(balHome + File.separator + "resources" + File.separator + "composer" + File.separator + "web"
                + File.separator + "js" + File.separator + "source-gen-script" + File.separator + "r.js");
        paths.add(balHome + File.separator + "resources" + File.separator + "composer" + File.separator + "web"
                + File.separator + "js" + File.separator + "source-gen-script" + File.separator
                + "generate-from-model.js");
        Bindings bind = new SimpleBindings();
        bind.put("jsonModel", jsonModel);
        bind.put("basePath", basePath);
        engine.setBindings(bind, ScriptContext.GLOBAL_SCOPE);
        for (String p : paths) {
            engine.eval(new FileReader(p));
        }
        Object result = engine.get("result");
        return result.toString();
    }
}

Related

  1. executeAppleScript(String script)
  2. executeScript(String script)
  3. executeScript(String strScript)
  4. fillStringCollection(ScriptObjectMirror som, Collection collection)
  5. generateToken(String val)
  6. getDynamicInput(String key)
  7. getScript(String name)
  8. getScriptCompileMsg(ScriptException ex)
  9. getSpParamOfWeiboLogin(String pwd, String servicetime, String nonce)