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

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Thu May 8 19:44:29 MST 2008


Author: dmsmith
Date: 2008-05-08 19:44:29 -0700 (Thu, 08 May 2008)
New Revision: 1847

Modified:
   trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/RawLDBackend.java
   trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/SwordDictionary.java
Log:
Fixed a daily devotional bug that was recently introduced.

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/RawLDBackend.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/RawLDBackend.java	2008-05-09 01:00:28 UTC (rev 1846)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/RawLDBackend.java	2008-05-09 02:44:29 UTC (rev 1847)
@@ -25,6 +25,9 @@
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.net.URI;
+import java.util.Date;
+import java.text.MessageFormat;
+import java.text.ParseException;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
 import java.util.Locale;
@@ -142,7 +145,23 @@
                 if (index < getCardinality())
                 {
                     DataEntry entry = getEntry(getBookMetaData().getInitials(), index);
-                    return new DefaultLeafKeyList(entry.getKey());
+                    SwordBookMetaData bmd = getBookMetaData();
+
+                    boolean isDailyDevotional = bmd.getBookCategory().equals(BookCategory.DAILY_DEVOTIONS);
+
+                    Calendar greg = new GregorianCalendar();
+                    DateFormatter nameDF = DateFormatter.getDateInstance();
+                    String keytitle = entry.getKey();
+
+                    if (isDailyDevotional && keytitle.length() >= 3)
+                    {
+                        String[] spec = StringUtil.splitAll(keytitle, '.');
+                        greg.set(Calendar.MONTH, Integer.parseInt(spec[0]) - 1);
+                        greg.set(Calendar.DATE, Integer.parseInt(spec[1]));
+                        keytitle = nameDF.format(greg.getTime());
+                    }
+
+                    return new DefaultLeafKeyList(keytitle);
                 }
             }
             catch (IOException e)
@@ -333,10 +352,23 @@
 
         boolean isDailyDevotional = bmd.getBookCategory().equals(BookCategory.DAILY_DEVOTIONS);
 
-        Calendar greg = new GregorianCalendar();
-        DateFormatter nameDF = DateFormatter.getDateInstance();
-
         String target = key.toUpperCase(Locale.US);
+        if (isDailyDevotional)
+        {
+            Calendar greg = new GregorianCalendar();
+            DateFormatter nameDF = DateFormatter.getDateInstance();
+            nameDF.setLenient(true);
+            try
+            {
+                Date date = nameDF.parse(key);
+                greg.setTime(date);
+                target = external2internal(greg);
+            }
+            catch (ParseException e)
+            {
+                assert false : e;
+            }
+        }
 
         int low = 1;
         int high = getCardinality() - 1;
@@ -349,15 +381,6 @@
             DataEntry entry = getEntry(key, mid);
             String midVal = entry.getKey();
 
-            // Massage midVal if can be.
-            if (isDailyDevotional && midVal.length() >= 3)
-            {
-                String[] spec = StringUtil.splitAll(midVal, '.');
-                greg.set(Calendar.MONTH, Integer.parseInt(spec[0]) - 1);
-                greg.set(Calendar.DATE, Integer.parseInt(spec[1]));
-                midVal = nameDF.format(greg.getTime());
-            }
-
             int cmp = midVal.toUpperCase(Locale.US).compareTo(target);
 
             if (cmp < 0)
@@ -379,15 +402,6 @@
         DataEntry entry = getEntry(key, 0);
         String midVal = entry.getKey();
 
-        // Massage midVal if can be.
-        if (isDailyDevotional && midVal.length() >= 3)
-        {
-            String[] spec = StringUtil.splitAll(midVal, '.');
-            greg.set(Calendar.MONTH, Integer.parseInt(spec[0]) - 1);
-            greg.set(Calendar.DATE, Integer.parseInt(spec[1]));
-            midVal = nameDF.format(greg.getTime());
-        }
-
         int cmp = midVal.toUpperCase(Locale.US).compareTo(target);
         if (cmp == 0)
         {
@@ -398,6 +412,19 @@
     }
 
     /**
+     * Convert the Gregorian Calendar to a string.
+     * @param externalKey
+     * @return
+     */
+    public static String external2internal(Calendar externalKey)
+    {
+        Object[] objs = {new Integer(1 + externalKey.get(Calendar.MONTH)),
+                         new Integer(externalKey.get(Calendar.DATE))};
+        return KEY_FORMAT.format(objs);
+
+    }
+
+    /**
      * How many bytes in the offset pointers in the index
      */
     private static final int OFFSETSIZE = 4;
@@ -443,6 +470,11 @@
     private RandomAccessFile datRaf;
 
     /**
+     * Date formatter
+     */
+    private static final MessageFormat KEY_FORMAT = new MessageFormat("{0,number,00}.{1,number,00}"); //$NON-NLS-1$
+
+    /**
      * Serialization ID
      */
     private static final long serialVersionUID = 818089833394450383L;

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/SwordDictionary.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/SwordDictionary.java	2008-05-09 01:00:28 UTC (rev 1846)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/sword/SwordDictionary.java	2008-05-09 02:44:29 UTC (rev 1847)
@@ -208,17 +208,19 @@
             text = text.substring(0, pos);
         }
 
-        // Get the number after the G or H
-        int strongsNumber = Integer.parseInt(text.substring(1));
 
         Key key = null;
         String internalName = sbmd.getInitials();
         if ("StrongsGreek".equals(internalName)) //$NON-NLS-1$
         {
+            // Get the number after the G or H
+            int strongsNumber = Integer.parseInt(text.substring(1));
             key = backend.get(backend.indexOf(new DefaultLeafKeyList(ZERO_PAD.format(strongsNumber))));
         }
         else if ("StrongsHebrew".equals(internalName)) //$NON-NLS-1$
         {
+            // Get the number after the G or H
+            int strongsNumber = Integer.parseInt(text.substring(1));
             key = backend.get(backend.indexOf(new DefaultLeafKeyList(ZERO_PAD.format(strongsNumber))));
         }
         return key;




More information about the jsword-svn mailing list