1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | |
22 | package com.github.javaparser.utils; |
23 | |
24 | import com.github.javaparser.JavaParser; |
25 | import com.github.javaparser.ParseProblemException; |
26 | import com.github.javaparser.ParseResult; |
27 | import com.github.javaparser.ParserConfiguration; |
28 | import com.github.javaparser.ast.CompilationUnit; |
29 | |
30 | import java.io.IOException; |
31 | import java.nio.file.FileSystems; |
32 | import java.nio.file.Path; |
33 | import java.nio.file.PathMatcher; |
34 | import java.util.Optional; |
35 | |
36 | /** |
37 | * A strategy for discovering the structure of a project. |
38 | * Implementations could read a pom.xml, a Gradle build file, a makefile... |
39 | */ |
40 | public interface CollectionStrategy { |
41 | |
42 | ParserConfiguration getParserConfiguration(); |
43 | |
44 | ProjectRoot collect(Path path); |
45 | |
46 | default Optional<Path> getRoot(Path file) { |
47 | try { |
48 | final JavaParser javaParser = new JavaParser(getParserConfiguration()); |
49 | final ParseResult<CompilationUnit> parseResult = javaParser.parse(file); |
50 | |
51 | if (parseResult.isSuccessful()) { |
52 | if (parseResult.getProblems().isEmpty()) { |
53 | if (parseResult.getResult().isPresent()) { |
54 | final CompilationUnit compilationUnit = parseResult.getResult().get(); |
55 | final Optional<CompilationUnit.Storage> storage = compilationUnit.getStorage(); |
56 | if (storage.isPresent()) { |
57 | final Optional<Path> sourceRootPath = storage.map(CompilationUnit.Storage::getSourceRoot); |
58 | return sourceRootPath; |
59 | } else { |
60 | Log.info("Storage information not present -- an issue with providing a string rather than file reference?"); |
61 | } |
62 | } else { |
63 | Log.info("Parse result not present"); |
64 | } |
65 | } else { |
66 | Log.info("There were (%d) problems parsing file: %s", () -> parseResult.getProblems().size(), () -> parseResult.getProblems()); |
67 | } |
68 | } else { |
69 | Log.info("Parsing was not successful."); |
70 | Log.info("There were (%d) problems parsing file: %s", () -> parseResult.getProblems().size(), () -> parseResult.getProblems()); |
71 | } |
72 | } catch (ParseProblemException e) { |
73 | Log.info("Problem parsing file %s", () -> file); |
74 | } catch (RuntimeException e) { |
75 | Log.info("Could not parse file %s", () -> file); |
76 | } catch (IOException e) { |
77 | Log.info("Could not read file %s", () -> file); |
78 | } |
79 | return Optional.empty(); |
80 | } |
81 | |
82 | default PathMatcher getPathMatcher(String pattern) { |
83 | return FileSystems.getDefault().getPathMatcher(pattern); |
84 | } |
85 | } |
86 |
Members