Selaa lähdekoodia

add function to get underlying text from reference

Daniel Sheffield 2 vuotta sitten
vanhempi
sitoutus
e9ba1b5135
3 muutettua tiedostoa jossa 60 lisäystä ja 3 poistoa
  1. 6 1
      pom.xml
  2. 45 2
      src/main/java/PlJavaJSword.java
  3. 9 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.1-SNAPSHOT</version>
+  <version>0.0.2-SNAPSHOT</version>
 
   <!-- Coordinates are nice, but so are names and descriptions for humans. -->
 
@@ -31,6 +31,11 @@
   <!-- Here's where you say your project depends on a pljava-api version. -->
 
   <dependencies>
+    <dependency>
+      <groupId>org.jdom</groupId>
+      <artifactId>jdom2</artifactId>
+      <version>2.0.6.1</version>
+    </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>

+ 45 - 2
src/main/java/PlJavaJSword.java

@@ -2,7 +2,7 @@ package one.shandan;
 
 import java.lang.String;
 import java.lang.Boolean;
-import java.security.AccessControlException;
+import java.util.Iterator;
 import org.crosswire.jsword.passage.VerseRangeFactory;
 import org.crosswire.jsword.versification.system.SystemDefault;
 import org.crosswire.jsword.versification.BibleBook;
@@ -10,13 +10,22 @@ import org.crosswire.jsword.versification.BibleNames;
 import org.crosswire.jsword.passage.NoSuchVerseException;
 import org.crosswire.jsword.versification.Versification;
 import org.crosswire.jsword.versification.system.Versifications;
+import org.crosswire.jsword.book.Books;
+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.Content;
 import org.postgresql.pljava.annotation.Function;
 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;
 
 public class PlJavaJSword {
-  private static Versification kjv = Versifications.instance().getVersification("KJV");
+  private static final String DEFAULT_BOOK = "KJV";
+  private static final String DEFAULT_VERSIFICATION = "KJV";
+  private static final Versification kjv = Versifications.instance().getVersification(DEFAULT_VERSIFICATION);
+  private static final Books BOOKS = Books.installed();
 
   @Function(onNullInput=RETURNS_NULL, effects=IMMUTABLE, trust=SANDBOXED)
   public static Boolean isValidVerse(String reference) {
@@ -35,5 +44,39 @@ public class PlJavaJSword {
       return null;
     }
   }
+  @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;
+    }
+    try {
+        Key key = book.getKey(reference);
+        String ret = "";
+        for (Iterator<Content> i = book.getOsisIterator(key, false, false); i.hasNext();){
+            ret = ret + i.next().getValue();
+        }
+        return ret;
+    } catch (NoSuchKeyException ex){
+        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;
+    }
+    try {
+        Key key = book.getKey(reference);
+        String ret = "";
+        for (Iterator<Content> i = book.getOsisIterator(key, false, false); i.hasNext();){
+            ret = ret + i.next().getValue();
+        }
+        return ret;
+    } catch (NoSuchKeyException ex){
+        return null;
+    }
+  }
 }
 

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

@@ -1,5 +1,6 @@
 package one.shandan;
 import one.shandan.PlJavaJSword;
+import org.crosswire.jsword.book.BookException;
 import org.junit.jupiter.api.Test;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
@@ -12,4 +13,12 @@ public class TestPlJavaJSword {
   public void testIsValidVerseInvalidVerse(){
     assertEquals(false, PlJavaJSword.isValidVerse("Bogus 1:1"));
   }
+  @Test
+  public void testText() throws BookException {
+    assertEquals("In the beginning God created the heaven and the earth.", PlJavaJSword.getText("KJV", "Genesis 1:1"));
+  }
+  @Test
+  public void testDefaultText() throws BookException {
+    assertEquals("In the beginning God created the heaven and the earth.", PlJavaJSword.getDefaultText("Genesis 1:1"));
+  }
 }