[jsword-svn] r1785 - in trunk/jsword/src/main/java/org/crosswire/jsword: book/install book/install/sword examples

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Thu Apr 3 14:11:31 MST 2008


Author: dmsmith
Date: 2008-04-03 14:11:30 -0700 (Thu, 03 Apr 2008)
New Revision: 1785

Added:
   trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookExporter.java
   trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookIndexer.java
   trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookInstaller.java
Modified:
   trunk/jsword/src/main/java/org/crosswire/jsword/book/install/Installer.java
   trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/AbstractSwordInstaller.java
   trunk/jsword/src/main/java/org/crosswire/jsword/examples/APIExamples.java
Log:
Added isolated examples.

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/install/Installer.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/install/Installer.java	2008-03-20 11:40:27 UTC (rev 1784)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/install/Installer.java	2008-04-03 21:11:30 UTC (rev 1785)
@@ -71,6 +71,11 @@
     List getBooks();
 
     /**
+     * Get a Book matching the name from the local cache. Null if none is found.
+     */
+    Book getBook(String Book);
+
+    /**
      * Return true if the book is not installed or there is a newer
      * version to install.
      * @param book The book meta-data to check on.
@@ -105,10 +110,10 @@
     /**
      * Download a search index for the given Book.
      * The installation of the search index is the responsibility of the
-     * IndexManager.
+     * BookIndexer.
      * @param book The book to download a search index for.
      * @param tempDest A temporary URI for downloading to. Passed to the
-     * IndexManager for installation.
+     * BookIndexer for installation.
      */
     void downloadSearchIndex(Book book, URI tempDest) throws InstallException;
 }

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/AbstractSwordInstaller.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/AbstractSwordInstaller.java	2008-03-20 11:40:27 UTC (rev 1784)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/AbstractSwordInstaller.java	2008-04-03 21:11:30 UTC (rev 1785)
@@ -26,14 +26,15 @@
 import java.io.InputStream;
 import java.net.URI;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.zip.GZIPInputStream;
 
 import org.crosswire.common.progress.JobManager;
 import org.crosswire.common.progress.Progress;
+import org.crosswire.common.util.CollectionUtil;
 import org.crosswire.common.util.IOUtil;
 import org.crosswire.common.util.Logger;
 import org.crosswire.common.util.NetUtil;
@@ -41,6 +42,10 @@
 import org.crosswire.jsword.book.Book;
 import org.crosswire.jsword.book.BookDriver;
 import org.crosswire.jsword.book.BookException;
+import org.crosswire.jsword.book.BookFilter;
+import org.crosswire.jsword.book.BookFilterIterator;
+import org.crosswire.jsword.book.BookMetaData;
+import org.crosswire.jsword.book.BookSet;
 import org.crosswire.jsword.book.Books;
 import org.crosswire.jsword.book.basic.AbstractBookList;
 import org.crosswire.jsword.book.install.InstallException;
@@ -136,10 +141,8 @@
             }
 
             // We need to create a List from the Set returned by
-            // entries.values() so we can create an unmodifiable list from it.
-            List mutable = new ArrayList();
-            mutable.addAll(entries.values());
-            return Collections.unmodifiableList(mutable);
+            // entries.values() so the underlying list is not modified.
+            return new ArrayList(entries.values());
         }
         catch (InstallException ex)
         {
@@ -149,6 +152,69 @@
     }
 
     /* (non-Javadoc)
+     * @see org.crosswire.jsword.book.BookList#getBook(java.lang.String)
+     */
+    public synchronized Book getBook(String name)
+    {
+        // Check name first
+        // First check for exact matches
+        List books = getBooks();
+        Iterator iter = books.iterator();
+        while (iter.hasNext())
+        {
+            Book book = (Book) iter.next();
+            if (name.equals(book.getName()))
+            {
+                return book;
+            }
+        }
+
+        // Next check for case-insensitive matches
+        iter = books.iterator();
+        while (iter.hasNext())
+        {
+            Book book = (Book) iter.next();
+            if (name.equalsIgnoreCase(book.getName()))
+            {
+                return book;
+            }
+        }
+
+        // Then check initials
+        // First check for exact matches
+        iter = books.iterator();
+        while (iter.hasNext())
+        {
+            Book book = (Book) iter.next();
+            BookMetaData bmd = book.getBookMetaData();
+            if (name.equals(bmd.getInitials()))
+            {
+                return book;
+            }
+        }
+
+        // Next check for case-insensitive matches
+        iter = books.iterator();
+        while (iter.hasNext())
+        {
+            Book book = (Book) iter.next();
+            if (name.equalsIgnoreCase(book.getInitials()))
+            {
+                return book;
+            }
+        }
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.crosswire.jsword.book.BookList#getBooks(org.crosswire.jsword.book.BookFilter)
+     */
+    public synchronized List getBooks(BookFilter filter)
+    {
+        List temp = CollectionUtil.createList(new BookFilterIterator(getBooks(), filter));
+        return new BookSet(temp);
+    }
+    /* (non-Javadoc)
      * @see org.crosswire.jsword.book.install.Installer#install(org.crosswire.jsword.book.Book)
      */
     public void install(Book book)

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/examples/APIExamples.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/examples/APIExamples.java	2008-03-20 11:40:27 UTC (rev 1784)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/examples/APIExamples.java	2008-04-03 21:11:30 UTC (rev 1785)
@@ -54,9 +54,7 @@
 import org.crosswire.jsword.passage.Passage;
 import org.crosswire.jsword.passage.PassageTally;
 import org.crosswire.jsword.passage.RestrictionType;
-import org.crosswire.jsword.passage.Verse;
 import org.crosswire.jsword.util.ConverterFactory;
-import org.crosswire.jsword.versification.BibleInfo;
 import org.xml.sax.SAXException;
 
 /**
@@ -160,7 +158,7 @@
 
         System.out.println("The following verses contain both moses and aaron: " + key.getName()); //$NON-NLS-1$
 
-        // You can also trim the result to a more managable quantity.
+        // You can also trim the result to a more manageable quantity.
         // The test here is not necessary since we are working with a bible. It is necessary if we don't know what it is.
         if (key instanceof Passage)
         {
@@ -238,26 +236,6 @@
     }
 
     /**
-     * An example of how to get the text of a book for export.
-     *
-     * @throws NoSuchKeyException
-     * @throws BookException
-     */
-    public void export() throws NoSuchKeyException, BookException
-    {
-        Book bible = Books.installed().getBook(BIBLE_NAME);
-        Key keys = bible.getKey("Gen"); //$NON-NLS-1$
-        // Get a verse iterator
-        Iterator iter = keys.iterator();
-        while (iter.hasNext())
-        {
-            Verse verse = (Verse) iter.next();
-            BookData data = new BookData(bible, verse);
-            System.out.println('|' + BibleInfo.getPreferredBookName(verse.getBook()) + '|' + verse.getChapter() + '|' + verse.getVerse() + '|' + OSISUtil.getCanonicalText(data.getOsisFragment()));
-        }
-    }
-
-    /**
      * This is an example of the different ways to select a Book from the
      * selection available.
      * @see org.crosswire.common.config.Config
@@ -428,6 +406,5 @@
         examples.search();
         examples.rankedSearch();
         examples.searchAndShow();
-        examples.export();
     }
 }

Added: trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookExporter.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookExporter.java	                        (rev 0)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookExporter.java	2008-04-03 21:11:30 UTC (rev 1785)
@@ -0,0 +1,122 @@
+/**
+ * Distribution License:
+ * JSword is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License, version 2.1 as published by
+ * the Free Software Foundation. This program is distributed in the hope
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * The License is available on the internet at:
+ *       http://www.gnu.org/copyleft/lgpl.html
+ * or by writing to:
+ *      Free Software Foundation, Inc.
+ *      59 Temple Place - Suite 330
+ *      Boston, MA 02111-1307, USA
+ *
+ * Copyright: 2008
+ *     The copyright to this program is held by it's authors.
+ *
+ * ID: $Id: BookIndexer.java 1466 2007-07-02 02:48:09Z dmsmith $
+ */
+package org.crosswire.jsword.examples;
+
+import java.util.Iterator;
+
+import org.crosswire.jsword.book.Book;
+import org.crosswire.jsword.book.BookCategory;
+import org.crosswire.jsword.book.BookException;
+import org.crosswire.jsword.book.Books;
+import org.crosswire.jsword.passage.Key;
+import org.crosswire.jsword.versification.BibleInfo;
+
+/**
+ * Exports the Book in SWORD's imp format.
+ * This is identical to SWORD's mod2imp.
+ * Note: it does not work with GenBook.
+ * 
+ * @see gnu.lgpl.License for license details.
+ *      The copyright to this program is held by it's authors.
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class BookExporter
+{
+
+    public BookExporter(Book book)
+    {
+        this.book = book;
+    }
+
+    public void mod2imp() throws BookException
+    {
+        // Use short key names for Bibles.
+        if (BookCategory.BIBLE.equals(book.getBookCategory()))
+        {
+            BibleInfo.setFullBookName(false);
+        }
+
+        Key keys = book.getGlobalKeyList();
+
+        Iterator iter = keys.iterator();
+        StringBuffer buf = new StringBuffer();
+        while (iter.hasNext())
+        {
+            Key key = (Key) iter.next();
+            String rawText = book.getRawText(key);
+            if (rawText != null && rawText.trim().length() > 0)
+            {
+                buf.delete(0, buf.length());
+                buf.append("$$$").append(key).append('\n').append(rawText); //$NON-NLS-1$
+                System.out.println(buf.toString());
+            }
+        }
+    }
+
+    private Book book;
+
+    /**
+     * Call with <operation> book.
+     * Where operation can be one of:
+     * <ul>
+     * <li>check - returns "TRUE" or "FALSE" indicating whether the index exists or not</li>
+     * <li>create - (re)create the index</li>
+     * <li>delete - delete the index if it exists</li>
+     * </ul>
+     * And book is the initials of a book, e.g. KJV.
+     * 
+     * @param args
+     */
+    public static void main(String[] args)
+    {
+        if (args.length != 1)
+        {
+            usage();
+        }
+
+        System.err.println("BookExporter " + args[0]); //$NON-NLS-1$
+
+        Book b = Books.installed().getBook(args[0]);
+        if (b == null)
+        {
+            System.err.println("Book not found"); //$NON-NLS-1$
+            System.exit(1);
+        }
+
+        BookExporter exporter = new BookExporter(b);
+        try
+        {
+            exporter.mod2imp();
+        }
+        catch (BookException e)
+        {
+            System.err.println("Error while exporting"); //$NON-NLS-1$
+            e.printStackTrace();
+        }
+    }
+
+    public static void usage()
+    {
+        System.err.println("Usage: BookExporter book"); //$NON-NLS-1$
+        System.exit(1);
+    }
+}

Added: trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookIndexer.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookIndexer.java	                        (rev 0)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookIndexer.java	2008-04-03 21:11:30 UTC (rev 1785)
@@ -0,0 +1,202 @@
+/**
+ * Distribution License:
+ * JSword is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License, version 2.1 as published by
+ * the Free Software Foundation. This program is distributed in the hope
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * The License is available on the internet at:
+ *       http://www.gnu.org/copyleft/lgpl.html
+ * or by writing to:
+ *      Free Software Foundation, Inc.
+ *      59 Temple Place - Suite 330
+ *      Boston, MA 02111-1307, USA
+ *
+ * Copyright: 2008
+ *     The copyright to this program is held by it's authors.
+ *
+ * ID: $Id: BookIndexer.java 1466 2007-07-02 02:48:09Z dmsmith $
+ */
+package org.crosswire.jsword.examples;
+
+import org.crosswire.jsword.book.Book;
+import org.crosswire.jsword.book.BookException;
+import org.crosswire.jsword.book.Books;
+import org.crosswire.jsword.index.IndexManager;
+import org.crosswire.jsword.index.IndexManagerFactory;
+import org.crosswire.jsword.index.IndexStatusEvent;
+import org.crosswire.jsword.index.IndexStatusListener;
+
+/**
+ * BookIndexer allows one to check the status of an index, build an index or delete an index.
+ * This is similar to SWORD's mkfastmod.
+ * 
+ * @see gnu.lgpl.License for license details.
+ *      The copyright to this program is held by it's authors.
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class BookIndexer
+{
+
+    public BookIndexer(Book book)
+    {
+        this.book = book;
+        done = true; // not busy
+        indexManager = IndexManagerFactory.getIndexManager();
+        isl = new StatusListener(this);
+    }
+
+    public boolean isIndexed()
+    {
+        // If we are busy then the index is being created
+        // or it is being deleted. So for all practical purposes
+        // it is not indexed.
+        return done && indexManager.isIndexed(book);
+    }
+
+    public void deleteIndex() throws BookException
+    {
+        if (done)
+        {
+            done = false;
+            book.addIndexStatusListener(isl);
+            indexManager.deleteIndex(book);
+            while (!done)
+            {
+                try
+                {
+                    Thread.sleep(100);
+                }
+                catch (InterruptedException e)
+                {
+                    // ok to be interrupted
+                }
+            }
+            book.removeIndexStatusListener(isl);
+        }
+    }
+
+    public void createIndex() throws BookException
+    {
+        if (done)
+        {
+            done = false;
+            book.addIndexStatusListener(isl);
+            if (isIndexed())
+            {
+                deleteIndex();
+            }
+            indexManager.scheduleIndexCreation(book);
+            while (!done)
+            {
+                try
+                {
+                    Thread.sleep(100);
+                }
+                catch (InterruptedException e)
+                {
+                    // ok to be interrupted
+                }
+            }
+            book.removeIndexStatusListener(isl);
+        }
+    }
+
+    protected void setDone(boolean state)
+    {
+        done = state;
+    }
+
+    private Book book;
+    private IndexManager indexManager;
+    private IndexStatusListener isl;
+    private boolean done;
+
+    public static final class StatusListener implements IndexStatusListener
+    {
+        public StatusListener(BookIndexer indexer)
+        {
+            this.indexer = indexer;
+        }
+
+        public void statusChanged(IndexStatusEvent ev)
+        {
+            indexer.setDone(true);
+        }
+
+        BookIndexer indexer;
+        
+    }
+
+    /**
+     * Call with &lt;operation&gt; book.
+     * Where operation can be one of:
+     * <ul>
+     * <li>check - returns "TRUE" or "FALSE" indicating whether the index exists or not</li>
+     * <li>create - (re)create the index</li>
+     * <li>delete - delete the index if it exists</li>
+     * </ul>
+     * And book is the initials of a book, e.g. KJV.
+     * 
+     * @param args
+     */
+    public static void main(String[] args)
+    {
+        if (args.length != 2)
+        {
+            usage();
+        }
+
+        System.err.println("BookIndexer " + args[0] + " " + args[1]); //$NON-NLS-1$ //$NON-NLS-2$
+
+        String operation = args[0];
+        Book b = Books.installed().getBook(args[1]);
+        if (b == null)
+        {
+            System.err.println("Book not found"); //$NON-NLS-1$
+            System.exit(1);
+        }
+
+        BookIndexer indexer = new BookIndexer(b);
+        if (operation.equalsIgnoreCase("create")) //$NON-NLS-1$
+        {
+            try
+            {
+                indexer.createIndex();
+            }
+            catch (BookException e)
+            {
+                System.err.println("Unable to re-index book."); //$NON-NLS-1$
+                e.printStackTrace();
+            }
+        }
+        else if (operation.equalsIgnoreCase("delete")) //$NON-NLS-1$
+        {
+            try
+            {
+                indexer.deleteIndex();
+            }
+            catch (BookException e)
+            {
+                System.err.println("Unable to delete index for book."); //$NON-NLS-1$
+                e.printStackTrace();
+            }
+        }
+        else if (operation.equalsIgnoreCase("check")) //$NON-NLS-1$
+        {
+            System.err.println(indexer.isIndexed());
+        }
+        else
+        {
+            usage();
+        }
+    }
+
+    public static void usage()
+    {
+        System.err.println("Usage: BookIndexer operation book"); //$NON-NLS-1$
+        System.exit(1);
+    }
+}

Added: trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookInstaller.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookInstaller.java	                        (rev 0)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/examples/BookInstaller.java	2008-04-03 21:11:30 UTC (rev 1785)
@@ -0,0 +1,298 @@
+/**
+ * Distribution License:
+ * JSword is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License, version 2.1 as published by
+ * the Free Software Foundation. This program is distributed in the hope
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * The License is available on the internet at:
+ *       http://www.gnu.org/copyleft/lgpl.html
+ * or by writing to:
+ *      Free Software Foundation, Inc.
+ *      59 Temple Place - Suite 330
+ *      Boston, MA 02111-1307, USA
+ *
+ * Copyright: 2008
+ *     The copyright to this program is held by it's authors.
+ *
+ * ID: $Id: BookIndexer.java 1466 2007-07-02 02:48:09Z dmsmith $
+ */
+package org.crosswire.jsword.examples;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.crosswire.jsword.book.Book;
+import org.crosswire.jsword.book.BookException;
+import org.crosswire.jsword.book.Books;
+import org.crosswire.jsword.book.install.InstallException;
+import org.crosswire.jsword.book.install.InstallManager;
+import org.crosswire.jsword.book.install.Installer;
+
+/**
+ * Exports the Book in SWORD's imp format.
+ * This is identical to SWORD's mod2imp.
+ * Note: it does not work with GenBook.
+ * 
+ * @see gnu.lgpl.License for license details.
+ *      The copyright to this program is held by it's authors.
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class BookInstaller
+{
+
+    public BookInstaller()
+    {
+        installManager = new InstallManager();
+    }
+
+    /**
+     * Uninstall a book.
+     * @param book the book to delete
+     * @throws BookException 
+     */
+    public void deleteBook(Book book) throws BookException
+    {
+        // Make the book unavailable.
+        // This is normally done via listeners.
+        Books.installed().removeBook(book);
+
+        // Actually do the delete
+        // This should be a call on installer.
+        book.getDriver().delete(book);
+
+    }
+
+    /**
+     * Get a list of all know installers.
+     * 
+     * @return
+     */
+    public Map getInstallers()
+    {
+        // Ask the Install Manager for a map of all known remote repositories sites
+        return installManager.getInstallers();
+    }
+
+    /**
+     * Get a list of all installed books.
+     * @return the list of installed books
+     */
+    public List getInstalledBooks()
+    {
+        return Books.installed().getBooks();
+    }
+
+    /**
+     * Get a list of all known books for an installer.
+     * @param repositoryName
+     * @return the list of books at that repository
+     */
+    public List getRepositoryBooks(String repositoryName)
+    {
+        return installManager.getInstaller(repositoryName).getBooks();
+    }
+
+    /**
+     * Reload the local cache for a remote repository.
+     * 
+     * @param repositoryName
+     * @throws InstallException 
+     */
+    public void reloadBookList(String repositoryName) throws InstallException
+    {
+        installManager.getInstaller(repositoryName).reloadBookList();
+    }
+
+    /**
+     * Get a Book from the repository. Note this does not install it.
+     * 
+     * @param repositoryName the repository from which to get the book
+     * @param bookName the name of the book to get
+     * @return
+     */
+    public Book getBook(String repositoryName, String bookName)
+    {
+        return installManager.getInstaller(repositoryName).getBook(bookName);     
+    }
+
+    /**
+     * Install a book, overwriting it if the book to be installed is newer.
+     * 
+     * @param repositoryName the name of the repository from which to get the book
+     * @param bookName the book to get
+     * @throws BookException 
+     * @throws InstallException 
+     */
+    public void installBook(String repositoryName, Book book) throws BookException, InstallException
+    {
+        // An installer knows how to install books
+        Installer installer = installManager.getInstaller(repositoryName);
+
+        // Delete the book, if present
+        // At the moment, JSword will not re-install. Later it will, if the
+        // remote version is greater.
+        if (Books.installed().getBook(book.getInitials()) != null)
+        {
+            deleteBook(book);
+        }
+
+        // Now install it. Note this is a background task.
+        installer.install(book);
+    }
+
+    private InstallManager installManager;
+
+    /**
+     * BookInstaller can manage the installation of books with the following capabilities.
+     * 
+     * Usage: BookInstaller [option]<br/>
+     * Options:
+     * <table border="0">
+     * <tr><td>uninstall</td><td>bookName               </td><td>Uninstall book</td></tr>
+     * <tr><td>sources  </td><td>&nbsp;                 </td><td>List source repositories</td></tr>
+     * <tr><td>list     </td><td>&nbsp;                 </td><td>List installed books</td></tr>
+     * <tr><td>list     </td><td>repositoryName         </td><td>list available books from a repository</td></tr>
+     * <tr><td>reload   </td><td>repositoryName         </td><td>Reload the local cache for a repository</td></tr>
+     * <tr><td>install  </td><td>repositoryName bookName</td><td>Install a book from a repository</td></tr>
+     * </table>
+     * 
+     * @param args
+     */
+    public static void main(String[] args)
+    {
+        if (args.length < 1)
+        {
+            usage();
+        }
+
+        System.err.print("BookExporter" + args[0]); //$NON-NLS-1$
+        for (int i = 1; i < args.length; i++)
+        {
+            System.err.print(' ');
+            System.err.print(args[i]);
+        }
+
+        BookInstaller installer = new BookInstaller();
+
+        String operation = args[0];
+        if (operation.equalsIgnoreCase("uninstall")) //$NON-NLS-1$
+        {
+            Book b = Books.installed().getBook(args[1]);
+            if (b == null)
+            {
+                System.err.println("Book not found"); //$NON-NLS-1$
+                System.exit(1);
+            }
+            try
+            {
+                installer.deleteBook(b);
+            }
+            catch (BookException e)
+            {
+                e.printStackTrace();
+            }
+        }
+        else if (operation.equalsIgnoreCase("sources")) //$NON-NLS-1$
+        {
+            // Get all the installers one after the other
+            Map installers = installer.getInstallers();
+            Iterator iter = installers.keySet().iterator();
+            while (iter.hasNext())
+            {
+                System.out.println(iter.next());
+            }
+        }
+        else if (operation.equalsIgnoreCase("list")) //$NON-NLS-1$
+        {
+            if (args.length == 1)
+            {
+                Iterator iter = installer.getInstalledBooks().iterator();
+                while (iter.hasNext())
+                {
+                    Book book = (Book) iter.next();
+                    System.out.println(book.getInitials());
+                }
+            }
+            else if (args.length == 2)
+            {
+                Iterator iter =  installer.getRepositoryBooks(args[1]).iterator();
+                while (iter.hasNext())
+                {
+                    Book book = (Book) iter.next();
+                    System.out.println(book.getInitials());
+                }                
+            }
+            else
+            {
+                usage();
+            }
+        }
+        else if (operation.equalsIgnoreCase("reload")) //$NON-NLS-1$
+        {
+           if (args.length == 2)
+           {
+               try
+               {
+                   installer.reloadBookList(args[1]);
+               }
+               catch (InstallException e)
+               {
+                   e.printStackTrace();
+               }
+            }
+            else
+            {
+                usage();
+            }
+        }
+        else if (operation.equalsIgnoreCase("install")) //$NON-NLS-1$
+        {
+            if (args.length == 3)
+            {
+                Book b = installer.getBook(args[1], args[2]);
+                if (b == null)
+                {
+                    System.err.println("Book not found"); //$NON-NLS-1$
+                    System.exit(1);
+                }
+                try
+                {
+                    installer.installBook(args[1], b);
+                }
+                catch (BookException e)
+                {
+                    e.printStackTrace();
+                }
+                catch (InstallException e)
+                {
+                    e.printStackTrace();
+                }
+            }
+            else
+            {
+                usage();
+            }
+        }
+        else
+        {
+            usage();
+        }
+    }
+
+    public static void usage()
+    {
+        System.err.println("usage: BookInstaller <option>"); //$NON-NLS-1$
+        System.err.println("Options:"); //$NON-NLS-1$
+        System.err.println("    uninstall bookName                 Uninstall book"); //$NON-NLS-1$
+        System.err.println("    sources                            List remote source repositories"); //$NON-NLS-1$
+        System.err.println("    list                               List installed books"); //$NON-NLS-1$
+        System.err.println("    list      repositoryName           List available books from a repository"); //$NON-NLS-1$
+        System.err.println("    refresh   repositoryName           Reload local cache for a repository"); //$NON-NLS-1$
+        System.err.println("    install   repositoryName bookName  Install a book from a repository"); //$NON-NLS-1$
+        System.exit(1);
+    }
+}




More information about the jsword-svn mailing list