From da0669ba55b959610a135820c360037081c909cb Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sun, 23 Jan 2022 19:40:19 +0530 Subject: [PATCH 1/7] created the script file and and created dagger target and library. --- scripts/BUILD.bazel | 7 +++++++ .../org/oppia/android/scripts/xml/BUILD.bazel | 11 +++++++++++ .../android/scripts/xml/TextViewStyleCheck.kt | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt diff --git a/scripts/BUILD.bazel b/scripts/BUILD.bazel index 2fcfb81eba5..7c34492b39e 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.TextViewStyleCheck", + 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..f92cfa311ec --- /dev/null +++ b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt @@ -0,0 +1,19 @@ +package org.oppia.android.scripts.xml + +import org.oppia.android.scripts.common.RepositoryFile +import org.xml.sax.SAXParseException +import java.io.File +import javax.xml.parsers.DocumentBuilder +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" + ) + +} From d33f39d46110870ade7e4ab9a7949e5b1343795a Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sun, 23 Jan 2022 19:41:20 +0530 Subject: [PATCH 2/7] optimised imports. --- .../java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt index f92cfa311ec..e694b50819b 100644 --- a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt +++ b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt @@ -1,10 +1,6 @@ package org.oppia.android.scripts.xml import org.oppia.android.scripts.common.RepositoryFile -import org.xml.sax.SAXParseException -import java.io.File -import javax.xml.parsers.DocumentBuilder -import javax.xml.parsers.DocumentBuilderFactory fun main(vararg args: String) { // Path of the repo to be analyzed. @@ -15,5 +11,4 @@ fun main(vararg args: String) { repoPath = repoPath, expectedExtension = ".xml" ) - } From 422aed8cc839f5fbfa711fb082f7714d17a87baf Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Mon, 24 Jan 2022 12:22:04 +0530 Subject: [PATCH 3/7] parsed the document. --- .../oppia/android/scripts/xml/TextViewStyleCheck.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt index e694b50819b..2b42b86d332 100644 --- a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt +++ b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt @@ -1,6 +1,8 @@ package org.oppia.android.scripts.xml import org.oppia.android.scripts.common.RepositoryFile +import javax.xml.parsers.DocumentBuilder +import javax.xml.parsers.DocumentBuilderFactory fun main(vararg args: String) { // Path of the repo to be analyzed. @@ -11,4 +13,15 @@ fun main(vararg args: String) { repoPath = repoPath, expectedExtension = ".xml" ) + + val builderFactory = DocumentBuilderFactory.newInstance() + + // val allErrorsList = searchFiles.flatMap { file -> + val docBuilder = builderFactory.newDocumentBuilder() + val xmlSyntaxErrorHandler = XmlSyntaxErrorHandler() + docBuilder.setErrorHandler(xmlSyntaxErrorHandler) + val xmlDocument = docBuilder.parse(file.toFile()) + val list= xmlDocument. + // fileErrorList.map + // } } From 2dca720c3f06bbd55eaf48c6e1bd9a7a3c4a8258 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Mon, 7 Feb 2022 02:32:37 +0530 Subject: [PATCH 4/7] formatted the code. --- .../org/oppia/android/scripts/xml/TextViewStyleCheck.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt index 2b42b86d332..715c8e52b83 100644 --- a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt +++ b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt @@ -1,7 +1,6 @@ package org.oppia.android.scripts.xml import org.oppia.android.scripts.common.RepositoryFile -import javax.xml.parsers.DocumentBuilder import javax.xml.parsers.DocumentBuilderFactory fun main(vararg args: String) { @@ -16,12 +15,11 @@ fun main(vararg args: String) { val builderFactory = DocumentBuilderFactory.newInstance() - // val allErrorsList = searchFiles.flatMap { file -> + val allErrorsList = searchFiles.flatMap { file -> val docBuilder = builderFactory.newDocumentBuilder() val xmlSyntaxErrorHandler = XmlSyntaxErrorHandler() docBuilder.setErrorHandler(xmlSyntaxErrorHandler) val xmlDocument = docBuilder.parse(file.toFile()) - val list= xmlDocument. - // fileErrorList.map - // } + val list = xmlDocument.fileErrorList.map + } } From 9df8ea9df7e37863a61dbcfbcf2ccc17fdb2dce1 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sat, 12 Feb 2022 15:52:34 +0530 Subject: [PATCH 5/7] fixed the bazel target added a short script Signed-off-by: yash10019coder --- scripts/BUILD.bazel | 2 +- .../android/scripts/xml/TextViewStyleCheck.kt | 38 ++++++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/scripts/BUILD.bazel b/scripts/BUILD.bazel index 7c34492b39e..e84cca1f315 100644 --- a/scripts/BUILD.bazel +++ b/scripts/BUILD.bazel @@ -95,7 +95,7 @@ kt_jvm_binary( kt_jvm_binary( name = "text_view_style_check", testonly = True, - main_class = "org.oppia.android.scripts.xml.TextViewStyleCheck", + main_class = "org.oppia.android.scripts.xml.TextViewStyleCheckKt", runtime_deps = ["//scripts/src/java/org/oppia/android/scripts/xml:text_view_style_check_lib"], ) diff --git a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt index 715c8e52b83..a8a18cddb3d 100644 --- a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt +++ b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt @@ -2,6 +2,7 @@ package org.oppia.android.scripts.xml import org.oppia.android.scripts.common.RepositoryFile import javax.xml.parsers.DocumentBuilderFactory +import org.w3c.dom.Node fun main(vararg args: String) { // Path of the repo to be analyzed. @@ -9,17 +10,36 @@ fun main(vararg args: String) { // A list of all XML files in the repo to be analyzed. val searchFiles = RepositoryFile.collectSearchFiles( - repoPath = repoPath, - expectedExtension = ".xml" + repoPath = repoPath, expectedExtension = ".xml" ) - + val profileEditFragmentFile = searchFiles[452] val builderFactory = DocumentBuilderFactory.newInstance() + val builder = builderFactory.newDocumentBuilder() + val doc = builder.parse(profileEditFragmentFile) + val root = doc.getElementsByTagName("TextView") + for (i in 0 until root.length) { + val node = root.item(i) + textViewRTL(node as Node) + } +} - val allErrorsList = searchFiles.flatMap { file -> - val docBuilder = builderFactory.newDocumentBuilder() - val xmlSyntaxErrorHandler = XmlSyntaxErrorHandler() - docBuilder.setErrorHandler(xmlSyntaxErrorHandler) - val xmlDocument = docBuilder.parse(file.toFile()) - val list = xmlDocument.fileErrorList.map +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:layout_paddingLeft" -> { + throw Exception("TextView has layout_paddingLeft") + } + "android:layout_paddingRight" -> { + throw Exception("TextView has layout_paddingRight") + } + } } } From 3b0595ac19967019b6425e38ab527fa8511408a6 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sat, 12 Feb 2022 16:19:17 +0530 Subject: [PATCH 6/7] fixed some of the naming checking all files now. --- .../android/scripts/xml/TextViewStyleCheck.kt | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt index a8a18cddb3d..1630c598dc8 100644 --- a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt +++ b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt @@ -12,14 +12,15 @@ fun main(vararg args: String) { val searchFiles = RepositoryFile.collectSearchFiles( repoPath = repoPath, expectedExtension = ".xml" ) - val profileEditFragmentFile = searchFiles[452] val builderFactory = DocumentBuilderFactory.newInstance() - val builder = builderFactory.newDocumentBuilder() - val doc = builder.parse(profileEditFragmentFile) - val root = doc.getElementsByTagName("TextView") - for (i in 0 until root.length) { - val node = root.item(i) - textViewRTL(node as Node) + 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) + } } } @@ -34,11 +35,11 @@ private fun textViewRTL(node: Node) { "android:layout_marginRight" -> { throw Exception("TextView has layout_marginRight") } - "android:layout_paddingLeft" -> { - throw Exception("TextView has layout_paddingLeft") + "android:paddingLeft" -> { + throw Exception("TextView has paddingLeft") } - "android:layout_paddingRight" -> { - throw Exception("TextView has layout_paddingRight") + "android:paddingRight" -> { + throw Exception("TextView has paddingRight") } } } From d4765f88a14d880bfa0b0a95094bbb994e8d1df0 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sat, 12 Feb 2022 16:26:59 +0530 Subject: [PATCH 7/7] nit fixes. --- .../java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt index 1630c598dc8..04e3ac2798e 100644 --- a/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt +++ b/scripts/src/java/org/oppia/android/scripts/xml/TextViewStyleCheck.kt @@ -1,8 +1,8 @@ package org.oppia.android.scripts.xml import org.oppia.android.scripts.common.RepositoryFile -import javax.xml.parsers.DocumentBuilderFactory import org.w3c.dom.Node +import javax.xml.parsers.DocumentBuilderFactory fun main(vararg args: String) { // Path of the repo to be analyzed.