Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Feb 1, 2024
2 parents 196fa14 + e824d65 commit 91b095d
Show file tree
Hide file tree
Showing 14 changed files with 934 additions and 340 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/crosswire/jsword/passage/Passage.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package org.crosswire.jsword.passage;

import org.crosswire.jsword.versification.Versification;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
Expand Down Expand Up @@ -186,6 +188,18 @@ public interface Passage extends VerseKey<Passage> {
*/
Iterator<VerseRange> rangeIterator(RestrictionType restrict);

@Override
default boolean isValidIn(Versification targetVersification) {
Iterator<VerseRange> rangeIterator = rangeIterator(RestrictionType.NONE);
while(rangeIterator.hasNext()) {
if(!rangeIterator.next().isValidIn(targetVersification)) {
// If any of the included ranges is invalid, this passage is invalid.
return false;
}
}
return true;
}

/**
* Returns true if this collection contains all the specified Verse
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/crosswire/jsword/passage/Verse.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ public Versification getVersification() {
return v11n;
}

@Override
public boolean isValidIn(Versification targetVersification) {
try {
return targetVersification.validate(book, chapter, verse, false);
} catch (NoSuchVerseException e) {
return false;
}
}

/* (non-Javadoc)
* @see org.crosswire.jsword.passage.Passage#reversify(org.crosswire.jsword.versification.Versification)
*/
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/crosswire/jsword/passage/VerseKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ public interface VerseKey<T extends VerseKey> extends Key {
*/
Versification getVersification();

/**
* Check if this key is valid in a different versification.
* @param targetVersification The versification to check for.
* @return True if all verses in this verse key map to existing verses in the given
* versification, false otherwise.
*/
boolean isValidIn(Versification targetVersification);

/**
* Cast this VerseKey into another Versification. OSIS Sub Identifiers are ignored.
*
* <p>
* Note: This is dangerous as it does not consider chapter boundaries
* or whether the verses in this VerseKey are actually part of the
* new versification. It should only be used when the start and end
* verses are in both Versifications. You have been warned.
* new versification.
* If in doubt, always check with {@link #isValidIn(Versification)} before usage.
* </p>
*
* @param newVersification
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/crosswire/jsword/passage/VerseRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ public Versification getVersification() {
return v11n;
}

@Override
public boolean isValidIn(Versification targetVersification) {
Iterator<Key> verseIterator = iterator();
while(verseIterator.hasNext()) {
if(!((Verse) verseIterator.next()).isValidIn(targetVersification)) {
return false;
}
}
return true;
}

/* (non-Javadoc)
* @see org.crosswire.jsword.passage.VerseKey#reversify(org.crosswire.jsword.versification.Versification)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.crosswire.jsword.book.ReferenceSystem;
import org.crosswire.jsword.passage.NoSuchVerseException;
import org.crosswire.jsword.passage.Verse;
import org.crosswire.jsword.passage.VerseKey;
import org.crosswire.jsword.passage.VerseRange;

/**
Expand Down Expand Up @@ -1064,6 +1065,14 @@ public boolean validate(BibleBook book, int chapter, int verse, boolean silent)
throw new NoSuchVerseException(JSOtherMsg.lookupText("Book must not be null"));
}

if(!bookList.contains(book)) {
if (silent) {
return false;
}
// TRANSLATOR: The user supplied a book which does not exist in this versification.
throw new NoSuchVerseException(JSOtherMsg.lookupText("Book must be present in the versification"));
}

// Check the chapter
int maxChapter = getLastChapter(book);
if (chapter < 0 || chapter > maxChapter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@
import org.crosswire.jsword.passage.Verse;
import org.crosswire.jsword.passage.VerseKey;
import org.crosswire.jsword.passage.VerseRange;
import org.crosswire.jsword.versification.system.SystemKJVA;
import org.crosswire.jsword.versification.system.Versifications;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A Versification mapper allows you to a map a given verse to the KJV versification,
* or unmap it from the KJV versification into your own versification.
* Note : in order to allow for all known bible contents (including deuterocanonical texts) to be mapped, within this
* class "KJV" actually stands for the "KJVA" versification system.
*
* <p>
* A properties-like file will contain the non-KJV versification as they key, and the KJV versification value
* as the target value... Duplicate keys are allowed.
Expand Down Expand Up @@ -529,14 +533,19 @@ public List<QualifiedKey> map(QualifiedKey qualifiedKey) {
VerseKey key = qualifiedKey.getKey();
if (key instanceof Verse) {
List<QualifiedKey> kjvKeys = this.getQualifiedKeys(key);
if (kjvKeys == null || kjvKeys.size() == 0) {
//then we found no mapping, so we're essentially going to return the same key back...
//unless it's a verse 0 and then we'll check the global flag.
if(kjvKeys != null && !kjvKeys.isEmpty()) {
// Found an explicit mapping to KJV, return it.
return kjvKeys;
}
else if(key.isValidIn(KJV)) {
// No explicit mapping, but key is valid in KJV : implicit straight mapping.
// Safe to reversify.
kjvKeys = new ArrayList<QualifiedKey>();
kjvKeys.add(qualifiedKey.reversify(KJV));
return kjvKeys;
}
return kjvKeys;
// Else, verses just don't map to KJV : return the empty list.
return new ArrayList<QualifiedKey>();
}

return new ArrayList<QualifiedKey>();
Expand All @@ -557,14 +566,22 @@ public VerseKey unmap(final QualifiedKey kjvVerse) {
left = this.fromKJVMappings.get(new QualifiedKey(kjvVerse.getVerse().getWhole()));
}

//if we have no mapping, then we are in 1 of two scenarios
//the verse is either totally absent, or the verse is not part of the mappings, meaning it is a straight map
if (left == null) {
// We didn't find a mapped passage in the left (non-KJV) versification.
VerseKey vk = kjvVerse.getKey();
if (vk != null && this.absentVerses.contains(vk)) {
// If the verse was explicitly listed as absent, return an empty passage.
return createEmptyPassage(KJV);
}
else if(vk != null && vk.isValidIn(this.nonKjv)) {
// If the verse is not explicitly absent, and exists in the left versification,
// then it's a straight mapping, safe to cast to reversify.
return kjvVerse.reversify(this.nonKjv).getKey();
}
else {
// No explicit mapping, and invalid direct cast : these KJV verses do not map to anything.
return createEmptyPassage(KJV);
}
return kjvVerse.reversify(this.nonKjv).getKey();
}
return left;
}
Expand Down Expand Up @@ -655,6 +672,6 @@ private Passage createEmptyPassage(Versification versification) {

private OsisParser osisParser = new OsisParser();

private static final Versification KJV = Versifications.instance().getVersification(Versifications.DEFAULT_V11N);
private static final Versification KJV = Versifications.instance().getVersification(SystemKJVA.V11N_NAME);
private static final Logger LOGGER = LoggerFactory.getLogger(VersificationToKJVMapper.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.crosswire.jsword.passage.RangedPassage;
import org.crosswire.jsword.passage.Verse;
import org.crosswire.jsword.passage.VerseKey;
import org.crosswire.jsword.versification.system.SystemKJVA;
import org.crosswire.jsword.versification.system.Versifications;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -251,7 +252,7 @@ private void ensure(final Versification versification) {
}

private static volatile VersificationsMapper instance;
private static final Versification KJV = Versifications.instance().getVersification(Versifications.DEFAULT_V11N);
private static final Versification KJV = Versifications.instance().getVersification(SystemKJVA.V11N_NAME);
private static final Map<Versification, VersificationToKJVMapper> MAPPERS = new HashMap<Versification, VersificationToKJVMapper>();
private static final Logger LOGGER = LoggerFactory.getLogger(VersificationsMapper.class);
}
Loading

0 comments on commit 91b095d

Please sign in to comment.