Search in sources :

Example 1 with UnifiedScriptureXMLWriter

use of biblemulticonverter.format.paratext.utilities.UnifiedScriptureXMLWriter in project BibleMultiConverter by schierlm.

the class USX3 method doExportBook.

@Override
protected void doExportBook(ParatextBook book, File outFile) throws Exception {
    ObjectFactory of = new ObjectFactory();
    Usx usx = of.createUsx();
    usx.setVersion("3.0");
    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 ParatextBook.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.setSid(location.toString());
            ch.setNumber(BigInteger.valueOf(location.chapter));
            usx.getParaOrTableOrChapter().add(ch);
            currentContent = null;
            currentTable = null;
        }

        @Override
        public void visitChapterEnd(ChapterIdentifier location) throws IOException {
            Chapter ch = new Chapter();
            ch.setEid(location.toString());
            usx.getParaOrTableOrChapter().add(ch);
            currentContent = null;
            currentTable = null;
        }

        @Override
        public void visitParagraphStart(ParatextBook.ParagraphKind kind) throws IOException {
            if (kind == ParatextBook.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 {
                Para para = new Para();
                para.setStyle(PARA_KIND_MAP.get(kind));
                usx.getParaOrTableOrChapter().add(para);
                currentContent = para.getContent();
                currentTable = null;
            }
        }

        @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(ParatextBook.ParagraphKind.PARAGRAPH_P);
            content.accept(new USX3.USXCharacterContentVisitor(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 : Marshaller(javax.xml.bind.Marshaller) Table(biblemulticonverter.schema.usx3.Table) Para(biblemulticonverter.schema.usx3.Para) UnifiedScriptureXMLWriter(biblemulticonverter.format.paratext.utilities.UnifiedScriptureXMLWriter) FileWriter(java.io.FileWriter) Chapter(biblemulticonverter.schema.usx3.Chapter) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) ObjectFactory(biblemulticonverter.schema.usx3.ObjectFactory) Usx(biblemulticonverter.schema.usx3.Usx) Row(biblemulticonverter.schema.usx3.Row) ChapterIdentifier(biblemulticonverter.format.paratext.model.ChapterIdentifier) Map(java.util.Map) EnumMap(java.util.EnumMap) Cell(biblemulticonverter.schema.usx3.Cell)

Example 2 with UnifiedScriptureXMLWriter

use of biblemulticonverter.format.paratext.utilities.UnifiedScriptureXMLWriter 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 UnifiedScriptureXMLWriter (biblemulticonverter.format.paratext.utilities.UnifiedScriptureXMLWriter)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 EnumMap (java.util.EnumMap)2 Map (java.util.Map)2 JAXBContext (javax.xml.bind.JAXBContext)2 Marshaller (javax.xml.bind.Marshaller)2 ParagraphKind (biblemulticonverter.format.paratext.ParatextBook.ParagraphKind)1 Cell (biblemulticonverter.schema.usx.Cell)1 Chapter (biblemulticonverter.schema.usx.Chapter)1 ObjectFactory (biblemulticonverter.schema.usx.ObjectFactory)1 Para (biblemulticonverter.schema.usx.Para)1 ParaStyle (biblemulticonverter.schema.usx.ParaStyle)1 Row (biblemulticonverter.schema.usx.Row)1 Table (biblemulticonverter.schema.usx.Table)1 Usx (biblemulticonverter.schema.usx.Usx)1 Cell (biblemulticonverter.schema.usx3.Cell)1 Chapter (biblemulticonverter.schema.usx3.Chapter)1 ObjectFactory (biblemulticonverter.schema.usx3.ObjectFactory)1