Example usage for org.jdom2.output XMLOutputter XMLOutputter

List of usage examples for org.jdom2.output XMLOutputter XMLOutputter

Introduction

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

Prototype

public XMLOutputter() 

Source Link

Document

This will create an XMLOutputter with a default Format and XMLOutputProcessor .

Usage

From source file:at.ac.tuwien.ims.latex2mobiformulaconv.converter.Converter.java

License:Open Source License

/**
 * Saves the html document to a file with .html extension
 *
 * @param document JDOM Document representing the HTML
 * @return written HTML File object/*from   w w w  .j a  v a  2 s.  c o m*/
 */
private File saveHtmlFile(Document document) {
    Path tempFilepath = null;

    Path tempDirPath = formulaConverter.getTempDirPath();

    ClassLoader classLoader = getClass().getClassLoader();
    InputStream mainCssIs = classLoader.getResourceAsStream(MAIN_CSS_FILENAME);

    logger.debug("Copying main.css file to temp dir: " + tempDirPath.toAbsolutePath().toString());
    try {
        Files.copy(mainCssIs, tempDirPath.resolve(MAIN_CSS_FILENAME));
    } catch (FileAlreadyExistsException e) {
        // do nothing
    } catch (IOException e) {
        logger.error("could not copy main.css file to temp dir!");
    }

    tempFilepath = tempDirPath.resolve("latex2mobi.html");

    logger.debug("tempFile created at: " + tempFilepath.toAbsolutePath().toString());
    try {
        Files.write(tempFilepath, new XMLOutputter().outputString(document).getBytes(Charset.forName("UTF-8")));

        if (debugMarkupOutput) {
            logger.info("Debug markup will be generated.");
        }

    } catch (IOException e) {
        logger.error("Error writing HTML to temp dir!");
        logger.error(e.getMessage(), e);
    }

    return tempFilepath.toFile();
}

From source file:at.ac.tuwien.ims.latex2mobiformulaconv.converter.mathml2html.FormulaConverter.java

License:Open Source License

/**
 * Replaces all formulas with the html representation of the mapped formula objects
 *
 * @param doc        JDOM Document where to replace the formulas
 * @param formulaMap Map of the indexed Formula Objects
 * @return JDOM Document with replaced formulas
 *///ww  w.jav a 2s. c  om
public Document replaceFormulas(Document doc, Map<Integer, Formula> formulaMap) {
    List<Element> foundFormulas = xpath.evaluate(doc);

    if (foundFormulas.size() > 0) {
        Map<String, Element> formulaMarkupMap = new HashMap<>();

        // Initialize markup map
        for (Element element : foundFormulas) {
            formulaMarkupMap.put(element.getAttribute("id").getValue(), element);
        }

        // Replace all found formulas
        Iterator<Integer> formulaIterator = formulaMap.keySet().iterator();
        while (formulaIterator.hasNext()) {
            Integer id = formulaIterator.next();

            Element formulaMarkupRoot = formulaMarkupMap.get(FORMULA_ID_PREFIX + id);
            Formula formula = formulaMap.get(id);

            formulaMarkupRoot.removeAttribute("class");
            formulaMarkupRoot.removeContent();
            formulaMarkupRoot.setName("div");

            Element div = (Element) formulaMarkupRoot.getParent();
            div.setName("div");
            div.setAttribute("class", "formula");

            // Potentially there's text inside the paragraph...
            List<Text> texts = div.getContent(Filters.textOnly());
            if (texts.isEmpty() == false) {
                String textString = "";
                for (Text text : texts) {
                    textString += text.getText();
                }
                Element textSpan = new Element("span");
                textSpan.setAttribute("class", "text");
                textSpan.setText(textString);
                div.addContent(textSpan);

                List<Content> content = div.getContent();
                content.removeAll(texts);
            }

            if (generateDebugMarkup) {
                div.setAttribute("style", "border: 1px solid black;");

                // Header
                Element h4 = new Element("h4");
                h4.setText("DEBUG - Formula #" + formula.getId());
                div.addContent(h4);

                // Render LaTeX source
                Element latexPre = new Element("pre");
                latexPre.setAttribute("class", "debug-latex");
                latexPre.setText(formula.getLatexCode());
                div.addContent(latexPre);

                // Render MathML markup
                Element mathmlPre = new Element("pre");
                mathmlPre.setAttribute("class", "debug-mathml");
                mathmlPre.setText(formula.getMathMl());
                div.addContent(mathmlPre);

                // Render HTML Markup
                Element htmlPre = new Element("pre");
                htmlPre.setAttribute("class", "debug-html");
                XMLOutputter xmlOutputter = new XMLOutputter();
                xmlOutputter.setFormat(Format.getRawFormat());
                htmlPre.setText(xmlOutputter.outputString(formula.getHtml()));

                div.addContent(htmlPre);

            }

            // Set formula into
            formulaMarkupRoot.addContent(formula.getHtml());
        }
    }
    return doc;
}

From source file:at.newmedialab.ldpath.model.functions.XPathFunction.java

License:Apache License

private LinkedList<String> doFilter(String in, Set<String> xpaths) throws IOException {
    LinkedList<String> result = new LinkedList<String>();
    try {//from  www .  j ava 2 s .c  o  m
        Document doc = new SAXBuilder(XMLReaders.NONVALIDATING).build(new StringReader(in));
        XMLOutputter out = new XMLOutputter();

        for (String xp : xpaths) {
            XPathExpression<Content> xpath = XPathFactory.instance().compile(xp, Filters.content());
            for (Content node : xpath.evaluate(doc)) {
                if (node instanceof Element)
                    result.add(out.outputString((Element) node));
                else if (node instanceof Text)
                    result.add(out.outputString((Text) node));
            }
        }
        return result;
    } catch (JDOMException xpe) {
        throw new IllegalArgumentException("error while processing xpath expressions: '" + xpaths + "'", xpe);
    }
}

From source file:backtesting.BackTesterNinety.java

private static void SaveSettings(BTSettings settings) {
    BufferedWriter output = null;
    try {/*w ww.  j  a v a2s. com*/
        Element rootElement = new Element("Settings");
        Document doc = new Document(rootElement);
        rootElement.setAttribute("start", settings.startDate.toString());
        rootElement.setAttribute("end", settings.endDate.toString());

        rootElement.setAttribute("capital", Double.toString(settings.capital));
        rootElement.setAttribute("leverage", Double.toString(settings.leverage));

        rootElement.setAttribute("reinvest", Boolean.toString(settings.reinvest));

        XMLOutputter xmlOutput = new XMLOutputter();

        File fileSettings = new File("backtest/cache/_settings.xml");
        fileSettings.createNewFile();
        FileOutputStream oFile = new FileOutputStream(fileSettings, false);

        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, oFile);

    } catch (IOException ex) {
        Logger.getLogger(BackTesterNinety.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException ex) {
                logger.log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:br.com.nfe.util.Chave.java

public void criarChave(String uf, String tpEmis, String nNfe, String cNf, String serie, String AnoMes,
        String cnpj) {//from w  w  w  .  j a  v  a2 s. co m
    this.cNf = cNf;
    Element gerarChave = new Element("gerarChave");
    Document documento = new Document(gerarChave);
    Element UF = new Element("UF");
    UF.setText(uf);

    Element NNFE = new Element("nNF");
    NNFE.setText(nNfe);

    Element CNF = new Element("cNF");
    CNF.setText(cNf);

    Element TPEMIS = new Element("tpEmis");
    TPEMIS.setText(tpEmis);

    Element SERIE = new Element("serie");
    SERIE.setText(serie);

    Element ANOMES = new Element("AAMM");
    ANOMES.setText(AnoMes);

    Element CNPJ = new Element("CNPJ");
    CNPJ.setText(cnpj);

    gerarChave.addContent(UF);
    gerarChave.addContent(TPEMIS);
    gerarChave.addContent(NNFE);
    //gerarChave.addContent(CNF);
    gerarChave.addContent(SERIE);
    gerarChave.addContent(ANOMES);
    gerarChave.addContent(CNPJ);
    XMLOutputter xout = new XMLOutputter();
    try {

        FileWriter arquivo = new FileWriter(
                new File("c:/unimake/uninfe/" + pasta + "/envio/" + cNf + "-gerar-chave.xml"));

        xout.output(documento, arquivo);

    } catch (IOException e) {

        e.printStackTrace();

    }

}

From source file:ca.nrc.cadc.ac.xml.AbstractReaderWriter.java

License:Open Source License

/**
 * Write to root Element to a writer./*from   ww  w. ja v  a2  s.co  m*/
 *
 * @param root Root Element to write.
 * @param writer Writer to write to.
 * @throws IOException if the writer fails to write.
 */
protected void write(Element root, Writer writer) throws IOException {
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    outputter.output(new Document(root), writer);
}

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

License:Open Source License

public void write(ArtifactAccess aa, Writer writer) throws IOException {
    Element root = new Element(ArtifactAccessReader.ENAMES.artifactAccess.name());

    Element ae = new Element(ArtifactAccessReader.ENAMES.artifact.name());
    root.addContent(ae);/*from   w  ww  .ja v a2 s . co  m*/

    addChild(ae, ArtifactAccessReader.ENAMES.uri.name(), aa.getArtifact().getURI().toASCIIString());
    addChild(ae, ArtifactAccessReader.ENAMES.productType.name(), aa.getArtifact().getProductType().getValue());
    addChild(ae, ArtifactAccessReader.ENAMES.releaseType.name(), aa.getArtifact().getReleaseType().getValue());
    if (aa.getArtifact().contentChecksum != null) {
        addChild(ae, ArtifactAccessReader.ENAMES.contentChecksum.name(),
                aa.getArtifact().contentChecksum.toASCIIString());
    }
    if (aa.getArtifact().contentLength != null) {
        addChild(ae, ArtifactAccessReader.ENAMES.contentLength.name(),
                aa.getArtifact().contentLength.toString());
    }
    if (aa.getArtifact().contentType != null) {
        addChild(ae, ArtifactAccessReader.ENAMES.contentType.name(), aa.getArtifact().contentType);
    }

    Element pub = new Element(ArtifactAccessReader.ENAMES.isPublic.name());
    pub.setText(Boolean.toString(aa.isPublic));
    root.addContent(pub);

    Element rg = new Element(ArtifactAccessReader.ENAMES.readGroups.name());
    if (!aa.getReadGroups().isEmpty() || writeEmptyCollections) {
        root.addContent(rg);
    }
    addGroups(aa.getReadGroups(), rg);

    Document doc = new Document(root);
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    outputter.output(doc, writer);
}

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

License:Open Source License

/**
 * Write the root Element to a writer.//ww  w . j av a  2  s .c  o m
 *
 * @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:ca.nrc.cadc.conformance.uws.Util.java

License:Open Source License

/**
 * Get the XML string of an XML Document object.
 *  /* www.  j a  va2 s  .co  m*/
 * @param document
 * @return String
 * @author zhangsa
 */
public static String getXmlString(Document document) {
    XMLOutputter xmlOutputter = new XMLOutputter();
    return xmlOutputter.outputString(document);
}

From source file:ca.nrc.cadc.dali.tables.votable.VOTableWriter.java

License:Open Source License

/**
 * Write the VOTable to the specified Writer, only writing maxrec rows.
 * If the VOTable contains more than maxrec rows, appends an INFO element with
 * name="QUERY_STATUS" value="OVERFLOW" to the VOTable.
 *
 * @param votable VOTable object to write.
 * @param writer Writer to write to./*from   w ww. j a v a 2  s.  c  o m*/
 * @param maxRec maximum number of rows to write.
 * @throws IOException if problem writing to the writer.
 */
public void write(VOTable votable, Writer writer, Long maxrec) throws IOException {
    log.debug("write, maxrec=" + maxrec);

    // VOTable document and root element.
    Document document = createDocument();
    Element root = document.getRootElement();
    Namespace namespace = root.getNamespace();

    // Create the RESOURCE element and add to the VOTABLE element.
    Element resource = new Element("RESOURCE", namespace);
    if (votable.getResourceName() != null) {
        resource.setAttribute("name", votable.getResourceName());
    }
    root.addContent(resource);

    // Create the INFO element and add to the RESOURCE element.
    for (Info in : votable.getInfos()) {
        Element info = new Element("INFO", namespace);
        info.setAttribute("name", in.getName());
        info.setAttribute("value", in.getValue());
        resource.addContent(info);
    }

    // Create the TABLE element and add to the RESOURCE element.
    Element table = new Element("TABLE", namespace);
    resource.addContent(table);

    // Add the metadata elements.
    for (TableParam param : votable.getParams()) {
        table.addContent(new ParamElement(param, namespace));
    }
    for (TableField field : votable.getColumns()) {
        table.addContent(new FieldElement(field, namespace));
    }

    // Create the DATA and TABLEDATA elements.
    Element data = new Element("DATA", namespace);
    table.addContent(data);

    // Add content.
    try {
        Iterator<List<Object>> it = votable.getTableData().iterator();

        TabledataContentConverter elementConverter = new TabledataContentConverter(namespace);
        TabledataMaxIterations maxIterations = new TabledataMaxIterations(maxrec, resource, namespace);

        IterableContent<Element, List<Object>> tabledata = new IterableContent<Element, List<Object>>(
                "TABLEDATA", namespace, it, elementConverter, maxIterations);

        data.addContent(tabledata);
    } catch (Throwable t) {
        log.debug("failure while iterating", t);
        Element info = new Element("INFO", namespace);
        info.setAttribute("name", "QUERY_STATUS");
        info.setAttribute("value", "ERROR");
        info.setText(t.toString());
        resource.addContent(info);
    }

    // Write out the VOTABLE.
    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(org.jdom2.output.Format.getPrettyFormat());
    outputter.output(document, writer);
}