5: Add Manifest to plugins

This commit is contained in:
Bartłomiej Pluta
2019-04-02 14:31:25 +02:00
parent f6cbf89dbe
commit f2003389b5
8 changed files with 72 additions and 26 deletions

View File

@@ -1,5 +1,6 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.core.xml.XmlHelper;
import com.bartek.esa.file.matcher.GlobMatcher;
import java.io.File;
@@ -7,8 +8,8 @@ import java.io.File;
public abstract class AndroidManifestPlugin extends XmlPlugin {
private final GlobMatcher globMatcher;
public AndroidManifestPlugin(GlobMatcher globMatcher) {
super(globMatcher);
public AndroidManifestPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
super(globMatcher, xmlHelper);
this.globMatcher = globMatcher;
}

View File

@@ -2,6 +2,7 @@ package com.bartek.esa.core.archetype;
import com.bartek.esa.core.model.enumeration.Severity;
import com.bartek.esa.core.model.object.Issue;
import org.w3c.dom.Document;
import java.io.File;
import java.util.ArrayList;
@@ -9,11 +10,13 @@ import java.util.List;
public abstract class BasePlugin implements Plugin {
private List<Issue> issues = new ArrayList<>();
private Document manifest;
private File file;
@Override
public void update(File file) {
public void update(File file, Document manifest) {
this.file = file;
this.manifest = manifest;
this.issues.clear();
}
@@ -45,4 +48,8 @@ public abstract class BasePlugin implements Plugin {
protected File getOriginalFile() {
return file;
}
protected Document getManifest() {
return manifest;
}
}

View File

@@ -1,12 +1,13 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.core.model.object.Issue;
import org.w3c.dom.Document;
import java.io.File;
import java.util.List;
public interface Plugin {
boolean supports(File file);
void update(File file);
void update(File file, Document manifest);
List<Issue> runForIssues();
}

View File

@@ -1,5 +1,6 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.core.xml.XmlHelper;
import com.bartek.esa.file.matcher.GlobMatcher;
import java.io.File;
@@ -7,8 +8,8 @@ import java.io.File;
public abstract class ResourceLayoutPlugin extends XmlPlugin {
private final GlobMatcher globMatcher;
public ResourceLayoutPlugin(GlobMatcher globMatcher) {
super(globMatcher);
public ResourceLayoutPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
super(globMatcher, xmlHelper);
this.globMatcher = globMatcher;
}

View File

@@ -1,21 +1,19 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.error.EsaException;
import com.bartek.esa.core.xml.XmlHelper;
import com.bartek.esa.file.matcher.GlobMatcher;
import io.vavr.control.Try;
import org.w3c.dom.Document;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathFactory;
import java.io.File;
public abstract class XmlPlugin extends BasePlugin {
private final GlobMatcher globMatcher;
private final XmlHelper xmlHelper;
public XmlPlugin(GlobMatcher globMatcher) {
public XmlPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
this.globMatcher = globMatcher;
this.xmlHelper = xmlHelper;
}
@Override
@@ -25,16 +23,13 @@ public abstract class XmlPlugin extends BasePlugin {
@Override
protected void run(File file) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newDefaultInstance();
DocumentBuilder builder = Try.of(factory::newDocumentBuilder).getOrElseThrow(EsaException::new);
Document document = Try.of(() -> builder.parse(file)).getOrElseThrow(EsaException::new);
run(document);
Document xml = xmlHelper.parseXml(file);
run(xml);
}
protected abstract void run(Document xml);
protected Object xPath(Document xml, String expression, QName returnType) {
return Try.of(() -> XPathFactory.newDefaultInstance().newXPath().evaluate(expression, xml, returnType))
.getOrElseThrow(EsaException::new);
return xmlHelper.xPath(xml, expression, returnType);
}
}

View File

@@ -3,6 +3,7 @@ package com.bartek.esa.core.di;
import com.bartek.esa.core.desc.provider.DescriptionProvider;
import com.bartek.esa.core.executor.PluginExecutor;
import com.bartek.esa.core.java.JavaSyntaxRegexProvider;
import com.bartek.esa.core.xml.XmlHelper;
import dagger.Module;
import dagger.Provides;
@@ -10,8 +11,8 @@ import dagger.Provides;
public class CoreModule {
@Provides
public PluginExecutor pluginExecutor() {
return new PluginExecutor();
public PluginExecutor pluginExecutor(XmlHelper xmlHelper) {
return new PluginExecutor(xmlHelper);
}
@Provides
@@ -23,4 +24,9 @@ public class CoreModule {
public DescriptionProvider descriptionProvider() {
return new DescriptionProvider();
}
@Provides
public XmlHelper xmlHelper() {
return new XmlHelper();
}
}

View File

@@ -2,6 +2,8 @@ package com.bartek.esa.core.executor;
import com.bartek.esa.core.archetype.Plugin;
import com.bartek.esa.core.model.object.Issue;
import com.bartek.esa.core.xml.XmlHelper;
import org.w3c.dom.Document;
import javax.inject.Inject;
import java.io.File;
@@ -10,24 +12,26 @@ import java.util.List;
import static java.util.stream.Collectors.toList;
public class PluginExecutor {
private final XmlHelper xmlHelper;
@Inject
public PluginExecutor() {
public PluginExecutor(XmlHelper xmlHelper) {
this.xmlHelper = xmlHelper;
}
public List<Issue> executeForFiles(List<File> files, List<Plugin> plugins) {
public List<Issue> executeForFiles(File manifest, List<File> files, List<Plugin> plugins) {
return files.stream()
.map(file -> executeForFile(file, plugins))
.map(file -> executeForFile(manifest, file, plugins))
.flatMap(List::stream)
.collect(toList());
}
private List<Issue> executeForFile(File file, List<Plugin> plugins) {
private List<Issue> executeForFile(File manifest, File file, List<Plugin> plugins) {
Document xmlManifest = xmlHelper.parseXml(manifest);
return plugins.stream()
.filter(plugin -> plugin.supports(file))
.map(plugin -> {
plugin.update(file);
plugin.update(file, xmlManifest);
return plugin.runForIssues();
})
.flatMap(List::stream)

View File

@@ -0,0 +1,31 @@
package com.bartek.esa.core.xml;
import com.bartek.esa.error.EsaException;
import io.vavr.control.Try;
import org.w3c.dom.Document;
import javax.inject.Inject;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathFactory;
import java.io.File;
public class XmlHelper {
@Inject
public XmlHelper() {
}
public Document parseXml(File file) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newDefaultInstance();
DocumentBuilder builder = Try.of(factory::newDocumentBuilder).getOrElseThrow(EsaException::new);
return Try.of(() -> builder.parse(file)).getOrElseThrow(EsaException::new);
}
public Object xPath(Document xml, String expression, QName returnType) {
return Try.of(() -> XPathFactory.newDefaultInstance().newXPath().evaluate(expression, xml, returnType))
.getOrElseThrow(EsaException::new);
}
}