Search in sources :

Example 1 with Books

use of org.crosswire.jsword.book.Books in project BibleMultiConverter by schierlm.

the class SWORDDownloader method run.

@Override
public void run(String... args) throws Exception {
    InstallManager imanager = new InstallManager();
    if (args.length == 0) {
        for (Map.Entry<String, Installer> mapEntry : imanager.getInstallers().entrySet()) {
            System.out.println(mapEntry.getKey().replace(' ', '_') + ": " + mapEntry.getValue().getInstallerDefinition());
        }
        return;
    }
    // initialize list of installed books (this may create some log
    // output...)
    System.out.println("Loading locally installed books...");
    SwordBookPath.setDownloadDir(new File(args[0]));
    Books installedBooks = Books.installed();
    System.out.println("======");
    if (args.length == 1) {
        for (Book bk : installedBooks.getBooks(BookFilters.getOnlyBibles())) {
            System.out.println(bk.getInitials() + " (" + bk.getName() + "): " + bk.getProperty("Version"));
        }
        return;
    }
    Installer installer;
    if (args[1].contains("|")) {
        String[] parts = args[1].split("\\|");
        if (parts.length != 3)
            throw new IOException("Invalid repository: " + args[1]);
        HttpSwordInstaller httpInstaller = new HttpSwordInstaller();
        httpInstaller.setHost(parts[0]);
        httpInstaller.setPackageDirectory(parts[1]);
        httpInstaller.setCatalogDirectory(parts[2]);
        installer = httpInstaller;
    } else {
        installer = imanager.getInstaller(args[1].replace('_', ' '));
        if (installer == null)
            throw new IOException("Unknown repository: " + args[1]);
    }
    System.out.println("Loading remote book list...");
    installer.reloadBookList();
    List<Book> availableBooks = installer.getBooks(BookFilters.getOnlyBibles());
    System.out.println("======");
    if (args.length == 2) {
        args = new String[] { args[0], args[1], "*?" };
    }
    for (int i = 2; i < args.length; i++) {
        boolean force = false, updateOnly = false, printOnly = false;
        String name = args[i].replace('@', '*');
        if (name.endsWith("?")) {
            printOnly = true;
            name = name.substring(0, name.length() - 1);
        }
        if (name.endsWith("!")) {
            force = true;
            name = name.substring(0, name.length() - 1);
        } else if (name.endsWith("+")) {
            updateOnly = true;
            name = name.substring(0, name.length() - 1);
        }
        for (Book bk : availableBooks) {
            if (bk.getInitials().equalsIgnoreCase(name) || (name.endsWith("*") && bk.getInitials().toLowerCase().startsWith(name.substring(0, name.length() - 1).toLowerCase()))) {
                Book installedBook = installedBooks.getBook(bk.getInitials());
                if (installedBook == null) {
                    if (updateOnly)
                        continue;
                } else if (installedBook != null && installedBook.getProperty("Version").equals(bk.getProperty("Version"))) {
                    if (!force)
                        continue;
                }
                if (printOnly) {
                    System.out.print(bk.getInitials() + " (" + bk.getName() + "): ");
                    if (installedBook == null) {
                        System.out.println("NEW -> " + bk.getProperty("Version"));
                    } else if (installedBook.getProperty("Version").equals(bk.getProperty("Version"))) {
                        System.out.println(installedBook.getProperty("Version"));
                    } else {
                        System.out.println(installedBook.getProperty("Version") + " -> " + bk.getProperty("Version"));
                    }
                } else {
                    System.out.println("Installing " + bk.getInitials() + " (" + bk.getName() + ")...");
                    try {
                        installer.install(bk);
                    } catch (ArithmeticException ex) {
                        // happens for some IBT modules...
                        System.err.println(ex.toString());
                    }
                }
            }
        }
    }
}
Also used : HttpSwordInstaller(org.crosswire.jsword.book.install.sword.HttpSwordInstaller) Installer(org.crosswire.jsword.book.install.Installer) IOException(java.io.IOException) HttpSwordInstaller(org.crosswire.jsword.book.install.sword.HttpSwordInstaller) Book(org.crosswire.jsword.book.Book) Books(org.crosswire.jsword.book.Books) Map(java.util.Map) File(java.io.File) InstallManager(org.crosswire.jsword.book.install.InstallManager)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Book (org.crosswire.jsword.book.Book)1 Books (org.crosswire.jsword.book.Books)1 InstallManager (org.crosswire.jsword.book.install.InstallManager)1 Installer (org.crosswire.jsword.book.install.Installer)1 HttpSwordInstaller (org.crosswire.jsword.book.install.sword.HttpSwordInstaller)1