From f2003389b594bde6305bdd4f67d807ac27051a98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Tue, 2 Apr 2019 14:31:25 +0200 Subject: [PATCH] 5: Add Manifest to plugins --- .../core/archetype/AndroidManifestPlugin.java | 5 +-- .../bartek/esa/core/archetype/BasePlugin.java | 9 +++++- .../com/bartek/esa/core/archetype/Plugin.java | 3 +- .../core/archetype/ResourceLayoutPlugin.java | 5 +-- .../bartek/esa/core/archetype/XmlPlugin.java | 19 +++++------- .../com/bartek/esa/core/di/CoreModule.java | 10 ++++-- .../esa/core/executor/PluginExecutor.java | 16 ++++++---- .../com/bartek/esa/core/xml/XmlHelper.java | 31 +++++++++++++++++++ 8 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/bartek/esa/core/xml/XmlHelper.java diff --git a/src/main/java/com/bartek/esa/core/archetype/AndroidManifestPlugin.java b/src/main/java/com/bartek/esa/core/archetype/AndroidManifestPlugin.java index b319569..b01718e 100644 --- a/src/main/java/com/bartek/esa/core/archetype/AndroidManifestPlugin.java +++ b/src/main/java/com/bartek/esa/core/archetype/AndroidManifestPlugin.java @@ -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; } diff --git a/src/main/java/com/bartek/esa/core/archetype/BasePlugin.java b/src/main/java/com/bartek/esa/core/archetype/BasePlugin.java index 1ba596b..391ac16 100644 --- a/src/main/java/com/bartek/esa/core/archetype/BasePlugin.java +++ b/src/main/java/com/bartek/esa/core/archetype/BasePlugin.java @@ -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 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; + } } diff --git a/src/main/java/com/bartek/esa/core/archetype/Plugin.java b/src/main/java/com/bartek/esa/core/archetype/Plugin.java index 67928bb..ddbf1d4 100644 --- a/src/main/java/com/bartek/esa/core/archetype/Plugin.java +++ b/src/main/java/com/bartek/esa/core/archetype/Plugin.java @@ -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 runForIssues(); } diff --git a/src/main/java/com/bartek/esa/core/archetype/ResourceLayoutPlugin.java b/src/main/java/com/bartek/esa/core/archetype/ResourceLayoutPlugin.java index 4c50792..aa924e7 100644 --- a/src/main/java/com/bartek/esa/core/archetype/ResourceLayoutPlugin.java +++ b/src/main/java/com/bartek/esa/core/archetype/ResourceLayoutPlugin.java @@ -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; } diff --git a/src/main/java/com/bartek/esa/core/archetype/XmlPlugin.java b/src/main/java/com/bartek/esa/core/archetype/XmlPlugin.java index bc839b1..c1bbedd 100644 --- a/src/main/java/com/bartek/esa/core/archetype/XmlPlugin.java +++ b/src/main/java/com/bartek/esa/core/archetype/XmlPlugin.java @@ -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); } } diff --git a/src/main/java/com/bartek/esa/core/di/CoreModule.java b/src/main/java/com/bartek/esa/core/di/CoreModule.java index 29c133b..59d666d 100644 --- a/src/main/java/com/bartek/esa/core/di/CoreModule.java +++ b/src/main/java/com/bartek/esa/core/di/CoreModule.java @@ -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(); + } } 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 5b363fa..e114489 100644 --- a/src/main/java/com/bartek/esa/core/executor/PluginExecutor.java +++ b/src/main/java/com/bartek/esa/core/executor/PluginExecutor.java @@ -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 executeForFiles(List files, List plugins) { + public List executeForFiles(File manifest, List files, List plugins) { return files.stream() - .map(file -> executeForFile(file, plugins)) + .map(file -> executeForFile(manifest, file, plugins)) .flatMap(List::stream) .collect(toList()); } - private List executeForFile(File file, List plugins) { + private List executeForFile(File manifest, File file, List 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) diff --git a/src/main/java/com/bartek/esa/core/xml/XmlHelper.java b/src/main/java/com/bartek/esa/core/xml/XmlHelper.java new file mode 100644 index 0000000..7a9ceee --- /dev/null +++ b/src/main/java/com/bartek/esa/core/xml/XmlHelper.java @@ -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); + } +}