| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package org.crosswire.jsword.examples; |
| 21 | |
|
| 22 | |
import java.io.BufferedWriter; |
| 23 | |
import java.io.FileWriter; |
| 24 | |
import java.io.IOException; |
| 25 | |
import java.io.PrintWriter; |
| 26 | |
import java.util.List; |
| 27 | |
import java.util.regex.Matcher; |
| 28 | |
import java.util.regex.Pattern; |
| 29 | |
|
| 30 | |
import org.crosswire.jsword.book.Book; |
| 31 | |
import org.crosswire.jsword.book.BookException; |
| 32 | |
import org.crosswire.jsword.book.BookFilter; |
| 33 | |
import org.crosswire.jsword.book.BookFilters; |
| 34 | |
import org.crosswire.jsword.book.BookMetaData; |
| 35 | |
import org.crosswire.jsword.book.Books; |
| 36 | |
import org.crosswire.jsword.passage.Key; |
| 37 | |
import org.crosswire.jsword.passage.TreeKey; |
| 38 | |
import org.slf4j.Logger; |
| 39 | |
import org.slf4j.LoggerFactory; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public final class GatherAllReferences { |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | private GatherAllReferences() { |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public static void main(String[] args) throws IOException { |
| 61 | 0 | out = new PrintWriter(new BufferedWriter(new FileWriter("passages.log"))); |
| 62 | |
|
| 63 | 0 | log.warn("*** Reading all known Books"); |
| 64 | 0 | BookFilter filter = BookFilters.getCustom("GlobalOptionFilter=ThMLScripref;Category=Biblical Texts"); |
| 65 | 0 | List<Book> comments = Books.installed().getBooks(filter); |
| 66 | 0 | for (Book book : comments) { |
| 67 | |
|
| 68 | 0 | if (!book.isLocked()) { |
| 69 | 0 | BookMetaData bmd = book.getBookMetaData(); |
| 70 | |
|
| 71 | 0 | if (bmd.getProperty("SourceType") != null) { |
| 72 | 0 | Key set = book.getGlobalKeyList(); |
| 73 | |
|
| 74 | 0 | readBook(book, set); |
| 75 | |
} |
| 76 | 0 | } |
| 77 | |
} |
| 78 | 0 | out.flush(); |
| 79 | 0 | out.close(); |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
private static void readBook(Book book, Key set) { |
| 86 | 0 | int[] stats = new int[] { |
| 87 | |
0, 0 |
| 88 | |
}; |
| 89 | |
|
| 90 | 0 | boolean first = true; |
| 91 | 0 | for (Key key : set) { |
| 92 | |
|
| 93 | 0 | if (first) { |
| 94 | 0 | first = false; |
| 95 | 0 | if (key instanceof TreeKey && key.getName().length() == 0) { |
| 96 | 0 | continue; |
| 97 | |
} |
| 98 | |
} |
| 99 | 0 | readKey(book, key, stats); |
| 100 | |
} |
| 101 | 0 | log.warn(book.getInitials() + ':' + stats[0] + ':' + stats[1]); |
| 102 | |
|
| 103 | 0 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
private static void readKey(Book book, Key key, int[] stats) { |
| 109 | |
String orig; |
| 110 | |
try { |
| 111 | 0 | orig = book.getRawText(key); |
| 112 | 0 | } catch (BookException ex) { |
| 113 | 0 | log.warn("Failed to read: {}({}):{}", book.getInitials(), key.getOsisID(), ex.getMessage(), ex); |
| 114 | 0 | return; |
| 115 | 0 | } |
| 116 | |
|
| 117 | 0 | Matcher matcher = null; |
| 118 | 0 | if (orig.indexOf("passage=\"") != -1) { |
| 119 | 0 | matcher = thmlPassagePattern.matcher(orig); |
| 120 | 0 | } else if (orig.indexOf("osisRef=\"") != -1) { |
| 121 | 0 | matcher = osisPassagePattern.matcher(orig); |
| 122 | 0 | } else if (orig.indexOf("<RX>") != -1) { |
| 123 | 0 | matcher = gbfPassagePattern.matcher(orig); |
| 124 | |
} |
| 125 | |
|
| 126 | 0 | if (matcher != null) { |
| 127 | 0 | while (matcher.find()) { |
| 128 | 0 | String rawRef = matcher.group(2); |
| 129 | 0 | stats[0]++; |
| 130 | 0 | String message = book.getInitials() + ':' + key.getOsisRef() + '/' + rawRef; |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | 0 | out.println(message); |
| 142 | 0 | } |
| 143 | |
} |
| 144 | 0 | } |
| 145 | |
|
| 146 | 0 | private static Pattern thmlPassagePattern = Pattern.compile("(osisRef|passage)=\"([^\"]*)"); |
| 147 | 0 | private static Pattern gbfPassagePattern = Pattern.compile("(<RX>)([^<]*)"); |
| 148 | 0 | private static Pattern osisPassagePattern = Pattern.compile("(osisRef)=\"([^\"]*)"); |
| 149 | |
private static PrintWriter out; |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | 0 | private static final Logger log = LoggerFactory.getLogger(GatherAllReferences.class); |
| 155 | |
} |