2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.meviz.htmlindexer;
12 import java.awt.image.BufferedImage;
13 import java.io.BufferedReader;
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;
23 import javax.imageio.ImageIO;
25 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
29 private static File lastLoadedFile;
30 private static final String UTF_8 = "UTF-8";
32 private static BufferedImage lastLoadedBufferedImage;
35 * Load image into {@link BufferedImage} and return it. Caches last loaded
36 * image to speed up subsequent loading attempts.
38 * @throws ImageFormatError
41 public static BufferedImage getBufferedImage(final File file)
42 throws ImageFormatError, IOException {
43 if (file.equals(lastLoadedFile))
44 return lastLoadedBufferedImage;
46 System.out.println("Loading image: " + file.getPath());
47 lastLoadedBufferedImage = ImageIO.read(file);
48 lastLoadedFile = file;
50 if (lastLoadedBufferedImage == null) {
51 System.out.println("Error reading image: " + file);
52 throw new ImageFormatError("File: " + file
53 + " is not a valid image.");
57 return lastLoadedBufferedImage;
60 public static File getLayoutIndexFile(final Layout layout,
61 final File directoryToIndex) {
63 final String indexFilePath = directoryToIndex.getAbsolutePath()
64 + "/index" + layout.getFileNameSuffix() + ".html";
66 return new File(indexFilePath);
69 public static HashSet<Layout> getLayouts() {
70 final HashSet<Layout> layouts = new HashSet<Layout>();
71 layouts.add(new MixedLayout());
75 public static String getStringCrcAsHex(final String input) {
77 // create a new CRC-calculating object
78 final CRC32 crc = new CRC32();
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())
85 // note use crc.value, not crc.getValue()
86 final String hex = Integer.toHexString((int) crc.getValue())
89 // System.out.println("Input string: " + input);
90 // System.out.println("Result: " + hex);
95 public static File getThumbnailsDirectory(final File directoryToIndex) {
96 return new File(getThumbnailsDirectoryPath(directoryToIndex));
99 public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
100 return directoryToIndex.getAbsolutePath() + "/"
101 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
104 public static boolean isMevizGeneratedIndexFile(final File indexFile)
105 throws FileNotFoundException, IOException {
107 boolean isMevizFile = false;
109 final FileReader fileReader = new FileReader(indexFile);
110 final BufferedReader reader = new BufferedReader(fileReader);
114 final String line = reader.readLine();
119 if (line.contains(Constants.HTML_MAGIC_STRING)) {
131 public static String urlEncode(String string) {
132 if (string.startsWith("./"))
133 string = string.substring(2);
136 return URLEncoder.encode(string, UTF_8).replace("+", "%20");
137 } catch (UnsupportedEncodingException e) {
138 throw new RuntimeException(e);