Simplify the code with a vector instead of an if / elseif list
Sylvestre Ledru [Tue, 30 Jun 2009 08:37:24 +0000 (10:37 +0200)]
scilab/modules/graphic_export/src/java/org/scilab/modules/graphic_export/ExportRenderer.java

index 31e5403..a085557 100644 (file)
@@ -12,6 +12,9 @@
 
 package org.scilab.modules.graphic_export;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+
 import javax.media.opengl.GLEventListener;
 import com.sun.opengl.util.FileUtil;
 
@@ -53,9 +56,9 @@ public abstract class ExportRenderer implements GLEventListener {
        private static int fileType;
        private static int fileOrientation;
 
-       /** File extension */
-        private static String fileExtension;
-       
+       /** File extension */
+       private static String fileExtension;
+
        /** give the type of the error */
        private static int errorNumber;
 
@@ -173,38 +176,22 @@ public abstract class ExportRenderer implements GLEventListener {
                int pos = this.fileName.lastIndexOf('.'); // position of the dot
 
                if (suffix != null) {
+
+                       String[] extensionsAllowed = {"bmp","gif","jpg","jpeg","png","ppm","eps","pdf","svg","ps"};
+
+                       int[] fileTypeAllowed = {ExportRenderer.BMP_EXPORT, ExportRenderer.GIF_EXPORT, ExportRenderer.JPG_EXPORT, ExportRenderer.PNG_EXPORT, ExportRenderer.PPM_EXPORT, ExportRenderer.EPS_EXPORT, ExportRenderer.PDF_EXPORT, ExportRenderer.SVG_EXPORT, ExportRenderer.PS_EXPORT};
+
                        suffix = suffix.toLowerCase();
-                       if (suffix.equalsIgnoreCase("bmp") && this.fileType == ExportRenderer.BMP_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "bmp";
-                       } else if (suffix.equalsIgnoreCase("gif") && this.fileType == ExportRenderer.GIF_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "gif";
-                       } else if (suffix.equalsIgnoreCase("jpg") && this.fileType == ExportRenderer.JPG_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "jpg";
-                       } else if (suffix.equalsIgnoreCase("jpeg") && this.fileType == ExportRenderer.JPG_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "jpeg";
-                       } else if (suffix.equalsIgnoreCase("png") && this.fileType == ExportRenderer.PNG_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "png";
-                       } else if (suffix.equalsIgnoreCase("ppm") && this.fileType == ExportRenderer.PPM_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "ppm";
-                       } else if (suffix.equalsIgnoreCase("eps") && this.fileType == ExportRenderer.EPS_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "eps";
-                       } else if (suffix.equalsIgnoreCase("pdf") && this.fileType == ExportRenderer.PDF_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "pdf";
-                       } else if (suffix.equalsIgnoreCase("svg") && this.fileType == ExportRenderer.SVG_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "svg";
-                       } else if (suffix.equalsIgnoreCase("ps") && this.fileType == ExportRenderer.PS_EXPORT) {
-                               this.fileName = this.fileName.substring(0, pos);
-                               this.fileExtension = "ps";
-                       }       
+
+                       /* Transform the array to vector to have access to search methods*/
+
+                       ArrayList<String> extensionsAllowedV = new ArrayList<String>(Arrays.asList(extensionsAllowed));
+                       ArrayList<Integer> fileTypeAllowedV = new ArrayList(Arrays.asList(fileTypeAllowed));
+
+                       if (extensionsAllowedV.contains(suffix) && fileTypeAllowedV.contains(this.fileType)){
+                               this.fileName = this.fileName.substring(0, pos); /* Physically removed the extension */
+                               this.fileExtension = suffix; /* Store the extension... we want the same as used initially (ex: jpeg) */
+                       } 
                }
        }