| 1 | package be.nikiroo.fanfix.supported; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.io.InputStream; |
| 6 | import java.net.URISyntaxException; |
| 7 | import java.net.URL; |
| 8 | import java.util.AbstractMap; |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.List; |
| 11 | import java.util.Map.Entry; |
| 12 | import java.util.Scanner; |
| 13 | |
| 14 | import org.jsoup.nodes.Document; |
| 15 | |
| 16 | import be.nikiroo.fanfix.Instance; |
| 17 | import be.nikiroo.fanfix.bundles.Config; |
| 18 | import be.nikiroo.fanfix.data.Chapter; |
| 19 | import be.nikiroo.fanfix.data.MetaData; |
| 20 | import be.nikiroo.fanfix.data.Paragraph; |
| 21 | import be.nikiroo.utils.Image; |
| 22 | import be.nikiroo.utils.ImageUtils; |
| 23 | import be.nikiroo.utils.Progress; |
| 24 | import be.nikiroo.utils.streams.MarkableFileInputStream; |
| 25 | |
| 26 | /** |
| 27 | * Support class for local stories encoded in textual format, with a few rules: |
| 28 | * <ul> |
| 29 | * <li>The title must be on the first line</li> |
| 30 | * <li>The author (preceded by nothing, "by " or "©") must be on the second |
| 31 | * line, possibly with the publication date in parenthesis (i.e., " |
| 32 | * <tt>By Unknown (3rd October 1998)</tt>")</li> |
| 33 | * <li>Chapters must be declared with "<tt>Chapter x</tt>" or " |
| 34 | * <tt>Chapter x: NAME OF THE CHAPTER</tt>", where "<tt>x</tt>" is the chapter |
| 35 | * number</li> |
| 36 | * <li>A description of the story must be given as chapter number 0</li> |
| 37 | * <li>A cover may be present, with the same filename but a PNG, JPEG or JPG |
| 38 | * extension</li> |
| 39 | * </ul> |
| 40 | * |
| 41 | * @author niki |
| 42 | */ |
| 43 | class Text extends BasicSupport { |
| 44 | private File sourceFile; |
| 45 | private InputStream in; |
| 46 | |
| 47 | protected File getSourceFile() { |
| 48 | return sourceFile; |
| 49 | } |
| 50 | |
| 51 | protected InputStream getInput() { |
| 52 | if (in != null) { |
| 53 | try { |
| 54 | in.reset(); |
| 55 | } catch (IOException e) { |
| 56 | Instance.getInstance().getTraceHandler().error(new IOException("Cannot reset the Text stream", e)); |
| 57 | } |
| 58 | |
| 59 | return in; |
| 60 | } |
| 61 | |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected boolean isHtml() { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected Document loadDocument(URL source) throws IOException { |
| 72 | try { |
| 73 | sourceFile = new File(source.toURI()); |
| 74 | in = new MarkableFileInputStream(sourceFile); |
| 75 | } catch (URISyntaxException e) { |
| 76 | throw new IOException("Cannot load the text document: " + source); |
| 77 | } |
| 78 | |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected MetaData getMeta() throws IOException { |
| 84 | MetaData meta = new MetaData(); |
| 85 | |
| 86 | meta.setTitle(getTitle()); |
| 87 | meta.setAuthor(getAuthor()); |
| 88 | meta.setDate(getDate()); |
| 89 | meta.setTags(new ArrayList<String>()); |
| 90 | meta.setSource(getType().getSourceName()); |
| 91 | meta.setUrl(getSourceFile().toURI().toURL().toString()); |
| 92 | meta.setPublisher(""); |
| 93 | meta.setUuid(getSourceFile().toString()); |
| 94 | meta.setLuid(""); |
| 95 | meta.setLang(getLang()); // default is EN |
| 96 | meta.setSubject(getSourceFile().getParentFile().getName()); |
| 97 | meta.setType(getType().toString()); |
| 98 | meta.setImageDocument(false); |
| 99 | meta.setCover(getCover(getSourceFile())); |
| 100 | |
| 101 | return meta; |
| 102 | } |
| 103 | |
| 104 | private String getLang() { |
| 105 | @SuppressWarnings("resource") |
| 106 | Scanner scan = new Scanner(getInput(), "UTF-8"); |
| 107 | scan.useDelimiter("\\n"); |
| 108 | scan.next(); // Title |
| 109 | scan.next(); // Author (Date) |
| 110 | String chapter0 = scan.next(); // empty or Chapter 0 |
| 111 | while (chapter0.isEmpty()) { |
| 112 | chapter0 = scan.next(); |
| 113 | } |
| 114 | |
| 115 | String lang = detectChapter(chapter0, 0); |
| 116 | if (lang == null) { |
| 117 | // No description?? |
| 118 | lang = detectChapter(chapter0, 1); |
| 119 | } |
| 120 | |
| 121 | if (lang == null) { |
| 122 | lang = "en"; |
| 123 | } else { |
| 124 | lang = lang.toLowerCase(); |
| 125 | } |
| 126 | |
| 127 | return lang; |
| 128 | } |
| 129 | |
| 130 | private String getTitle() { |
| 131 | @SuppressWarnings("resource") |
| 132 | Scanner scan = new Scanner(getInput(), "UTF-8"); |
| 133 | scan.useDelimiter("\\n"); |
| 134 | return scan.next(); |
| 135 | } |
| 136 | |
| 137 | private String getAuthor() { |
| 138 | @SuppressWarnings("resource") |
| 139 | Scanner scan = new Scanner(getInput(), "UTF-8"); |
| 140 | scan.useDelimiter("\\n"); |
| 141 | scan.next(); |
| 142 | String authorDate = scan.next(); |
| 143 | |
| 144 | String author = authorDate; |
| 145 | int pos = authorDate.indexOf('('); |
| 146 | if (pos >= 0) { |
| 147 | author = authorDate.substring(0, pos); |
| 148 | } |
| 149 | |
| 150 | return bsHelper.fixAuthor(author); |
| 151 | } |
| 152 | |
| 153 | private String getDate() { |
| 154 | @SuppressWarnings("resource") |
| 155 | Scanner scan = new Scanner(getInput(), "UTF-8"); |
| 156 | scan.useDelimiter("\\n"); |
| 157 | scan.next(); |
| 158 | String authorDate = scan.next(); |
| 159 | |
| 160 | String date = ""; |
| 161 | int pos = authorDate.indexOf('('); |
| 162 | if (pos >= 0) { |
| 163 | date = authorDate.substring(pos + 1).trim(); |
| 164 | pos = date.lastIndexOf(')'); |
| 165 | if (pos >= 0) { |
| 166 | date = date.substring(0, pos).trim(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return date; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | protected String getDesc() throws IOException { |
| 175 | String content = getChapterContent(null, 0, null).trim(); |
| 176 | if (!content.isEmpty()) { |
| 177 | Chapter desc = bsPara.makeChapter(this, null, 0, "Description", |
| 178 | content, isHtml(), null); |
| 179 | StringBuilder builder = new StringBuilder(); |
| 180 | for (Paragraph para : desc) { |
| 181 | if (builder.length() > 0) { |
| 182 | builder.append("\n"); |
| 183 | } |
| 184 | builder.append(para.getContent()); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return content; |
| 189 | } |
| 190 | |
| 191 | private Image getCover(File sourceFile) { |
| 192 | String path = sourceFile.getName(); |
| 193 | |
| 194 | for (String ext : new String[] { ".txt", ".text", ".story" }) { |
| 195 | if (path.endsWith(ext)) { |
| 196 | path = path.substring(0, path.length() - ext.length()); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | Image cover = bsImages.getImage(this, sourceFile.getParentFile(), path); |
| 201 | if (cover != null) { |
| 202 | try { |
| 203 | File tmp = Instance.getInstance().getTempFiles().createTempFile("test_cover_image"); |
| 204 | ImageUtils.getInstance().saveAsImage(cover, tmp, "png"); |
| 205 | tmp.delete(); |
| 206 | } catch (IOException e) { |
| 207 | cover = null; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return cover; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | protected List<Entry<String, URL>> getChapters(Progress pg) |
| 216 | throws IOException { |
| 217 | List<Entry<String, URL>> chaps = new ArrayList<Entry<String, URL>>(); |
| 218 | @SuppressWarnings("resource") |
| 219 | Scanner scan = new Scanner(getInput(), "UTF-8"); |
| 220 | scan.useDelimiter("\\n"); |
| 221 | boolean prevLineEmpty = false; |
| 222 | while (scan.hasNext()) { |
| 223 | String line = scan.next(); |
| 224 | if (prevLineEmpty && detectChapter(line, chaps.size() + 1) != null) { |
| 225 | String chapName = Integer.toString(chaps.size() + 1); |
| 226 | int pos = line.indexOf(':'); |
| 227 | if (pos >= 0 && pos + 1 < line.length()) { |
| 228 | chapName = line.substring(pos + 1).trim(); |
| 229 | } |
| 230 | |
| 231 | chaps.add(new AbstractMap.SimpleEntry<String, URL>(// |
| 232 | chapName, // |
| 233 | getSourceFile().toURI().toURL())); |
| 234 | } |
| 235 | |
| 236 | prevLineEmpty = line.trim().isEmpty(); |
| 237 | } |
| 238 | |
| 239 | return chaps; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | protected String getChapterContent(URL source, int number, Progress pg) |
| 244 | throws IOException { |
| 245 | StringBuilder builder = new StringBuilder(); |
| 246 | @SuppressWarnings("resource") |
| 247 | Scanner scan = new Scanner(getInput(), "UTF-8"); |
| 248 | scan.useDelimiter("\\n"); |
| 249 | boolean inChap = false; |
| 250 | while (scan.hasNext()) { |
| 251 | String line = scan.next(); |
| 252 | if (!inChap && detectChapter(line, number) != null) { |
| 253 | inChap = true; |
| 254 | } else if (detectChapter(line, number + 1) != null) { |
| 255 | break; |
| 256 | } else if (inChap) { |
| 257 | builder.append(line); |
| 258 | builder.append("\n"); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return builder.toString(); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | protected void close() { |
| 267 | InputStream in = getInput(); |
| 268 | if (in != null) { |
| 269 | try { |
| 270 | in.close(); |
| 271 | } catch (IOException e) { |
| 272 | Instance.getInstance().getTraceHandler() |
| 273 | .error(new IOException("Cannot close the text source file input", e)); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | super.close(); |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | protected boolean supports(URL url) { |
| 282 | return supports(url, false); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Check if we supports this {@link URL}, that is, if the info file can be |
| 287 | * found OR not found. |
| 288 | * <p> |
| 289 | * It must also be a file, not another kind of URL. |
| 290 | * |
| 291 | * @param url |
| 292 | * the {@link URL} to check |
| 293 | * @param info |
| 294 | * TRUE to require the info file, FALSE to forbid the info file |
| 295 | * |
| 296 | * @return TRUE if it is supported |
| 297 | */ |
| 298 | protected boolean supports(URL url, boolean info) { |
| 299 | if (!"file".equals(url.getProtocol())) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | boolean infoPresent = false; |
| 304 | File file; |
| 305 | try { |
| 306 | file = new File(url.toURI()); |
| 307 | file = assureNoTxt(file); |
| 308 | file = new File(file.getPath() + ".info"); |
| 309 | } catch (URISyntaxException e) { |
| 310 | Instance.getInstance().getTraceHandler().error(e); |
| 311 | file = null; |
| 312 | } |
| 313 | |
| 314 | infoPresent = (file != null && file.exists()); |
| 315 | |
| 316 | return infoPresent == info; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Remove the ".txt" extension if it is present. |
| 321 | * |
| 322 | * @param file |
| 323 | * the file to process |
| 324 | * |
| 325 | * @return the same file or a copy of it without the ".txt" extension if it |
| 326 | * was present |
| 327 | */ |
| 328 | protected File assureNoTxt(File file) { |
| 329 | if (file.getName().endsWith(".txt")) { |
| 330 | file = new File(file.getPath().substring(0, |
| 331 | file.getPath().length() - 4)); |
| 332 | } |
| 333 | |
| 334 | return file; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Check if the given line looks like the given starting chapter in a |
| 339 | * supported language, and return the language if it does (or NULL if not). |
| 340 | * |
| 341 | * @param line |
| 342 | * the line to check |
| 343 | * @param number |
| 344 | * the specific chapter number to check for |
| 345 | * |
| 346 | * @return the language or NULL |
| 347 | */ |
| 348 | static private String detectChapter(String line, int number) { |
| 349 | line = line.toUpperCase(); |
| 350 | for (String lang : Instance.getInstance().getConfig().getList(Config.CONF_CHAPTER)) { |
| 351 | String chapter = Instance.getInstance().getConfig().getStringX(Config.CONF_CHAPTER, lang); |
| 352 | if (chapter != null && !chapter.isEmpty()) { |
| 353 | chapter = chapter.toUpperCase() + " "; |
| 354 | if (line.startsWith(chapter)) { |
| 355 | // We want "[CHAPTER] [number]: [name]", with ": [name]" |
| 356 | // optional |
| 357 | String test = line.substring(chapter.length()).trim(); |
| 358 | |
| 359 | String possibleNum = test.trim(); |
| 360 | if (possibleNum.indexOf(':') > 0) { |
| 361 | possibleNum = possibleNum.substring(0, |
| 362 | possibleNum.indexOf(':')).trim(); |
| 363 | } |
| 364 | |
| 365 | if (test.startsWith(Integer.toString(number))) { |
| 366 | test = test |
| 367 | .substring(Integer.toString(number).length()) |
| 368 | .trim(); |
| 369 | if (test.isEmpty() || test.startsWith(":")) { |
| 370 | return lang; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return null; |
| 378 | } |
| 379 | } |