| 1 | package be.nikiroo.fanfix; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.net.MalformedURLException; |
| 6 | import java.net.URL; |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.List; |
| 9 | |
| 10 | import javax.net.ssl.SSLException; |
| 11 | |
| 12 | import be.nikiroo.fanfix.bundles.Config; |
| 13 | import be.nikiroo.fanfix.bundles.StringId; |
| 14 | import be.nikiroo.fanfix.data.Chapter; |
| 15 | import be.nikiroo.fanfix.data.MetaData; |
| 16 | import be.nikiroo.fanfix.data.Story; |
| 17 | import be.nikiroo.fanfix.library.BasicLibrary; |
| 18 | import be.nikiroo.fanfix.library.CacheLibrary; |
| 19 | import be.nikiroo.fanfix.library.LocalLibrary; |
| 20 | import be.nikiroo.fanfix.library.RemoteLibrary; |
| 21 | import be.nikiroo.fanfix.library.RemoteLibraryServer; |
| 22 | import be.nikiroo.fanfix.output.BasicOutput; |
| 23 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
| 24 | import be.nikiroo.fanfix.reader.BasicReader; |
| 25 | import be.nikiroo.fanfix.reader.Reader; |
| 26 | import be.nikiroo.fanfix.reader.Reader.ReaderType; |
| 27 | import be.nikiroo.fanfix.searchable.BasicSearchable; |
| 28 | import be.nikiroo.fanfix.supported.BasicSupport; |
| 29 | import be.nikiroo.fanfix.supported.SupportType; |
| 30 | import be.nikiroo.utils.Progress; |
| 31 | import be.nikiroo.utils.Version; |
| 32 | import be.nikiroo.utils.serial.server.ServerObject; |
| 33 | |
| 34 | /** |
| 35 | * Main program entry point. |
| 36 | * |
| 37 | * @author niki |
| 38 | */ |
| 39 | public class Main { |
| 40 | private enum MainAction { |
| 41 | IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, SET_READER, START, VERSION, SERVER, STOP_SERVER, REMOTE, SET_SOURCE, SET_TITLE, SET_AUTHOR, SEARCH, SEARCH_TAG |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Main program entry point. |
| 46 | * <p> |
| 47 | * Known environment variables: |
| 48 | * <ul> |
| 49 | * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode |
| 50 | * {@link String}s when possible</li> |
| 51 | * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files |
| 52 | * before taking the usual ones; they will also be saved/updated into this |
| 53 | * path when the program starts</li> |
| 54 | * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR |
| 55 | * configuration value with 'true'</li> |
| 56 | * </ul> |
| 57 | * <p> |
| 58 | * <ul> |
| 59 | * <li>--import [URL]: import into library</li> |
| 60 | * <li>--export [id] [output_type] [target]: export story to target</li> |
| 61 | * <li>--convert [URL] [output_type] [target] (+info): convert URL into |
| 62 | * target</li> |
| 63 | * <li>--read [id] ([chapter number]): read the given story from the library |
| 64 | * </li> |
| 65 | * <li>--read-url [URL] ([chapter number]): convert on the fly and read the |
| 66 | * story, without saving it</li> |
| 67 | * <li>--search: list the supported websites (where)</li> |
| 68 | * <li>--search [where] [keywords] (page [page]) (item [item]): search on |
| 69 | * the supported website and display the given results page of stories it |
| 70 | * found, or the story details if asked</li> |
| 71 | * <li>--search-tag [where]: list all the tags supported by this website</li> |
| 72 | * <li>--search-tag [index 1]... (page [page]) (item [item]): search for the |
| 73 | * given stories or subtags, tag by tag, and display information about a |
| 74 | * specific page of results or about a specific item if requested</li> |
| 75 | * <li>--list ([type]): list the stories present in the library</li> |
| 76 | * <li>--set-source [id] [new source]: change the source of the given story</li> |
| 77 | * <li>--set-title [id] [new title]: change the title of the given story</li> |
| 78 | * <li>--set-author [id] [new author]: change the author of the given story</li> |
| 79 | * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL |
| 80 | * for this command</li> |
| 81 | * <li>--version: get the version of the program</li> |
| 82 | * <li>--server: start the server mode (see config file for parameters)</li> |
| 83 | * <li>--stop-server: stop the running server on this port if any</li> |
| 84 | * <li>--remote [key] [host] [port]: use a the given remote library</li> |
| 85 | * </ul> |
| 86 | * |
| 87 | * @param args |
| 88 | * see method description |
| 89 | */ |
| 90 | public static void main(String[] args) { |
| 91 | // Only one line, but very important: |
| 92 | Instance.init(); |
| 93 | |
| 94 | String urlString = null; |
| 95 | String luid = null; |
| 96 | String sourceString = null; |
| 97 | String titleString = null; |
| 98 | String authorString = null; |
| 99 | String chapString = null; |
| 100 | String target = null; |
| 101 | String key = null; |
| 102 | MainAction action = MainAction.START; |
| 103 | Boolean plusInfo = null; |
| 104 | String host = null; |
| 105 | Integer port = null; |
| 106 | SupportType searchOn = null; |
| 107 | String search = null; |
| 108 | List<Integer> tags = new ArrayList<Integer>(); |
| 109 | Integer page = null; |
| 110 | Integer item = null; |
| 111 | |
| 112 | boolean noMoreActions = false; |
| 113 | |
| 114 | int exitCode = 0; |
| 115 | for (int i = 0; exitCode == 0 && i < args.length; i++) { |
| 116 | // Action (--) handling: |
| 117 | if (!noMoreActions && args[i].startsWith("--")) { |
| 118 | if (args[i].equals("--")) { |
| 119 | noMoreActions = true; |
| 120 | } else { |
| 121 | try { |
| 122 | action = MainAction.valueOf(args[i].substring(2) |
| 123 | .toUpperCase().replace("-", "_")); |
| 124 | } catch (Exception e) { |
| 125 | Instance.getInstance().getTraceHandler() |
| 126 | .error(new IllegalArgumentException("Unknown action: " + args[i], e)); |
| 127 | exitCode = 255; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | switch (action) { |
| 135 | case IMPORT: |
| 136 | if (urlString == null) { |
| 137 | urlString = args[i]; |
| 138 | } else { |
| 139 | exitCode = 255; |
| 140 | } |
| 141 | break; |
| 142 | case EXPORT: |
| 143 | if (luid == null) { |
| 144 | luid = args[i]; |
| 145 | } else if (sourceString == null) { |
| 146 | sourceString = args[i]; |
| 147 | } else if (target == null) { |
| 148 | target = args[i]; |
| 149 | } else { |
| 150 | exitCode = 255; |
| 151 | } |
| 152 | break; |
| 153 | case CONVERT: |
| 154 | if (urlString == null) { |
| 155 | urlString = args[i]; |
| 156 | } else if (sourceString == null) { |
| 157 | sourceString = args[i]; |
| 158 | } else if (target == null) { |
| 159 | target = args[i]; |
| 160 | } else if (plusInfo == null) { |
| 161 | if ("+info".equals(args[i])) { |
| 162 | plusInfo = true; |
| 163 | } else { |
| 164 | exitCode = 255; |
| 165 | } |
| 166 | } else { |
| 167 | exitCode = 255; |
| 168 | } |
| 169 | break; |
| 170 | case LIST: |
| 171 | if (sourceString == null) { |
| 172 | sourceString = args[i]; |
| 173 | } else { |
| 174 | exitCode = 255; |
| 175 | } |
| 176 | break; |
| 177 | case SET_SOURCE: |
| 178 | if (luid == null) { |
| 179 | luid = args[i]; |
| 180 | } else if (sourceString == null) { |
| 181 | sourceString = args[i]; |
| 182 | } else { |
| 183 | exitCode = 255; |
| 184 | } |
| 185 | break; |
| 186 | case SET_TITLE: |
| 187 | if (luid == null) { |
| 188 | luid = args[i]; |
| 189 | } else if (sourceString == null) { |
| 190 | titleString = args[i]; |
| 191 | } else { |
| 192 | exitCode = 255; |
| 193 | } |
| 194 | break; |
| 195 | case SET_AUTHOR: |
| 196 | if (luid == null) { |
| 197 | luid = args[i]; |
| 198 | } else if (sourceString == null) { |
| 199 | authorString = args[i]; |
| 200 | } else { |
| 201 | exitCode = 255; |
| 202 | } |
| 203 | break; |
| 204 | case READ: |
| 205 | if (luid == null) { |
| 206 | luid = args[i]; |
| 207 | } else if (chapString == null) { |
| 208 | chapString = args[i]; |
| 209 | } else { |
| 210 | exitCode = 255; |
| 211 | } |
| 212 | break; |
| 213 | case READ_URL: |
| 214 | if (urlString == null) { |
| 215 | urlString = args[i]; |
| 216 | } else if (chapString == null) { |
| 217 | chapString = args[i]; |
| 218 | } else { |
| 219 | exitCode = 255; |
| 220 | } |
| 221 | break; |
| 222 | case SEARCH: |
| 223 | if (searchOn == null) { |
| 224 | searchOn = SupportType.valueOfAllOkUC(args[i]); |
| 225 | |
| 226 | if (searchOn == null) { |
| 227 | Instance.getInstance().getTraceHandler().error("Website not known: <" + args[i] + ">"); |
| 228 | exitCode = 41; |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | if (BasicSearchable.getSearchable(searchOn) == null) { |
| 233 | Instance.getInstance().getTraceHandler().error("Website not supported: " + searchOn); |
| 234 | exitCode = 42; |
| 235 | break; |
| 236 | } |
| 237 | } else if (search == null) { |
| 238 | search = args[i]; |
| 239 | } else if (page != null && page == -1) { |
| 240 | try { |
| 241 | page = Integer.parseInt(args[i]); |
| 242 | } catch (Exception e) { |
| 243 | page = -2; |
| 244 | } |
| 245 | } else if (item != null && item == -1) { |
| 246 | try { |
| 247 | item = Integer.parseInt(args[i]); |
| 248 | } catch (Exception e) { |
| 249 | item = -2; |
| 250 | } |
| 251 | } else if (page == null || item == null) { |
| 252 | if (page == null && "page".equals(args[i])) { |
| 253 | page = -1; |
| 254 | } else if (item == null && "item".equals(args[i])) { |
| 255 | item = -1; |
| 256 | } else { |
| 257 | exitCode = 255; |
| 258 | } |
| 259 | } else { |
| 260 | exitCode = 255; |
| 261 | } |
| 262 | break; |
| 263 | case SEARCH_TAG: |
| 264 | if (searchOn == null) { |
| 265 | searchOn = SupportType.valueOfAllOkUC(args[i]); |
| 266 | |
| 267 | if (searchOn == null) { |
| 268 | Instance.getInstance().getTraceHandler().error("Website not known: <" + args[i] + ">"); |
| 269 | exitCode = 255; |
| 270 | } |
| 271 | |
| 272 | if (BasicSearchable.getSearchable(searchOn) == null) { |
| 273 | Instance.getInstance().getTraceHandler().error("Website not supported: " + searchOn); |
| 274 | exitCode = 255; |
| 275 | } |
| 276 | } else if (page == null && item == null) { |
| 277 | if ("page".equals(args[i])) { |
| 278 | page = -1; |
| 279 | } else if ("item".equals(args[i])) { |
| 280 | item = -1; |
| 281 | } else { |
| 282 | try { |
| 283 | int index = Integer.parseInt(args[i]); |
| 284 | tags.add(index); |
| 285 | } catch (NumberFormatException e) { |
| 286 | Instance.getInstance().getTraceHandler().error("Invalid tag index: " + args[i]); |
| 287 | exitCode = 255; |
| 288 | } |
| 289 | } |
| 290 | } else if (page != null && page == -1) { |
| 291 | try { |
| 292 | page = Integer.parseInt(args[i]); |
| 293 | } catch (Exception e) { |
| 294 | page = -2; |
| 295 | } |
| 296 | } else if (item != null && item == -1) { |
| 297 | try { |
| 298 | item = Integer.parseInt(args[i]); |
| 299 | } catch (Exception e) { |
| 300 | item = -2; |
| 301 | } |
| 302 | } else if (page == null || item == null) { |
| 303 | if (page == null && "page".equals(args[i])) { |
| 304 | page = -1; |
| 305 | } else if (item == null && "item".equals(args[i])) { |
| 306 | item = -1; |
| 307 | } else { |
| 308 | exitCode = 255; |
| 309 | } |
| 310 | } else { |
| 311 | exitCode = 255; |
| 312 | } |
| 313 | break; |
| 314 | case HELP: |
| 315 | exitCode = 255; |
| 316 | break; |
| 317 | case SET_READER: |
| 318 | exitCode = setReaderType(args[i]); |
| 319 | action = MainAction.START; |
| 320 | break; |
| 321 | case START: |
| 322 | exitCode = 255; // not supposed to be selected by user |
| 323 | break; |
| 324 | case VERSION: |
| 325 | exitCode = 255; // no arguments for this option |
| 326 | break; |
| 327 | case SERVER: |
| 328 | exitCode = 255; // no arguments for this option |
| 329 | break; |
| 330 | case STOP_SERVER: |
| 331 | exitCode = 255; // no arguments for this option |
| 332 | break; |
| 333 | case REMOTE: |
| 334 | if (key == null) { |
| 335 | key = args[i]; |
| 336 | } else if (host == null) { |
| 337 | host = args[i]; |
| 338 | } else if (port == null) { |
| 339 | port = Integer.parseInt(args[i]); |
| 340 | |
| 341 | BasicLibrary lib = new RemoteLibrary(key, host, port); |
| 342 | lib = new CacheLibrary(Instance.getInstance().getRemoteDir(host), lib, |
| 343 | Instance.getInstance().getUiConfig()); |
| 344 | |
| 345 | BasicReader.setDefaultLibrary(lib); |
| 346 | |
| 347 | action = MainAction.START; |
| 348 | } else { |
| 349 | exitCode = 255; |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | final Progress mainProgress = new Progress(0, 80); |
| 356 | mainProgress.addProgressListener(new Progress.ProgressListener() { |
| 357 | private int current = mainProgress.getMin(); |
| 358 | |
| 359 | @Override |
| 360 | public void progress(Progress progress, String name) { |
| 361 | int diff = progress.getProgress() - current; |
| 362 | current += diff; |
| 363 | |
| 364 | if (diff <= 0) |
| 365 | return; |
| 366 | |
| 367 | StringBuilder builder = new StringBuilder(); |
| 368 | for (int i = 0; i < diff; i++) { |
| 369 | builder.append('.'); |
| 370 | } |
| 371 | |
| 372 | System.err.print(builder.toString()); |
| 373 | |
| 374 | if (progress.isDone()) { |
| 375 | System.err.println(""); |
| 376 | } |
| 377 | } |
| 378 | }); |
| 379 | Progress pg = new Progress(); |
| 380 | mainProgress.addProgress(pg, mainProgress.getMax()); |
| 381 | |
| 382 | VersionCheck updates = VersionCheck.check(); |
| 383 | if (updates.isNewVersionAvailable()) { |
| 384 | // Sent to syserr so not to cause problem if one tries to capture a |
| 385 | // story content in text mode |
| 386 | System.err |
| 387 | .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases"); |
| 388 | System.err.println(""); |
| 389 | for (Version v : updates.getNewer()) { |
| 390 | System.err.println("\tVersion " + v); |
| 391 | System.err.println("\t-------------"); |
| 392 | System.err.println(""); |
| 393 | for (String it : updates.getChanges().get(v)) { |
| 394 | System.err.println("\t- " + it); |
| 395 | } |
| 396 | System.err.println(""); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (exitCode == 0) { |
| 401 | switch (action) { |
| 402 | case IMPORT: |
| 403 | exitCode = imprt(urlString, pg); |
| 404 | updates.ok(); // we consider it read |
| 405 | break; |
| 406 | case EXPORT: |
| 407 | exitCode = export(luid, sourceString, target, pg); |
| 408 | updates.ok(); // we consider it read |
| 409 | break; |
| 410 | case CONVERT: |
| 411 | exitCode = convert(urlString, sourceString, target, |
| 412 | plusInfo == null ? false : plusInfo, pg); |
| 413 | updates.ok(); // we consider it read |
| 414 | break; |
| 415 | case LIST: |
| 416 | if (BasicReader.getReader() == null) { |
| 417 | Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); |
| 418 | exitCode = 10; |
| 419 | break; |
| 420 | } |
| 421 | exitCode = list(sourceString); |
| 422 | break; |
| 423 | case SET_SOURCE: |
| 424 | try { |
| 425 | Instance.getInstance().getLibrary().changeSource(luid, sourceString, pg); |
| 426 | } catch (IOException e1) { |
| 427 | Instance.getInstance().getTraceHandler().error(e1); |
| 428 | exitCode = 21; |
| 429 | } |
| 430 | break; |
| 431 | case SET_TITLE: |
| 432 | try { |
| 433 | Instance.getInstance().getLibrary().changeTitle(luid, titleString, pg); |
| 434 | } catch (IOException e1) { |
| 435 | Instance.getInstance().getTraceHandler().error(e1); |
| 436 | exitCode = 22; |
| 437 | } |
| 438 | break; |
| 439 | case SET_AUTHOR: |
| 440 | try { |
| 441 | Instance.getInstance().getLibrary().changeAuthor(luid, authorString, pg); |
| 442 | } catch (IOException e1) { |
| 443 | Instance.getInstance().getTraceHandler().error(e1); |
| 444 | exitCode = 23; |
| 445 | } |
| 446 | break; |
| 447 | case READ: |
| 448 | if (BasicReader.getReader() == null) { |
| 449 | Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); |
| 450 | exitCode = 10; |
| 451 | break; |
| 452 | } |
| 453 | exitCode = read(luid, chapString, true); |
| 454 | break; |
| 455 | case READ_URL: |
| 456 | if (BasicReader.getReader() == null) { |
| 457 | Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); |
| 458 | exitCode = 10; |
| 459 | break; |
| 460 | } |
| 461 | exitCode = read(urlString, chapString, false); |
| 462 | break; |
| 463 | case SEARCH: |
| 464 | page = page == null ? 1 : page; |
| 465 | if (page < 0) { |
| 466 | Instance.getInstance().getTraceHandler().error("Incorrect page number"); |
| 467 | exitCode = 255; |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | item = item == null ? 0 : item; |
| 472 | if (item < 0) { |
| 473 | Instance.getInstance().getTraceHandler().error("Incorrect item number"); |
| 474 | exitCode = 255; |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | if (BasicReader.getReader() == null) { |
| 479 | Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); |
| 480 | exitCode = 10; |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | try { |
| 485 | if (searchOn == null) { |
| 486 | BasicReader.getReader().search(true); |
| 487 | } else if (search != null) { |
| 488 | |
| 489 | BasicReader.getReader().search(searchOn, search, page, |
| 490 | item, true); |
| 491 | } else { |
| 492 | exitCode = 255; |
| 493 | } |
| 494 | } catch (IOException e1) { |
| 495 | Instance.getInstance().getTraceHandler().error(e1); |
| 496 | exitCode = 20; |
| 497 | } |
| 498 | |
| 499 | break; |
| 500 | case SEARCH_TAG: |
| 501 | if (searchOn == null) { |
| 502 | exitCode = 255; |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | page = page == null ? 1 : page; |
| 507 | if (page < 0) { |
| 508 | Instance.getInstance().getTraceHandler().error("Incorrect page number"); |
| 509 | exitCode = 255; |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | item = item == null ? 0 : item; |
| 514 | if (item < 0) { |
| 515 | Instance.getInstance().getTraceHandler().error("Incorrect item number"); |
| 516 | exitCode = 255; |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | if (BasicReader.getReader() == null) { |
| 521 | Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); |
| 522 | exitCode = 10; |
| 523 | break; |
| 524 | } |
| 525 | |
| 526 | try { |
| 527 | BasicReader.getReader().searchTag(searchOn, page, item, |
| 528 | true, tags.toArray(new Integer[] {})); |
| 529 | } catch (IOException e1) { |
| 530 | Instance.getInstance().getTraceHandler().error(e1); |
| 531 | } |
| 532 | |
| 533 | break; |
| 534 | case HELP: |
| 535 | syntax(true); |
| 536 | exitCode = 0; |
| 537 | break; |
| 538 | case SET_READER: |
| 539 | exitCode = 255; |
| 540 | break; |
| 541 | case VERSION: |
| 542 | System.out |
| 543 | .println(String.format("Fanfix version %s" |
| 544 | + "%nhttps://github.com/nikiroo/fanfix/" |
| 545 | + "%n\tWritten by Nikiroo", |
| 546 | Version.getCurrentVersion())); |
| 547 | updates.ok(); // we consider it read |
| 548 | break; |
| 549 | case START: |
| 550 | if (BasicReader.getReader() == null) { |
| 551 | Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); |
| 552 | exitCode = 10; |
| 553 | break; |
| 554 | } |
| 555 | try { |
| 556 | BasicReader.getReader().browse(null); |
| 557 | } catch (IOException e) { |
| 558 | Instance.getInstance().getTraceHandler().error(e); |
| 559 | exitCode = 66; |
| 560 | } |
| 561 | break; |
| 562 | case SERVER: |
| 563 | key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY); |
| 564 | port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT); |
| 565 | if (port == null) { |
| 566 | System.err.println("No port configured in the config file"); |
| 567 | exitCode = 15; |
| 568 | break; |
| 569 | } |
| 570 | try { |
| 571 | ServerObject server = new RemoteLibraryServer(key, port); |
| 572 | server.setTraceHandler(Instance.getInstance().getTraceHandler()); |
| 573 | server.run(); |
| 574 | } catch (IOException e) { |
| 575 | Instance.getInstance().getTraceHandler().error(e); |
| 576 | } |
| 577 | return; |
| 578 | case STOP_SERVER: |
| 579 | // Can be given via "--remote XX XX XX" |
| 580 | if (key == null) |
| 581 | key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY); |
| 582 | if (port == null) |
| 583 | port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT); |
| 584 | |
| 585 | if (port == null) { |
| 586 | System.err.println("No port given nor configured in the config file"); |
| 587 | exitCode = 15; |
| 588 | break; |
| 589 | } |
| 590 | try { |
| 591 | new RemoteLibrary(key, host, port).exit(); |
| 592 | } catch (SSLException e) { |
| 593 | Instance.getInstance().getTraceHandler().error( |
| 594 | "Bad access key for remote library"); |
| 595 | exitCode = 43; |
| 596 | } catch (IOException e) { |
| 597 | Instance.getInstance().getTraceHandler().error(e); |
| 598 | exitCode = 44; |
| 599 | } |
| 600 | |
| 601 | break; |
| 602 | case REMOTE: |
| 603 | exitCode = 255; // should not be reachable (REMOTE -> START) |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | try { |
| 609 | Instance.getInstance().getTempFiles().close(); |
| 610 | } catch (IOException e) { |
| 611 | Instance.getInstance().getTraceHandler().error(new IOException("Cannot dispose of the temporary files", e)); |
| 612 | } |
| 613 | |
| 614 | if (exitCode == 255) { |
| 615 | syntax(false); |
| 616 | } |
| 617 | |
| 618 | System.exit(exitCode); |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Import the given resource into the {@link LocalLibrary}. |
| 623 | * |
| 624 | * @param urlString |
| 625 | * the resource to import |
| 626 | * @param pg |
| 627 | * the optional progress reporter |
| 628 | * |
| 629 | * @return the exit return code (0 = success) |
| 630 | */ |
| 631 | public static int imprt(String urlString, Progress pg) { |
| 632 | try { |
| 633 | MetaData meta = Instance.getInstance().getLibrary().imprt(BasicReader.getUrl(urlString), pg); |
| 634 | System.out.println(meta.getLuid() + ": \"" + meta.getTitle() + "\" imported."); |
| 635 | } catch (IOException e) { |
| 636 | Instance.getInstance().getTraceHandler().error(e); |
| 637 | return 1; |
| 638 | } |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Export the {@link Story} from the {@link LocalLibrary} to the given |
| 645 | * target. |
| 646 | * |
| 647 | * @param luid |
| 648 | * the story LUID |
| 649 | * @param typeString |
| 650 | * the {@link OutputType} to use |
| 651 | * @param target |
| 652 | * the target |
| 653 | * @param pg |
| 654 | * the optional progress reporter |
| 655 | * |
| 656 | * @return the exit return code (0 = success) |
| 657 | */ |
| 658 | public static int export(String luid, String typeString, String target, |
| 659 | Progress pg) { |
| 660 | OutputType type = OutputType.valueOfNullOkUC(typeString, null); |
| 661 | if (type == null) { |
| 662 | Instance.getInstance().getTraceHandler().error(new Exception(trans(StringId.OUTPUT_DESC, typeString))); |
| 663 | return 1; |
| 664 | } |
| 665 | |
| 666 | try { |
| 667 | Instance.getInstance().getLibrary().export(luid, type, target, pg); |
| 668 | } catch (IOException e) { |
| 669 | Instance.getInstance().getTraceHandler().error(e); |
| 670 | return 4; |
| 671 | } |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * List the stories of the given source from the {@link LocalLibrary} |
| 678 | * (unless NULL is passed, in which case all stories will be listed). |
| 679 | * |
| 680 | * @param source |
| 681 | * the source to list the known stories of, or NULL to list all |
| 682 | * stories |
| 683 | * |
| 684 | * @return the exit return code (0 = success) |
| 685 | */ |
| 686 | private static int list(String source) { |
| 687 | BasicReader.setDefaultReaderType(ReaderType.CLI); |
| 688 | try { |
| 689 | BasicReader.getReader().browse(source); |
| 690 | } catch (IOException e) { |
| 691 | Instance.getInstance().getTraceHandler().error(e); |
| 692 | return 66; |
| 693 | } |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Start the current reader for this {@link Story}. |
| 700 | * |
| 701 | * @param story |
| 702 | * the LUID of the {@link Story} in the {@link LocalLibrary} |
| 703 | * <b>or</b> the {@link Story} {@link URL} |
| 704 | * @param chapString |
| 705 | * which {@link Chapter} to read (starting at 1), or NULL to get |
| 706 | * the {@link Story} description |
| 707 | * @param library |
| 708 | * TRUE if the source is the {@link Story} LUID, FALSE if it is a |
| 709 | * {@link URL} |
| 710 | * |
| 711 | * @return the exit return code (0 = success) |
| 712 | */ |
| 713 | private static int read(String story, String chapString, boolean library) { |
| 714 | try { |
| 715 | Reader reader = BasicReader.getReader(); |
| 716 | if (library) { |
| 717 | reader.setMeta(story); |
| 718 | } else { |
| 719 | reader.setMeta(BasicReader.getUrl(story), null); |
| 720 | } |
| 721 | |
| 722 | if (chapString != null) { |
| 723 | try { |
| 724 | reader.setChapter(Integer.parseInt(chapString)); |
| 725 | reader.read(true); |
| 726 | } catch (NumberFormatException e) { |
| 727 | Instance.getInstance().getTraceHandler() |
| 728 | .error(new IOException("Chapter number cannot be parsed: " + chapString, e)); |
| 729 | return 2; |
| 730 | } |
| 731 | } else { |
| 732 | reader.read(true); |
| 733 | } |
| 734 | } catch (IOException e) { |
| 735 | Instance.getInstance().getTraceHandler().error(e); |
| 736 | return 1; |
| 737 | } |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * Convert the {@link Story} into another format. |
| 744 | * |
| 745 | * @param urlString |
| 746 | * the source {@link Story} to convert |
| 747 | * @param typeString |
| 748 | * the {@link OutputType} to convert to |
| 749 | * @param target |
| 750 | * the target file |
| 751 | * @param infoCover |
| 752 | * TRUE to also export the cover and info file, even if the given |
| 753 | * {@link OutputType} does not usually save them |
| 754 | * @param pg |
| 755 | * the optional progress reporter |
| 756 | * |
| 757 | * @return the exit return code (0 = success) |
| 758 | */ |
| 759 | public static int convert(String urlString, String typeString, |
| 760 | String target, boolean infoCover, Progress pg) { |
| 761 | int exitCode = 0; |
| 762 | |
| 763 | Instance.getInstance().getTraceHandler().trace("Convert: " + urlString); |
| 764 | String sourceName = urlString; |
| 765 | try { |
| 766 | URL source = BasicReader.getUrl(urlString); |
| 767 | sourceName = source.toString(); |
| 768 | if (sourceName.startsWith("file://")) { |
| 769 | sourceName = sourceName.substring("file://".length()); |
| 770 | } |
| 771 | |
| 772 | OutputType type = OutputType.valueOfAllOkUC(typeString, null); |
| 773 | if (type == null) { |
| 774 | Instance.getInstance().getTraceHandler() |
| 775 | .error(new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE, typeString))); |
| 776 | |
| 777 | exitCode = 2; |
| 778 | } else { |
| 779 | try { |
| 780 | BasicSupport support = BasicSupport.getSupport(source); |
| 781 | |
| 782 | if (support != null) { |
| 783 | Instance.getInstance().getTraceHandler().trace("Support found: " + support.getClass()); |
| 784 | Progress pgIn = new Progress(); |
| 785 | Progress pgOut = new Progress(); |
| 786 | if (pg != null) { |
| 787 | pg.setMax(2); |
| 788 | pg.addProgress(pgIn, 1); |
| 789 | pg.addProgress(pgOut, 1); |
| 790 | } |
| 791 | |
| 792 | Story story = support.process(pgIn); |
| 793 | try { |
| 794 | target = new File(target).getAbsolutePath(); |
| 795 | BasicOutput.getOutput(type, infoCover, infoCover).process(story, target, pgOut); |
| 796 | } catch (IOException e) { |
| 797 | Instance.getInstance().getTraceHandler() |
| 798 | .error(new IOException(trans(StringId.ERR_SAVING, target), e)); |
| 799 | exitCode = 5; |
| 800 | } |
| 801 | } else { |
| 802 | Instance.getInstance().getTraceHandler() |
| 803 | .error(new IOException(trans( StringId.ERR_NOT_SUPPORTED, source))); |
| 804 | |
| 805 | exitCode = 4; |
| 806 | } |
| 807 | } catch (IOException e) { |
| 808 | Instance.getInstance().getTraceHandler() |
| 809 | .error(new IOException(trans(StringId.ERR_LOADING, sourceName), e)); |
| 810 | exitCode = 3; |
| 811 | } |
| 812 | } |
| 813 | } catch (MalformedURLException e) { |
| 814 | Instance.getInstance().getTraceHandler().error(new IOException(trans(StringId.ERR_BAD_URL, sourceName), e)); |
| 815 | exitCode = 1; |
| 816 | } |
| 817 | |
| 818 | return exitCode; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * Simple shortcut method to call {link Instance#getTrans()#getString()}. |
| 823 | * |
| 824 | * @param id |
| 825 | * the ID to translate |
| 826 | * |
| 827 | * @return the translated result |
| 828 | */ |
| 829 | private static String trans(StringId id, Object... params) { |
| 830 | return Instance.getInstance().getTrans().getString(id, params); |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Display the correct syntax of the program to the user to stdout, or an |
| 835 | * error message if the syntax used was wrong on stderr. |
| 836 | * |
| 837 | * @param showHelp |
| 838 | * TRUE to show the syntax help, FALSE to show "syntax error" |
| 839 | */ |
| 840 | private static void syntax(boolean showHelp) { |
| 841 | if (showHelp) { |
| 842 | StringBuilder builder = new StringBuilder(); |
| 843 | for (SupportType type : SupportType.values()) { |
| 844 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), |
| 845 | type.getDesc())); |
| 846 | builder.append('\n'); |
| 847 | } |
| 848 | |
| 849 | String typesIn = builder.toString(); |
| 850 | builder.setLength(0); |
| 851 | |
| 852 | for (OutputType type : OutputType.values()) { |
| 853 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), |
| 854 | type.getDesc(true))); |
| 855 | builder.append('\n'); |
| 856 | } |
| 857 | |
| 858 | String typesOut = builder.toString(); |
| 859 | |
| 860 | System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut)); |
| 861 | } else { |
| 862 | System.err.println(trans(StringId.ERR_SYNTAX)); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Set the default reader type for this session only (it can be changed in |
| 868 | * the configuration file, too, but this value will override it). |
| 869 | * |
| 870 | * @param readerTypeString |
| 871 | * the type |
| 872 | */ |
| 873 | private static int setReaderType(String readerTypeString) { |
| 874 | try { |
| 875 | ReaderType readerType = ReaderType.valueOf(readerTypeString |
| 876 | .toUpperCase()); |
| 877 | BasicReader.setDefaultReaderType(readerType); |
| 878 | return 0; |
| 879 | } catch (IllegalArgumentException e) { |
| 880 | Instance.getInstance().getTraceHandler() |
| 881 | .error(new IOException("Unknown reader type: " + readerTypeString, e)); |
| 882 | return 1; |
| 883 | } |
| 884 | } |
| 885 | } |