diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1ce62a8..2e10a56 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
+- [#47](https://github.com/green-code-initiative/ecoCode-java/pull/47) EC203 rule : Detect unoptimized image format
+
### Changed
### Deleted
diff --git a/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java b/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java
index f4ef343..4174215 100644
--- a/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java
+++ b/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java
@@ -30,6 +30,7 @@
import fr.greencodeinitiative.java.checks.AvoidSpringRepositoryCallInLoopOrStreamCheck;
import fr.greencodeinitiative.java.checks.AvoidStatementForDMLQueries;
import fr.greencodeinitiative.java.checks.AvoidUsageOfStaticCollections;
+import fr.greencodeinitiative.java.checks.DetectUnoptimizedImageFormat;
import fr.greencodeinitiative.java.checks.FreeResourcesOfAutoCloseableInterface;
import fr.greencodeinitiative.java.checks.IncrementCheck;
import fr.greencodeinitiative.java.checks.InitializeBufferWithAppropriateSize;
@@ -61,6 +62,7 @@ public class JavaCheckRegistrar implements CheckRegistrar {
OptimizeReadFileExceptions.class,
InitializeBufferWithAppropriateSize.class,
AvoidSetConstantInBatchUpdate.class,
+ DetectUnoptimizedImageFormat.class,
FreeResourcesOfAutoCloseableInterface.class,
AvoidMultipleIfElseStatement.class
);
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java b/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java
new file mode 100644
index 0000000..799badc
--- /dev/null
+++ b/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java
@@ -0,0 +1,65 @@
+/*
+ * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
+ * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package fr.greencodeinitiative.java.checks;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.annotation.Nonnull;
+
+import org.sonar.check.Rule;
+import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
+import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
+import org.sonar.plugins.java.api.tree.LiteralTree;
+import org.sonar.plugins.java.api.tree.Tree;
+
+@Rule(key = "EC203")
+public class DetectUnoptimizedImageFormat extends IssuableSubscriptionVisitor {
+
+ protected static final String MESSAGERULE = "Detect unoptimized image format";
+ protected static final Pattern IMG_EXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)");
+
+ private final DetectUnoptimizedImageFormatVisitor visitor = new DetectUnoptimizedImageFormatVisitor();
+
+ @Override
+ public List nodesToVisit() {
+ return Collections.singletonList(
+ Tree.Kind.STRING_LITERAL
+ );
+ }
+
+ @Override
+ public void visitNode(@Nonnull Tree tree) {
+ tree.accept(visitor);
+ }
+
+ private class DetectUnoptimizedImageFormatVisitor extends BaseTreeVisitor {
+
+ @Override
+ public void visitLiteral(@Nonnull LiteralTree tree) {
+ final String strValue = tree.token().text();
+ final Matcher matcher = IMG_EXTENSION.matcher(strValue);
+ if (matcher.find()) {
+ reportIssue(tree, MESSAGERULE);
+ } else {
+ super.visitLiteral(tree);
+ }
+ }
+ }
+}
diff --git a/src/test/files/DetectUnoptimizedImageFormat.java b/src/test/files/DetectUnoptimizedImageFormat.java
new file mode 100644
index 0000000..a2af38a
--- /dev/null
+++ b/src/test/files/DetectUnoptimizedImageFormat.java
@@ -0,0 +1,61 @@
+/*
+ * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
+ * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package fr.greencodeinitiative.java.checks;
+
+/**
+ * Not compliant
+ */
+public class DetectUnoptimizedImageFormat {
+
+ public String testImage(String image) {
+ return "path/to/" + image;
+ }
+
+ public String testImageFormat2() {
+
+ String img_bmp = "test/image.bmp"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_ico = "image.ico"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_tiff = "test/path/to/image.tiff"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_webp = "test/path/to/" + "image.webp"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_jpg = "image.jpg"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_jpeg = "image.jpeg"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_jfif = "image.jfif"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_pjpeg = "image.pjpeg"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_pjp = "image.pjp"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_gif = "image.gif"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_avif = "image.avif"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ String img_apng = "image.apng"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+
+ String image_format = testImage("image.jpg"); // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+
+ return "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "" // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}}
+ + "";
+ }
+}
\ No newline at end of file
diff --git a/src/test/files/DetectUnoptimizedImageFormatCompliant.java b/src/test/files/DetectUnoptimizedImageFormatCompliant.java
new file mode 100644
index 0000000..cbe1da0
--- /dev/null
+++ b/src/test/files/DetectUnoptimizedImageFormatCompliant.java
@@ -0,0 +1,43 @@
+/*
+ * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
+ * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package fr.greencodeinitiative.java.checks;
+
+/**
+ * Compliant
+ */
+public class DetectUnoptimizedImageFormatCompliant {
+
+ public String testImage(String image) {
+ return "path/to/" + image;
+ }
+
+ public String testImageFormat2() {
+
+
+ String img_svg = "test/image.svg"; // Compliant
+
+ String image_format = testImage("image.svg"); // Compliant
+
+ String image_svg_html = "";
+
+ return "" // Compliant
+ + "";
+ }
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java b/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java
index 02270ca..5933a93 100644
--- a/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java
+++ b/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java
@@ -31,7 +31,7 @@ void checkNumberRules() {
final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
registrar.register(context);
- assertThat(context.checkClasses()).hasSize(15);
+ assertThat(context.checkClasses()).hasSize(16);
assertThat(context.testCheckClasses()).isEmpty();
}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java b/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java
new file mode 100644
index 0000000..977b837
--- /dev/null
+++ b/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java
@@ -0,0 +1,40 @@
+/*
+ * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
+ * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package fr.greencodeinitiative.java.checks;
+
+import org.junit.jupiter.api.Test;
+import org.sonar.java.checks.verifier.CheckVerifier;
+
+public class DetectUnoptimizedImageFormatTest {
+
+ @Test
+ void testHasIssues() {
+ CheckVerifier.newVerifier()
+ .onFile("src/test/files/DetectUnoptimizedImageFormat.java")
+ .withCheck(new DetectUnoptimizedImageFormat())
+ .verifyIssues();
+ }
+
+ @Test
+ void testNoIssues() {
+ CheckVerifier.newVerifier()
+ .onFile("src/test/files/DetectUnoptimizedImageFormatCompliant.java")
+ .withCheck(new DetectUnoptimizedImageFormat())
+ .verifyNoIssues();
+ }
+}