X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=meviz.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fmeviz%2Fhtmlindexer%2FUtils.java;h=41c2eedab08f8c2957c88ba4bbde143ce4e01f33;hp=d193dba012944a97196377f997f5046ef64e8372;hb=cf6e4ace4972f24f40f88ea12fcf99c763e4e40a;hpb=062dae345ca08bc63c65f971d5bb0d2fa9bbeac8 diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java index d193dba..41c2eed 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java @@ -1,6 +1,6 @@ /* * Meviz - Various tools collection to work with multimedia. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * Copyright (C) 2012 -- 2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License @@ -9,130 +9,134 @@ package eu.svjatoslav.meviz.htmlindexer; +import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout; + +import javax.imageio.ImageIO; 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.io.UnsupportedEncodingException; -import java.net.URLEncoder; 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 final String UTF_8 = "UTF-8"; - - 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) { - System.out.println("Error reading image: " + file); - 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 getLayouts() { - final HashSet layouts = new HashSet(); - 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); - - // note use crc.value, not crc.getValue() - final String hex = Integer.toHexString((int) crc.getValue()) - .toUpperCase(); - - // System.out.println("Input string: " + input); - // System.out.println("Result: " + hex); - - return hex; - } - - public static File getThumbnailsDirectory(final File directoryToIndex) { - return new File(getThumbnailsDirectoryPath(directoryToIndex)); - } - - public static String getThumbnailsDirectoryPath(final File directoryToIndex) { - return directoryToIndex.getAbsolutePath() + "/" - + Constants.THUMBNAILS_DIRECTORY_NAME + "/"; - } - - public static boolean isMevizGeneratedIndexFile(final File indexFile) - throws FileNotFoundException, IOException { - - boolean isMevizFile = false; - - final FileReader fileReader = new FileReader(indexFile); - final BufferedReader reader = new BufferedReader(fileReader); - - parseFile: { - while (true) { - final String line = reader.readLine(); - - if (line == null) - break parseFile; - - if (line.contains(Constants.HTML_MAGIC_STRING)) { - isMevizFile = true; - break parseFile; - } - } - } - - reader.close(); - fileReader.close(); - return isMevizFile; - } - - public static String urlEncode(String string) { - try { - return URLEncoder.encode(string, UTF_8).replace("+", "%20"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } + private static final String UTF_8 = "UTF-8"; + 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) { + System.out.println("Error reading image: " + file); + 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 getLayouts() { + final HashSet layouts = new HashSet<>(); + 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); + + // note use crc.value, not crc.getValue() + + return Integer.toHexString((int) crc.getValue()) + .toUpperCase(); + } + + public static File getThumbnailsDirectory(final File directoryToIndex) { + return new File(getThumbnailsDirectoryPath(directoryToIndex)); + } + + public static String getThumbnailsDirectoryPath(final File directoryToIndex) { + return directoryToIndex.getAbsolutePath() + "/" + + Constants.THUMBNAILS_DIRECTORY_NAME + "/"; + } + + public static boolean isMevizGeneratedIndexFile(final File indexFile) + throws IOException { + + boolean isMevizFile = false; + + final FileReader fileReader = new FileReader(indexFile); + final BufferedReader reader = new BufferedReader(fileReader); + + parseFile: + { + while (true) { + final String line = reader.readLine(); + + if (line == null) + break parseFile; + + if (line.contains(Constants.HTML_MAGIC_STRING)) { + isMevizFile = true; + break parseFile; + } + } + } + + reader.close(); + fileReader.close(); + return isMevizFile; + } + + /** + * TODO: URL path component is encoded differently from URL query parameter. + * Also some URL encoding might work for HTML on local filesystem, while other + * stuff works for web. Things must be cleared up here. Currently they are mixed and + * hacked together. + */ + public static String urlEncode(String string) { + if (string.startsWith("./")) + string = string.substring(2); + + // TODO: get rid of UrlParamEncoder. + return UrlParamEncoder.encode(string); + // +// try { +// return URLEncoder.encode(string, UTF_8).replace("+", "%20"); +// } catch (UnsupportedEncodingException e) { +// throw new RuntimeException(e); +// } + } }