X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FText.java;h=252aca0443fbfe0f71db294c0c8c1d3a816deb72;hp=f6803cd09d6358880290cdf8e2c487caf5794e16;hb=cfdaf6052ddc5ca44cf19f1f6d9f154cc8443024;hpb=7445f8565be9e9237ffb3e16fd4dcb61f8c36cd5 diff --git a/src/be/nikiroo/fanfix/supported/Text.java b/src/be/nikiroo/fanfix/supported/Text.java index f6803cd..252aca0 100644 --- a/src/be/nikiroo/fanfix/supported/Text.java +++ b/src/be/nikiroo/fanfix/supported/Text.java @@ -1,7 +1,6 @@ package be.nikiroo.fanfix.supported; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; @@ -18,8 +17,9 @@ 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.MarkableFileInputStream; +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: @@ -51,8 +51,7 @@ class Text extends BasicSupport { try { in.reset(); } catch (IOException e) { - Instance.getTraceHandler().error( - new IOException("Cannot reset the Text stream", e)); + Instance.getInstance().getTraceHandler().error(new IOException("Cannot reset the Text stream", e)); } return in; @@ -66,16 +65,11 @@ class Text extends BasicSupport { return false; } - @Override - public String getSourceName() { - return "text"; - } - @Override protected Document loadDocument(URL source) throws IOException { try { sourceFile = new File(source.toURI()); - in = new MarkableFileInputStream(new FileInputStream(sourceFile)); + in = new MarkableFileInputStream(sourceFile); } catch (URISyntaxException e) { throw new IOException("Cannot load the text document: " + source); } @@ -89,24 +83,21 @@ class Text extends BasicSupport { meta.setTitle(getTitle()); meta.setAuthor(getAuthor()); - meta.setDate(getDate()); + meta.setDate(bsHelper.formatDate(getDate())); meta.setTags(new ArrayList()); - meta.setSource(getSourceName()); meta.setUrl(getSourceFile().toURI().toURL().toString()); - meta.setPublisher(""); meta.setUuid(getSourceFile().toString()); meta.setLuid(""); meta.setLang(getLang()); // default is EN meta.setSubject(getSourceFile().getParentFile().getName()); - meta.setType(getType().toString()); meta.setImageDocument(false); meta.setCover(getCover(getSourceFile())); - + return meta; } private String getLang() { - @SuppressWarnings("resource") + @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); scan.next(); // Title @@ -132,14 +123,14 @@ class Text extends BasicSupport { } private String getTitle() { - @SuppressWarnings("resource") + @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); return scan.next(); } private String getAuthor() { - @SuppressWarnings("resource") + @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); scan.next(); @@ -151,11 +142,11 @@ class Text extends BasicSupport { author = authorDate.substring(0, pos); } - return BasicSupportHelper.fixAuthor(author); + return bsHelper.fixAuthor(author); } private String getDate() { - @SuppressWarnings("resource") + @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); scan.next(); @@ -176,10 +167,10 @@ class Text extends BasicSupport { @Override protected String getDesc() throws IOException { - return getChapterContent(null, 0, null); + return getChapterContent(null, 0, null).trim(); } - private Image getCover(File sourceFile) { + protected Image getCover(File sourceFile) { String path = sourceFile.getName(); for (String ext : new String[] { ".txt", ".text", ".story" }) { @@ -188,20 +179,31 @@ class Text extends BasicSupport { } } - return BasicSupportImages.getImage(this, sourceFile.getParentFile(), - path); + Image cover = bsImages.getImage(this, sourceFile.getParentFile(), path); + if (cover != null) { + try { + File tmp = Instance.getInstance().getTempFiles().createTempFile("test_cover_image"); + ImageUtils.getInstance().saveAsImage(cover, tmp, "png"); + tmp.delete(); + } catch (IOException e) { + cover = null; + } + } + + return cover; } @Override protected List> getChapters(Progress pg) throws IOException { List> chaps = new ArrayList>(); - @SuppressWarnings("resource") + @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - boolean prevLineEmpty = false; + String line = "first is not empty"; while (scan.hasNext()) { - String line = scan.next(); + boolean prevLineEmpty = line.trim().isEmpty(); + line = scan.next(); if (prevLineEmpty && detectChapter(line, chaps.size() + 1) != null) { String chapName = Integer.toString(chaps.size() + 1); int pos = line.indexOf(':'); @@ -213,10 +215,8 @@ class Text extends BasicSupport { chapName, // getSourceFile().toURI().toURL())); } - - prevLineEmpty = line.trim().isEmpty(); } - + return chaps; } @@ -224,17 +224,27 @@ class Text extends BasicSupport { protected String getChapterContent(URL source, int number, Progress pg) throws IOException { StringBuilder builder = new StringBuilder(); - @SuppressWarnings("resource") + @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - boolean inChap = false; + scan.next(); // title + scan.next(); // author + scan.next(); // date or empty + Boolean inChap = null; + String line = ""; while (scan.hasNext()) { - String line = scan.next(); - if (detectChapter(line, number) != null) { + if (number == 0 && !line.trim().isEmpty()) { + // We found pre-chapter content, we are checking for + // Chapter 0 (fake chapter) --> keep the content + if (inChap == null) + inChap = true; + } + line = scan.next(); + if ((inChap == null || !inChap) && detectChapter(line, number) != null) { inChap = true; - } else if (inChap && detectChapter(line, number + 1) != null) { + } else if (detectChapter(line, number + 1) != null) { break; - } else if (inChap) { + } else if (inChap != null && inChap) { builder.append(line); builder.append("\n"); } @@ -250,9 +260,8 @@ class Text extends BasicSupport { try { in.close(); } catch (IOException e) { - Instance.getTraceHandler().error( - new IOException( - "Cannot close the text source file input", e)); + Instance.getInstance().getTraceHandler() + .error(new IOException("Cannot close the text source file input", e)); } } @@ -267,6 +276,8 @@ class Text extends BasicSupport { /** * Check if we supports this {@link URL}, that is, if the info file can be * found OR not found. + *

+ * It must also be a file, not another kind of URL. * * @param url * the {@link URL} to check @@ -276,26 +287,28 @@ class Text extends BasicSupport { * @return TRUE if it is supported */ protected boolean supports(URL url, boolean info) { - boolean infoPresent = false; - if ("file".equals(url.getProtocol())) { - File file; - try { - file = new File(url.toURI()); - file = assureNoTxt(file); - file = new File(file.getPath() + ".info"); - } catch (URISyntaxException e) { - Instance.getTraceHandler().error(e); - file = null; - } + if (!"file".equals(url.getProtocol())) { + return false; + } - infoPresent = (file != null && file.exists()); + 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" extension if it is present. + * Remove the ".txt" (or ".text") extension if it is present. * * @param file * the file to process @@ -304,9 +317,11 @@ class Text extends BasicSupport { * was present */ protected File assureNoTxt(File file) { - if (file.getName().endsWith(".txt")) { - file = new File(file.getPath().substring(0, - file.getPath().length() - 4)); + 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; @@ -318,21 +333,28 @@ class Text extends BasicSupport { * * @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.getConfig().getString(Config.CHAPTER) - .split(",")) { - String chapter = Instance.getConfig().getStringX(Config.CHAPTER, - lang); + 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())