Beispiel #1
0
 /**
  * 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);
    }
  }
 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());
 }