Search in sources :

Example 1 with ChapterIdentifier

use of biblemulticonverter.format.paratext.model.ChapterIdentifier in project BibleMultiConverter by schierlm.

the class USFX method parseElement.

private void parseElement(ParatextBook result, List<ParatextCharacterContentContainer> containerStack, JAXBElement<?> element, ImportBookContext context) {
    String localName = element.getName().getLocalPart();
    if (localName.equals("rem") || localName.equals("cl")) {
        result.getAttributes().put(localName, TextUtilities.whitespaceNormalization((String) element.getValue()).trim());
    } else if (localName.equals("h")) {
        Usfx.Book.H h = (Usfx.Book.H) element.getValue();
        result.getAttributes().put("h" + (h.getLevel() == null ? "" : h.getLevel()), TextUtilities.whitespaceNormalization(h.getValue()).trim());
    } else if (localName.equals("b") && element.getValue() instanceof Usfx.Book.B) {
        Usfx.Book.B b = (Usfx.Book.B) element.getValue();
        String tag = (b.getSfm() == null ? localName : b.getSfm());
        ParagraphKind kind = USFM.PARAGRAPH_TAGS.get(tag);
        if (kind == null) {
            System.out.println("WARNING: Unsupported paragraph kind: " + kind);
            kind = ParagraphKind.PARAGRAPH_P;
        }
        result.getContent().add(new ParagraphStart(kind));
        containerStack.clear();
    } else if (Arrays.asList("p", "q", "d", "s", "mt", "b").contains(localName)) {
        PType pt = (PType) element.getValue();
        String tag = (pt.getSfm() == null ? localName : pt.getSfm()) + (pt.getLevel() == null ? "" : "" + pt.getLevel());
        ParagraphKind kind = USFM.PARAGRAPH_TAGS.get(tag);
        if (kind == null) {
            System.out.println("WARNING: Unsupported paragraph kind: " + kind);
            kind = ParagraphKind.PARAGRAPH_P;
        }
        result.getContent().add(new ParagraphStart(kind));
        containerStack.clear();
        parseElements(result, containerStack, pt.getContent(), context);
    } else if (Arrays.asList("sectionBoundary", "ca", "milestone", "va", "fm", "fig", "gw", "cs", "wr").contains(localName)) {
        System.out.println("WARNING: Skipping unsupported tag: " + localName);
    } else if (Arrays.asList("generated", "cp", "vp", "wtp", "da", "fs").contains(localName)) {
    // to be skipped
    } else if (localName.equals("c")) {
        ImportUtilities.closeOpenChapter(result, context.openChapter);
        String id;
        if (element.getValue() instanceof Usfx.Book.C) {
            Usfx.Book.C c = (Usfx.Book.C) element.getValue();
            id = c.getId();
        } else if (element.getValue() instanceof PType.C) {
            PType.C c = (PType.C) element.getValue();
            id = c.getId();
        } else {
            throw new IllegalStateException(element.getValue().getClass().getName());
        }
        context.openChapter = new ChapterStart(new ChapterIdentifier(result.getId(), Integer.parseInt(id)));
        result.getContent().add(context.openChapter);
        containerStack.clear();
    } else if (localName.equals("toc")) {
        StyledString ss = (StyledString) element.getValue();
        result.getAttributes().put("toc" + ss.getLevel(), TextUtilities.whitespaceNormalization(ss.getContent().stream().filter(c -> c instanceof String).map(Serializable::toString).collect(Collectors.joining())).trim());
    } else if (localName.equals("table") && element.getValue() instanceof Usfx.Book.Table) {
        Usfx.Book.Table table = (Usfx.Book.Table) element.getValue();
        for (Usfx.Book.Table.Tr tr : table.getTr()) {
            result.getContent().add(new ParagraphStart(ParagraphKind.TABLE_ROW));
            for (JAXBElement<PType> cell : tr.getThOrThrOrTc()) {
                result.getContent().add(new TableCellStart(cell.getName().getLocalPart() + cell.getValue().getLevel()));
                containerStack.clear();
                parseElements(result, containerStack, cell.getValue().getContent(), context);
            }
        }
    } else if (localName.equals("table") && element.getValue() instanceof PType.Table) {
        PType.Table table = (PType.Table) element.getValue();
        for (PType.Table.Tr tr : table.getTr()) {
            result.getContent().add(new ParagraphStart(ParagraphKind.TABLE_ROW));
            for (JAXBElement<PType> cell : tr.getThOrThrOrTc()) {
                result.getContent().add(new TableCellStart(cell.getName().getLocalPart() + cell.getValue().getLevel()));
                containerStack.clear();
                parseElements(result, containerStack, cell.getValue().getContent(), context);
            }
        }
    } else if (localName.equals("periph")) {
        result.getContent().add(new ParagraphStart(ParagraphKind.PERIPHERALS));
        containerStack.clear();
        ParatextCharacterContent container = new ParatextCharacterContent();
        Text text = Text.from((String) element.getValue());
        if (text != null) {
            container.getContent().add(text);
        }
        containerStack.add(container);
        result.getContent().add(container);
    } else if (localName.equals("v")) {
        String id;
        if (element.getValue() instanceof Usfx.Book.V) {
            Usfx.Book.V v = (Usfx.Book.V) element.getValue();
            id = v.getId();
        } else if (element.getValue() instanceof PType.V) {
            PType.V v = (PType.V) element.getValue();
            id = v.getId();
        } else {
            throw new IllegalStateException(element.getValue().getClass().getName());
        }
        if (containerStack.isEmpty()) {
            ParatextCharacterContent container = new ParatextCharacterContent();
            containerStack.add(container);
            result.getContent().add(container);
        }
        ChapterStart chapter = result.findLastBookContent(ChapterStart.class);
        if (chapter == null) {
            throw new IllegalStateException("Verse found before chapter start: " + id);
        }
        VerseIdentifier location = new VerseIdentifier(result.getId(), chapter.getChapter(), id);
        containerStack.get(containerStack.size() - 1).getContent().add(new VerseStart(location, id));
    } else if (localName.equals("ve")) {
        VerseStart start = result.findLastCharacterContent(VerseStart.class);
        if (start == null) {
            throw new IllegalStateException("Verse end found before verse start!");
        }
        if (containerStack.isEmpty()) {
            ParatextCharacterContent container = new ParatextCharacterContent();
            containerStack.add(container);
            result.getContent().add(container);
        }
        containerStack.get(containerStack.size() - 1).getContent().add(new ParatextCharacterContent.VerseEnd(start.getLocation()));
    } else if (Arrays.asList("f", "x", "fe").contains(localName)) {
        NoteContents nc = (NoteContents) element.getValue();
        String sfm = nc.getSfm();
        if (sfm == null || sfm.isEmpty())
            sfm = localName;
        String caller = nc.getCaller();
        if (caller == null || caller.isEmpty())
            caller = "+";
        FootnoteXref nextContainer = new FootnoteXref(USFM.FOOTNOTE_XREF_TAGS.get(sfm), caller);
        if (containerStack.isEmpty()) {
            ParatextCharacterContent container = new ParatextCharacterContent();
            containerStack.add(container);
            result.getContent().add(container);
        }
        containerStack.get(containerStack.size() - 1).getContent().add(nextContainer);
        containerStack.add(nextContainer);
        parseElements(result, containerStack, nc.getContent(), context);
        containerStack.remove(nextContainer);
    } else if (Arrays.asList("fp", "fr", "fk", "fq", "fqa", "fl", "fdc", "fv", "ft", "fm", "xo", "xk", "xq", "xt", "xot", "xnt", "xdc").contains(localName) || (Arrays.asList("nd", "c", "tl", "it", "qt", "sls", "dc", "bdit", "bk", "pn", "k", "ord", "add", "bd", "sc", "wh", "wg", "wr", "wj", "cs", "em").contains(localName) && element.getValue() instanceof NoteContents)) {
        NoteContents nc = (NoteContents) element.getValue();
        String sfm = nc.getSfm();
        if (sfm == null || sfm.isEmpty())
            sfm = localName;
        if (containerStack.isEmpty()) {
            ParatextCharacterContent container = new ParatextCharacterContent();
            containerStack.add(container);
            result.getContent().add(container);
        }
        AutoClosingFormatting nextContainer = new AutoClosingFormatting(USFM.AUTO_CLOSING_TAGS.get(sfm), false);
        containerStack.get(containerStack.size() - 1).getContent().add(nextContainer);
        containerStack.add(nextContainer);
        parseElements(result, containerStack, nc.getContent(), context);
        containerStack.remove(nextContainer);
    } else if (localName.equals("optionalLineBreak")) {
        System.out.println("WARNING: Skipping optional line break");
    } else if (localName.equals("ref")) {
        RefType rt = (RefType) element.getValue();
        ParatextCharacterContentPart ref = Text.from(rt.getContent());
        // This code does not allow for a second book, as in: ISA.7.14-ISA.7.15.
        if (rt.getTgt() == null || !rt.getTgt().matches("[A-Z1-4]{3}\\.[0-9]+\\.[0-9]+(-[0-9]+(\\.[0-9]+)?)?")) {
            System.out.println("WARNING: Unsupported structured reference format - replaced by plain text: " + rt.getTgt());
        } else {
            String[] parts = rt.getTgt().split("[ .-]");
            ParatextID id = ParatextID.fromIdentifier(parts[0]);
            if (id == null) {
                System.out.println("WARNING: Unsupported book in structured reference - replaced by plain text: " + parts[0]);
            } else {
                int c1 = Integer.parseInt(parts[1]);
                String v1 = parts[2];
                if (parts.length > 3) {
                    // second verse
                    String v2 = parts[parts.length - 1];
                    if (parts.length == 5) {
                        // second chapter
                        int c2 = Integer.parseInt(parts[3]);
                        ref = Reference.verseRange(id, c1, v1, c2, v2, rt.getContent());
                    } else {
                        // No second chapter, but we do have a second verse, use first chapter as second chapter.
                        ref = Reference.verseRange(id, c1, v1, c1, v2, rt.getContent());
                    }
                } else {
                    ref = Reference.verse(id, c1, v1, rt.getContent());
                }
            }
        }
        if (ref != null) {
            if (containerStack.isEmpty()) {
                ParatextCharacterContent container = new ParatextCharacterContent();
                containerStack.add(container);
                result.getContent().add(container);
            }
            containerStack.get(containerStack.size() - 1).getContent().add(ref);
        }
    } else if (localName.equals("w")) {
        PType.W w = (PType.W) element.getValue();
        String sfm = w.getSfm();
        if (sfm == null || sfm.isEmpty())
            sfm = localName;
        AutoClosingFormatting nextContainer = new AutoClosingFormatting(USFM.AUTO_CLOSING_TAGS.get(sfm), false);
        if (w.getL() != null && !w.getL().isEmpty())
            nextContainer.getAttributes().put("lemma", w.getL());
        if (w.getS() != null && !w.getS().isEmpty())
            nextContainer.getAttributes().put("strong", w.getS());
        if (w.getM() != null && !w.getM().isEmpty())
            nextContainer.getAttributes().put("x-morph", w.getM());
        if (w.getSrcloc() != null && !w.getSrcloc().isEmpty())
            nextContainer.getAttributes().put("x-srcloc", w.getSrcloc());
        if (w.isPlural() != null)
            nextContainer.getAttributes().put("x-plural", "" + w.isPlural());
        containerStack.get(containerStack.size() - 1).getContent().add(nextContainer);
        containerStack.add(nextContainer);
        parseElements(result, containerStack, w.getContent(), context);
        containerStack.remove(nextContainer);
    } else if (localName.equals("quoteStart")) {
        PType.QuoteStart qs = (PType.QuoteStart) element.getValue();
        Text text = Text.from(qs.getValue());
        if (text != null) {
            if (containerStack.isEmpty()) {
                ParatextCharacterContent container = new ParatextCharacterContent();
                containerStack.add(container);
                result.getContent().add(container);
            }
            containerStack.get(containerStack.size() - 1).getContent().add(text);
        }
    } else if (localName.equals("quoteRemind") || localName.equals("quoteEnd")) {
        Text text = Text.from((String) element.getValue());
        if (text != null) {
            if (containerStack.isEmpty()) {
                ParatextCharacterContent container = new ParatextCharacterContent();
                containerStack.add(container);
                result.getContent().add(container);
            }
            containerStack.get(containerStack.size() - 1).getContent().add(text);
        }
    } else if (Arrays.asList("rq", "em", "qt", "nd", "tl", "qs", "qac", "sls", "dc", "bk", "k", "add", "sig", "bd", "it", "bdit", "sc", "wj").contains(localName)) {
        PType v = (PType) element.getValue();
        String sfm = v.getSfm();
        if (sfm == null || sfm.isEmpty())
            sfm = localName;
        AutoClosingFormatting nextContainer = new AutoClosingFormatting(USFM.AUTO_CLOSING_TAGS.get(sfm), false);
        if (containerStack.isEmpty()) {
            ParatextCharacterContent container = new ParatextCharacterContent();
            containerStack.add(container);
            result.getContent().add(container);
        }
        containerStack.get(containerStack.size() - 1).getContent().add(nextContainer);
        containerStack.add(nextContainer);
        parseElements(result, containerStack, v.getContent(), context);
        containerStack.remove(nextContainer);
    } else if (Arrays.asList("pn", "ord", "no", "ndx", "wh", "wg", "ior").contains(localName)) {
        AutoClosingFormatting nextContainer = new AutoClosingFormatting(USFM.AUTO_CLOSING_TAGS.get(localName), false);
        if (containerStack.isEmpty()) {
            ParatextCharacterContent container = new ParatextCharacterContent();
            containerStack.add(container);
            result.getContent().add(container);
        }
        containerStack.get(containerStack.size() - 1).getContent().add(nextContainer);
        Text text = Text.from((String) element.getValue());
        if (text != null) {
            nextContainer.getContent().add(text);
        }
    } else {
        System.out.println("WARNING: Unexpected tag: " + localName);
    }
}
Also used : TableCellStart(biblemulticonverter.format.paratext.ParatextBook.TableCellStart) StyledString(biblemulticonverter.schema.usfx.StyledString) RefType(biblemulticonverter.schema.usfx.RefType) ParatextID(biblemulticonverter.format.paratext.ParatextBook.ParatextID) Book(biblemulticonverter.schema.usfx.Usfx.Book) NoteContents(biblemulticonverter.schema.usfx.NoteContents) AutoClosingFormatting(biblemulticonverter.format.paratext.ParatextCharacterContent.AutoClosingFormatting) PType(biblemulticonverter.schema.usfx.PType) Text(biblemulticonverter.format.paratext.ParatextCharacterContent.Text) StyledString(biblemulticonverter.schema.usfx.StyledString) JAXBElement(javax.xml.bind.JAXBElement) ChapterStart(biblemulticonverter.format.paratext.ParatextBook.ChapterStart) FootnoteXref(biblemulticonverter.format.paratext.ParatextCharacterContent.FootnoteXref) VerseIdentifier(biblemulticonverter.format.paratext.model.VerseIdentifier) VerseStart(biblemulticonverter.format.paratext.ParatextCharacterContent.VerseStart) ParatextCharacterContentPart(biblemulticonverter.format.paratext.ParatextCharacterContent.ParatextCharacterContentPart) ParagraphKind(biblemulticonverter.format.paratext.ParatextBook.ParagraphKind) Usfx(biblemulticonverter.schema.usfx.Usfx) ChapterIdentifier(biblemulticonverter.format.paratext.model.ChapterIdentifier) ParagraphStart(biblemulticonverter.format.paratext.ParatextBook.ParagraphStart)

Example 2 with ChapterIdentifier

use of biblemulticonverter.format.paratext.model.ChapterIdentifier in project BibleMultiConverter by schierlm.

the class USX3 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();
    ParatextBook.ParatextID id = ParatextBook.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;
    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 ParatextCharacterContent.AutoClosingFormatting(ParatextCharacterContent.AutoClosingFormattingKind.PAGE_BREAK, false));
            } else if (PARA_STYLE_UNSUPPORTED.contains(para.getStyle())) {
                // skip
                charContent = null;
            } else {
                result.getContent().add(new ParatextBook.ParagraphStart(PARA_STYLE_MAP.get(para.getStyle())));
                charContent = null;
                if (!para.getContent().isEmpty()) {
                    charContent = new ParatextCharacterContent();
                    result.getContent().add(charContent);
                    parseCharContent(para.getContent(), charContent);
                }
            }
        } else if (o instanceof Table) {
            Table table = (Table) o;
            for (Row row : table.getRow()) {
                result.getContent().add(new ParatextBook.ParagraphStart(ParatextBook.ParagraphKind.TABLE_ROW));
                for (Object oo : row.getVerseOrCell()) {
                    if (oo instanceof Verse) {
                        Verse verse = (Verse) oo;
                        ParatextCharacterContent.ParatextCharacterContentPart verseStartOrEnd = handleVerse(verse);
                        charContent = new ParatextCharacterContent();
                        result.getContent().add(charContent);
                        charContent.getContent().add(verseStartOrEnd);
                    } 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);
                    } else {
                        throw new IOException("Unsupported table row element: " + o.getClass().getName());
                    }
                }
            }
            charContent = null;
        } else if (o instanceof Chapter) {
            Chapter chapter = (Chapter) o;
            if (chapter.getSid() != null) {
                // Assume start chapter
                result.getContent().add(new ParatextBook.ChapterStart(new ChapterIdentifier(result.getId(), ((Chapter) o).getNumber().intValue())));
            } else if (chapter.getEid() != null) {
                // Assume end chapter
                ChapterIdentifier location = ChapterIdentifier.fromLocationString(chapter.getEid());
                if (location == null) {
                    throw new IOException("Invalid chapter eid found: " + chapter.getEid());
                }
                result.getContent().add(new ParatextBook.ChapterEnd(location));
            } else {
                throw new IOException("Invalid chapter found, both sid and eid are undefined: " + chapter);
            }
            charContent = null;
        } else if (o instanceof Note) {
            if (charContent == null) {
                charContent = new ParatextCharacterContent();
                result.getContent().add(charContent);
            }
            Note note = (Note) o;
            ParatextCharacterContent.FootnoteXref nx = new ParatextCharacterContent.FootnoteXref(NOTE_STYLE_MAP.get(note.getStyle()), note.getCaller());
            charContent.getContent().add(nx);
            parseCharContent(note.getContent(), nx);
        } 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());
        }
    }
    return result;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Cell(biblemulticonverter.schema.usx3.Cell) Table(biblemulticonverter.schema.usx3.Table) Para(biblemulticonverter.schema.usx3.Para) Chapter(biblemulticonverter.schema.usx3.Chapter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Note(biblemulticonverter.schema.usx3.Note) Usx(biblemulticonverter.schema.usx3.Usx) Row(biblemulticonverter.schema.usx3.Row) ChapterIdentifier(biblemulticonverter.format.paratext.model.ChapterIdentifier) XMLInputFactory(javax.xml.stream.XMLInputFactory) Verse(biblemulticonverter.schema.usx3.Verse) Sidebar(biblemulticonverter.schema.usx3.Sidebar)

Example 3 with ChapterIdentifier

use of biblemulticonverter.format.paratext.model.ChapterIdentifier 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 4 with ChapterIdentifier

use of biblemulticonverter.format.paratext.model.ChapterIdentifier in project BibleMultiConverter by schierlm.

the class USFM method doExportBook.

@Override
protected void doExportBook(ParatextBook book, File outFile) throws IOException {
    try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), StandardCharsets.UTF_8))) {
        bw.write("\\id " + book.getId().getIdentifier() + " " + escape(book.getBibleName(), false));
        bw.write("\n\\ide UTF-8");
        for (Map.Entry<String, String> attr : book.getAttributes().entrySet()) {
            // Never write a charset other than the charset we are using to write this file
            if (!attr.getKey().equals("ide")) {
                bw.write("\n\\" + attr.getKey() + " " + escape(attr.getValue(), false));
            }
        }
        book.accept(new ParatextBookContentVisitor<IOException>() {

            private final USFMExportContext context = new USFMExportContext(warningLogger);

            @Override
            public void visitChapterStart(ChapterIdentifier location) throws IOException {
                bw.write("\n\\c " + location.chapter);
                context.needSpace = true;
            }

            @Override
            public void visitChapterEnd(ChapterIdentifier location) throws IOException {
            // Chapter end marker is not supported in USFM 2.
            }

            @Override
            public void visitParagraphStart(ParagraphKind kind) throws IOException {
                if (USFM_2_PARAGRAPH_KINDS.contains(kind)) {
                    bw.write("\n\\" + kind.getTag());
                    context.needSpace = true;
                } else {
                    visitUnsupportedParagraphStart(kind);
                }
            }

            private void visitUnsupportedParagraphStart(ParagraphKind kind) throws IOException {
                if (kind == ParagraphKind.HEBREW_NOTE) {
                    // According to documentation this is very similar to `d` (ParagraphKind.DESCRIPTIVE_TITLE)
                    warningLogger.logReplaceWarning(kind, ParagraphKind.DESCRIPTIVE_TITLE);
                    visitParagraphStart(ParagraphKind.DESCRIPTIVE_TITLE);
                } else if (kind.isSameBase(ParagraphKind.SEMANTIC_DIVISION)) {
                    // TODO maybe add more than 1 blank line?
                    warningLogger.logReplaceWarning(kind, ParagraphKind.BLANK_LINE);
                    visitParagraphStart(ParagraphKind.BLANK_LINE);
                } else if (kind == ParagraphKind.PARAGRAPH_PO || kind == ParagraphKind.PARAGRAPH_LH || kind == ParagraphKind.PARAGRAPH_LF) {
                    warningLogger.logReplaceWarning(kind, ParagraphKind.PARAGRAPH_P);
                    visitParagraphStart(ParagraphKind.PARAGRAPH_P);
                } else if (kind.getTag().startsWith(ParagraphKind.PARAGRAPH_LIM.getTag())) {
                    // Documentation is not entirely clear on what the exact difference is between `lim#` and `li#`
                    // one is "embedded" the other is not: https://ubsicap.github.io/usfm/lists/index.html#lim
                    // The assumption is made here that `lim#` is directly replaceable with `li#`
                    ParagraphKind replacement = ParagraphKind.PARAGRAPH_LI.getWithNumber(kind.getNumber());
                    warningLogger.logReplaceWarning(kind, replacement);
                    visitParagraphStart(replacement);
                } else {
                    throw new RuntimeException("Could not export to USFM 2 because an unhandled paragraph type `" + kind + "` from a newer USFM/USX version was found.");
                }
            }

            @Override
            public void visitTableCellStart(String tag) throws IOException {
                if (context.needSpace) {
                    bw.write(" ");
                }
                bw.write("\\" + tag);
                context.needSpace = true;
            }

            @Override
            public void visitParatextCharacterContent(ParatextCharacterContent content) throws IOException {
                content.accept(new USFMCharacterContentVisitor(bw, context));
            }
        });
    }
}
Also used : IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ParagraphKind(biblemulticonverter.format.paratext.ParatextBook.ParagraphKind) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ChapterIdentifier(biblemulticonverter.format.paratext.model.ChapterIdentifier) Map(java.util.Map)

Example 5 with ChapterIdentifier

use of biblemulticonverter.format.paratext.model.ChapterIdentifier 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)

Aggregations

ChapterIdentifier (biblemulticonverter.format.paratext.model.ChapterIdentifier)10 IOException (java.io.IOException)7 ParagraphKind (biblemulticonverter.format.paratext.ParatextBook.ParagraphKind)6 ChapterStart (biblemulticonverter.format.paratext.ParatextBook.ChapterStart)4 ParagraphStart (biblemulticonverter.format.paratext.ParatextBook.ParagraphStart)4 JAXBContext (javax.xml.bind.JAXBContext)4 ParatextID (biblemulticonverter.format.paratext.ParatextBook.ParatextID)3 TableCellStart (biblemulticonverter.format.paratext.ParatextBook.TableCellStart)3 AutoClosingFormatting (biblemulticonverter.format.paratext.ParatextCharacterContent.AutoClosingFormatting)3 FootnoteXref (biblemulticonverter.format.paratext.ParatextCharacterContent.FootnoteXref)3 VerseIdentifier (biblemulticonverter.format.paratext.model.VerseIdentifier)3 FileInputStream (java.io.FileInputStream)3 EnumMap (java.util.EnumMap)3 Map (java.util.Map)3 Book (biblemulticonverter.data.Book)2 Chapter (biblemulticonverter.data.Chapter)2 VerseStart (biblemulticonverter.format.paratext.ParatextCharacterContent.VerseStart)2 UnifiedScriptureXMLWriter (biblemulticonverter.format.paratext.utilities.UnifiedScriptureXMLWriter)2 Cell (biblemulticonverter.schema.usx.Cell)2 Chapter (biblemulticonverter.schema.usx.Chapter)2