[jsword-svn] r1346 - in trunk: jsword/src/main/java/org/crosswire/jsword/book jsword/src/main/java/org/crosswire/jsword/book/basic jsword-limbo/src/main/java/org/crosswire/jsword/book jsword-limbo/src/main/java/org/crosswire/jsword/book/raw

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Thu May 24 05:07:16 MST 2007


Author: dmsmith
Date: 2007-05-24 05:07:15 -0700 (Thu, 24 May 2007)
New Revision: 1346

Modified:
   trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/StudyTool.java
   trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/raw/RawBook.java
   trunk/jsword/src/main/java/org/crosswire/jsword/book/OSISUtil.java
   trunk/jsword/src/main/java/org/crosswire/jsword/book/basic/AbstractPassageBook.java
Log:
Fundamental addition that core methods in OSISUtil no longer need a full blown OSIS document to work.

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/OSISUtil.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/OSISUtil.java	2007-05-24 02:02:14 UTC (rev 1345)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/OSISUtil.java	2007-05-24 12:07:15 UTC (rev 1346)
@@ -526,6 +526,36 @@
     }
 
     /**
+     * Dig past the osis and osisText element, if present, to get the meaningful content of the document.
+     * 
+     * @return a fragment
+     */
+    public static List getFragment(Element root)
+    {
+        Element content = root;
+        if (OSISUtil.OSIS_ELEMENT_OSIS.equals(root.getName()))
+        {
+            content = root.getChild(OSISUtil.OSIS_ELEMENT_OSISTEXT);
+        }
+
+        // At this point we are at something interesting, possibly null.
+        // If this was a semantically valid OSIS document then it is a div.
+        // As long as this node has one child dig deeper.
+        while (content != null && content.getContentSize() == 1)
+        {
+            Content firstChild = content.getContent(0);
+            if (firstChild instanceof Element && OSISUtil.OSIS_ELEMENT_DIV.equals(((Element) firstChild).getName()))
+            {
+                content = (Element) firstChild;
+            }
+            break;
+        }
+
+        assert content != null;
+        return content.getContent();
+    }
+
+    /**
      * Get the canonical text from an osis document consisting of a single fragment.
      * The document is assumed to be valid OSIS2.0 XML. While xml valid
      * is rigidly defined as meaning that an xml parser can validate the document,
@@ -565,10 +595,10 @@
     {
         StringBuffer buffer = new StringBuffer();
 
-        Element osisText = root.getChild(OSISUtil.OSIS_ELEMENT_OSISTEXT);
-        Element div = osisText.getChild(OSISUtil.OSIS_ELEMENT_DIV);
+        // Dig past osis, osisText, if present, to get to the real content.
+        List frag = OSISUtil.getFragment(root);
 
-        Iterator dit = div.getContent().iterator();
+        Iterator dit = frag.iterator();
         String sID = null;
         Object data = null;
         Element ele = null;
@@ -607,33 +637,8 @@
      */
     public static String getPlainText(Element root)
     {
-        StringBuffer buffer = new StringBuffer();
-
-        Element osisText = root.getChild(OSISUtil.OSIS_ELEMENT_OSISTEXT);
-        List divs = osisText.getChildren(OSISUtil.OSIS_ELEMENT_DIV);
-
-        Iterator divIter = divs.iterator();
-        while (divIter.hasNext())
-        {
-            Element div = (Element) divIter.next();
-
-            Iterator contentIter = div.getContent().iterator();
-            while (contentIter.hasNext())
-            {
-                Object data = contentIter.next();
-                if (data instanceof Element)
-                {
-                    Element ele = (Element) data;
-                    if (ele.getName().equals(OSISUtil.OSIS_ELEMENT_VERSE))
-                    {
-                        String txt = OSISUtil.getTextContent((Element) data);
-                        buffer.append(txt);
-                    }
-                }
-            }
-        }
-
-        return buffer.toString().trim();
+        // Dig past osis, osisText, if present, to get to the real content.
+        return getTextContent(OSISUtil.getFragment(root));
     }
 
     /**
@@ -732,7 +737,11 @@
             String attr = ele.getAttributeValue(OSISUtil.OSIS_ATTR_TYPE);
             if (attr == null || !attr.equals(NOTETYPE_REFERENCE))
             {
-                buffer.append(OSISUtil.getTextContent(ele));
+                if (buffer.length() > 0)
+                {
+                    buffer.append(' ');
+                }
+                buffer.append(OSISUtil.getTextContent(ele.getContent()));
             }
         }
 
@@ -792,11 +801,11 @@
         }
     }
 
-    private static String getTextContent(Element ele)
+    private static String getTextContent(List fragment)
     {
         StringBuffer buffer = new StringBuffer();
 
-        Iterator contentIter = ele.getContent().iterator();
+        Iterator contentIter = fragment.iterator();
         while (contentIter.hasNext())
         {
             Content next = (Content) contentIter.next();

Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/basic/AbstractPassageBook.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/basic/AbstractPassageBook.java	2007-05-24 02:02:14 UTC (rev 1345)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/basic/AbstractPassageBook.java	2007-05-24 12:07:15 UTC (rev 1346)
@@ -145,12 +145,12 @@
     protected abstract Filter getFilter();
 
     /**
-     * For when we want to add writing functionality
+     * For when we want to add writing functionality. This does not work.
      */
     public void setDocument(Key key, BookData bdata) throws BookException
     {
         // For all of the sections
-        Iterator sit = bdata.getOsis().getChild(OSISUtil.OSIS_ELEMENT_OSISTEXT).getChildren(OSISUtil.OSIS_ELEMENT_DIV).iterator();
+        Iterator sit = OSISUtil.getFragment(bdata.getOsis()).iterator();
         while (sit.hasNext())
         {
             Element div = (Element) sit.next();

Modified: trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/StudyTool.java
===================================================================
--- trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/StudyTool.java	2007-05-24 02:02:14 UTC (rev 1345)
+++ trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/StudyTool.java	2007-05-24 12:07:15 UTC (rev 1346)
@@ -55,7 +55,7 @@
         Map reply = new HashMap();
 
         // Loop through all the divs in this BookData
-        Iterator oit = data.getOsis().getChild(OSISUtil.OSIS_ELEMENT_OSISTEXT).getChildren(OSISUtil.OSIS_ELEMENT_DIV).iterator();
+        Iterator oit = OSISUtil.getFragment(data.getOsis()).iterator();
         while (oit.hasNext())
         {
             Element div = (Element) oit.next();
@@ -111,7 +111,7 @@
         Map reply = new HashMap();
 
         // Loop through all the divs in this BookData
-        Iterator oit = data.getOsis().getChild(OSISUtil.OSIS_ELEMENT_OSISTEXT).getChildren(OSISUtil.OSIS_ELEMENT_DIV).iterator();
+        Iterator oit = OSISUtil.getFragment(data.getOsis()).iterator();
         while (oit.hasNext())
         {
             Element div = (Element) oit.next();

Modified: trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/raw/RawBook.java
===================================================================
--- trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/raw/RawBook.java	2007-05-24 02:02:14 UTC (rev 1345)
+++ trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/raw/RawBook.java	2007-05-24 12:07:15 UTC (rev 1346)
@@ -401,7 +401,7 @@
     public void setDocument(Verse verse, BookData bdata) throws BookException
     {
         // For all of the sections
-        Iterator sit = bdata.getOsis().getChild(OSISUtil.OSIS_ELEMENT_OSISTEXT).getChildren(OSISUtil.OSIS_ELEMENT_DIV).iterator();
+        Iterator sit = OSISUtil.getFragment(bdata.getOsis()).iterator();
         while (sit.hasNext())
         {
             Element div = (Element) sit.next();




More information about the jsword-svn mailing list