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