[jsword-svn] r1011 - trunk/jsword/src/main/java/org/crosswire/jsword/examples

dmsmith at crosswire.org dmsmith at crosswire.org
Sun Feb 26 14:59:08 MST 2006


Author: dmsmith
Date: 2006-02-26 14:59:04 -0700 (Sun, 26 Feb 2006)
New Revision: 1011

Modified:
   trunk/jsword/src/main/java/org/crosswire/jsword/examples/ModToOsis.java
Log:
creating a mod2osis ability for kjv2003.

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/examples/ModToOsis.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/examples/ModToOsis.java	2006-02-25 04:38:29 UTC (rev 1010)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/examples/ModToOsis.java	2006-02-26 21:59:04 UTC (rev 1011)
@@ -10,8 +10,8 @@
 
 import org.crosswire.jsword.book.Book;
 import org.crosswire.jsword.book.BookException;
+import org.crosswire.jsword.book.BookMetaData;
 import org.crosswire.jsword.book.Books;
-import org.crosswire.jsword.book.OSISUtil;
 import org.crosswire.jsword.passage.BibleInfo;
 import org.crosswire.jsword.passage.Key;
 import org.crosswire.jsword.passage.NoSuchKeyException;
@@ -31,15 +31,23 @@
      */
     public static void main(String[] args)
     {
+        new ModToOsis().dump(BIBLE_NAME, BIBLE_RANGE);
+    }
+
+    public void dump(String name, String range)
+    {
         Books books = Books.installed();
-        Book bible = books.getBook(BIBLE_NAME);
-        int lastBook = -1;
+        Book bible = books.getBook(name);
+        BookMetaData bmd = bible.getBookMetaData();
+        String lastBookName = ""; //$NON-NLS-1$
         int lastChapter = -1;
         StringBuffer buf = null;
+        boolean inPreVerse = false;
         
+
         try
         {
-            Key key = bible.getKey(BIBLE_RANGE);
+            Key key = bible.getKey(range);
 
             // Get a verse iterator
             Iterator iter = key.iterator();
@@ -49,73 +57,81 @@
                 String raw = bible.getRawData(verse);
                 String osisID = verse.getOsisID();
 
-                int currentBook = verse.getBook();
+                String currentBookName = BibleInfo.getOSISName(verse.getBook());
                 int currentChapter = verse.getChapter();
-                if (lastBook != currentBook)
+
+                boolean newBookFound = !lastBookName.equals(currentBookName);
+
+                if (newBookFound)
                 {
-                    if (lastBook != -1)
+                    if (lastBookName.length() > 0)
                     {
                         if (currentChapter == 1)
                         {
-                            buf.append("</").append(OSISUtil.OSIS_ELEMENT_CHAPTER).append(">\n"); //$NON-NLS-1$ //$NON-NLS-2$
+                            if (inPreVerse)
+                            {
+                                buildPreVerseClose(buf);
+                                inPreVerse = false;
+                            }
+                            buildChapterClose(buf);
                         }
-                        buf.append("</").append(OSISUtil.OSIS_ELEMENT_DIV).append(">\n"); //$NON-NLS-1$ //$NON-NLS-2$
-                        Writer writer = new OutputStreamWriter(new FileOutputStream(BibleInfo.getOSISName(lastBook) + ".xml"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
-                        writer.write(buf.toString());
-                        writer.close();
+                        buildBookClose(buf);
+                        buildDocumentClose(buf);
+                        writeDocument(buf, lastBookName);
                     }
 
                     buf = new StringBuffer();
-
-                    buf.append('<').append(OSISUtil.OSIS_ELEMENT_DIV);
-                    buf.append(' ');
-                    buf.append(OSISUtil.OSIS_ATTR_TYPE).append("=\"").append(OSISUtil.ATTRIBUTE_DIV_BOOK); //$NON-NLS-1$
-                    buf.append("\" "); //$NON-NLS-1$
-                    buf.append(OSISUtil.OSIS_ATTR_OSISID).append("=\"").append(BibleInfo.getOSISName(currentBook));  //$NON-NLS-1$
-                    buf.append("\">\n"); //$NON-NLS-1$
+                    buildDocumentOpen(buf, bmd, currentBookName);
+                    buildBookOpen(buf, currentBookName);
                 }
 
-                if (lastBook != currentBook || lastChapter != currentChapter)
+                if (newBookFound || lastChapter != currentChapter)
                 {
                     if (currentChapter != 1)
                     {
-                        buf.append("</").append(OSISUtil.OSIS_ELEMENT_CHAPTER).append(">\n"); //$NON-NLS-1$ //$NON-NLS-2$
+                        buildChapterClose(buf);
                     }
-                    buf.append('<').append(OSISUtil.OSIS_ELEMENT_CHAPTER);
-                    buf.append(' ');
-                    buf.append(OSISUtil.OSIS_ATTR_OSISID).append("=\"").append(BibleInfo.getOSISName(currentBook)).append('.').append(currentChapter);  //$NON-NLS-1$
-                    buf.append("\">"); //$NON-NLS-1$
+                    buildChapterOpen(buf, currentBookName, currentChapter);
                 }
 
                 /* Output the verse */
-                buf.append('<').append(OSISUtil.OSIS_ELEMENT_VERSE);
-                buf.append(' ');
-                buf.append(OSISUtil.OSIS_ATTR_OSISID).append("=\"").append(osisID);  //$NON-NLS-1$
-                buf.append("\" "); //$NON-NLS-1$
-                buf.append(OSISUtil.OSIS_ATTR_SID).append("=\"").append(osisID); //$NON-NLS-1$
-                buf.append("\"/>"); //$NON-NLS-1$
-                buf.append(raw);
-                buf.append('<').append(OSISUtil.OSIS_ELEMENT_VERSE);
-                buf.append(' ');
-                buf.append(OSISUtil.OSIS_ATTR_EID).append("=\"").append(osisID); //$NON-NLS-1$
-                buf.append("\"/>"); //$NON-NLS-1$               
-
-                if (lastChapter != currentChapter)
+                
+                /* TODO(DMS):
+                 * If the "raw" verse contains a "preverse" pull it out.
+                 * If there were a former preverse then close the "section" div
+                 * before outputting it before the verse.
+                 */
+                boolean foundPreVerse = false;
+                String preVerseText = "title"; //$NON-NLS-1$
+                if (foundPreVerse)
                 {
-                    lastChapter = currentChapter;
+                    if (inPreVerse)
+                    {
+                        buildPreVerseClose(buf);
+                    }
+                    buildPreVerseOpen(buf, preVerseText); //$NON-NLS-1$
+                    inPreVerse = true;
                 }
 
-                if (lastBook != currentBook)
-                {
-                    lastBook = currentBook;
-                }
+                buildVerseOpen(buf, osisID);
+                buf.append(raw);
+                buildVerseClose(buf, osisID);
+
+                lastChapter = currentChapter;
+                lastBookName = currentBookName;
             }
 
-            buf.append("</").append(OSISUtil.OSIS_ELEMENT_CHAPTER).append(">\n"); //$NON-NLS-1$ //$NON-NLS-2$
-            buf.append("</").append(OSISUtil.OSIS_ELEMENT_DIV).append(">\n"); //$NON-NLS-1$ //$NON-NLS-2$
-            Writer writer = new OutputStreamWriter(new FileOutputStream(BibleInfo.getOSISName(lastBook) + ".xml"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
-            writer.write(buf.toString());
-            writer.close();
+            // Close everything that is open
+            if (inPreVerse)
+            {
+                buildPreVerseClose(buf);
+                inPreVerse = false;
+            }
+
+            buildChapterClose(buf);
+            buildBookClose(buf);
+            buildDocumentClose(buf);
+            writeDocument(buf, lastBookName);
         }
         catch (BookException e)
         {
@@ -131,19 +147,90 @@
         }
         catch (UnsupportedEncodingException e)
         {
-            // XXX Auto-generated catch block
             e.printStackTrace();
         }
         catch (FileNotFoundException e)
         {
-            // XXX Auto-generated catch block
             e.printStackTrace();
         }
         catch (IOException e)
         {
-            // XXX Auto-generated catch block
             e.printStackTrace();
         }
     }
 
+    private void buildDocumentOpen(StringBuffer buf, BookMetaData bmd, String range)
+    {
+        buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); //$NON-NLS-1$
+        buf.append("<osis"); //$NON-NLS-1$
+        buf.append("\n  xmlns=\"http://www.bibletechnologies.net/2003/OSIS/namespace\""); //$NON-NLS-1$
+        buf.append("\n  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");  //$NON-NLS-1$
+        buf.append("\n  xsi:schemaLocation=\"http://www.bibletechnologies.net/2003/OSIS/namespace osisCore.2.1.xsd\">"); //$NON-NLS-1$
+        buf.append("\n    <osisText osisIDWork=\"").append(bmd.getInitials()).append('"').append(" osisRefWork=\"defaultReferenceScheme\">"); //$NON-NLS-1$ //$NON-NLS-2$
+        buf.append("\n    <header>"); //$NON-NLS-1$
+        buf.append("\n      <work osisWork=\"").append(bmd.getInitials()).append("\">"); //$NON-NLS-1$ //$NON-NLS-2$
+        buf.append("\n      <title>").append(bmd.getName()).append("</title>"); //$NON-NLS-1$ //$NON-NLS-2$
+        buf.append("\n      <identifier type=\"OSIS\">Bible.").append(bmd.getInitials()).append("</identifier>"); //$NON-NLS-1$ //$NON-NLS-2$
+        buf.append("\n      <refSystem>Bible.KJV</refSystem>"); //$NON-NLS-1$
+        buf.append("\n      <scope>").append(range).append("</scope>"); //$NON-NLS-1$ //$NON-NLS-2$
+        buf.append("\n    </work>"); //$NON-NLS-1$
+        buf.append("\n    <work osisWork=\"defaultReferenceScheme\">"); //$NON-NLS-1$
+        buf.append("\n      <refSystem>Bible.KJV</refSystem>"); //$NON-NLS-1$
+        buf.append("\n    </work>"); //$NON-NLS-1$
+        buf.append("\n  </header>"); //$NON-NLS-1$
+        buf.append('\n');
+
+    }
+
+    private void buildDocumentClose(StringBuffer buf)
+    {
+        buf.append("</osisText>\n</osis>\n"); //$NON-NLS-1$
+    }
+
+    private void buildBookOpen(StringBuffer buf, String bookName)
+    {
+        buf.append("<div type=\"book\" osisID=\"").append(bookName).append("\">\n"); //$NON-NLS-1$ //$NON-NLS-2$
+    }
+
+    private void buildBookClose(StringBuffer buf)
+    {
+        buf.append("</div>\n"); //$NON-NLS-1$
+    }
+
+    private void buildChapterClose(StringBuffer buf)
+    {
+        buf.append("</chapter>\n"); //$NON-NLS-1$
+    }
+
+    private void buildChapterOpen(StringBuffer buf, String bookName, int chapter)
+    {
+        buf.append("<chapter osisID=\"").append(bookName).append('.').append(chapter).append("\">"); //$NON-NLS-1$ //$NON-NLS-2$
+    }
+
+    private void buildPreVerseOpen(StringBuffer buf, String preVerse)
+    {
+        buf.append("<div type=\"section\"><title>").append(preVerse).append("</title>"); //$NON-NLS-1$ //$NON-NLS-2$
+    }
+
+    private void buildPreVerseClose(StringBuffer buf)
+    {
+        buf.append("</div>\n"); //$NON-NLS-1$
+    }
+
+    private void buildVerseOpen(StringBuffer buf, String osisID)
+    {
+        buf.append("<verse sID=\"").append(osisID).append("\" osisID=\"").append(osisID).append("\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+    }
+
+    private void buildVerseClose(StringBuffer buf, String osisID)
+    {
+        buf.append("<verse eID=\"").append(osisID).append("\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+    }
+    
+    private void writeDocument(StringBuffer buf, String filename) throws IOException
+    {
+        Writer writer = new OutputStreamWriter(new FileOutputStream(filename + ".xml"), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
+        writer.write(buf.toString());
+        writer.close();
+    }
 }



More information about the jsword-svn mailing list