6: Enable checking package of Java files

This commit is contained in:
Bartłomiej Pluta
2019-04-02 22:14:52 +02:00
parent 1a435d131d
commit 4a70fd1c75
3 changed files with 29 additions and 5 deletions

View File

@@ -1,18 +1,25 @@
package com.bartek.esa.core.archetype; 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.error.EsaException;
import com.bartek.esa.file.matcher.GlobMatcher; import com.bartek.esa.file.matcher.GlobMatcher;
import com.github.javaparser.StaticJavaParser; import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import io.vavr.control.Try; import io.vavr.control.Try;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.xpath.XPathConstants;
import java.io.File; import java.io.File;
public abstract class JavaPlugin extends BasePlugin { public abstract class JavaPlugin extends BasePlugin {
private final GlobMatcher globMatcher; private final GlobMatcher globMatcher;
private final XmlHelper xmlHelper;
public JavaPlugin(GlobMatcher globMatcher) { public JavaPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
this.globMatcher = globMatcher; this.globMatcher = globMatcher;
this.xmlHelper = xmlHelper;
} }
@Override @Override
@@ -22,9 +29,27 @@ public abstract class JavaPlugin extends BasePlugin {
@Override @Override
protected void run(File file) { protected void run(File file) {
if(!isApplicationPackageFile(file)) {
return;
}
CompilationUnit compilationUnit = Try.of(() -> StaticJavaParser.parse(file)).getOrElseThrow(EsaException::new); CompilationUnit compilationUnit = Try.of(() -> StaticJavaParser.parse(file)).getOrElseThrow(EsaException::new);
run(compilationUnit); 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); public abstract void run(CompilationUnit compilationUnit);
} }

View File

@@ -30,11 +30,9 @@ public class PluginExecutor {
private List<Issue> executeForFile(File manifest, File file, Set<Plugin> plugins) { private List<Issue> executeForFile(File manifest, File file, Set<Plugin> plugins) {
Document xmlManifest = xmlHelper.parseXml(manifest); Document xmlManifest = xmlHelper.parseXml(manifest);
return plugins.stream() return plugins.stream()
.peek(plugin -> plugin.update(file, xmlManifest))
.filter(plugin -> plugin.supports(file)) .filter(plugin -> plugin.supports(file))
.map(plugin -> { .map(Plugin::runForIssues)
plugin.update(file, xmlManifest);
return plugin.runForIssues();
})
.flatMap(List::stream) .flatMap(List::stream)
.collect(toList()); .collect(toList());
} }

View File

@@ -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.