/** * Convert the passageReference into a Passage or an empty Passage, * if there is an error. Note, this is not recommended as it throws * away the error. * * @param v11n * The Versification to which this Passage belongs. * @param passageReference * A String containing the text of the Passage * @param basis * The basis by which to interpret passageReference * @return a new Passage filled with the desired Verses or an empty Passage */ public Key getValidKey(Versification v11n, String passageReference, Key basis) { try { return getKey(v11n, passageReference, basis); } catch (NoSuchKeyException e) { return createEmptyKeyList(v11n); } }
public final Key createEmptyKeyList() { return keyf.createEmptyKeyList(Versifications.instance().getVersification(versification)); }
/** * Return a Key representing the passage passed in or an empty passage if it can't be parsed. * @param passage Textual ref * @return */ public Key getKey(String passage) { Key key = null; try { // spaces confuse the osis parser passage = passage.trim(); // If expecting OSIS then use OSIS parser key = osisParser.parseOsisRef(v11n, passage); // OSIS parser is strict so try treating as normal ref if osis parser fails if (key==null) { Log.d(TAG, "Non OSIS Reading plan passage:"+passage); key = PassageKeyFactory.instance().getKey(v11n, passage); } } catch (Exception e) { Log.e(TAG, "Invalid passage reference in reading plan:"+passage); } // If all else fails return an empty passage to prevent NPE if (key==null) { key = PassageKeyFactory.instance().createEmptyKeyList(v11n); } return key; } }
/** * Cast a Key to a Passage. Only those keys that are a Passage or can * be held by a Passage (i.e. Verse and VerseRange) may be cast to one. * If you pass a null key into this method, you get a null Passage out. * * @param key The key to cast * @return The key cast to a Passage * @throws ClassCastException */ public static Passage getPassage(Key key) { if (key == null) { return null; } if (key instanceof Passage) { return (Passage) key; } if (key instanceof VerseKey) { VerseKey verseKey = (VerseKey) key; Key ref = PassageKeyFactory.instance().createEmptyKeyList(verseKey.getVersification()); ref.addAll(verseKey); return (Passage) ref; } throw new ClassCastException("Expected key to be a Verse, VerseRange or Passage"); }
/** * Read a passage from a given stream * * @param in * The stream to read from * @return a newly built Passage * @throws IOException * If there was trouble reading the stream * @throws NoSuchKeyException * if the data was not a valid passage */ public static Passage readPassage(Reader in) throws IOException, NoSuchKeyException { // Get any versification. It does not matter. readDescripton will overwrite it. Versification rs = Versifications.instance().getVersification("KJV"); Passage ref = (Passage) keyf.createEmptyKeyList(rs); ref.readDescription(in); return ref; }
/** * Flexible converter for the generic Key base class. * Return the key in the required versification, mapping if necessary * Currently only handles Passage, RangedPassage, and Verse */ public Key convert(Key key, Versification toVersification) { try { if (key instanceof RangedPassage) { return convert((RangedPassage)key, toVersification); } else if (key instanceof VerseRange) { return convert((VerseRange)key, toVersification); } else if (key instanceof Passage) { return convert((Passage)key, toVersification); } else if (key instanceof Verse) { return convert((Verse)key, toVersification); } } catch (Exception e) { // unexpected problem during mapping Log.e(TAG, "JSword Versification mapper failed to map "+key.getOsisID()+" to "+toVersification.getName(), e); } return PassageKeyFactory.instance().createEmptyKeyList(toVersification); }
/** * A space separate string containing osisID from the reference element. * We pass book and key because the xref may not be valid and it needs to be reported. * * @param book the book to which the references refer * @param key the verse containing the cross references * @param v11n the versification * @param root the osis element in question * @return The references in the text */ public static String getReferences(Book book, Key key, Versification v11n, Element root) { PassageKeyFactory keyf = PassageKeyFactory.instance(); Key collector = keyf.createEmptyKeyList(v11n); for (Content content : getDeepContent(root, OSISUtil.OSIS_ELEMENT_REFERENCE)) { Element ele = (Element) content; String attr = ele.getAttributeValue(OSISUtil.OSIS_ATTR_REF); if (attr != null) { try { collector.addAll(keyf.getKey(v11n, attr)); } catch (NoSuchKeyException e) { DataPolice.report(book, key, "Unable to parse: " + attr + " - No such reference:" + e.getMessage()); } } } return collector.getOsisID(); }
Passage ref = (Passage) keyf.createEmptyKeyList(rs);