Example usage for org.jdom2 ProcessingInstruction ProcessingInstruction

List of usage examples for org.jdom2 ProcessingInstruction ProcessingInstruction

Introduction

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

Prototype

public ProcessingInstruction(String target, String data) 

Source Link

Document

This will create a new ProcessingInstruction with the specified target and data.

Usage

From source file:ca.nrc.cadc.caom2.xml.ObservationWriter.java

License:Open Source License

/**
 * Write the root Element to a writer.//  w w w .java2s . c om
 *
 * @param root
 *            Root Element to write.
 * @param writer
 *            Writer to write to.
 * @throws IOException
 *             if the writer fails to write.
 */
@SuppressWarnings("unchecked")
protected void write(Element root, Writer writer) throws IOException {
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    Document document = new Document(root);
    if (stylesheetURL != null) {
        Map<String, String> instructionMap = new HashMap<String, String>(2);
        instructionMap.put("type", "text/xsl");
        instructionMap.put("href", stylesheetURL);
        ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", instructionMap);
        document.getContent().add(0, pi);
    }
    outputter.output(document, writer);
}

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  .  j a va 2 s.  c  o 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 w w w  .  ja v a  2 s.  c  om

    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  av a  2s.  c o  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:edu.utep.cs.jasg.apiGenerator.APIGenerator.java

License:Open Source License

/** Creates a XML file with an XSL reference using JDom */
private void createXMLFileWithXSLLink() {
    try (FileWriter fileWriter = new FileWriter(outputPath + File.separator + fileName + ".xml");) {

        // new XMLOutputter().output(doc, System.out);
        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        xmlOutput.setFormat(Format.getPrettyFormat());

        HashMap<String, String> piMap = new HashMap<String, String>();
        piMap.put("type", "text/xsl");
        piMap.put("href", "stylesheet.xsl");
        ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", piMap);

        doc.getContent().add(0, pi);/*  w w w.jav a 2  s . c o  m*/

        xmlOutput.output(doc, fileWriter);

        System.out.println("File \"" + outputPath + File.separator + fileName + ".xml" + "\" created");

    } catch (IOException io) {
        System.err.println("IOException in APIGenerator: " + io.getMessage());
    }
}

From source file:odml.core.Writer.java

License:Open Source License

/**
 * Creates the Dom document from the section.
 *
 * @param rootSection {@link Section}: the section to start the dom creation.
 * @param asTerminology {@link boolean}: flag to indicate whether Template is used or not
 *
 */// ww w. j ava  2 s. c om
private void createDom(Section rootSection, boolean asTerminology) {
    doc = new Document();
    ProcessingInstruction instruction;
    ProcessingInstruction alternativeInstruction;
    if (asTerminology) {
        alternativeInstruction = new ProcessingInstruction("xml-stylesheet",
                "type=\"text/xsl\" href=\"odml.xsl\"");
        instruction = new ProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"odmlTerms.xsl\"");
    } else {
        alternativeInstruction = new ProcessingInstruction("xml-stylesheet",
                "type=\"text/xsl\" href=\"odmlTerms.xsl\"");
        instruction = new ProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"odml.xsl\"");
    }
    doc.addContent(instruction);
    doc.addContent(alternativeInstruction);
    Element rootElement = new Element("odML");
    rootElement.setAttribute("version", "1");
    doc.setRootElement(rootElement);

    Section dummyRoot;
    if (rootSection.propertyCount() != 0) {
        dummyRoot = new Section();
        dummyRoot.add(rootSection);
        dummyRoot.setDocumentAuthor(rootSection.getDocumentAuthor());
        dummyRoot.setDocumentDate(rootSection.getDocumentDate());
        dummyRoot.setDocumentVersion(rootSection.getDocumentVersion());
        dummyRoot.setRepository(rootSection.getRepository());
    } else {
        dummyRoot = rootSection;
    }
    String author = dummyRoot.getDocumentAuthor();
    if (author != null) {
        Element authorElement = new Element("author");
        authorElement.setText(author);
        rootElement.addContent(authorElement);
    }
    String version = dummyRoot.getDocumentVersion();
    if (version != null) {
        Element versionElement = new Element("version");
        versionElement.setText(version);
        rootElement.addContent(versionElement);
    }
    String dateString;
    Date date = dummyRoot.getDocumentDate();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    if (date != null) {
        dateString = sdf.format(date);
    } else {
        date = new Date(Calendar.getInstance().getTimeInMillis());
        dateString = sdf.format(date);
    }
    Element dateElement = new Element("date");
    dateElement.setText(dateString);
    rootElement.addContent(dateElement);
    URL repository = dummyRoot.getRepository();
    if (repository != null) {
        Element repElement = new Element("repository");
        repElement.setText(repository.toString());
        rootElement.addContent(repElement);
    }
    for (int i = 0; i < dummyRoot.sectionCount(); i++) {
        appendSection(rootElement, dummyRoot.getSection(i), asTerminology);
    }
}

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  ww w  . j a  v  a2s  .  co  m*/
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;
}

From source file:org.rascalmpl.library.lang.xml.DOM.java

License:Open Source License

private Content nodeToContent(IConstructor n) {
    if (n.getConstructorType() == Factory.Node_element) {
        return nodeToElement(n);
    }//from  w  w  w  . j a v a 2  s  .  co  m
    if (n.getConstructorType() == Factory.Node_pi) {
        IString target = (IString) n.get(0);
        IString data = (IString) n.get(1);
        return new ProcessingInstruction(target.getValue(), data.getValue());

    }
    if (n.getConstructorType() == Factory.Node_charRef) {
        IInteger code = (IInteger) n.get(0);
        int c = java.lang.Integer.parseInt(code.getStringRepresentation());
        return new Text(new java.lang.String(Character.toChars(c)));
    }
    if (n.getConstructorType() == Factory.Node_entityRef) {
        return new EntityRef(((IString) n.get(0)).getValue());
    }

    java.lang.String text = ((IString) n.get(0)).getValue();
    if (n.getConstructorType() == Factory.Node_cdata) {
        return new CDATA(text);
    }
    if (n.getConstructorType() == Factory.Node_charData) {
        return new Text(text);
    }
    if (n.getConstructorType() == Factory.Node_comment) {
        return new Comment(text);
    }

    wellformednessError();
    return null;
}

From source file:projetxml.model.XMLManagement.java

/**
 * Create file that we'll put datas about the serie
 * This method creates also the xsd and the xsl equivalent
 * @param fichier//from w w  w .  j  a  v a2 s .c  o  m
 * @throws FileNotFoundException
 * @throws IOException
 */
public void creerFichier(String fichier) throws FileNotFoundException, IOException {

    Element racine = new Element("episodes");
    documentOutput = new Document(racine);
    //Creation du repertoire de la serie                
    new File(this.pathOutput + "\\" + fichier).mkdir();
    //Ajout des fichier xsl et xsd
    copier(this.pathOutput + "\\output.xsd", this.pathOutput + "\\" + fichier + "\\output.xsd");
    copier(this.pathOutput + "\\output.xsl", this.pathOutput + "\\" + fichier + "\\output.xsl");
    //Ajout de la balise de lien pour le xsl
    Map instructions = new HashMap();
    instructions.put("href", "output.xsl");
    instructions.put("type", "text/xsl");
    ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", instructions);
    documentOutput.getContent().add(0, pi);

    sauverFichier(fichier);
}