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(XMLOutputProcessor processor) 

Source Link

Document

This will create an XMLOutputter with the specified XMLOutputProcessor.

Usage

From source file:com.izforge.izpack.util.xmlmerge.merge.DefaultXmlMerge.java

License:Open Source License

@Override
public InputStream merge(InputStream[] sources) throws AbstractXmlMergeException {
    SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
    // Xerces-specific - see: http://xerces.apache.org/xerces-j/features.html
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    Document[] docs = new Document[sources.length];

    for (int i = 0; i < sources.length; i++) {
        try {/* w  w w  . j a  v  a2s.c  o  m*/
            docs[i] = builder.build(sources[i]);
        } catch (Exception e) {
            throw new ParseException(e);
        }
    }

    Document result = doMerge(docs);

    Format prettyFormatter = Format.getPrettyFormat();
    // Use system line seperator to avoid problems
    // with carriage return under linux
    prettyFormatter.setLineSeparator(System.getProperty("line.separator"));
    XMLOutputter sortie = new XMLOutputter(prettyFormatter);

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    try {
        sortie.output(result, buffer);
    } catch (IOException ex) {
        throw new DocumentException(result, ex);
    }

    return new ByteArrayInputStream(buffer.toByteArray());
}

From source file:com.izforge.izpack.util.xmlmerge.merge.DefaultXmlMerge.java

License:Open Source License

@Override
public void merge(File[] sources, File target) throws AbstractXmlMergeException {
    SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
    // Xerces-specific - see: http://xerces.apache.org/xerces-j/features.html
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    Document[] docs = new Document[sources.length];

    for (int i = 0; i < sources.length; i++) {
        try {/*  ww  w .  ja va  2  s.  co  m*/
            docs[i] = builder.build(sources[i]);
        } catch (Exception e) {
            throw new ParseException(e);
        }
    }

    Document result = doMerge(docs);

    Format prettyFormatter = Format.getPrettyFormat();
    // Use system line separator to avoid problems
    // with carriage return under linux
    prettyFormatter.setLineSeparator(System.getProperty("line.separator"));
    XMLOutputter sortie = new XMLOutputter(prettyFormatter);

    try {
        sortie.output(result, new FileOutputStream(target));
    } catch (IOException ex) {
        throw new DocumentException(result, ex);
    }
}

From source file:com.khodev.oradiff.io.XmlSchemaWriter.java

License:Open Source License

public void write(Schema schema) {
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
    XmlSchemaWriter writer = new XmlSchemaWriter(filename.replaceAll(".ini", "") + "_autosave.xml");
    try {//from  w  w  w  .  jav a  2  s .  c o  m
        output.output(getXml(schema), new FileOutputStream(filename));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.lapis.jsfexporter.xml.XMLExportType.java

License:Apache License

@Override
public void writeExport(ExternalContext externalContext) throws Exception {
    externalContext.setResponseCharacterEncoding("UTF-8");
    new XMLOutputter(Format.getPrettyFormat()).output(document, externalContext.getResponseOutputStream());
}

From source file:com.medvision360.medrecord.basex.AbstractXmlConverter.java

License:Creative Commons License

protected void outputDocument(Document d, OutputStream os, String encoding) throws IOException {
    Format format = Format.getPrettyFormat();
    format.setTextMode(Format.TextMode.PRESERVE); // this is the default, set just to be very safe
    format.setEncoding(encoding);/*from   w  w w  .j  ava  2 s  . co m*/
    XMLOutputter output = new XMLOutputter(format);
    output.output(d, os);
}

From source file:com.medvision360.medrecord.basex.MockLocatableSerializer.java

License:Creative Commons License

@Override
public void serialize(Locatable locatable, OutputStream os, String encoding) throws IOException {
    String rmEntity = locatable.getArchetypeDetails().getArchetypeId().rmEntity();
    Element root = new Element(rmEntity);
    root.setAttribute("archetype_node_id", locatable.getArchetypeNodeId());

    set(root, "/uid/value", locatable.getUid().getValue());
    set(root, "/archetype_id/value", locatable.getArchetypeDetails().getArchetypeId().getValue());
    set(root, "/name/value", locatable.getName().getValue());

    Document d = new Document(root);

    Format format = Format.getPrettyFormat();
    format.setEncoding(encoding);//from   w w  w.  j a v  a 2 s.c  om
    XMLOutputter output = new XMLOutputter(format);
    output.output(d, os);
}

From source file:com.rometools.rome.io.WireFeedOutput.java

License:Open Source License

/**
 * Creates a String with the XML representation for the given WireFeed.
 * <p>//from   ww  w.  j  av a 2  s .c o m
 * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is
 * the responsibility of the developer to ensure that if the String is written to a character
 * stream the stream charset is the same as the feed encoding property.
 * <p>
 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
 * <p>
 *
 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must
 *            match the type given to the FeedOuptut constructor.
 * @param prettyPrint pretty-print XML (true) oder collapsed
 * @return a String with the XML representation for the given WireFeed.
 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed
 *             don't match.
 * @throws FeedException thrown if the XML representation for the feed could not be created.
 *
 */
public String outputString(final WireFeed feed, final boolean prettyPrint)
        throws IllegalArgumentException, FeedException {
    final Document doc = outputJDom(feed);
    final String encoding = feed.getEncoding();
    Format format;
    if (prettyPrint) {
        format = Format.getPrettyFormat();
    } else {
        format = Format.getCompactFormat();
    }
    if (encoding != null) {
        format.setEncoding(encoding);
    }
    final XMLOutputter outputter = new XMLOutputter(format);
    return outputter.outputString(doc);
}

From source file:com.rometools.rome.io.WireFeedOutput.java

License:Open Source License

/**
 * Writes to an Writer the XML representation for the given WireFeed.
 * <p>//from www .ja  v  a2 s . c  o m
 * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is
 * the responsibility of the developer to ensure the Writer instance is using the same charset
 * encoding.
 * <p>
 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
 * <p>
 *
 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must
 *            match the type given to the FeedOuptut constructor.
 * @param writer Writer to write the XML representation for the given WireFeed.
 * @param prettyPrint pretty-print XML (true) oder collapsed
 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed
 *             don't match.
 * @throws IOException thrown if there was some problem writing to the Writer.
 * @throws FeedException thrown if the XML representation for the feed could not be created.
 *
 */
public void output(final WireFeed feed, final Writer writer, final boolean prettyPrint)
        throws IllegalArgumentException, IOException, FeedException {
    final Document doc = outputJDom(feed);
    final String encoding = feed.getEncoding();
    Format format;
    if (prettyPrint) {
        format = Format.getPrettyFormat();
    } else {
        format = Format.getCompactFormat();
    }
    if (encoding != null) {
        format.setEncoding(encoding);
    }
    final XMLOutputter outputter = new XMLOutputter(format);
    outputter.output(doc, writer);
}

From source file:com.soulgalore.web.pagesavings.reporters.XMLReporter.java

License:Apache License

public void report(Set<SiteResult> results, Map<String, DescriptiveStatistics> statistics) {

    Date reportDate = new Date();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    Element root = new Element("savings");
    root.setAttribute("date", "" + reportDate);

    Element resultsXML = new Element("results");
    root.addContent(resultsXML);//from  www . jav  a 2  s  . com

    Double totalPw = 0D;
    Double totalUnique = 0D;
    for (SiteResult siteResult : results) {
        totalPw += siteResult.getTotalSavings() * siteResult.getSite().getPageViews();
        totalUnique += siteResult.getTotalSavings() * siteResult.getSite().getUniqueBrowsers();
        Element site = new Element("site");
        Element url = new Element("url");
        Element weight = new Element("total-page-weight");
        Element savingsPerPage = new Element("savings-per-page");
        Element savingsForPW = new Element("savings-for-page-view");
        Element savingsForUnique = new Element("savings-for-unique-browsers");
        Element savingsPercentage = new Element("savings-percentage");

        url.addContent(new CDATA(siteResult.getSite().getUrl()));

        savingsPerPage.setAttribute(READABLE,
                humanReadableByteCount(Math.round(siteResult.getTotalSavings()), true));
        savingsPerPage.setAttribute(KB, "" + Math.round(siteResult.getTotalSavings()));

        savingsForPW.setAttribute(READABLE, humanReadableByteCount(
                Math.round(siteResult.getTotalSavings() * siteResult.getSite().getPageViews()), true));
        savingsForPW.setAttribute(KB,
                "" + Math.round(siteResult.getTotalSavings() * siteResult.getSite().getPageViews()));

        savingsForUnique.setAttribute(READABLE, humanReadableByteCount(
                Math.round(siteResult.getTotalSavings() * siteResult.getSite().getUniqueBrowsers()), true));
        savingsForUnique.setAttribute(KB,
                "" + Math.round(siteResult.getTotalSavings() * siteResult.getSite().getUniqueBrowsers()));

        savingsPercentage.addContent("" + Math.round(siteResult.getSavingsPercentage() * 100) / 100.0d);

        weight.setAttribute(READABLE, humanReadableByteCount(siteResult.getTotalSizeBytes() / 1000, true));
        weight.setAttribute(KB, "" + siteResult.getTotalSizeBytes() / 1000);

        Element rulesSavings = new Element("rule-savings");
        for (RuleResult ruleResult : siteResult.getResults()) {
            Element rule = new Element(ruleResult.getRule());
            rule.addContent("" + ruleResult.getSavings());
            rulesSavings.addContent(rule);
        }

        site.addContent(url);
        site.addContent(weight);
        site.addContent(savingsPerPage);
        site.addContent(savingsForPW);
        site.addContent(savingsForUnique);
        site.addContent(savingsPercentage);
        site.addContent(rulesSavings);
        resultsXML.addContent(site);
    }

    Element summary = new Element("summary");
    summary.setAttribute("nrofsites", "" + results.size());
    Element totalPW = new Element("total-pw");
    Element totalUniqueSummary = new Element("total-unique");
    totalPW.setAttribute(READABLE, humanReadableByteCount(Math.round(totalPw), true));
    totalPW.setAttribute(KB, "" + Math.round(totalPw));
    totalUniqueSummary.setAttribute(READABLE, humanReadableByteCount(Math.round(totalUnique), true));
    totalUniqueSummary.setAttribute(KB, "" + Math.round(totalUnique));
    summary.addContent(totalPW);
    summary.addContent(totalUniqueSummary);

    for (String rule : statistics.keySet()) {
        DescriptiveStatistics stats = statistics.get(rule);
        Element ruleElement = new Element(rule);
        Element max = new Element("max");
        Element median = new Element("median");
        max.addContent("" + stats.getMax());
        median.addContent("" + stats.getPercentile(50));
        ruleElement.addContent(max);
        ruleElement.addContent(median);
        summary.addContent(ruleElement);
    }

    resultsXML.addContent(summary);

    Document doc = new Document(root);
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());

    try {
        FileWriter writer = new FileWriter("report-" + df.format(reportDate) + ".xml");
        outputter.output(doc, writer);
    } catch (Exception e) {
    }
}

From source file:com.sun.syndication.io.WireFeedOutput.java

License:Open Source License

/**
 * Creates a String with the XML representation for the given WireFeed.
 * <p>/*from w ww  . j a  v  a 2  s . c o  m*/
 * If the feed encoding is not NULL, it will be used in the XML prolog encoding attribute. It is the responsibility
 * of the developer to ensure that if the String is written to a character stream the stream charset is the same as
 * the feed encoding property.
 * <p>
 * NOTE: This method delages to the 'Document WireFeedOutput#outputJDom(WireFeed)'.
 * <p>
 * @param feed Abstract feed to create XML representation from. The type of the WireFeed must match
 *        the type given to the FeedOuptut constructor.
 * @param prettyPrint pretty-print XML (true) oder collapsed
 * @return a String with the XML representation for the given WireFeed.
 * @throws IllegalArgumentException thrown if the feed type of the WireFeedOutput and WireFeed don't match.
 * @throws FeedException thrown if the XML representation for the feed could not be created.
 *
 */
public String outputString(WireFeed feed, boolean prettyPrint) throws IllegalArgumentException, FeedException {
    Document doc = outputJDom(feed);
    String encoding = feed.getEncoding();
    Format format = prettyPrint ? Format.getPrettyFormat() : Format.getCompactFormat();
    if (encoding != null) {
        format.setEncoding(encoding);
    }
    XMLOutputter outputter = new XMLOutputter(format);
    return outputter.outputString(doc);
}