/** * equality is tricky if comparing TreeKeys (as used by GenBooks) because some child keys can have * the same name but different parents */ private static boolean isMatch(Key key1, Key key2, boolean checkParent) { boolean isMatch = false; if (key1 == null && key2 == null) { isMatch = true; } else if (key1 != null && key2 != null) { if (key1.getName().equals(key2.getName())) { // keys match so now default to match = true unless parents exist and are different isMatch = true; // KeyTrees nodes can have the same name but different parents if (checkParent) { isMatch = isMatch(key1.getParent(), key2.getParent(), true); } } } return isMatch; }
/** Perform a test read on an iterator over a set of keys */ private static void readBook(Book book, Key set) { int[] stats = new int[] {0, 0}; boolean first = true; for (Key key : set) { // skip the root of a TreeKey as it often is not addressable. if (first) { first = false; if (key instanceof TreeKey && key.getName().length() == 0) { continue; } } readKey(book, key, stats); } log.warn(book.getInitials() + ':' + stats[0] + ':' + stats[1]); }
/** Perform a test read on a single key */ private static void readKey(Book book, Key key, int[] stats) { try { String orig; try { orig = book.getRawText(key); } catch (BookException ex) { log.warn( "Failed to read: {}({}):{}", book.getInitials(), key.getOsisID(), ex.getMessage(), ex); return; } Matcher matcher = null; if (orig.indexOf("passage=\"") != -1) { matcher = thmlPassagePattern.matcher(orig); } else if (orig.indexOf("osisRef=\"") != -1) { matcher = osisPassagePattern.matcher(orig); } else if (orig.indexOf("<RX>") != -1) { matcher = gbfPassagePattern.matcher(orig); } if (matcher != null) { while (matcher.find()) { String rawRef = matcher.group(2); stats[0]++; String message = book.getInitials() + ':' + key.getOsisRef() + '/' + rawRef; /* try { Key ref = book.getKey(rawRef); message += '/' + ref.getOsisRef(); } catch (NoSuchKeyException e) { message += '!' + e.getMessage(); stats[1]++; } */ out.println(message); } } } catch (Throwable ex) { log.error("Unexpected error reading: {} ({})", book.getInitials(), key.getName(), ex); } }
/** * Books like Josephus have hierarchical chapters. Only the current chapter should be returned, * not child chapters. * * @throws Exception */ @Test public void testHierarchicalBook() throws Exception { Book josephus = Books.installed().getBook("Josephus"); String section1Text = "THOSE who undertake to write histories"; if (josephus != null) { // navigate down to the Preface Key theAntiquitiesOfTheJewsKey = josephus.getGlobalKeyList().get(1); Key prefaceKey = theAntiquitiesOfTheJewsKey.get(0); String prefaceText = josephus.getRawText(prefaceKey); Assert.assertFalse("Child keys returned in raw text", prefaceText.contains(section1Text)); // Now attempt to parse a key but get the problem is that all child text is returned too BookData bookData = new BookData(josephus, prefaceKey); final Element osisFragment = bookData.getOsisFragment(); final XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat()); String prefaceXml = xmlOutputter.outputString(osisFragment); Assert.assertFalse("Child keys returned in xml", prefaceXml.contains(section1Text)); } }
private void doFindTest(Book book, String test, int expected) throws BookException { Key key = book.find(test); System.out.println(test + " found " + key.getCardinality() + " occurences: " + key.getName()); assertEquals(test + " find count wrong", expected, key.getCardinality()); }