Browse Source

fix parsing of xml

Daniel Sheffield 2 years ago
parent
commit
4bff688ce5
3 changed files with 60 additions and 21 deletions
  1. 6 1
      pom.xml
  2. 50 20
      src/main/java/PlJavaJSword.java
  3. 4 0
      src/test/java/TestPlJavaJSword.java

+ 6 - 1
pom.xml

@@ -10,7 +10,7 @@
 
   <groupId>one.shandan</groupId>
   <artifactId>pljava-jsword</artifactId>
-  <version>0.0.2-SNAPSHOT</version>
+  <version>0.0.3-SNAPSHOT</version>
 
   <!-- Coordinates are nice, but so are names and descriptions for humans. -->
 
@@ -41,6 +41,11 @@
       <artifactId>slf4j-api</artifactId>
       <version>1.7.36</version>
     </dependency>
+    <!--<dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-simple</artifactId>
+      <version>1.7.36</version>
+    </dependency> -->
     <dependency>
       <groupId>org.postgresql</groupId>
       <artifactId>pljava-api</artifactId>

+ 50 - 20
src/main/java/PlJavaJSword.java

@@ -3,6 +3,8 @@ package one.shandan;
 import java.lang.String;
 import java.lang.Boolean;
 import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
 import org.crosswire.jsword.passage.VerseRangeFactory;
 import org.crosswire.jsword.versification.system.SystemDefault;
 import org.crosswire.jsword.versification.BibleBook;
@@ -14,8 +16,14 @@ import org.crosswire.jsword.book.Book;
 import org.crosswire.jsword.book.BookException;
 import org.crosswire.jsword.passage.Key;
 import org.crosswire.jsword.passage.NoSuchKeyException;
+import org.jdom2.Attribute;
+import org.jdom2.Element;
+import org.jdom2.Text;
 import org.jdom2.Content;
+import org.jdom2.Content.CType;
 import org.postgresql.pljava.annotation.Function;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE;
 import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL;
 import static org.postgresql.pljava.annotation.Function.Trust.SANDBOXED;
@@ -25,6 +33,7 @@ public class PlJavaJSword {
   private static final String DEFAULT_VERSIFICATION = "KJV";
   private static final Versification kjv = Versifications.instance().getVersification(DEFAULT_VERSIFICATION);
   private static final Books BOOKS = Books.installed();
+  private static final Logger logger = LoggerFactory.getLogger(PlJavaJSword.class);
 
   @Function(onNullInput=RETURNS_NULL, effects=IMMUTABLE, trust=SANDBOXED)
   public static Boolean isValidVerse(String reference) {
@@ -43,42 +52,63 @@ public class PlJavaJSword {
       return null;
     }
   }
+  private static String parseVerse(Iterator<Content> content){
+    String verse = "";
+    while (content.hasNext()){
+      Content c = content.next();
+      if (c.getCType() == CType.Element){
+        Element e = (Element)c;
+        if (e.getName() != "note"){
+          verse += " " + e.getValue().trim();
+        } else {
+          verse += " ";
+        }
+      } else if (c.getCType() == CType.Text){
+        Text t = (Text)c;
+        verse += t.getValue().trim();
+      }
+    }
+    return verse.trim();
+  }
+  private static String parseContent(Iterator<Content> content)
+  {
+     List<String> ret = new ArrayList<String>();
+     while (content.hasNext()){
+       Content c = content.next();
+       if (c.getCType() == CType.Element){
+         Element e = (Element)c;
+         if (e.getName() == "verse"){
+           String part = parseVerse(e.getContent().listIterator());
+           ret.add(part);
+         }
+       }
+     }
+     return String.join(" ", ret);
+  }
   @Function(onNullInput=RETURNS_NULL, effects=IMMUTABLE, trust=SANDBOXED)
   public static String getText(String translation, String reference) throws BookException {
     Book book = BOOKS.getBook(translation);
     if (book == null){
-        return null;
+      return null;
     }
     try {
-        Key key = book.getKey(reference);
-        String ret = "";
-	Iterator<Content> i = book.getOsisIterator(key, false);
-	i.next();
-        while (i.hasNext()){
-            ret = ret + " " + i.next().getValue().trim();
-        }
-        return ret.trim();
+      Key key = book.getKey(reference);
+      return parseContent(book.getOsisIterator(key, false));
     } catch (NoSuchKeyException ex){
-        return null;
+      return null;
     }
   }
   @Function(onNullInput=RETURNS_NULL, effects=IMMUTABLE, trust=SANDBOXED)
   public static String getDefaultText(String reference) throws BookException {
     Book book = BOOKS.getBook(DEFAULT_BOOK);
     if (book == null){
-        return null;
+      return null;
     }
     try {
-        Key key = book.getKey(reference);
-        String ret = "";
-	Iterator<Content> i = book.getOsisIterator(key, false);
-	i.next();
-        while (i.hasNext()){
-            ret = ret + " " + i.next().getValue().trim();
-        }
-        return ret.trim();
+      Key key = book.getKey(reference);
+      return parseContent(book.getOsisIterator(key, false));
     } catch (NoSuchKeyException ex){
-        return null;
+      return null;
     }
   }
 }

+ 4 - 0
src/test/java/TestPlJavaJSword.java

@@ -19,6 +19,10 @@ public class TestPlJavaJSword {
     assertEquals("In the beginning God created the heaven and the earth.", PlJavaJSword.getText("KJV", "Genesis 1:1"));
   }
   @Test
+  public void testTextParse() throws BookException {
+    assertEquals("In the beginning, God created the heavens and the earth.", PlJavaJSword.getText("ESV", "Genesis 1:1"));
+  }
+  @Test
   public void testTextMany() throws BookException {
     assertEquals(GEN_1_1_3, PlJavaJSword.getText("KJV", "Genesis 1:1-3"));
   }