Search in sources :

Example 81 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class Generator method createDocForNode.

/**
 * Create documentation for annotations.
 * @param annotationNode ballerina annotation node.
 * @return documentation for annotation.
 */
public static AnnotationDoc createDocForNode(BLangAnnotation annotationNode) {
    String annotationName = annotationNode.getName().getValue();
    List<Variable> attributes = new ArrayList<>();
    // Iterate through the attributes of the annotation
    if (annotationNode.getAttributes().size() > 0) {
        for (BLangAnnotAttribute annotAttribute : annotationNode.getAttributes()) {
            String dataType = getTypeName(annotAttribute.getTypeNode());
            String desc = annotFieldAnnotation(annotationNode, annotAttribute);
            Variable variable = new Variable(annotAttribute.getName().value, dataType, desc);
            attributes.add(variable);
        }
    }
    return new AnnotationDoc(annotationName, description(annotationNode), new ArrayList<>(), attributes);
}
Also used : AnnotationDoc(org.ballerinalang.docgen.model.AnnotationDoc) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Variable(org.ballerinalang.docgen.model.Variable) ArrayList(java.util.ArrayList) BLangAnnotAttribute(org.wso2.ballerinalang.compiler.tree.BLangAnnotAttribute)

Example 82 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class Generator method createDocForNode.

/**
 * Create documentation for structs.
 * @param structNode ballerina struct node.
 * @return documentation for structs.
 */
public static StructDoc createDocForNode(BLangStruct structNode) {
    String structName = structNode.getName().value;
    // Check if its an anonymous struct
    if (structName.contains(ANONYMOUS_STRUCT)) {
        structName = "Anonymous Struct";
    }
    List<Field> fields = new ArrayList<>();
    // Iterate through the struct fields
    if (structNode.getFields().size() > 0) {
        for (BLangVariable param : structNode.getFields()) {
            String dataType = type(param);
            String desc = fieldAnnotation(structNode, param);
            String defaultValue = "";
            if (null != param.getInitialExpression()) {
                defaultValue = param.getInitialExpression().toString();
            }
            Field variable = new Field(param.getName().value, dataType, desc, defaultValue);
            fields.add(variable);
        }
    }
    return new StructDoc(structName, description(structNode), new ArrayList<>(), fields);
}
Also used : Field(org.ballerinalang.docgen.model.Field) ArrayList(java.util.ArrayList) StructDoc(org.ballerinalang.docgen.model.StructDoc) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 83 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class Generator method createDocForNode.

/**
 * Create documentation for actions.
 * @param actionNode ballerina action node.
 * @return documentation for actions.
 */
public static ActionDoc createDocForNode(BLangAction actionNode) {
    String actionName = actionNode.getName().value;
    List<Variable> parameters = new ArrayList<>();
    List<Variable> returnParams = new ArrayList<>();
    // Iterate through the parameters
    if (actionNode.getParameters().size() > 0) {
        for (BLangVariable param : actionNode.getParameters()) {
            String dataType = type(param);
            String desc = paramAnnotation(actionNode, param);
            Variable variable = new Variable(param.getName().value, dataType, desc);
            parameters.add(variable);
        }
    }
    // Iterate through the return types
    if (actionNode.getReturnParameters().size() > 0) {
        for (int i = 0; i < actionNode.getReturnParameters().size(); i++) {
            BLangVariable returnParam = actionNode.getReturnParameters().get(i);
            String dataType = type(returnParam);
            String desc = returnParamAnnotation(actionNode, i);
            Variable variable = new Variable(returnParam.getName().value, dataType, desc);
            returnParams.add(variable);
        }
    }
    return new ActionDoc(actionName, description(actionNode), new ArrayList<>(), parameters, returnParams);
}
Also used : ActionDoc(org.ballerinalang.docgen.model.ActionDoc) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Variable(org.ballerinalang.docgen.model.Variable) ArrayList(java.util.ArrayList) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 84 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class SwaggerConverterUtils method getAlias.

/**
 * Gets the alias for a given package from a bLang file root node.
 * @param topCompilationUnit The root node.
 * @param packageName The package name.
 * @return The alias.
 */
private static String getAlias(BLangCompilationUnit topCompilationUnit, String packageName) {
    for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
        if (topLevelNode instanceof BLangImportPackage) {
            BLangImportPackage importPackage = (BLangImportPackage) topLevelNode;
            String packagePath = importPackage.getPackageName().stream().map(BLangIdentifier::getValue).collect(Collectors.joining("."));
            if (packageName.equals(packagePath)) {
                return importPackage.getAlias().getValue();
            }
        }
    }
    return null;
}
Also used : BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 85 with Node

use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.

the class ResourceContextHolder method buildContext.

public static ResourceContextHolder buildContext(ResourceNode resource) throws CodeGeneratorException {
    ResourceContextHolder context = new ResourceContextHolder();
    context.name = resource.getName().getValue();
    context.parameters = new ArrayList<>();
    for (VariableNode node : resource.getParameters()) {
        ParameterContextHolder parameter = ParameterContextHolder.buildContext(node);
        if (parameter != null) {
            context.parameters.add(parameter);
        }
    }
    // Iterate through all resource level annotations and find out resource configuration information
    AnnotationAttachmentNode ann = GeneratorUtils.getAnnotationFromList(GeneratorConstants.RES_CONFIG_ANNOTATION, GeneratorConstants.HTTP_PKG_ALIAS, resource.getAnnotationAttachments());
    if (ann == null) {
        throw new CodeGeneratorException("Incomplete resource configuration found");
    }
    BLangRecordLiteral bLiteral = ((BLangRecordLiteral) ((BLangAnnotationAttachment) ann).getExpression());
    List<BLangRecordLiteral.BLangRecordKeyValue> list = bLiteral.getKeyValuePairs();
    Map<String, String[]> attrs = GeneratorUtils.getKeyValuePairAsMap(list);
    // We don't expect multiple http methods to be supported by single action
    // We only consider first content type for a single resource
    context.method = attrs.get(GeneratorConstants.ATTR_METHODS) != null ? attrs.get(GeneratorConstants.ATTR_METHODS)[0] : null;
    context.contentType = attrs.get(GeneratorConstants.ATTR_CONSUMES) != null ? attrs.get(GeneratorConstants.ATTR_CONSUMES)[0] : null;
    String path = attrs.get(GeneratorConstants.ATTR_PATH) != null ? attrs.get(GeneratorConstants.ATTR_PATH)[0] : null;
    context.path = context.getTemplatePath(path);
    return context;
}
Also used : VariableNode(org.ballerinalang.model.tree.VariableNode) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Aggregations

ArrayList (java.util.ArrayList)19 Node (org.w3c.dom.Node)13 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)13 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)13 List (java.util.List)12 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)11 Map (java.util.Map)10 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)10 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)10 QName (javax.xml.namespace.QName)8 NodeList (org.w3c.dom.NodeList)8 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 Name (org.wso2.ballerinalang.compiler.util.Name)8 Arrays (java.util.Arrays)7 HashMap (java.util.HashMap)7 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)7 Node (org.ballerinalang.model.tree.Node)7 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)7 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)7 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)7