Search in sources :

Example 6 with ModelPackage

use of org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage in project ballerina by ballerina-lang.

the class SwaggerConverterUtils method getTopLevelNodeFromBallerinaFile.

/**
 * Generate ballerina fine from the String definition.
 *
 * @param bFile ballerina string definition
 * @return ballerina file created from ballerina string definition
 * @throws IOException IO exception
 */
public static BLangCompilationUnit getTopLevelNodeFromBallerinaFile(BFile bFile) throws IOException {
    String filePath = bFile.getFilePath();
    String fileName = bFile.getFileName();
    String content = bFile.getContent();
    Path fileRoot = Paths.get(filePath);
    org.wso2.ballerinalang.compiler.tree.BLangPackage model;
    // Sometimes we are getting Ballerina content without a file in the file-system.
    if (!Files.exists(Paths.get(filePath, fileName))) {
        BallerinaFile ballerinaFile = ParserUtils.getBallerinaFileForContent(fileRoot, fileName, content, CompilerPhase.CODE_ANALYZE);
        model = ballerinaFile.getBLangPackage();
    } else {
        BallerinaFile ballerinaFile = ParserUtils.getBallerinaFile(filePath, fileName);
        model = ballerinaFile.getBLangPackage();
    }
    final Map<String, ModelPackage> modelPackage = new HashMap<>();
    ParserUtils.loadPackageMap(Constants.CURRENT_PACKAGE_NAME, model, modelPackage);
    Optional<BLangCompilationUnit> compilationUnit = model.getCompilationUnits().stream().filter(compUnit -> fileName.equals(compUnit.getName())).findFirst();
    return compilationUnit.orElse(null);
}
Also used : Path(java.nio.file.Path) Constants(org.ballerinalang.ballerina.swagger.convertor.Constants) SwaggerConverter(io.swagger.v3.parser.converter.SwaggerConverter) Files(java.nio.file.Files) Yaml(io.swagger.v3.core.util.Yaml) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) Swagger(io.swagger.models.Swagger) BFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BFile) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) IOException(java.io.IOException) HashMap(java.util.HashMap) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) StringUtils(org.apache.commons.lang3.StringUtils) ParserUtils(org.ballerinalang.composer.service.ballerina.parser.service.util.ParserUtils) Collectors(java.util.stream.Collectors) ServiceNode(org.ballerinalang.model.tree.ServiceNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Paths(java.nio.file.Paths) Map(java.util.Map) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) Optional(java.util.Optional) Path(java.nio.file.Path) BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) HashMap(java.util.HashMap) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)

Example 7 with ModelPackage

use of org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage in project ballerina by ballerina-lang.

the class ParserUtils method extractFunction.

/**
 * Extract Functions from ballerina lang.
 *
 * @param packages    packages to send.
 * @param packagePath package path
 * @param function    function.
 */
private static void extractFunction(Map<String, ModelPackage> packages, String packagePath, BLangFunction function) {
    String fileName = function.getPosition().getSource().getCompilationUnitName();
    if (packages.containsKey(packagePath)) {
        ModelPackage modelPackage = packages.get(packagePath);
        List<Parameter> parameters = new ArrayList<>();
        addParameters(parameters, function.getParameters());
        List<Parameter> returnParameters = new ArrayList<>();
        addParameters(returnParameters, function.getReturnParameters());
        List<AnnotationAttachment> annotations = new ArrayList<>();
        addAnnotations(annotations, function.getAnnotationAttachments());
        String receiverType = getReceiverType(function.getReceiver());
        // Check if the function is public or not
        boolean isPublic = function.getFlags().contains(Flag.PUBLIC);
        modelPackage.addFunctionsItem(createNewFunction(function.getName().getValue(), annotations, parameters, returnParameters, receiverType, isPublic, fileName));
    } else {
        ModelPackage modelPackage = new ModelPackage();
        modelPackage.setName(packagePath);
        List<Parameter> parameters = new ArrayList<>();
        addParameters(parameters, function.getParameters());
        List<Parameter> returnParameters = new ArrayList<>();
        addParameters(returnParameters, function.getReturnParameters());
        List<AnnotationAttachment> annotations = new ArrayList<>();
        addAnnotations(annotations, function.getAnnotationAttachments());
        String receiverType = getReceiverType(function.getReceiver());
        // Check if the function is public or not
        boolean isPublic = function.getFlags().contains(Flag.PUBLIC);
        modelPackage.addFunctionsItem(createNewFunction(function.getName().getValue(), annotations, parameters, returnParameters, receiverType, isPublic, fileName));
        packages.put(packagePath, modelPackage);
    }
}
Also used : AnnotationAttachment(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.AnnotationAttachment) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) ArrayList(java.util.ArrayList) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) Parameter(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Parameter)

Example 8 with ModelPackage

use of org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage in project ballerina by ballerina-lang.

the class ParserUtils method extractAnnotation.

/**
 * Extract annotations from ballerina lang.
 *
 * @param packages   packages to send
 * @param annotation annotation
 */
private static void extractAnnotation(Map<String, ModelPackage> packages, String packagePath, BLangAnnotation annotation) {
    if (packages.containsKey(packagePath)) {
        ModelPackage modelPackage = packages.get(packagePath);
        modelPackage.addAnnotationsItem(AnnotationDef.convertToPackageModel(annotation));
    } else {
        ModelPackage modelPackage = new ModelPackage();
        modelPackage.setName(packagePath);
        modelPackage.addAnnotationsItem(AnnotationDef.convertToPackageModel(annotation));
        packages.put(packagePath, modelPackage);
    }
}
Also used : ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage)

Example 9 with ModelPackage

use of org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage in project ballerina by ballerina-lang.

the class ParserUtils method extractConnector.

/**
 * Extract connectors from ballerina lang.
 *
 * @param packages  packages to send
 * @param connector connector
 */
private static void extractConnector(Map<String, ModelPackage> packages, String packagePath, BLangConnector connector) {
    String fileName = connector.getPosition().getSource().getCompilationUnitName();
    if (packages.containsKey(packagePath)) {
        ModelPackage modelPackage = packages.get(packagePath);
        List<Parameter> parameters = new ArrayList<>();
        addParameters(parameters, connector.getParameters());
        List<AnnotationAttachment> annotations = new ArrayList<>();
        addAnnotations(annotations, connector.getAnnotationAttachments());
        List<Action> actions = new ArrayList<>();
        addActions(actions, connector.getActions());
        modelPackage.addConnectorsItem(createNewConnector(connector.getName().getValue(), annotations, actions, parameters, null, fileName));
    } else {
        ModelPackage modelPackage = new ModelPackage();
        modelPackage.setName(packagePath);
        List<Parameter> parameters = new ArrayList<>();
        addParameters(parameters, connector.getParameters());
        List<AnnotationAttachment> annotations = new ArrayList<>();
        addAnnotations(annotations, connector.getAnnotationAttachments());
        List<Action> actions = new ArrayList<>();
        addActions(actions, connector.getActions());
        modelPackage.addConnectorsItem(createNewConnector(connector.getName().getValue(), annotations, actions, parameters, null, fileName));
        packages.put(packagePath, modelPackage);
    }
}
Also used : BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) Action(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Action) AnnotationAttachment(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.AnnotationAttachment) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) ArrayList(java.util.ArrayList) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) Parameter(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Parameter)

Aggregations

ModelPackage (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage)9 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BallerinaFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile)2 AnnotationAttachment (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.AnnotationAttachment)2 Parameter (org.ballerinalang.composer.service.ballerina.parser.service.model.lang.Parameter)2 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)2 BLangCompilationUnit (org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)2 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Swagger (io.swagger.models.Swagger)1 Yaml (io.swagger.v3.core.util.Yaml)1 SwaggerConverter (io.swagger.v3.parser.converter.SwaggerConverter)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1