10: Create TextInputValidationPlugin

This commit is contained in:
Bartłomiej Pluta
2019-04-10 14:35:47 +02:00
parent ae773347b9
commit 40e50cb0f4
3 changed files with 47 additions and 0 deletions

View File

@@ -107,4 +107,10 @@ public class PluginModule {
public Plugin dangerousPermissionPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) { public Plugin dangerousPermissionPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
return new DangerousPermissionPlugin(globMatcher, xmlHelper); return new DangerousPermissionPlugin(globMatcher, xmlHelper);
} }
@Provides
@IntoSet
public Plugin textInputValidationPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
return new TextInputValidationPlugin(globMatcher, xmlHelper);
}
} }

View File

@@ -0,0 +1,36 @@
package com.bartek.esa.core.plugin;
import com.bartek.esa.core.archetype.ResourceLayoutPlugin;
import com.bartek.esa.core.model.enumeration.Severity;
import com.bartek.esa.core.xml.XmlHelper;
import com.bartek.esa.file.matcher.GlobMatcher;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.inject.Inject;
import java.util.Optional;
public class TextInputValidationPlugin extends ResourceLayoutPlugin {
@Inject
public TextInputValidationPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
super(globMatcher, xmlHelper);
}
@Override
protected void run(Document xml) {
NodeList editTextNodes = xml.getElementsByTagName("EditText");
stream(editTextNodes)
.filter(this::doesNotHaveInputType)
.forEach(n -> addIssue(Severity.WARNING, null, tagString(n)));
}
private boolean doesNotHaveInputType(Node editText) {
Boolean doesHaveInputType = Optional.ofNullable(editText.getAttributes().getNamedItem("android:inputType"))
.map(Node::getNodeValue)
.map(v -> !v.isEmpty())
.orElse(false);
return !doesHaveInputType;
}
}

View File

@@ -112,3 +112,8 @@ com.bartek.esa.core.plugin.DangerousPermissionPlugin=Custom permission without d
Custom permission with 'dangerous' protection level was found and it doesn't have any description.\n\ Custom permission with 'dangerous' protection level was found and it doesn't have any description.\n\
As long as the permission requires user attention, he should have provided a meaningful description about\n\ As long as the permission requires user attention, he should have provided a meaningful description about\n\
permission. permission.
com.bartek.esa.core.plugin.TextInputValidationPlugin=Input type is no selected.\n\
The EditText view doesn't have a input type selected.\n\
Consider associating a input type with this view.\n\
For example: <EditText android:inputType="number" ...