Example usage for org.jdom2.output Format setLineSeparator

List of usage examples for org.jdom2.output Format setLineSeparator

Introduction

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

Prototype

public Format setLineSeparator(LineSeparator separator) 

Source Link

Document

This will set the newline separator sequence.

Usage

From source file:com.c4om.utils.xmlutils.JDOMUtils.java

License:Apache License

/**
 * Method that converts a JDOM2 {@link Document} to String
 * @param document the document/*from ww  w.  j  av  a  2  s  .  c om*/
 * @return the string
 */
public static String convertJDOMDocumentToString(Document document) {
    Format xmlFormat = Format.getPrettyFormat();
    xmlFormat.setLineSeparator(LineSeparator.SYSTEM);
    XMLOutputter outputter = new XMLOutputter(xmlFormat);
    String result = outputter.outputString(document);
    return result;
}

From source file:com.github.cat.yum.store.util.YumUtil.java

private static void xmlToFile(Document doc, File outfile) throws IOException {
    FileOutputStream fileOutputStream = null;
    try {/*from  ww w. j a va  2 s  . co m*/
        Format formate = Format.getPrettyFormat();
        formate.setOmitEncoding(true);
        formate.setLineSeparator(LineSeparator.NL);
        // System.out.println(new XMLOutputter(formate).outputString(doc));
        fileOutputStream = new FileOutputStream(outfile, false);
        new XMLOutputter(formate).output(doc, fileOutputStream);
    } finally {
        try {
            if (null != fileOutputStream) {
                fileOutputStream.close();
            }
        } catch (IOException ignore) {

        }
    }
}

From source file:com.googlesource.gerrit.plugins.manifest.CustomOutputter.java

License:Apache License

@Override
public void process(Writer out, Format format, Document doc) throws IOException {
    format.setLineSeparator("\n");
    super.process(out, format, doc);
}

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 {/*from  w ww . j  ava2s  . 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 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 {// w w w  . j ava2s  .c  om
            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.novell.ldapchai.cr.ChaiResponseSet.java

License:Open Source License

static String rsToChaiXML(final ChaiResponseSet rs) throws ChaiValidationException, ChaiOperationException {
    final Element rootElement = new Element(XML_NODE_ROOT);
    rootElement.setAttribute(XML_ATTRIBUTE_MIN_RANDOM_REQUIRED,
            String.valueOf(rs.getChallengeSet().getMinRandomRequired()));
    rootElement.setAttribute(XML_ATTRIBUTE_LOCALE, rs.getChallengeSet().getLocale().toString());
    rootElement.setAttribute(XML_ATTRIBUTE_VERSION, VALUE_VERSION);
    rootElement.setAttribute(XML_ATTRIBUTE_CHAI_VERSION, ChaiConstant.CHAI_API_VERSION);

    if (rs.caseInsensitive) {
        rootElement.setAttribute(XML_ATTRIBUTE_CASE_INSENSITIVE, "true");
    }//from w  w w .ja va 2 s.c om

    if (rs.csIdentifier != null) {
        rootElement.setAttribute(XML_ATTRIBUTE_CHALLENGE_SET_IDENTIFER, rs.csIdentifier);
    }

    if (rs.timestamp != null) {
        rootElement.setAttribute(XML_ATTRIBUTE_TIMESTAMP, DATE_FORMATTER.format(rs.timestamp));
    }

    if (rs.crMap != null) {
        for (final Challenge loopChallenge : rs.crMap.keySet()) {
            final Answer answer = rs.crMap.get(loopChallenge);
            final Element responseElement = challengeToXml(loopChallenge, answer, XML_NODE_RESPONSE);
            rootElement.addContent(responseElement);
        }
    }

    if (rs.helpdeskCrMap != null) {
        for (final Challenge loopChallenge : rs.helpdeskCrMap.keySet()) {
            final Answer answer = rs.helpdeskCrMap.get(loopChallenge);
            final Element responseElement = challengeToXml(loopChallenge, answer, XML_NODE_HELPDESK_RESPONSE);
            rootElement.addContent(responseElement);
        }
    }

    final Document doc = new Document(rootElement);
    final XMLOutputter outputter = new XMLOutputter();
    final Format format = Format.getRawFormat();
    format.setTextMode(Format.TextMode.PRESERVE);
    format.setLineSeparator("");
    outputter.setFormat(format);
    return outputter.outputString(doc);
}

From source file:com.novell.ldapchai.impl.edir.NmasResponseSet.java

License:Open Source License

static String csToNmasXML(final ChallengeSet cs, final String guidValue) {
    final Element rootElement = new Element(NMAS_XML_ROOTNODE);
    rootElement.setAttribute(NMAS_XML_ATTR_RANDOM_COUNT, String.valueOf(cs.getMinRandomRequired()));
    if (guidValue != null) {
        rootElement.setAttribute("GUID", guidValue);
    } else {/*from   w  w w. ja  v a2 s .c o m*/
        rootElement.setAttribute("GUID", "0");
    }

    for (final Challenge challenge : cs.getChallenges()) {
        final Element loopElement = new Element(NMAS_XML_NODE_CHALLENGE);
        if (challenge.getChallengeText() != null) {
            loopElement.setText(challenge.getChallengeText());
        }

        if (challenge.isAdminDefined()) {
            loopElement.setAttribute(NMAS_XML_ATTR_DEFINE, "Admin");
        } else {
            loopElement.setAttribute(NMAS_XML_ATTR_DEFINE, "User");
        }

        if (challenge.isRequired()) {
            loopElement.setAttribute(NMAS_XML_ATTR_TYPE, "Required");
        } else {
            loopElement.setAttribute(NMAS_XML_ATTR_TYPE, "Random");
        }

        loopElement.setAttribute(NMAS_XML_ATTR_MIN_LENGTH, String.valueOf(challenge.getMinLength()));
        loopElement.setAttribute(NMAS_XML_ATTR_MAX_LENGTH, String.valueOf(challenge.getMaxLength()));

        rootElement.addContent(loopElement);
    }

    final XMLOutputter outputter = new XMLOutputter();
    final Format format = Format.getRawFormat();
    format.setTextMode(Format.TextMode.PRESERVE);
    format.setLineSeparator("");
    outputter.setFormat(format);
    return outputter.outputString(rootElement);
}

From source file:es.upm.dit.xsdinferencer.Results.java

License:Apache License

/**
 * Converts a Map<String,Document> to a Map<String,String> with the same keys and String representations 
 * of the documents as values.<br/>
 * The XMLs are generated using the format returned by {@link Format#getPrettyFormat()} and the system line separator. 
 * For more information about this, see {@link Format}.
 * @param inputMap a map between Strings and Documents
 * @return a map between the Strings and the String representation of the documents (null if inputMap is null).
 *//*from  ww  w.  j  av  a2 s .  co  m*/
private Map<String, String> getStringMapFromXMLMap(Map<String, Document> inputMap, TextMode textMode) {
    if (inputMap == null) {
        return null;
    }
    Map<String, String> results = new HashMap<>(inputMap.size());
    Format xmlFormat = Format.getPrettyFormat();
    xmlFormat.setLineSeparator(LineSeparator.SYSTEM);
    //xmlFormat.setTextMode(textMode);
    XMLOutputter outputter = new XMLOutputter(xmlFormat);
    for (String fileName : inputMap.keySet()) {
        Document currentDocument = inputMap.get(fileName);
        String xsdString = outputter.outputString(currentDocument);
        results.put(fileName, xsdString);
    }
    return ImmutableMap.copyOf(results);
}

From source file:jetbrains.buildServer.tools.XmlUtil.java

License:Apache License

public static void saveDocument(@NotNull Document document, @NotNull OutputStream os) throws IOException {
    OutputStreamWriter writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));

    try {/*from  www.ja  va  2 s .c o m*/
        final Format format = Format.getPrettyFormat();
        format.setLineSeparator(System.getProperty("line.separator"));
        format.setEncoding("UTF-8");
        new XMLOutputter(format).output(document, writer);
    } finally {
        writer.flush();
        writer.close();
    }
}

From source file:jodtemplate.util.JDOMHelper.java

License:Apache License

public String getRawContents(final Document dom) throws IOException {
    final Format format = Format.getRawFormat();
    format.setLineSeparator(LineSeparator.UNIX);
    final XMLOutputter outputter = new XMLOutputter(format, new StandaloneOutputProcessor());
    final Writer writer = new StringWriter();
    outputter.output(dom, writer);//from  w w  w  . jav a 2  s  . c o m
    return writer.toString();
}