From 4a70fd1c75171f81eae2382d9c95883f7f22e0ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Tue, 2 Apr 2019 22:14:52 +0200 Subject: [PATCH] 6: Enable checking package of Java files --- .../bartek/esa/core/archetype/JavaPlugin.java | 27 ++++++++++++++++++- .../esa/core/executor/PluginExecutor.java | 6 ++--- src/main/resources/description.properties | 1 + 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/bartek/esa/core/archetype/JavaPlugin.java b/src/main/java/com/bartek/esa/core/archetype/JavaPlugin.java index f735329..d617c13 100644 --- a/src/main/java/com/bartek/esa/core/archetype/JavaPlugin.java +++ b/src/main/java/com/bartek/esa/core/archetype/JavaPlugin.java @@ -1,18 +1,25 @@ package com.bartek.esa.core.archetype; +import com.bartek.esa.core.model.enumeration.Severity; +import com.bartek.esa.core.xml.XmlHelper; import com.bartek.esa.error.EsaException; import com.bartek.esa.file.matcher.GlobMatcher; import com.github.javaparser.StaticJavaParser; import com.github.javaparser.ast.CompilationUnit; import io.vavr.control.Try; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import javax.xml.xpath.XPathConstants; import java.io.File; public abstract class JavaPlugin extends BasePlugin { private final GlobMatcher globMatcher; + private final XmlHelper xmlHelper; - public JavaPlugin(GlobMatcher globMatcher) { + public JavaPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) { this.globMatcher = globMatcher; + this.xmlHelper = xmlHelper; } @Override @@ -22,9 +29,27 @@ public abstract class JavaPlugin extends BasePlugin { @Override protected void run(File file) { + if(!isApplicationPackageFile(file)) { + return; + } + CompilationUnit compilationUnit = Try.of(() -> StaticJavaParser.parse(file)).getOrElseThrow(EsaException::new); run(compilationUnit); } + private boolean isApplicationPackageFile(File file) { + Document manifest = getManifest(); + Node root = (Node) xmlHelper.xPath(manifest, "/manifest", XPathConstants.NODE); + Node packageValue = root.getAttributes().getNamedItem("package"); + + if(packageValue == null) { + addIssue(Severity.ERROR, ".PACKAGE_LACK", null, null); + return false; + } + + String path = packageValue.getNodeValue().replaceAll("\\.", "/"); + return globMatcher.fileMatchesGlobPattern(file, String.format("**/%s/**", path)); + } + public abstract void run(CompilationUnit compilationUnit); } diff --git a/src/main/java/com/bartek/esa/core/executor/PluginExecutor.java b/src/main/java/com/bartek/esa/core/executor/PluginExecutor.java index c3b585a..10d0041 100644 --- a/src/main/java/com/bartek/esa/core/executor/PluginExecutor.java +++ b/src/main/java/com/bartek/esa/core/executor/PluginExecutor.java @@ -30,11 +30,9 @@ public class PluginExecutor { private List executeForFile(File manifest, File file, Set plugins) { Document xmlManifest = xmlHelper.parseXml(manifest); return plugins.stream() + .peek(plugin -> plugin.update(file, xmlManifest)) .filter(plugin -> plugin.supports(file)) - .map(plugin -> { - plugin.update(file, xmlManifest); - return plugin.runForIssues(); - }) + .map(Plugin::runForIssues) .flatMap(List::stream) .collect(toList()); } diff --git a/src/main/resources/description.properties b/src/main/resources/description.properties index e69de29..39d333c 100644 --- a/src/main/resources/description.properties +++ b/src/main/resources/description.properties @@ -0,0 +1 @@ +com.bartek.esa.core.plugin.JavaPlugin.PACKAGE_LACK=There is no package defined in AndroidManifest.xml file. Please check fix to use this tool. \ No newline at end of file