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