use of biblemulticonverter.schema.usx.Sidebar in project BibleMultiConverter by schierlm.
the class USX method doImportBook.
@Override
protected ParatextBook doImportBook(File inputFile) throws Exception {
if (!inputFile.getName().toLowerCase().endsWith(".usx"))
return null;
ValidateXML.validateFileBeforeParsing(getSchema(), inputFile);
JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(inputFile));
Unmarshaller u = ctx.createUnmarshaller();
u.setListener(unmarshallerLocationListener);
unmarshallerLocationListener.setXMLStreamReader(inputFile.getName(), xsr);
Usx doc = (Usx) u.unmarshal(xsr);
xsr.close();
ParatextID id = ParatextID.fromIdentifier(doc.getBook().getCode().toUpperCase());
if (id == null) {
System.out.println("WARNING: Skipping book with unknown ID: " + doc.getBook().getCode());
return null;
}
ParatextBook result = new ParatextBook(id, doc.getBook().getContent());
ParatextCharacterContent charContent = null;
ImportContext context = new ImportContext();
for (Object o : doc.getParaOrTableOrChapter()) {
if (o instanceof Para) {
Para para = (Para) o;
if (BOOK_HEADER_ATTRIBUTE_TAGS.contains(para.getStyle().value())) {
String value = "";
for (Object oo : para.getContent()) {
if (oo instanceof String) {
value += ((String) oo).replaceAll("[ \r\n\t]+", " ");
} else {
throw new RuntimeException("Unsupported content in attribute: " + oo.getClass());
}
}
result.getAttributes().put(para.getStyle().value(), value);
charContent = null;
} else if (para.getStyle() == ParaStyle.PB) {
if (charContent == null) {
charContent = new ParatextCharacterContent();
result.getContent().add(charContent);
}
charContent.getContent().add(new AutoClosingFormatting(AutoClosingFormattingKind.PAGE_BREAK, false));
} else if (PARA_STYLE_UNSUPPORTED.contains(para.getStyle())) {
// skip
charContent = null;
} else {
result.getContent().add(new ParagraphStart(PARA_STYLE_MAP.get(para.getStyle())));
charContent = null;
if (!para.getContent().isEmpty()) {
charContent = new ParatextCharacterContent();
result.getContent().add(charContent);
parseCharContent(para.getContent(), charContent, result, context);
}
}
} else if (o instanceof Table) {
Table table = (Table) o;
for (Row row : table.getRow()) {
result.getContent().add(new ParagraphStart(ParagraphKind.TABLE_ROW));
for (Object oo : row.getVerseOrCell()) {
if (oo instanceof Verse) {
ImportUtilities.closeOpenVerse(result, context.openVerse);
context.openVerse = handleVerse(result, (Verse) oo);
charContent = new ParatextCharacterContent();
result.getContent().add(charContent);
charContent.getContent().add(context.openVerse);
} else if (oo instanceof Cell) {
Cell cell = (Cell) oo;
result.getContent().add(new ParatextBook.TableCellStart(cell.getStyle().value()));
charContent = new ParatextCharacterContent();
result.getContent().add(charContent);
parseCharContent(cell.getContent(), charContent, result, context);
} else {
throw new IOException("Unsupported table row element: " + o.getClass().getName());
}
}
}
charContent = null;
} else if (o instanceof Chapter) {
ImportUtilities.closeOpenVerse(result, context.openVerse);
context.openVerse = null;
// There is not really a good way to accurately determine where the end of a chapter should be placed
// based on USX 2 content. Maybe a title above this chapter marker was already intended to be part of
// this chapter. This is basically a best guess. This should not really matter when converting from
// USX 2 to USFM 2 or USFX (which is based on USFM 2), however when up-converting to USX 3 or USFM 3
// this might lead to unexpected results.
ImportUtilities.closeOpenChapter(result, context.openChapter);
context.openChapter = new ChapterStart(new ChapterIdentifier(result.getId(), ((Chapter) o).getNumber().intValue()));
result.getContent().add(context.openChapter);
charContent = null;
} else if (o instanceof Note) {
if (charContent == null) {
charContent = new ParatextCharacterContent();
result.getContent().add(charContent);
}
Note note = (Note) o;
FootnoteXref nx = new FootnoteXref(NOTE_STYLE_MAP.get(note.getStyle()), note.getCaller());
charContent.getContent().add(nx);
parseCharContent(note.getContent(), nx, result, context);
} else if (o instanceof Sidebar) {
System.out.println("WARNING: Skipping sidebar (study bible content)");
charContent = null;
} else {
throw new IOException("Unsupported book level element: " + o.getClass().getName());
}
}
ImportUtilities.closeOpenVerse(result, context.openVerse);
ImportUtilities.closeOpenChapter(result, context.openChapter);
return result;
}
Aggregations