[jsword-svn] r1262 - trunk/jsword/src/main/java/org/crosswire/jsword/book/sword

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Thu Mar 22 14:44:01 MST 2007


Author: dmsmith
Date: 2007-03-22 14:44:01 -0700 (Thu, 22 Mar 2007)
New Revision: 1262

Modified:
   trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/GenBookBackend.java
Log:
GenBook's now show content.

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/GenBookBackend.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/GenBookBackend.java	2007-03-22 21:01:01 UTC (rev 1261)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/GenBookBackend.java	2007-03-22 21:44:01 UTC (rev 1262)
@@ -114,61 +114,83 @@
     public String getRawText(Key key) throws BookException
     {
         checkActive();
+
+        try
+        {
+            TreeNode node = find(key);
+            byte [] userData = node.getUserData();
+
+            // Some entries may be empty.
+            if (userData.length == 8)
+            {
+                int start = SwordUtil.decodeLittleEndian32(userData, 0);
+                int size = SwordUtil.decodeLittleEndian32(userData, 4);
+                byte[] data = SwordUtil.readRAF(bdtRaf, start, size);
+                decipher(data);
+                return SwordUtil.decode(key, data, getBookMetaData().getBookCharset());
+            }
+
+            return ""; //$NON-NLS-1$
+        }
+        catch (IOException e)
+        {
+            throw new BookException(Msg.READ_FAIL);
+        }
+        
+    }
+
+    /**
+     * Given a Key, find the TreeNode for it.
+     * @param key The key to use for searching
+     * @return the found node, null otherwise
+     * @throws IOException
+     */
+    private TreeNode find(Key key) throws IOException
+    {
+        // We need to search from the root, so navigate to the root, saving as we go.
         List path = new ArrayList();
-        Key parentKey = key;
-        do
+        for (Key parentKey = key; parentKey != null && parentKey.getName().length() > 0; parentKey = parentKey.getParent())
         {
             path.add(parentKey.getName());
-            parentKey = parentKey.getParent();
         }
-        while (parentKey != null && parentKey.getName().length() > 0);
 
-        try
+        TreeNode node = index.getRoot();
+
+        node = index.getFirstChild(node);
+
+        for (int i = path.size() - 1; i >= 0; i-- )
         {
-            TreeNode node = index.getRoot();
+            String name = (String) path.get(i);
 
-            node = index.getFirstChild(node);
-
-            for (int i = path.size() - 1; i >= 0; i--)
+            // Search among the siblings for the current level.
+            while (node != null && !name.equals(node.getName()))
             {
-                String name = (String) path.get(i);
-                
-//                System.err.println("--" + name + "--"); //$NON-NLS-1$ //$NON-NLS-2$
-                while (!name.equals(node.getName()))
+                if (node.hasNextSibling())
                 {
-//                    System.err.println("compare to " + node.getName()); //$NON-NLS-1$
-                    if (node.hasNextSibling())
-                    {
-                        node = index.getNextSibling(node);
-                    }
-                    else
-                    {
-                        log.error("Could not find " + name); //$NON-NLS-1$
-                        break;
-                    }
+                    node = index.getNextSibling(node);
                 }
-//                System.err.println("compare to " + node.getName()); //$NON-NLS-1$
-
-                if (name.equals(node.getName()))
+                else
                 {
-                    if (i > 0)
-                    {
-                        node = index.getFirstChild(node);
-                    }
+                    log.error("Could not find " + name); //$NON-NLS-1$
+                    node = null;
                 }
             }
 
-            if (node.getName().equals(key.getName()))
+            // If we have found it but have not exhausted the path
+            // we need to get more
+            if (node != null && name.equals(node.getName()) && i > 0)
             {
-                return "Content of " + key.getName(); //$NON-NLS-1$
+                node = index.getFirstChild(node);
             }
         }
-        catch (IOException e)
+
+        // At this point we have either found it, returning it or have not, returning null
+        if (node != null && node.getName().equals(key.getName()))
         {
-            log.error("Could not get GenBook text", e); //$NON-NLS-1$
+            return node;
         }
-        
-        return ""; //$NON-NLS-1$
+
+        return null;
     }
 
     /* (non-Javadoc)




More information about the jsword-svn mailing list