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.filter.gbf; |
21 | |
|
22 | |
import java.util.LinkedList; |
23 | |
|
24 | |
import org.crosswire.common.util.ClassUtil; |
25 | |
import org.crosswire.common.xml.XMLUtil; |
26 | |
import org.crosswire.jsword.book.Book; |
27 | |
import org.crosswire.jsword.book.DataPolice; |
28 | |
import org.crosswire.jsword.book.OSISUtil; |
29 | |
import org.crosswire.jsword.book.OSISUtil.OSISFactory; |
30 | |
import org.crosswire.jsword.passage.Key; |
31 | |
import org.crosswire.jsword.passage.NoSuchKeyException; |
32 | |
import org.crosswire.jsword.passage.Passage; |
33 | |
import org.jdom2.Content; |
34 | |
import org.jdom2.Element; |
35 | |
import org.jdom2.Text; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public final class GBFTags { |
45 | |
|
46 | |
|
47 | |
|
48 | 0 | private GBFTags() { |
49 | 0 | } |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public static final class DefaultEndTag extends AbstractTag { |
55 | |
|
56 | |
|
57 | |
|
58 | |
public DefaultEndTag(String name) { |
59 | 0 | super(name); |
60 | 0 | } |
61 | |
|
62 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
63 | 0 | if (stack.isEmpty()) { |
64 | 0 | DataPolice.report(book, key, "Ignoring end tag without corresponding start tag: " + getName()); |
65 | 0 | return; |
66 | |
} |
67 | 0 | stack.removeFirst(); |
68 | 0 | } |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public static final class BoldStartTag extends AbstractTag { |
75 | |
|
76 | |
|
77 | |
|
78 | |
public BoldStartTag(String name) { |
79 | 0 | super(name); |
80 | 0 | } |
81 | |
|
82 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
83 | 0 | Element ele = OSIS_FACTORY.createHI(); |
84 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_TYPE, OSISUtil.HI_BOLD); |
85 | 0 | GBFTags.updateOsisStack(stack, ele); |
86 | 0 | } |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public static final class BookTitleStartTag extends AbstractTag { |
93 | |
|
94 | |
|
95 | |
|
96 | |
public BookTitleStartTag(String name) { |
97 | 0 | super(name); |
98 | 0 | } |
99 | |
|
100 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
101 | 0 | Element ele = OSIS_FACTORY.createTitle(); |
102 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_TYPE, "main"); |
103 | 0 | GBFTags.updateOsisStack(stack, ele); |
104 | 0 | } |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public static final class CrossRefStartTag extends AbstractTag { |
111 | |
public CrossRefStartTag(String name) { |
112 | 0 | super(name); |
113 | 0 | } |
114 | |
|
115 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
116 | 0 | Element ele = OSIS_FACTORY.createReference(); |
117 | |
|
118 | 0 | String refstr = getName().substring(2); |
119 | |
try { |
120 | 0 | Passage ref = (Passage) book.getKey(refstr); |
121 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_REF, ref.getOsisRef()); |
122 | 0 | } catch (NoSuchKeyException ex) { |
123 | 0 | DataPolice.report(book, key, "unable to parse reference: " + refstr); |
124 | 0 | } |
125 | 0 | GBFTags.updateOsisStack(stack, ele); |
126 | 0 | } |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public static final class EOLTag extends AbstractTag { |
133 | |
|
134 | |
|
135 | |
|
136 | |
public EOLTag(String name) { |
137 | 0 | super(name); |
138 | 0 | } |
139 | |
|
140 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
141 | |
|
142 | 0 | Element p = OSIS_FACTORY.createLB(); |
143 | 0 | if (stack.isEmpty()) { |
144 | 0 | stack.addFirst(p); |
145 | |
} else { |
146 | 0 | Content top = stack.get(0); |
147 | 0 | if (top instanceof Element) { |
148 | 0 | Element current = (Element) top; |
149 | 0 | current.addContent(p); |
150 | |
} |
151 | |
} |
152 | 0 | } |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public static final class FootnoteStartTag extends AbstractTag { |
159 | |
|
160 | |
|
161 | |
|
162 | |
public FootnoteStartTag(String name) { |
163 | 0 | super(name); |
164 | 0 | } |
165 | |
|
166 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
167 | 0 | Element ele = OSIS_FACTORY.createNote(); |
168 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_TYPE, OSISUtil.NOTETYPE_STUDY); |
169 | 0 | GBFTags.updateOsisStack(stack, ele); |
170 | 0 | } |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public static final class FootnoteEndTag extends AbstractTag { |
177 | |
|
178 | |
|
179 | |
|
180 | |
public FootnoteEndTag(String name) { |
181 | 0 | super(name); |
182 | 0 | } |
183 | |
|
184 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
185 | 0 | if (stack.isEmpty()) { |
186 | 0 | DataPolice.report(book, key, "Ignoring end tag without corresponding start tag: " + getName()); |
187 | 0 | return; |
188 | |
} |
189 | |
|
190 | 0 | Object pop = stack.removeFirst(); |
191 | 0 | if (!(pop instanceof Element)) { |
192 | 0 | DataPolice.report(book, key, "expected to pop a Note, but found " + ClassUtil.getShortClassName(pop.getClass())); |
193 | 0 | return; |
194 | |
} |
195 | |
|
196 | 0 | Element note = (Element) pop; |
197 | 0 | if (note.getContentSize() < 1) { |
198 | 0 | Content top = stack.get(0); |
199 | 0 | if (top instanceof Element) { |
200 | 0 | Element ele = (Element) top; |
201 | 0 | ele.removeContent(note); |
202 | |
} |
203 | |
} |
204 | 0 | } |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public static final class HeaderStartTag extends AbstractTag { |
211 | |
|
212 | |
|
213 | |
|
214 | |
public HeaderStartTag(String name) { |
215 | 0 | super(name); |
216 | 0 | } |
217 | |
|
218 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
219 | 0 | GBFTags.updateOsisStack(stack, OSIS_FACTORY.createTitle()); |
220 | 0 | } |
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
public static final class IgnoredTag extends AbstractTag { |
227 | |
|
228 | |
|
229 | |
|
230 | |
public IgnoredTag(String name) { |
231 | 0 | super(name); |
232 | 0 | } |
233 | |
|
234 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
235 | 0 | } |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public static final class ItalicStartTag extends AbstractTag { |
242 | |
|
243 | |
|
244 | |
|
245 | |
public ItalicStartTag(String name) { |
246 | 0 | super(name); |
247 | 0 | } |
248 | |
|
249 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
250 | 0 | Element ele = OSIS_FACTORY.createHI(); |
251 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_TYPE, OSISUtil.HI_ITALIC); |
252 | 0 | GBFTags.updateOsisStack(stack, ele); |
253 | 0 | } |
254 | |
} |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
public static final class JustifyRightTag extends AbstractTag { |
260 | |
|
261 | |
|
262 | |
|
263 | |
public JustifyRightTag(String name) { |
264 | 0 | super(name); |
265 | 0 | } |
266 | |
|
267 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
268 | |
|
269 | 0 | Element ele = OSIS_FACTORY.createSeg(); |
270 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_TYPE, OSISUtil.SEG_JUSTIFYRIGHT); |
271 | 0 | GBFTags.updateOsisStack(stack, ele); |
272 | 0 | } |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
public static final class JustifyLeftTag extends AbstractTag { |
279 | |
|
280 | |
|
281 | |
|
282 | |
public JustifyLeftTag(String name) { |
283 | 0 | super(name); |
284 | 0 | } |
285 | |
|
286 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
287 | 0 | Element ele = OSIS_FACTORY.createSeg(); |
288 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_TYPE, OSISUtil.SEG_JUSTIFYLEFT); |
289 | 0 | GBFTags.updateOsisStack(stack, ele); |
290 | 0 | } |
291 | |
} |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
public static final class OTQuoteStartTag extends AbstractTag { |
297 | |
|
298 | |
|
299 | |
|
300 | |
public OTQuoteStartTag(String name) { |
301 | 0 | super(name); |
302 | 0 | } |
303 | |
|
304 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
305 | 0 | GBFTags.updateOsisStack(stack, OSIS_FACTORY.createQ()); |
306 | 0 | } |
307 | |
} |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
public static final class ParagraphTag extends AbstractTag { |
313 | |
|
314 | |
|
315 | |
|
316 | |
public ParagraphTag(String name) { |
317 | 0 | super(name); |
318 | 0 | } |
319 | |
|
320 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
321 | |
|
322 | 0 | if (stack.isEmpty()) { |
323 | 0 | Element p = OSIS_FACTORY.createLB(); |
324 | 0 | stack.addFirst(p); |
325 | 0 | } else { |
326 | 0 | Element p = OSIS_FACTORY.createP(); |
327 | 0 | Content top = stack.get(0); |
328 | 0 | if (top instanceof Element) { |
329 | 0 | Element current = (Element) top; |
330 | 0 | current.addContent(p); |
331 | |
} |
332 | |
} |
333 | 0 | } |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
public static final class PoetryStartTag extends AbstractTag { |
340 | |
|
341 | |
|
342 | |
|
343 | |
public PoetryStartTag(String name) { |
344 | 0 | super(name); |
345 | 0 | } |
346 | |
|
347 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
348 | 0 | GBFTags.updateOsisStack(stack, OSIS_FACTORY.createLG()); |
349 | 0 | } |
350 | |
} |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
public static final class PsalmStartTag extends AbstractTag { |
356 | |
|
357 | |
|
358 | |
|
359 | |
public PsalmStartTag(String name) { |
360 | 0 | super(name); |
361 | 0 | } |
362 | |
|
363 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
364 | |
|
365 | 0 | Element title = OSIS_FACTORY.createTitle(); |
366 | 0 | title.setAttribute(OSISUtil.OSIS_ATTR_TYPE, "psalm"); |
367 | 0 | title.setAttribute(OSISUtil.OSIS_ATTR_SUBTYPE, "x-preverse"); |
368 | 0 | title.setAttribute(OSISUtil.OSIS_ATTR_CANONICAL, "true"); |
369 | 0 | GBFTags.updateOsisStack(stack, title); |
370 | 0 | } |
371 | |
} |
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
public static final class RedLetterStartTag extends AbstractTag { |
377 | |
|
378 | |
|
379 | |
|
380 | |
public RedLetterStartTag(String name) { |
381 | 0 | super(name); |
382 | 0 | } |
383 | |
|
384 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
385 | 0 | Element ele = OSIS_FACTORY.createQ(); |
386 | 0 | ele.setAttribute(OSISUtil.ATTRIBUTE_Q_WHO, "Jesus"); |
387 | 0 | GBFTags.updateOsisStack(stack, ele); |
388 | 0 | } |
389 | |
} |
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
public static final class StrongsMorphTag extends AbstractTag { |
395 | |
public StrongsMorphTag(String name) { |
396 | 0 | super(name); |
397 | 0 | } |
398 | |
|
399 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
400 | 0 | String name = getName().trim(); |
401 | |
|
402 | 0 | Content top = stack.get(0); |
403 | 0 | if (top instanceof Element) { |
404 | 0 | Element ele = (Element) top; |
405 | 0 | int size = ele.getContentSize(); |
406 | 0 | if (size == 0) { |
407 | 0 | DataPolice.report(book, key, "No content to attach Strong's Morph tag to: <" + name + ">."); |
408 | 0 | return; |
409 | |
} |
410 | |
|
411 | 0 | int lastIndex = size - 1; |
412 | 0 | Content prevObj = ele.getContent(lastIndex); |
413 | 0 | Element word = null; |
414 | |
|
415 | 0 | if (prevObj instanceof Text) { |
416 | 0 | word = OSIS_FACTORY.createW(); |
417 | 0 | ele.removeContent(prevObj); |
418 | 0 | word.addContent(prevObj); |
419 | 0 | ele.addContent(word); |
420 | 0 | } else if (prevObj instanceof Element) { |
421 | 0 | word = (Element) prevObj; |
422 | |
} else { |
423 | 0 | DataPolice.report(book, key, "No words to attach Strong's Morph tag to: <" + name + ">."); |
424 | 0 | return; |
425 | |
} |
426 | |
|
427 | 0 | String existingMorph = word.getAttributeValue(OSISUtil.ATTRIBUTE_W_MORPH); |
428 | 0 | StringBuilder newMorph = new StringBuilder(); |
429 | |
|
430 | 0 | if (existingMorph != null && existingMorph.length() > 0) { |
431 | 0 | newMorph.append(existingMorph).append('|'); |
432 | |
} |
433 | 0 | newMorph.append(OSISUtil.MORPH_STRONGS).append(name.substring(2)); |
434 | 0 | word.setAttribute(OSISUtil.ATTRIBUTE_W_MORPH, newMorph.toString()); |
435 | |
} |
436 | 0 | } |
437 | |
} |
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
public static final class StrongsWordTag extends AbstractTag { |
443 | |
|
444 | |
|
445 | |
|
446 | |
public StrongsWordTag(String name) { |
447 | 0 | super(name); |
448 | 0 | } |
449 | |
|
450 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
451 | 0 | boolean empty = false; |
452 | 0 | String name = getName().trim(); |
453 | 0 | Element word = null; |
454 | |
|
455 | 0 | Content top = stack.get(0); |
456 | 0 | if (top instanceof Element) { |
457 | 0 | Element ele = (Element) top; |
458 | 0 | int size = ele.getContentSize(); |
459 | 0 | if (size > 0) { |
460 | 0 | int lastIndex = size - 1; |
461 | 0 | Content prevObj = ele.getContent(lastIndex); |
462 | |
|
463 | 0 | if (prevObj instanceof Text) { |
464 | 0 | Text textItem = (Text) prevObj; |
465 | 0 | word = OSIS_FACTORY.createW(); |
466 | 0 | ele.removeContent(textItem); |
467 | 0 | word.addContent(textItem); |
468 | 0 | ele.addContent(word); |
469 | 0 | } else if (prevObj instanceof Element) { |
470 | 0 | word = (Element) prevObj; |
471 | |
} |
472 | |
} |
473 | |
} |
474 | |
|
475 | 0 | if (word == null) { |
476 | 0 | word = OSIS_FACTORY.createW(); |
477 | 0 | empty = true; |
478 | |
} |
479 | |
|
480 | 0 | String existingLemma = word.getAttributeValue(OSISUtil.ATTRIBUTE_W_LEMMA); |
481 | 0 | StringBuilder newLemma = new StringBuilder(); |
482 | |
|
483 | |
|
484 | 0 | if (existingLemma != null && existingLemma.length() > 0) { |
485 | 0 | newLemma.append(existingLemma).append(' '); |
486 | |
} |
487 | |
|
488 | |
|
489 | 0 | newLemma.append(OSISUtil.LEMMA_STRONGS).append(name.substring(1)); |
490 | 0 | word.setAttribute(OSISUtil.ATTRIBUTE_W_LEMMA, newLemma.toString()); |
491 | |
|
492 | 0 | if (empty) { |
493 | |
|
494 | |
|
495 | 0 | top = stack.getLast(); |
496 | 0 | if (top instanceof Element) { |
497 | 0 | Element ele = (Element) top; |
498 | 0 | ele.addContent(word); |
499 | |
} |
500 | |
} |
501 | 0 | } |
502 | |
} |
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
public static final class TextFootnoteTag extends AbstractTag { |
508 | |
|
509 | |
|
510 | |
|
511 | |
public TextFootnoteTag(String name) { |
512 | 0 | super(name); |
513 | 0 | } |
514 | |
|
515 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
516 | 0 | Element ele = OSIS_FACTORY.createNote(); |
517 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_TYPE, OSISUtil.NOTETYPE_STUDY); |
518 | 0 | GBFTags.updateOsisStack(stack, ele); |
519 | 0 | } |
520 | |
} |
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
public static final class TextTag extends AbstractTag { |
526 | |
|
527 | |
|
528 | |
|
529 | |
public TextTag(String name) { |
530 | 0 | super(name); |
531 | 0 | } |
532 | |
|
533 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
534 | |
|
535 | 0 | String text = XMLUtil.escape(getName()); |
536 | 0 | if (stack.isEmpty()) { |
537 | 0 | stack.addFirst(new Text(text)); |
538 | |
} else { |
539 | 0 | Content top = stack.get(0); |
540 | 0 | if (top instanceof Element) { |
541 | 0 | Element ele = (Element) top; |
542 | |
|
543 | 0 | if (OSISUtil.OSIS_ELEMENT_W.equals(ele.getName())) { |
544 | |
|
545 | 0 | top = stack.getLast(); |
546 | 0 | if (top instanceof Element) { |
547 | 0 | ele = (Element) top; |
548 | 0 | ele.addContent(new Text(text)); |
549 | |
} |
550 | |
} else { |
551 | 0 | ele.addContent(text); |
552 | |
} |
553 | |
} |
554 | |
} |
555 | 0 | } |
556 | |
} |
557 | |
|
558 | |
|
559 | |
|
560 | |
|
561 | |
public static final class TitleStartTag extends AbstractTag { |
562 | |
|
563 | |
|
564 | |
|
565 | |
public TitleStartTag(String name) { |
566 | 0 | super(name); |
567 | 0 | } |
568 | |
|
569 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
570 | 0 | GBFTags.updateOsisStack(stack, OSIS_FACTORY.createTitle()); |
571 | 0 | } |
572 | |
} |
573 | |
|
574 | |
|
575 | |
|
576 | |
|
577 | |
public static final class UnderlineStartTag extends AbstractTag { |
578 | |
|
579 | |
|
580 | |
|
581 | |
public UnderlineStartTag(String name) { |
582 | 0 | super(name); |
583 | 0 | } |
584 | |
|
585 | |
public void updateOsisStack(Book book, Key key, LinkedList<Content> stack) { |
586 | 0 | Element ele = OSIS_FACTORY.createHI(); |
587 | 0 | ele.setAttribute(OSISUtil.OSIS_ATTR_TYPE, OSISUtil.HI_UNDERLINE); |
588 | 0 | GBFTags.updateOsisStack(stack, ele); |
589 | 0 | } |
590 | |
} |
591 | |
|
592 | |
static void updateOsisStack(LinkedList<Content> stack, Content content) { |
593 | 0 | Content top = stack.get(0); |
594 | 0 | if (top instanceof Element) { |
595 | 0 | Element current = (Element) top; |
596 | 0 | current.addContent(content); |
597 | 0 | stack.addFirst(content); |
598 | |
} |
599 | 0 | } |
600 | |
|
601 | |
|
602 | |
|
603 | |
|
604 | 0 | static final OSISFactory OSIS_FACTORY = OSISUtil.factory(); |
605 | |
} |