package be.nikiroo.fanfix.supported; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import java.util.AbstractMap; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; import java.util.Scanner; import org.jsoup.nodes.Document; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.utils.Image; import be.nikiroo.utils.ImageUtils; import be.nikiroo.utils.Progress; import be.nikiroo.utils.streams.MarkableFileInputStream; /** * Support class for local stories encoded in textual format, with a few rules: *
* It must also be a file, not another kind of URL. * * @param url * the {@link URL} to check * @param info * TRUE to require the info file, FALSE to forbid the info file * * @return TRUE if it is supported */ protected boolean supports(URL url, boolean info) { if (!"file".equals(url.getProtocol())) { return false; } boolean infoPresent = false; File file; try { file = new File(url.toURI()); file = assureNoTxt(file); file = new File(file.getPath() + ".info"); } catch (URISyntaxException e) { Instance.getInstance().getTraceHandler().error(e); file = null; } infoPresent = (file != null && file.exists()); return infoPresent == info; } /** * Remove the ".txt" (or ".text") extension if it is present. * * @param file * the file to process * * @return the same file or a copy of it without the ".txt" extension if it * was present */ protected File assureNoTxt(File file) { for (String ext : new String[] { ".txt", ".text" }) { if (file.getName().endsWith(ext)) { file = new File(file.getPath().substring(0, file.getPath().length() - ext.length())); } } return file; } /** * Check if the given line looks like the given starting chapter in a * supported language, and return the language if it does (or NULL if not). * * @param line * the line to check * @param number * the specific chapter number to check for * * @return the language or NULL */ static private String detectChapter(String line, int number) { line = line.toUpperCase(); for (String lang : Instance.getInstance().getConfig().getList(Config.CONF_CHAPTER)) { String chapter = Instance.getInstance().getConfig().getStringX(Config.CONF_CHAPTER, lang); if (chapter != null && !chapter.isEmpty()) { chapter = chapter.toUpperCase() + " "; if (line.startsWith(chapter)) { // We want "[CHAPTER] [number]: [name]", with ": [name]" // optional String test = line.substring(chapter.length()).trim(); String possibleNum = test.trim(); if (possibleNum.indexOf(':') > 0) { possibleNum = possibleNum.substring(0, possibleNum.indexOf(':')).trim(); } if (test.startsWith(Integer.toString(number))) { test = test .substring(Integer.toString(number).length()) .trim(); if (test.isEmpty() || test.startsWith(":")) { return lang; } } } } } return null; } }