Search in sources :

Example 1 with Usfx

use of biblemulticonverter.schema.usfx.Usfx in project BibleMultiConverter by schierlm.

the class USFX method doExportBooks.

@Override
public void doExportBooks(List<ParatextBook> books, String... exportArgs) throws Exception {
    ObjectFactory of = new ObjectFactory();
    File file = new File(exportArgs[0]);
    Usfx usfx = of.createUsfx();
    for (ParatextBook book : books) {
        usfx.getContent().add(of.createUsfxBook(createBook(book)));
    }
    final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
    Marshaller m = ctx.createMarshaller();
    if (!Boolean.getBoolean("biblemulticonverter.skipxmlvalidation"))
        m.setSchema(getSchema());
    m.marshal(usfx, doc);
    doc.getDocumentElement().setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation", "usfx.xsd");
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.transform(new DOMSource(doc), new StreamResult(file));
}
Also used : Marshaller(javax.xml.bind.Marshaller) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) ObjectFactory(biblemulticonverter.schema.usfx.ObjectFactory) StreamResult(javax.xml.transform.stream.StreamResult) Usfx(biblemulticonverter.schema.usfx.Usfx) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document) File(java.io.File)

Example 2 with Usfx

use of biblemulticonverter.schema.usfx.Usfx in project BibleMultiConverter by schierlm.

the class USFX method doImportAllBooks.

@Override
protected List<ParatextBook> doImportAllBooks(File inputFile) throws Exception {
    ValidateXML.validateFileBeforeParsing(getSchema(), inputFile);
    JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
    Unmarshaller u = ctx.createUnmarshaller();
    Usfx doc = (Usfx) u.unmarshal(inputFile);
    List<ParatextBook> result = new ArrayList<ParatextBook>();
    for (Serializable s : doc.getContent()) {
        if (s instanceof String) {
            if (!s.toString().trim().isEmpty()) {
                System.out.println("WARNING: Skipping text outside of book: " + s);
            }
        } else if (s instanceof JAXBElement<?>) {
            JAXBElement<?> elem = (JAXBElement<?>) s;
            if (elem.getName().getLocalPart().equals("book")) {
                Usfx.Book book = (Usfx.Book) elem.getValue();
                ParatextBook bk = parseBook(book);
                if (bk != null)
                    result.add(bk);
            } else {
                System.out.println("WARNING: Skipping unsupported tag outside of book: " + elem.getName());
            }
        } else {
            System.out.println("WARNING: Skipping unsupported content outside of book " + s);
        }
    }
    result.sort(Comparator.comparing(ParatextBook::getId));
    return result;
}
Also used : Serializable(java.io.Serializable) Book(biblemulticonverter.schema.usfx.Usfx.Book) Usfx(biblemulticonverter.schema.usfx.Usfx) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) StyledString(biblemulticonverter.schema.usfx.StyledString) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

Usfx (biblemulticonverter.schema.usfx.Usfx)2 JAXBContext (javax.xml.bind.JAXBContext)2 ObjectFactory (biblemulticonverter.schema.usfx.ObjectFactory)1 StyledString (biblemulticonverter.schema.usfx.StyledString)1 Book (biblemulticonverter.schema.usfx.Usfx.Book)1 File (java.io.File)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 JAXBElement (javax.xml.bind.JAXBElement)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 Transformer (javax.xml.transform.Transformer)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Document (org.w3c.dom.Document)1