| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package org.crosswire.jsword.book; |
| 21 | |
|
| 22 | |
import java.util.ArrayList; |
| 23 | |
import java.util.Iterator; |
| 24 | |
import java.util.List; |
| 25 | |
import java.util.Map; |
| 26 | |
import java.util.TreeMap; |
| 27 | |
|
| 28 | |
import org.crosswire.common.diff.Diff; |
| 29 | |
import org.crosswire.common.diff.DiffCleanup; |
| 30 | |
import org.crosswire.common.diff.Difference; |
| 31 | |
import org.crosswire.common.util.Language; |
| 32 | |
import org.crosswire.common.xml.JDOMSAXEventProvider; |
| 33 | |
import org.crosswire.common.xml.SAXEventProvider; |
| 34 | |
import org.crosswire.jsword.passage.Key; |
| 35 | |
import org.crosswire.jsword.passage.KeyUtil; |
| 36 | |
import org.crosswire.jsword.passage.Passage; |
| 37 | |
import org.crosswire.jsword.passage.RestrictionType; |
| 38 | |
import org.crosswire.jsword.passage.Verse; |
| 39 | |
import org.crosswire.jsword.versification.Versification; |
| 40 | |
import org.crosswire.jsword.versification.VersificationsMapper; |
| 41 | |
import org.crosswire.jsword.versification.system.Versifications; |
| 42 | |
import org.jdom2.Content; |
| 43 | |
import org.jdom2.Document; |
| 44 | |
import org.jdom2.Element; |
| 45 | |
import org.jdom2.Namespace; |
| 46 | |
import org.jdom2.Text; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 0 | public class BookData implements BookProvider { |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 0 | public BookData(Book book, Key key) { |
| 68 | 0 | assert book != null; |
| 69 | 0 | assert key != null; |
| 70 | |
|
| 71 | 0 | this.key = key; |
| 72 | |
|
| 73 | 0 | books = new Book[1]; |
| 74 | 0 | books[0] = book; |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | 0 | public BookData(Book[] books, Key key, boolean compare) { |
| 85 | 0 | assert books != null && books.length > 0; |
| 86 | 0 | assert key != null; |
| 87 | |
|
| 88 | 0 | this.books = books.clone(); |
| 89 | 0 | this.key = key; |
| 90 | 0 | this.comparingBooks = compare; |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public Element getOsis() throws BookException { |
| 100 | 0 | if (osis == null) { |
| 101 | |
|
| 102 | |
|
| 103 | 0 | osis = OSISUtil.createOsisFramework(getFirstBook().getBookMetaData()); |
| 104 | 0 | Element text = osis.getChild(OSISUtil.OSIS_ELEMENT_OSISTEXT); |
| 105 | 0 | Element div = getOsisFragment(); |
| 106 | 0 | text.addContent(div); |
| 107 | |
} |
| 108 | |
|
| 109 | 0 | return osis; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public Element getOsisFragment() throws BookException { |
| 119 | 0 | if (fragment == null) { |
| 120 | 0 | fragment = getOsisContent(true); |
| 121 | |
} |
| 122 | |
|
| 123 | 0 | return fragment; |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public Element getOsisFragment(boolean allowGenTitles) throws BookException { |
| 134 | 0 | if (fragment == null) { |
| 135 | 0 | fragment = getOsisContent(allowGenTitles); |
| 136 | |
} |
| 137 | |
|
| 138 | 0 | return fragment; |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public SAXEventProvider getSAXEventProvider() throws BookException { |
| 148 | |
|
| 149 | 0 | Element frag = getOsisFragment(); |
| 150 | 0 | Document doc = frag.getDocument(); |
| 151 | 0 | if (doc == null) { |
| 152 | 0 | doc = new Document(frag); |
| 153 | |
} |
| 154 | 0 | return new JDOMSAXEventProvider(doc); |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
public Book[] getBooks() { |
| 163 | 0 | return books == null ? null : (Book[]) books.clone(); |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
public Book getFirstBook() { |
| 172 | 0 | return books != null && books.length > 0 ? books[0] : null; |
| 173 | |
} |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public Key getKey() { |
| 181 | 0 | return key; |
| 182 | |
} |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
public boolean isComparingBooks() { |
| 188 | 0 | return comparingBooks; |
| 189 | |
} |
| 190 | |
|
| 191 | |
private Element getOsisContent(boolean allowGenTitles) throws BookException { |
| 192 | 0 | Element div = OSISUtil.factory().createDiv(); |
| 193 | |
|
| 194 | 0 | if (books.length == 1) { |
| 195 | 0 | Iterator<Content> iter = books[0].getOsisIterator(key, false, allowGenTitles); |
| 196 | 0 | while (iter.hasNext()) { |
| 197 | 0 | Content content = iter.next(); |
| 198 | 0 | div.addContent(content); |
| 199 | 0 | } |
| 200 | 0 | } else { |
| 201 | 0 | Element table = OSISUtil.factory().createTable(); |
| 202 | 0 | Element row = OSISUtil.factory().createRow(); |
| 203 | 0 | Element cell = OSISUtil.factory().createCell(); |
| 204 | |
|
| 205 | 0 | table.addContent(row); |
| 206 | |
|
| 207 | 0 | Iterator<Content>[] iters = new Iterator[books.length]; |
| 208 | 0 | Passage[] passages = new Passage[books.length]; |
| 209 | 0 | boolean[] showDiffs = new boolean[books.length - 1]; |
| 210 | 0 | boolean doDiffs = false; |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | 0 | boolean[] ommittedVerses = new boolean[books.length]; |
| 215 | 0 | int numRangesInMasterPassage = 0; |
| 216 | 0 | for (int i = 0; i < books.length; i++) { |
| 217 | |
|
| 218 | |
|
| 219 | 0 | passages[i] = VersificationsMapper.instance().map(KeyUtil.getPassage(key), getVersification(i)); |
| 220 | |
|
| 221 | |
|
| 222 | 0 | iters[i] = books[i].getOsisIterator(passages[i], true, true); |
| 223 | |
|
| 224 | 0 | if (i == 0) { |
| 225 | |
|
| 226 | 0 | ommittedVerses[i] = false; |
| 227 | 0 | numRangesInMasterPassage = passages[i].countRanges(RestrictionType.NONE); |
| 228 | |
} else { |
| 229 | |
|
| 230 | |
|
| 231 | 0 | ommittedVerses[i] = passages[i].countRanges(RestrictionType.NONE) > numRangesInMasterPassage; |
| 232 | |
} |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | 0 | BookVerseContent[] booksContents = new BookVerseContent[books.length]; |
| 238 | 0 | for (int i = 0; i < books.length; i++) { |
| 239 | 0 | doDiffs |= addHeaderAndSetShowDiffsState(row, showDiffs, i, ommittedVerses[i]); |
| 240 | 0 | booksContents[i] = keyIteratorContentByVerse( |
| 241 | |
getVersification(i), |
| 242 | |
iters[i]); |
| 243 | |
} |
| 244 | |
|
| 245 | 0 | int cellCount = 0; |
| 246 | 0 | int rowCount = 0; |
| 247 | |
|
| 248 | |
|
| 249 | 0 | for (Map.Entry<Verse, List<Content>> verseContent : booksContents[0].entrySet()) { |
| 250 | 0 | cellCount = 0; |
| 251 | 0 | row = OSISUtil.factory().createRow(); |
| 252 | 0 | String firstText = ""; |
| 253 | |
|
| 254 | 0 | for (int i = 0; i < books.length; i++) { |
| 255 | 0 | Book book = books[i]; |
| 256 | 0 | cell = OSISUtil.factory().createCell(); |
| 257 | 0 | Language lang = book.getLanguage(); |
| 258 | 0 | if (lang != null) { |
| 259 | 0 | cell.setAttribute(OSISUtil.OSIS_ATTR_LANG, lang.getCode(), Namespace.XML_NAMESPACE); |
| 260 | |
} |
| 261 | |
|
| 262 | 0 | row.addContent(cell); |
| 263 | |
|
| 264 | 0 | StringBuilder newText = new StringBuilder(doDiffs ? 32 : 0); |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | 0 | Key verseInRelavantBookContents = VersificationsMapper.instance().mapVerse(verseContent.getKey(), getVersification(i)); |
| 269 | |
|
| 270 | |
|
| 271 | 0 | Passage passageOfInterest = KeyUtil.getPassage(verseInRelavantBookContents); |
| 272 | 0 | Iterator<Key> passageKeys = passageOfInterest.iterator(); |
| 273 | 0 | while (passageKeys.hasNext()) { |
| 274 | 0 | Key singleKey = passageKeys.next(); |
| 275 | |
|
| 276 | |
|
| 277 | 0 | if (!(singleKey instanceof Verse)) { |
| 278 | 0 | throw new UnsupportedOperationException("Iterating through a passage gives non-verses"); |
| 279 | |
} |
| 280 | |
|
| 281 | 0 | List<Content> xmlContent = booksContents[i].get(singleKey); |
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | 0 | if (xmlContent == null) { |
| 286 | 0 | xmlContent = new ArrayList<Content>(0); |
| 287 | |
} |
| 288 | |
|
| 289 | 0 | addText(doDiffs, newText, xmlContent); |
| 290 | |
|
| 291 | 0 | if (doDiffs) { |
| 292 | 0 | String thisText = newText.toString(); |
| 293 | 0 | if (unaccenter != null) { |
| 294 | 0 | thisText = unaccenter.unaccent(thisText); |
| 295 | |
} |
| 296 | |
|
| 297 | 0 | if (i > 0 && showDiffs[i - 1]) { |
| 298 | 0 | List<Difference> diffs = new Diff(firstText, thisText, false).compare(); |
| 299 | 0 | DiffCleanup.cleanupSemantic(diffs); |
| 300 | 0 | cell.addContent(OSISUtil.diffToOsis(diffs)); |
| 301 | |
|
| 302 | |
|
| 303 | 0 | cell = OSISUtil.factory().createCell(); |
| 304 | 0 | lang = book.getLanguage(); |
| 305 | 0 | cell.setAttribute(OSISUtil.OSIS_ATTR_LANG, lang.getCode(), Namespace.XML_NAMESPACE); |
| 306 | 0 | row.addContent(cell); |
| 307 | |
} |
| 308 | 0 | if (i == 0) { |
| 309 | 0 | firstText = thisText; |
| 310 | |
} |
| 311 | |
} |
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | 0 | addContentSafely(cell, xmlContent); |
| 317 | 0 | cellCount++; |
| 318 | 0 | } |
| 319 | |
} |
| 320 | |
|
| 321 | 0 | if (cellCount == 0) { |
| 322 | 0 | break; |
| 323 | |
} |
| 324 | |
|
| 325 | 0 | table.addContent(row); |
| 326 | 0 | rowCount++; |
| 327 | 0 | } |
| 328 | 0 | if (rowCount > 0) { |
| 329 | 0 | div.addContent(table); |
| 330 | |
} |
| 331 | |
} |
| 332 | |
|
| 333 | 0 | return div; |
| 334 | |
} |
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
private void addContentSafely(final Element cell, final List<Content> xmlContent) { |
| 345 | 0 | Element note = null; |
| 346 | 0 | for (Content c : xmlContent) { |
| 347 | 0 | if (c.getParent() == null) { |
| 348 | 0 | cell.addContent(c); |
| 349 | 0 | } else if (note != null) { |
| 350 | 0 | note.addContent(c.clone()); |
| 351 | |
} else { |
| 352 | |
|
| 353 | |
|
| 354 | 0 | note = appendVersificationNotice(cell, "duplicate"); |
| 355 | 0 | note.addContent(c.clone()); |
| 356 | |
} |
| 357 | |
} |
| 358 | 0 | } |
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
private Element appendVersificationNotice(Element parent, final String notice) { |
| 368 | 0 | Element note = OSISUtil.factory().createDiv(); |
| 369 | 0 | note.setAttribute(OSISUtil.OSIS_ATTR_TYPE, OSISUtil.GENERATED_CONTENT); |
| 370 | 0 | note.setAttribute(OSISUtil.OSIS_ATTR_SUBTYPE, OSISUtil.TYPE_X_PREFIX + notice); |
| 371 | 0 | parent.addContent(note); |
| 372 | 0 | return note; |
| 373 | |
} |
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
private Versification getVersification(final int i) { |
| 380 | 0 | return Versifications.instance().getVersification( |
| 381 | |
books[i].getBookMetaData().getProperty(BookMetaData.KEY_VERSIFICATION)); |
| 382 | |
} |
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
private BookVerseContent keyIteratorContentByVerse(Versification v11n, final Iterator<Content> iter) throws BookException { |
| 395 | 0 | BookVerseContent contentsByOsisID = new BookVerseContent(); |
| 396 | |
|
| 397 | |
|
| 398 | 0 | Verse currentVerse = null; |
| 399 | |
Content content; |
| 400 | |
|
| 401 | 0 | List<Content> contents = new ArrayList<Content>(); |
| 402 | 0 | while (iter.hasNext()) { |
| 403 | 0 | content = iter.next(); |
| 404 | 0 | if (content instanceof Element && OSISUtil.OSIS_ELEMENT_VERSE.equals(((Element) content).getName())) { |
| 405 | 0 | if (currentVerse != null) { |
| 406 | 0 | contentsByOsisID.put(currentVerse, contents); |
| 407 | 0 | contents = new ArrayList<Content>(); |
| 408 | |
} |
| 409 | |
|
| 410 | 0 | currentVerse = OSISUtil.getVerse(v11n, (Element) content); |
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | 0 | if (contents.size() > 0) { |
| 417 | 0 | Verse previousVerse = new Verse(currentVerse.getVersification(), currentVerse.getOrdinal() - 1); |
| 418 | 0 | contentsByOsisID.put(previousVerse, contents); |
| 419 | 0 | contents = new ArrayList<Content>(); |
| 420 | |
} |
| 421 | |
} |
| 422 | |
|
| 423 | 0 | contents.add(content); |
| 424 | |
} |
| 425 | |
|
| 426 | |
|
| 427 | 0 | if (currentVerse != null) { |
| 428 | 0 | contentsByOsisID.put(currentVerse, contents); |
| 429 | |
} |
| 430 | |
|
| 431 | 0 | return contentsByOsisID; |
| 432 | |
} |
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
private boolean addHeaderAndSetShowDiffsState(final Element row, final boolean[] showDiffs, final int i, final boolean ommittedVerse) { |
| 442 | 0 | boolean doDiffs = false; |
| 443 | 0 | Book book = books[i]; |
| 444 | 0 | Element cell = OSISUtil.factory().createHeaderCell(); |
| 445 | |
|
| 446 | 0 | if (i > 0) { |
| 447 | 0 | Book firstBook = books[0]; |
| 448 | 0 | BookCategory category = book.getBookCategory(); |
| 449 | |
|
| 450 | 0 | BookCategory prevCategory = firstBook.getBookCategory(); |
| 451 | 0 | String prevName = firstBook.getInitials(); |
| 452 | 0 | showDiffs[i - 1] = comparingBooks && BookCategory.BIBLE.equals(category) && category.equals(prevCategory) |
| 453 | |
&& book.getLanguage().equals(firstBook.getLanguage()) && !book.getInitials().equals(prevName); |
| 454 | |
|
| 455 | 0 | if (showDiffs[i - 1]) { |
| 456 | 0 | doDiffs = true; |
| 457 | 0 | StringBuilder buf = new StringBuilder(firstBook.getInitials()); |
| 458 | 0 | buf.append(" ==> "); |
| 459 | 0 | buf.append(book.getInitials()); |
| 460 | |
|
| 461 | 0 | cell.addContent(OSISUtil.factory().createText(buf.toString())); |
| 462 | 0 | row.addContent(cell); |
| 463 | 0 | cell = OSISUtil.factory().createHeaderCell(); |
| 464 | |
} |
| 465 | |
} |
| 466 | |
|
| 467 | 0 | final Text text = OSISUtil.factory().createText(book.getInitials()); |
| 468 | 0 | if (ommittedVerse) { |
| 469 | 0 | Element notice = this.appendVersificationNotice(cell, "omitted-verses"); |
| 470 | 0 | notice.addContent(text); |
| 471 | 0 | } else { |
| 472 | 0 | cell.addContent(text); |
| 473 | |
} |
| 474 | 0 | row.addContent(cell); |
| 475 | 0 | return doDiffs; |
| 476 | |
} |
| 477 | |
|
| 478 | |
|
| 479 | |
|
| 480 | |
|
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
private void addText(boolean doDiffs, StringBuilder newText, List<Content> contents) { |
| 486 | 0 | for (Content c : contents) { |
| 487 | 0 | addText(doDiffs, newText, c); |
| 488 | |
} |
| 489 | 0 | } |
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
private void addText(boolean doDiffs, StringBuilder newText, Content content) { |
| 499 | 0 | if (doDiffs) { |
| 500 | |
|
| 501 | 0 | if (newText.length() != 0) { |
| 502 | 0 | newText.append(' '); |
| 503 | |
} |
| 504 | |
|
| 505 | 0 | if (content instanceof Element) { |
| 506 | 0 | newText.append(OSISUtil.getCanonicalText((Element) content)); |
| 507 | 0 | } else if (content instanceof Text) { |
| 508 | 0 | newText.append(((Text) content).getText()); |
| 509 | |
} |
| 510 | |
} |
| 511 | 0 | } |
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | |
|
| 516 | |
public void setUnaccenter(UnAccenter unaccenter) { |
| 517 | 0 | this.unaccenter = unaccenter; |
| 518 | 0 | } |
| 519 | |
|
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
|
| 524 | 0 | class BookVerseContent extends TreeMap<Verse, List<Content>> { |
| 525 | |
|
| 526 | |
|
| 527 | |
|
| 528 | |
private static final long serialVersionUID = -6508118172314227362L; |
| 529 | |
} |
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
private Key key; |
| 535 | |
|
| 536 | |
|
| 537 | |
|
| 538 | |
|
| 539 | |
private Book[] books; |
| 540 | |
|
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
private boolean comparingBooks; |
| 545 | |
|
| 546 | |
|
| 547 | |
|
| 548 | |
|
| 549 | |
private Element osis; |
| 550 | |
|
| 551 | |
|
| 552 | |
|
| 553 | |
|
| 554 | |
private Element fragment; |
| 555 | |
|
| 556 | |
private UnAccenter unaccenter; |
| 557 | |
} |