Java Code Examples for org.codehaus.groovy.ast.builder.AstBuilder#buildFromString()

The following examples show how to use org.codehaus.groovy.ast.builder.AstBuilder#buildFromString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BuildGradleParser.java    From synopsys-detect with Apache License 2.0 6 votes vote down vote up
public Optional<DependencyGraph> parse(final InputStream inputStream) {
    final MutableDependencyGraph dependencyGraph = new MutableMapDependencyGraph();

    try {
        final String sourceContents = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        final AstBuilder astBuilder = new AstBuilder();
        final List<ASTNode> nodes = astBuilder.buildFromString(sourceContents);

        final DependenciesVisitor dependenciesVisitor = new DependenciesVisitor(externalIdFactory);
        for (final ASTNode node : nodes) {
            node.visit(dependenciesVisitor);
        }

        final List<Dependency> dependencies = dependenciesVisitor.getDependencies();
        dependencyGraph.addChildrenToRoot(dependencies);

        return Optional.of(dependencyGraph);
    } catch (final IOException e) {
        logger.error("Could not get the build file contents: " + e.getMessage());
    }

    return Optional.empty();
}
 
Example 2
Source File: FindAndroidVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public static final boolean visit(File buildGradle) throws FileNotFoundException, IOException {
    AstBuilder builder = new AstBuilder();
    List<ASTNode> nodes = builder.buildFromString(IOUtils.toString(new FileInputStream(buildGradle), "UTF-8"));
    FindAndroidVisitor visitor = new FindAndroidVisitor();
    for (ASTNode node : nodes) {
        node.visit(visitor);
    }
    return visitor.androidProject;
}
 
Example 3
Source File: FindAndroidVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public static final boolean visit(File buildGradle) throws FileNotFoundException, IOException {
    AstBuilder builder = new AstBuilder();
    List<ASTNode> nodes = builder.buildFromString(IOUtils.toString(new FileInputStream(buildGradle), "UTF-8"));
    FindAndroidVisitor visitor = new FindAndroidVisitor();
    for (ASTNode node : nodes) {
        node.visit(visitor);
    }
    return visitor.androidProject;
}
 
Example 4
Source File: FindRepositoriesVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public static final List<Repository> visit(File buildGradle) throws FileNotFoundException, IOException {
    AstBuilder builder = new AstBuilder();
    List<ASTNode> nodes = builder.buildFromString(IOUtils.toString(new FileInputStream(buildGradle), "UTF-8"));
    FindRepositoriesVisitor visitor = new FindRepositoriesVisitor();
    for (ASTNode node : nodes) {
        node.visit(visitor);
    }
    return visitor.repositories;
}
 
Example 5
Source File: AndroidGradleDependenciesVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public static AndroidGradleDependenciesVisitor parse(File file) throws IOException {
    AstBuilder builder = new AstBuilder();
    AndroidGradleDependenciesVisitor visitor = new AndroidGradleDependenciesVisitor();
    List<ASTNode> nodes = builder.buildFromString(IOUtils.toString(new FileInputStream(file), "UTF-8"));
    for (ASTNode node : nodes) {
        visitor.setRootBlockStatement((BlockStatement) node);
        node.visit(visitor);
    }
    return visitor;
}
 
Example 6
Source File: GithubImporter.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private List<String> getGradleDependencies(GitHubClient client, Repository rep, List<String> gradlePath) {
	List<String> result = new ArrayList<>();
	ContentsService contentService = new ContentsService(client);
	for (String gradleFile : gradlePath) {
		List<RepositoryContents> test;
		try {
			test = contentService.getContents(rep, gradleFile);
			String valueDecoded = "";
			for (RepositoryContents content : test) {
				String fileConent = content.getContent();
				valueDecoded = new String(Base64.decodeBase64(fileConent.getBytes()));
				valueDecoded += System.getProperty("line.separator");
			}
			AstBuilder builder = new AstBuilder();
			List<ASTNode> nodes = builder.buildFromString(valueDecoded);
			DependencyVisitor visitor = new DependencyVisitor();
			walkScript(visitor, nodes);

			List<String> partialResult = visitor.getDependencies();
			if (partialResult != null)
				result.addAll(partialResult);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

	}
	return result;
}