Example usage for org.jdom2 Document addContent

List of usage examples for org.jdom2 Document addContent

Introduction

In this page you can find the example usage for org.jdom2 Document addContent.

Prototype

@Override
public Document addContent(int index, Collection<? extends Content> c) 

Source Link

Document

Inserts the content in a collection into the content list at the given index.

Usage

From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java

License:Apache License

private TEIValidator.Errors extractMainCorpusHeader(Document doc)
        throws LaudatioException, IOException, SAXException {
    TEIValidator validator = corpusSchemeURL == null ? new TEICorpusValidator()
            : new FromURLValidator(corpusSchemeURL);

    Element corpusHeader = doc.getRootElement().getChild("teiHeader", null);

    if (corpusHeader != null) {
        File corpusDir = new File(outputDirectory, "CorpusHeader");
        if (!corpusDir.exists() && !corpusDir.mkdir()) {
            throw new LaudatioException(
                    messages.getString("COULD NOT CREATE DIRECTORY") + corpusDir.getAbsolutePath());
        }//w  ww .  jav a 2 s .co  m

        // create the subtree for the global corpus header
        Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0");
        Element newRootForCorpus = new Element("TEI", teiNS);
        newRootForCorpus.addContent(corpusHeader.clone());
        Document corpusDoc = new Document(newRootForCorpus);

        if (corpusSchemeURL == null) {
            corpusDoc.addContent(0, new ProcessingInstruction("xml-model",
                    "href=\"" + TEICorpusValidator.DEFAULT_SCHEME_URL + "\""));
        } else {
            corpusDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + corpusSchemeURL + "\""));
        }

        // we need to append an empty "text" element after the header
        Element text = new Element("text", teiNS);
        text.setText("");
        newRootForCorpus.addContent(text);

        // we work with the copy from now
        corpusHeader = newRootForCorpus.getChild("teiHeader", null);
        Preconditions.checkNotNull(corpusHeader, messages.getString("ERROR NO CORPUS TITLE GIVEN"));

        Preconditions.checkState("CorpusHeader".equals(corpusHeader.getAttributeValue("type")));

        Preconditions.checkNotNull(corpusHeader.getChild("fileDesc", null),
                messages.getString("ERROR NO CORPUS TITLE GIVEN"));
        Preconditions.checkNotNull(corpusHeader.getChild("fileDesc", null).getChild("titleStmt", null),
                messages.getString("ERROR NO CORPUS TITLE GIVEN"));

        String title = corpusHeader.getChild("fileDesc", null).getChild("titleStmt", null)
                .getChildTextNormalize("title", null);
        Preconditions.checkNotNull(title, messages.getString("ERROR NO CORPUS TITLE GIVEN"));

        // save the file with the title as file name
        File outputFile = new File(corpusDir, title + ".xml");
        XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
        xmlOut.output(corpusDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        log.info(messages.getString("WRITTEN CORPUS HEADER"), outputFile.getPath());

        validator.validate(outputFile);

    }
    return validator.getErrors();
}

From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java

License:Apache License

private TEIValidator.Errors extractDocumentHeaders(Document doc)
        throws LaudatioException, IOException, SAXException {
    TEIValidator validator = documentSchemeURL == null ? new TEIDocumentValidator()
            : new FromURLValidator(documentSchemeURL);

    File documentDir = new File(outputDirectory, "DocumentHeader");
    if (!documentDir.exists() && !documentDir.mkdir()) {
        throw new LaudatioException(
                messages.getString("COULD NOT CREATE DIRECTORY") + documentDir.getAbsolutePath());
    }/*from   www  .  j a  v a  2s  . c  o m*/

    Element documentRoot = Preconditions.checkNotNull(doc.getRootElement().getChild("teiCorpus", null));

    for (Element docHeader : documentRoot.getChildren("teiHeader", null)) {
        Preconditions.checkState("DocumentHeader".equals(docHeader.getAttributeValue("type")));

        // create the subtree for the global corpus header
        Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0");
        Element tei = new Element("TEI", teiNS);
        tei.addContent(docHeader.clone());
        Document newDoc = new Document(tei);

        if (documentSchemeURL == null) {
            newDoc.addContent(0, new ProcessingInstruction("xml-model",
                    "href=\"" + TEIDocumentValidator.DEFAULT_SCHEME_URL + "\""));
        } else {
            newDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + documentSchemeURL + "\""));
        }

        // we need to append an empty "text" element after the header
        Element text = new Element("text", teiNS);
        text.setText("");
        tei.addContent(text);

        Element fileDesc = Preconditions
                .checkNotNull(tei.getChild("teiHeader", null).getChild("fileDesc", null));

        String outName = UUID.randomUUID().toString();

        String id = fileDesc.getAttributeValue("id", Namespace.XML_NAMESPACE);
        if (id != null) {
            outName = id;
        } else {
            Element titleStmt = Preconditions.checkNotNull(fileDesc.getChild("titleStmt", null));

            String title = titleStmt.getChildText("title", null);
            if (title != null) {
                outName = title;
            }
        }

        File outputFile = new File(documentDir, outName + ".xml");
        XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
        xmlOut.output(newDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        log.info(messages.getString("WRITTEN DOCUMENT HEADER"), outputFile.getPath());

        validator.validate(outputFile);

    }
    return validator.getErrors();
}

From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java

License:Apache License

private TEIValidator.Errors extractPreparationSteps(Document doc)
        throws LaudatioException, IOException, SAXException {
    TEIValidator validator = preparationSchemeURL == null ? new TEIPreparationValidator()
            : new FromURLValidator(preparationSchemeURL);
    Multiset<String> knownPreparationTitles = HashMultiset.create();

    File documentDir = new File(outputDirectory, "PreparationHeader");
    if (!documentDir.exists() && !documentDir.mkdir()) {
        throw new LaudatioException(
                messages.getString("COULD NOT CREATE DIRECTORY") + documentDir.getAbsolutePath());
    }//from ww  w  .  j  a v a 2  s  .co m

    Preconditions.checkNotNull(doc.getRootElement().getChild("teiCorpus", null));
    Element preparationRoot = Preconditions
            .checkNotNull(doc.getRootElement().getChild("teiCorpus", null).getChild("teiCorpus", null));

    for (Element preparationHeader : preparationRoot.getChildren("teiHeader", null)) {
        Preconditions.checkState("PreparationHeader".equals(preparationHeader.getAttributeValue("type")));

        // create the subtree for the global corpus header
        Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0");
        Element tei = new Element("TEI", teiNS);
        tei.addContent(preparationHeader.clone());
        Document newDoc = new Document(tei);

        if (preparationSchemeURL == null) {
            newDoc.addContent(0, new ProcessingInstruction("xml-model",
                    "href=\"" + TEIPreparationValidator.DEFAULT_SCHEME_URL + "\""));
        } else {
            newDoc.addContent(0,
                    new ProcessingInstruction("xml-model", "href=\"" + preparationSchemeURL + "\""));
        }

        // we need to append an empty "text" element after the header
        Element text = new Element("text", teiNS);
        text.setText("");
        tei.addContent(text);

        Element fileDesc = Preconditions
                .checkNotNull(tei.getChild("teiHeader", null).getChild("fileDesc", null));

        String outName = UUID.randomUUID().toString();

        Element titleStmt = Preconditions.checkNotNull(fileDesc.getChild("titleStmt", null));
        Element title = Preconditions.checkNotNull(titleStmt.getChild("title", null));
        String corresp = title.getAttributeValue("corresp");
        if (corresp != null) {
            if (knownPreparationTitles.contains(corresp)) {
                knownPreparationTitles.add(corresp);
                outName = corresp + "_" + knownPreparationTitles.count(corresp);
                log.warn(messages.getString("MORE THAN ONE PREPARATION HEADER"), corresp);
            } else {
                outName = corresp;
                knownPreparationTitles.add(corresp);
            }
        }

        File outputFile = new File(documentDir, outName + ".xml");
        XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
        xmlOut.output(newDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        log.info(messages.getString("WRITTEN PREPARATION HEADER"), outputFile.getPath());

        validator.validate(outputFile);

    }
    return validator.getErrors();
}

From source file:org.mycore.oai.MCROAIDataProvider.java

License:Open Source License

/**
 * Add link to XSL stylesheet for displaying OAI response in web browser.
 *//*from w  ww.j  ava  2  s . c om*/
private Document addXSLStyle(Document doc) {
    String styleSheet = MCROAIAdapter.PREFIX + getServletName() + ".ResponseStylesheet";
    String xsl = MCRConfiguration.instance().getString(styleSheet, "oai/oai2.xsl");
    if (!xsl.isEmpty()) {
        Map<String, String> pairs = new HashMap<String, String>();
        pairs.put("type", "text/xsl");
        pairs.put("href", MCRFrontendUtil.getBaseURL() + xsl);
        doc.addContent(0, new ProcessingInstruction("xml-stylesheet", pairs));
    }
    return doc;
}