diff --git a/scripts/BUILD.bazel b/scripts/BUILD.bazel index 2fcfb81eba5..e84cca1f315 100644 --- a/scripts/BUILD.bazel +++ b/scripts/BUILD.bazel @@ -92,6 +92,13 @@ kt_jvm_binary( runtime_deps = ["//scripts/src/java/org/oppia/android/scripts/xml:xml_syntax_check_lib"], ) +kt_jvm_binary( + name = "text_view_style_check", + testonly = True, + main_class = "org.oppia.android.scripts.xml.TextViewStyleCheckKt", + runtime_deps = ["//scripts/src/java/org/oppia/android/scripts/xml:text_view_style_check_lib"], +) + TEST_FILE_EXEMPTION_ASSETS = generate_test_file_assets_list_from_text_protos( name = "test_file_exemption_assets", test_file_exemptions_name = ["test_file_exemptions"], diff --git a/scripts/src/java/org/oppia/android/scripts/xml/BUILD.bazel b/scripts/src/java/org/oppia/android/scripts/xml/BUILD.bazel index 56dc1789827..1b609b77a04 100644 --- a/scripts/src/java/org/oppia/android/scripts/xml/BUILD.bazel +++ b/scripts/src/java/org/oppia/android/scripts/xml/BUILD.bazel @@ -22,3 +22,14 @@ kt_jvm_library( "//scripts/src/java/org/oppia/android/scripts/common:repository_file", ], ) + +kt_jvm_library( + name = "text_view_style_check_lib", + testonly = True, + srcs = ["TextViewStyleCheck.kt"], + visibility = ["//scripts:oppia_script_binary_visibility"], + deps = [ + ":xml_syntax_error_handler", + "//scripts/src/java/org/oppia/android/scripts/common:repository_file", + ], +) diff --git a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt new file mode 100644 index 00000000000..04e3ac2798e --- /dev/null +++ b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt @@ -0,0 +1,46 @@ +package org.oppia.android.scripts.xml + +import org.oppia.android.scripts.common.RepositoryFile +import org.w3c.dom.Node +import javax.xml.parsers.DocumentBuilderFactory + +fun main(vararg args: String) { + // Path of the repo to be analyzed. + val repoPath = "${args[0]}/" + + // A list of all XML files in the repo to be analyzed. + val searchFiles = RepositoryFile.collectSearchFiles( + repoPath = repoPath, expectedExtension = ".xml" + ) + val builderFactory = DocumentBuilderFactory.newInstance() + searchFiles.forEach { file -> + val builder = builderFactory.newDocumentBuilder() + val doc = builder.parse(file) + val textViewsElements = doc.getElementsByTagName("TextView") + for (i in 0 until textViewsElements.length) { + val node = textViewsElements.item(i) + textViewRTL(node as Node) + } + } +} + +private fun textViewRTL(node: Node) { + val attributes = node.attributes + for (i in 0 until attributes.length) { + val attribute = attributes.item(i) + when (attribute.nodeName) { + "android:layout_marginLeft" -> { + throw Exception("TextView has layout_marginLeft") + } + "android:layout_marginRight" -> { + throw Exception("TextView has layout_marginRight") + } + "android:paddingLeft" -> { + throw Exception("TextView has paddingLeft") + } + "android:paddingRight" -> { + throw Exception("TextView has paddingRight") + } + } + } +}