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; package com.bartek.esa.core.archetype;
import com.bartek.esa.core.xml.XmlHelper;
import com.bartek.esa.file.matcher.GlobMatcher; import com.bartek.esa.file.matcher.GlobMatcher;
import java.io.File; import java.io.File;
@@ -7,8 +8,8 @@ import java.io.File;
public abstract class AndroidManifestPlugin extends XmlPlugin { public abstract class AndroidManifestPlugin extends XmlPlugin {
private final GlobMatcher globMatcher; private final GlobMatcher globMatcher;
public AndroidManifestPlugin(GlobMatcher globMatcher) { public AndroidManifestPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
super(globMatcher); super(globMatcher, xmlHelper);
this.globMatcher = globMatcher; 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.enumeration.Severity;
import com.bartek.esa.core.model.object.Issue; import com.bartek.esa.core.model.object.Issue;
import org.w3c.dom.Document;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@@ -9,11 +10,13 @@ import java.util.List;
public abstract class BasePlugin implements Plugin { public abstract class BasePlugin implements Plugin {
private List<Issue> issues = new ArrayList<>(); private List<Issue> issues = new ArrayList<>();
private Document manifest;
private File file; private File file;
@Override @Override
public void update(File file) { public void update(File file, Document manifest) {
this.file = file; this.file = file;
this.manifest = manifest;
this.issues.clear(); this.issues.clear();
} }
@@ -45,4 +48,8 @@ public abstract class BasePlugin implements Plugin {
protected File getOriginalFile() { protected File getOriginalFile() {
return file; return file;
} }
protected Document getManifest() {
return manifest;
}
} }

View File

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

View File

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

View File

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

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.desc.provider.DescriptionProvider;
import com.bartek.esa.core.executor.PluginExecutor; import com.bartek.esa.core.executor.PluginExecutor;
import com.bartek.esa.core.java.JavaSyntaxRegexProvider; import com.bartek.esa.core.java.JavaSyntaxRegexProvider;
import com.bartek.esa.core.xml.XmlHelper;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
@@ -10,8 +11,8 @@ import dagger.Provides;
public class CoreModule { public class CoreModule {
@Provides @Provides
public PluginExecutor pluginExecutor() { public PluginExecutor pluginExecutor(XmlHelper xmlHelper) {
return new PluginExecutor(); return new PluginExecutor(xmlHelper);
} }
@Provides @Provides
@@ -23,4 +24,9 @@ public class CoreModule {
public DescriptionProvider descriptionProvider() { public DescriptionProvider descriptionProvider() {
return new 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.archetype.Plugin;
import com.bartek.esa.core.model.object.Issue; 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 javax.inject.Inject;
import java.io.File; import java.io.File;
@@ -10,24 +12,26 @@ import java.util.List;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
public class PluginExecutor { public class PluginExecutor {
private final XmlHelper xmlHelper;
@Inject @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() return files.stream()
.map(file -> executeForFile(file, plugins)) .map(file -> executeForFile(manifest, file, plugins))
.flatMap(List::stream) .flatMap(List::stream)
.collect(toList()); .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() return plugins.stream()
.filter(plugin -> plugin.supports(file)) .filter(plugin -> plugin.supports(file))
.map(plugin -> { .map(plugin -> {
plugin.update(file); plugin.update(file, xmlManifest);
return plugin.runForIssues(); return plugin.runForIssues();
}) })
.flatMap(List::stream) .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);
}
}