code cleanup and formatting
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / Utils.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz.htmlindexer;
11
12 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
13
14 import javax.imageio.ImageIO;
15 import java.awt.image.BufferedImage;
16 import java.io.*;
17 import java.net.URLEncoder;
18 import java.util.HashSet;
19 import java.util.zip.CRC32;
20
21 public class Utils {
22
23     private static final String UTF_8 = "UTF-8";
24     private static File lastLoadedFile;
25     private static BufferedImage lastLoadedBufferedImage;
26
27     /**
28      * Load image into {@link BufferedImage} and return it. Caches last loaded
29      * image to speed up subsequent loading attempts.
30      *
31      * @throws ImageFormatError
32      * @throws IOException
33      */
34     public static BufferedImage getBufferedImage(final File file)
35             throws ImageFormatError, IOException {
36         if (file.equals(lastLoadedFile))
37             return lastLoadedBufferedImage;
38
39         System.out.println("Loading image: " + file.getPath());
40         lastLoadedBufferedImage = ImageIO.read(file);
41         lastLoadedFile = file;
42
43         if (lastLoadedBufferedImage == null) {
44             System.out.println("Error reading image: " + file);
45             throw new ImageFormatError("File: " + file
46                     + " is not a valid image.");
47
48         }
49
50         return lastLoadedBufferedImage;
51     }
52
53     public static File getLayoutIndexFile(final Layout layout,
54                                           final File directoryToIndex) {
55
56         final String indexFilePath = directoryToIndex.getAbsolutePath()
57                 + "/index" + layout.getFileNameSuffix() + ".html";
58
59         return new File(indexFilePath);
60     }
61
62     public static HashSet<Layout> getLayouts() {
63         final HashSet<Layout> layouts = new HashSet<Layout>();
64         layouts.add(new MixedLayout());
65         return layouts;
66     }
67
68     public static String getStringCrcAsHex(final String input) {
69
70         // create a new CRC-calculating object
71         final CRC32 crc = new CRC32();
72
73         // loop, calculating CRC for each byte of the string
74         // There is no CRC16.update(byte[]) method.
75         for (final byte b : input.getBytes())
76             crc.update(b);
77
78         // note use crc.value, not crc.getValue()
79         final String hex = Integer.toHexString((int) crc.getValue())
80                 .toUpperCase();
81
82         // System.out.println("Input string: " + input);
83         // System.out.println("Result: " + hex);
84
85         return hex;
86     }
87
88     public static File getThumbnailsDirectory(final File directoryToIndex) {
89         return new File(getThumbnailsDirectoryPath(directoryToIndex));
90     }
91
92     public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
93         return directoryToIndex.getAbsolutePath() + "/"
94                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
95     }
96
97     public static boolean isMevizGeneratedIndexFile(final File indexFile)
98             throws IOException {
99
100         boolean isMevizFile = false;
101
102         final FileReader fileReader = new FileReader(indexFile);
103         final BufferedReader reader = new BufferedReader(fileReader);
104
105         parseFile:
106         {
107             while (true) {
108                 final String line = reader.readLine();
109
110                 if (line == null)
111                     break parseFile;
112
113                 if (line.contains(Constants.HTML_MAGIC_STRING)) {
114                     isMevizFile = true;
115                     break parseFile;
116                 }
117             }
118         }
119
120         reader.close();
121         fileReader.close();
122         return isMevizFile;
123     }
124
125     public static String urlEncode(String string) {
126         if (string.startsWith("./"))
127             string = string.substring(2);
128
129         try {
130             return URLEncoder.encode(string, UTF_8).replace("+", "%20");
131         } catch (UnsupportedEncodingException e) {
132             throw new RuntimeException(e);
133         }
134     }
135 }