use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.
the class CommandUtil method getStructDocumentationByPosition.
/**
* Get the Documentation attachment for the struct definition.
* @param bLangPackage BLangPackage built
* @param line Start line of the struct in the source
* @return {@link String} Documentation attachment for the struct
*/
static DocAttachmentInfo getStructDocumentationByPosition(BLangPackage bLangPackage, int line) {
for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
if (topLevelNode instanceof BLangStruct) {
BLangStruct structNode = (BLangStruct) topLevelNode;
DiagnosticPos structPos = CommonUtil.toZeroBasedPosition(structNode.getPosition());
int structStart = structPos.getStartLine();
if (structStart == line) {
return getStructNodeDocumentation(structNode, line);
}
}
}
return null;
}
use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.
the class CommandUtil method getServiceDocumentationByPosition.
/**
* Get the Documentation attachment for the service.
* @param bLangPackage BLangPackage built
* @param line Start line of the service in the source
* @return {@link String} Documentation attachment for the service
*/
static DocAttachmentInfo getServiceDocumentationByPosition(BLangPackage bLangPackage, int line) {
// TODO: Currently resource position is invalid and we use the annotation attachment positions.
for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
if (topLevelNode instanceof BLangService) {
BLangService serviceNode = (BLangService) topLevelNode;
DiagnosticPos servicePos = CommonUtil.toZeroBasedPosition(serviceNode.getPosition());
List<BLangAnnotationAttachment> annotationAttachments = serviceNode.getAnnotationAttachments();
if (!annotationAttachments.isEmpty()) {
DiagnosticPos lastAttachmentPos = CommonUtil.toZeroBasedPosition(annotationAttachments.get(annotationAttachments.size() - 1).getPosition());
if (lastAttachmentPos.getEndLine() < line && line < servicePos.getEndLine()) {
return getServiceNodeDocumentation(serviceNode, lastAttachmentPos.getEndLine() + 1);
}
} else if (servicePos.getStartLine() == line) {
return getServiceNodeDocumentation(serviceNode, line);
}
}
}
return null;
}
use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.
the class SwaggerConverterUtils method generateSwaggerDefinitions.
/**
* This method will generate ballerina string from swagger definition. Since ballerina service definition is super
* set of swagger definition we will take both swagger and ballerina definition and merge swagger changes to
* ballerina definition selectively to prevent data loss
*
* @param ballerinaSource ballerina definition to be process as ballerina definition
* @param serviceName service name
* @return String representation of converted ballerina source
* @throws IOException when error occur while processing input swagger and ballerina definitions.
*/
public static String generateSwaggerDefinitions(String ballerinaSource, String serviceName) throws IOException {
// Get the ballerina model using the ballerina source code.
BFile balFile = new BFile();
balFile.setContent(ballerinaSource);
BLangCompilationUnit topCompilationUnit = SwaggerConverterUtils.getTopLevelNodeFromBallerinaFile(balFile);
String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME);
String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME);
SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias);
String swaggerSource = StringUtils.EMPTY;
for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
if (topLevelNode instanceof BLangService) {
ServiceNode serviceDefinition = (ServiceNode) topLevelNode;
// Generate swagger string for the mentioned service name.
if (StringUtils.isNotBlank(serviceName)) {
if (serviceDefinition.getName().getValue().equals(serviceName)) {
Swagger swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
break;
}
} else {
// If no service name mentioned, then generate swagger definition for the first service.
Swagger swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
break;
}
}
}
return swaggerSource;
}
use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.
the class SwaggerConverterUtils method generateOAS3Definitions.
/**
* This method will generate open API 3.X specification for given ballerina service. Since we will need to
* support both swagger 2.0 and OAS 3.0 it was implemented to convert to swagger by default and convert it
* to OAS on demand.
*
* @param ballerinaSource ballerina source to be converted to swagger/OAS definition
* @param serviceName specific service name within ballerina source that need to map OAS
* @return Generated OAS3 string output.
* @throws IOException When error occurs while converting, parsing input source.
*/
public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException {
// Get the ballerina model using the ballerina source code.
BFile balFile = new BFile();
balFile.setContent(ballerinaSource);
// Create empty swagger object.
Swagger swaggerDefinition = new Swagger();
BLangCompilationUnit topCompilationUnit = SwaggerConverterUtils.getTopLevelNodeFromBallerinaFile(balFile);
String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME);
String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME);
SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias);
String swaggerSource = StringUtils.EMPTY;
for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
if (topLevelNode instanceof BLangService) {
ServiceNode serviceDefinition = (ServiceNode) topLevelNode;
// Generate swagger string for the mentioned service name.
if (StringUtils.isNotBlank(serviceName)) {
if (serviceDefinition.getName().getValue().equals(serviceName)) {
swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
break;
}
} else {
// If no service name mentioned, then generate swagger definition for the first service.
swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
break;
}
}
}
swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
SwaggerConverter converter = new SwaggerConverter();
return Yaml.pretty(converter.readContents(swaggerSource, null, null).getOpenAPI());
}
use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
// Visitor methods
public void visit(BLangPackage pkgNode) {
SymbolEnv pkgEnv = this.symTable.pkgEnvMap.get(pkgNode.symbol);
// Then visit each top-level element sorted using the compilation unit
String fileName = documentServiceContext.get(DocumentServiceKeys.FILE_NAME_KEY);
List<TopLevelNode> topLevelNodes = pkgNode.topLevelNodes.stream().filter(node -> node.getPosition().getSource().getCompilationUnitName().equals(fileName)).collect(Collectors.toList());
if (topLevelNodes.isEmpty()) {
this.setTerminateVisitor(true);
acceptNode(null, null);
} else {
cursorPositionResolver = PackageNodeScopeResolver.class;
topLevelNodes.forEach(topLevelNode -> {
cursorPositionResolver = TopLevelNodeScopeResolver.class;
this.blockOwnerStack.push(pkgNode);
acceptNode((BLangNode) topLevelNode, pkgEnv);
});
}
}
Aggregations