possibility to remove artifacts generated by meviz index
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / Utils.java
index b21f8e9..f8c82fa 100755 (executable)
 package eu.svjatoslav.meviz.htmlindexer;
 
 import java.awt.image.BufferedImage;
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.IOException;
+import java.util.HashSet;
 import java.util.zip.CRC32;
 
 import javax.imageio.ImageIO;
 
+import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
+
 public class Utils {
 
-    private static File lastLoadedFile;
+       private static File lastLoadedFile;
+
+       private static BufferedImage lastLoadedBufferedImage;
+
+       /**
+        * Load image into {@link BufferedImage} and return it. Caches last loaded
+        * image to speed up subsequent loading attempts.
+        * 
+        * @throws ImageFormatError
+        * @throws IOException
+        */
+       public static BufferedImage getBufferedImage(final File file)
+                       throws ImageFormatError, IOException {
+               if (file.equals(lastLoadedFile))
+                       return lastLoadedBufferedImage;
+
+               System.out.println("Loading image: " + file.getPath());
+               lastLoadedBufferedImage = ImageIO.read(file);
+               lastLoadedFile = file;
+
+               if (lastLoadedBufferedImage == null)
+                       throw new ImageFormatError("File: " + file
+                                       + " is not a valid image.");
+
+               return lastLoadedBufferedImage;
+       }
+
+       public static File getLayoutIndexFile(final Layout layout,
+                       final File directoryToIndex) {
+
+               final String indexFilePath = directoryToIndex.getAbsolutePath()
+                               + "/index" + layout.getFileNameSuffix() + ".html";
+
+               return new File(indexFilePath);
+       }
+
+       public static HashSet<Layout> getLayouts() {
+               final HashSet<Layout> layouts = new HashSet<Layout>();
+               layouts.add(new MixedLayout());
+               return layouts;
+       }
+
+       public static String getStringCrcAsHex(final String input) {
+
+               // create a new CRC-calculating object
+               final CRC32 crc = new CRC32();
+
+               // loop, calculating CRC for each byte of the string
+               // There is no CRC16.update(byte[]) method.
+               for (final byte b : input.getBytes())
+                       crc.update(b);
 
-    private static BufferedImage lastLoadedBufferedImage;
+               // note use crc.value, not crc.getValue()
+               final String hex = Integer.toHexString((int) crc.getValue())
+                               .toUpperCase();
 
-    /**
-     * Load image into {@link BufferedImage} and return it. Caches last loaded
-     * image to speed up subsequent loading attempts.
-     * 
-     * @throws ImageFormatError
-     * @throws IOException
-     */
-    public static BufferedImage getBufferedImage(final File file) throws ImageFormatError, IOException {
-        if (file.equals(lastLoadedFile)) {
-            return lastLoadedBufferedImage;
-        }
+               // System.out.println("Input string: " + input);
+               // System.out.println("Result: " + hex);
 
-        System.out.println("Loading image: " + file.getPath());
-        lastLoadedBufferedImage = ImageIO.read(file);
-        lastLoadedFile = file;
+               return hex;
+       }
 
-        if (lastLoadedBufferedImage == null)
-            throw new ImageFormatError("File: " + file + " is not a valid image.");
+       public static File getThumbnailsDirectory(final File directoryToIndex) {
+               return new File(directoryToIndex.getAbsolutePath() + "/"
+                               + Constants.THUMBNAILS_DIRECTORY_NAME + "/");
+       }
 
-        return lastLoadedBufferedImage;
-    }
+       public static boolean isMevizGeneratedIndexFile(final File indexFile)
+                       throws FileNotFoundException, IOException {
 
-    public static String getStringCrcAsHex(final String input) {
+               boolean isMevizFile = false;
 
-        // create a new CRC-calculating object
-        final CRC32 crc = new CRC32();
+               final FileReader fileReader = new FileReader(indexFile);
+               final BufferedReader reader = new BufferedReader(fileReader);
 
-        // loop, calculating CRC for each byte of the string
-        // There is no CRC16.update(byte[]) method.
-        for (final byte b : input.getBytes()) {
-            crc.update(b);
-        }
+               parseFile: {
+                       while (true) {
+                               final String line = reader.readLine();
 
-        // note use crc.value, not crc.getValue()
-        final String hex = Integer.toHexString((int) crc.getValue()).toUpperCase();
+                               if (line == null)
+                                       break parseFile;
 
-        // System.out.println("Input string: " + input);
-        // System.out.println("Result: " + hex);
+                               if (line.contains(Constants.HTML_MAGIC_STRING)) {
+                                       isMevizFile = true;
+                                       break parseFile;
+                               }
+                       }
+               }
 
-        return hex;
-    }
+               reader.close();
+               fileReader.close();
+               return isMevizFile;
+       }
 
 }