Search in sources :

Example 1 with Cell

use of biblemulticonverter.schema.usx.Cell 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;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBContext(javax.xml.bind.JAXBContext) ParatextID(biblemulticonverter.format.paratext.ParatextBook.ParatextID) Unmarshaller(javax.xml.bind.Unmarshaller) Cell(biblemulticonverter.schema.usx.Cell) AutoClosingFormatting(biblemulticonverter.format.paratext.ParatextCharacterContent.AutoClosingFormatting) Table(biblemulticonverter.schema.usx.Table) Para(biblemulticonverter.schema.usx.Para) Chapter(biblemulticonverter.schema.usx.Chapter) IOException(java.io.IOException) ChapterStart(biblemulticonverter.format.paratext.ParatextBook.ChapterStart) FootnoteXref(biblemulticonverter.format.paratext.ParatextCharacterContent.FootnoteXref) FileInputStream(java.io.FileInputStream) Note(biblemulticonverter.schema.usx.Note) Usx(biblemulticonverter.schema.usx.Usx) Row(biblemulticonverter.schema.usx.Row) ChapterIdentifier(biblemulticonverter.format.paratext.model.ChapterIdentifier) ParagraphStart(biblemulticonverter.format.paratext.ParatextBook.ParagraphStart) XMLInputFactory(javax.xml.stream.XMLInputFactory) Verse(biblemulticonverter.schema.usx.Verse) Sidebar(biblemulticonverter.schema.usx.Sidebar)

Example 2 with Cell

use of biblemulticonverter.schema.usx.Cell in project BibleMultiConverter by schierlm.

the class USX method doExportBook.

@Override
protected void doExportBook(ParatextBook book, File outFile) throws Exception {
    ObjectFactory of = new ObjectFactory();
    Usx usx = of.createUsx();
    usx.setVersion("2.5");
    usx.setBook(of.createBook());
    usx.getBook().setStyle("id");
    usx.getBook().setCode(book.getId().getIdentifier());
    usx.getBook().setContent(book.getBibleName());
    for (Map.Entry<String, String> attr : book.getAttributes().entrySet()) {
        Para para = new Para();
        para.setStyle(ParaStyle.fromValue(attr.getKey()));
        para.getContent().add(attr.getValue());
        usx.getParaOrTableOrChapter().add(para);
    }
    book.accept(new ParatextBookContentVisitor<IOException>() {

        List<Object> currentContent = null;

        Table currentTable = null;

        @Override
        public void visitChapterStart(ChapterIdentifier location) throws IOException {
            Chapter ch = new Chapter();
            ch.setStyle("c");
            ch.setNumber(BigInteger.valueOf(location.chapter));
            usx.getParaOrTableOrChapter().add(ch);
            currentContent = null;
            currentTable = null;
        }

        @Override
        public void visitChapterEnd(ChapterIdentifier location) throws IOException {
        // Chapter end does not exist in USX 2
        }

        @Override
        public void visitParagraphStart(ParagraphKind kind) throws IOException {
            if (kind == ParagraphKind.TABLE_ROW) {
                if (currentTable == null) {
                    currentTable = new Table();
                    usx.getParaOrTableOrChapter().add(currentTable);
                }
                Row row = new Row();
                row.setStyle("tr");
                currentTable.getRow().add(row);
                currentContent = currentTable.getRow().get(currentTable.getRow().size() - 1).getVerseOrCell();
            } else if (USX_2_PARAGRAPH_KINDS.contains(kind)) {
                ParaStyle style = PARA_KIND_MAP.get(kind);
                if (style == null) {
                    throw new RuntimeException("Error could not get ParaStyle for ParagraphKind: " + kind);
                }
                Para para = new Para();
                para.setStyle(style);
                usx.getParaOrTableOrChapter().add(para);
                currentContent = para.getContent();
                currentTable = null;
            } else {
                visitUnsupportedParagraphStart(kind);
            }
        }

        private void visitUnsupportedParagraphStart(ParagraphKind kind) throws IOException {
            if (kind == ParagraphKind.HEBREW_NOTE) {
                // See: USFM.visitUnsupportedParagraphStart
                visitParagraphStart(ParagraphKind.DESCRIPTIVE_TITLE);
                logger.logReplaceWarning(kind, ParagraphKind.DESCRIPTIVE_TITLE);
            } else if (kind.isSameBase(ParagraphKind.SEMANTIC_DIVISION)) {
                // See: USFM.visitUnsupportedParagraphStart
                visitParagraphStart(ParagraphKind.BLANK_LINE);
                logger.logReplaceWarning(kind, ParagraphKind.BLANK_LINE);
            } else if (kind == ParagraphKind.PARAGRAPH_PO || kind == ParagraphKind.PARAGRAPH_LH || kind == ParagraphKind.PARAGRAPH_LF) {
                // See: USFM.visitUnsupportedParagraphStart
                logger.logReplaceWarning(kind, ParagraphKind.PARAGRAPH_P);
                visitParagraphStart(ParagraphKind.PARAGRAPH_P);
            } else if (kind.getTag().startsWith(ParagraphKind.PARAGRAPH_LIM.getTag())) {
                ParagraphKind replacement = ParagraphKind.PARAGRAPH_LI.getWithNumber(kind.getNumber());
                logger.logReplaceWarning(kind, replacement);
                visitParagraphStart(replacement);
            } else {
                throw new RuntimeException("Could not export to USX 2 because an unhandled paragraph type `" + kind + "` from a newer USFM/USX version was found.");
            }
        }

        @Override
        public void visitTableCellStart(String tag) throws IOException {
            if (currentTable == null) {
                System.out.println("WARNING: Table cell outside of table");
                return;
            }
            Row currentRow = currentTable.getRow().get(currentTable.getRow().size() - 1);
            Cell cell = new Cell();
            cell.setAlign(tag.contains("r") ? CellAlign.END : CellAlign.START);
            cell.setStyle(CellStyle.fromValue(tag));
            currentRow.getVerseOrCell().add(cell);
            currentContent = cell.getContent();
        }

        @Override
        public void visitParatextCharacterContent(ParatextCharacterContent content) throws IOException {
            if (currentContent == null)
                visitParagraphStart(ParagraphKind.PARAGRAPH_P);
            content.accept(new USXCharacterContentVisitor(logger, currentContent));
        }
    });
    JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
    Marshaller m = ctx.createMarshaller();
    if (!Boolean.getBoolean("biblemulticonverter.skipxmlvalidation"))
        m.setSchema(getSchema());
    m.marshal(usx, new UnifiedScriptureXMLWriter(new FileWriter(outFile), "UTF-8"));
}
Also used : FileWriter(java.io.FileWriter) JAXBContext(javax.xml.bind.JAXBContext) ObjectFactory(biblemulticonverter.schema.usx.ObjectFactory) ParaStyle(biblemulticonverter.schema.usx.ParaStyle) Cell(biblemulticonverter.schema.usx.Cell) Marshaller(javax.xml.bind.Marshaller) Table(biblemulticonverter.schema.usx.Table) Para(biblemulticonverter.schema.usx.Para) UnifiedScriptureXMLWriter(biblemulticonverter.format.paratext.utilities.UnifiedScriptureXMLWriter) Chapter(biblemulticonverter.schema.usx.Chapter) IOException(java.io.IOException) ParagraphKind(biblemulticonverter.format.paratext.ParatextBook.ParagraphKind) Usx(biblemulticonverter.schema.usx.Usx) Row(biblemulticonverter.schema.usx.Row) ChapterIdentifier(biblemulticonverter.format.paratext.model.ChapterIdentifier) Map(java.util.Map) EnumMap(java.util.EnumMap)

Aggregations

ChapterIdentifier (biblemulticonverter.format.paratext.model.ChapterIdentifier)2 Cell (biblemulticonverter.schema.usx.Cell)2 Chapter (biblemulticonverter.schema.usx.Chapter)2 Para (biblemulticonverter.schema.usx.Para)2 Row (biblemulticonverter.schema.usx.Row)2 Table (biblemulticonverter.schema.usx.Table)2 Usx (biblemulticonverter.schema.usx.Usx)2 IOException (java.io.IOException)2 JAXBContext (javax.xml.bind.JAXBContext)2 ChapterStart (biblemulticonverter.format.paratext.ParatextBook.ChapterStart)1 ParagraphKind (biblemulticonverter.format.paratext.ParatextBook.ParagraphKind)1 ParagraphStart (biblemulticonverter.format.paratext.ParatextBook.ParagraphStart)1 ParatextID (biblemulticonverter.format.paratext.ParatextBook.ParatextID)1 AutoClosingFormatting (biblemulticonverter.format.paratext.ParatextCharacterContent.AutoClosingFormatting)1 FootnoteXref (biblemulticonverter.format.paratext.ParatextCharacterContent.FootnoteXref)1 UnifiedScriptureXMLWriter (biblemulticonverter.format.paratext.utilities.UnifiedScriptureXMLWriter)1 Note (biblemulticonverter.schema.usx.Note)1 ObjectFactory (biblemulticonverter.schema.usx.ObjectFactory)1 ParaStyle (biblemulticonverter.schema.usx.ParaStyle)1 Sidebar (biblemulticonverter.schema.usx.Sidebar)1