Example usage for org.jdom2.output Format getPrettyFormat

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

Introduction

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

Prototype

public static Format getPrettyFormat() 

Source Link

Document

Returns a new Format object that performs whitespace beautification with 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy.

Usage

From source file:org.kitodo.docket.ExportXmlLog.java

License:Open Source License

/**
 * This method exports the production metadata for al list of processes as a
 * single file to a given stream.//w w  w . j  a v a 2 s.  c  o  m
 *
 * @param docketDataList
 *            a list of Docket data
 * @param outputStream
 *            The output stream, to write the docket to.
 */

void startMultipleExport(Iterable<DocketData> docketDataList, OutputStream outputStream) {
    Document answer = new Document();
    Element root = new Element("processes");
    answer.setRootElement(root);
    Namespace xmlns = Namespace.getNamespace(NAMESPACE);

    Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.addNamespaceDeclaration(xsi);
    root.setNamespace(xmlns);
    Attribute attSchema = new Attribute("schemaLocation", NAMESPACE + " XML-logfile.xsd", xsi);
    root.setAttribute(attSchema);
    for (DocketData docketData : docketDataList) {
        Document doc = createDocument(docketData, false);
        Element processRoot = doc.getRootElement();
        processRoot.detach();
        root.addContent(processRoot);
    }

    XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat());

    try {
        outp.output(answer, outputStream);
    } catch (IOException e) {
        logger.error("Generating XML Output failed.", e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                logger.error("Closing the output stream failed.", e);
            }
        }
    }

}

From source file:org.lendingclub.mercator.ucs.UCSClient.java

License:Apache License

public void logDebug(String message, Element element) {

    if (logger.isDebugEnabled()) {
        XMLOutputter out = new XMLOutputter();

        out.setFormat(Format.getPrettyFormat());

        element = element.clone();/*w  w  w  .  jav a2 s  . c o m*/
        if (!Strings.isNullOrEmpty(element.getAttributeValue("outCookie"))) {
            element.setAttribute("outCookie", "**********");
        }
        if (!Strings.isNullOrEmpty(element.getAttributeValue("cookie"))) {
            element.setAttribute("cookie", "**********");
        }
        logger.debug(message + "\n{}", out.outputString(element));
    }
}

From source file:org.mule.tools.apikit.output.MuleConfigGenerator.java

License:Open Source License

public void generate() {
    Map<API, Document> docs = new HashMap<API, Document>();

    for (GenerationModel flowEntry : flowEntries) {
        Document doc;/*from w ww  .j a v  a2s  . c  o  m*/

        API api = flowEntry.getApi();
        try {
            doc = getOrCreateDocument(docs, api);
        } catch (Exception e) {
            log.error("Error generating xml for file: [" + api.getYamlFile() + "]", e);
            continue;
        }

        // Generate each of the APIKit flows
        doc.getRootElement().addContent(new APIKitFlowScope(flowEntry).generate());
    }

    // Write everything to files
    for (Map.Entry<API, Document> yamlFileDescriptorDocumentEntry : docs.entrySet()) {
        Format prettyFormat = Format.getPrettyFormat();
        prettyFormat.setIndent(INDENTATION);
        prettyFormat.setLineSeparator(System.getProperty("line.separator"));
        prettyFormat.setEncoding("UTF-8");
        XMLOutputter xout = new XMLOutputter(prettyFormat);
        Document doc = yamlFileDescriptorDocumentEntry.getValue();
        File xmlFile = yamlFileDescriptorDocumentEntry.getKey().getXmlFile(rootDirectory);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(xmlFile);
            xout.output(doc, fileOutputStream);
            fileOutputStream.close();
            log.info("Updating file: [" + xmlFile + "]");
        } catch (IOException e) {
            log.error("Error writing to file: [" + xmlFile + "]", e);
        }
    }

    // Generate mule deploy properties file
    new MuleDeployWriter(rootDirectory).generate();
}

From source file:org.mycore.common.content.transformer.MCRToPrettyXML.java

License:Open Source License

@Override
public MCRContent transform(MCRContent source) throws IOException {
    MCRXMLContent content;// ww  w.  ja v a  2  s .  c om
    try {
        content = (source instanceof MCRXMLContent ? (MCRXMLContent) source
                : new MCRJDOMContent(source.asXML()));
    } catch (JDOMException | SAXException e) {
        throw new IOException(e);
    }
    if (content != source) {
        content.setName(source.getName());
        content.setLastModified(source.lastModified());
    }
    content.setFormat(Format.getPrettyFormat().setEncoding(getEncoding()));
    return content;
}

From source file:org.mycore.common.MCRMailer.java

License:Open Source License

/** Outputs xml to the LOGGER for debugging */
private static void debug(Element xml) {
    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
    LOGGER.debug(delimiter + xout.outputString(xml) + delimiter);
}

From source file:org.mycore.common.MCRUtils.java

License:Open Source License

/**
 * The method write a given JDOM Document to a file.
 * /*from  www  .  j  a v  a  2  s.c  om*/
 * @param jdom
 *            the JDOM Document
 * @param xml
 *            the File instance
 * @deprecated use {@link MCRJDOMContent#sendTo(Path, java.nio.file.CopyOption...)}
 */
@Deprecated
public static void writeJDOMToFile(Document jdom, File xml) {
    try {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(xml));
        xout.output(jdom, out);
        out.close();
    } catch (IOException ioe) {
        if (LOGGER.isDebugEnabled()) {
            ioe.printStackTrace();
        } else {
            LOGGER.error("Can't write org.jdom2.Document to file " + xml.getName() + ".");
        }
    }
}

From source file:org.mycore.common.MCRUtils.java

License:Open Source License

/**
 * The method write a given JDOM Document to the system output.
 * //w w w  .  j av a2s .com
 * @param jdom
 *            the JDOM Document
 * @deprecated use {@link MCRJDOMContent#sendTo(java.io.OutputStream)}
 */
@Deprecated
public static void writeJDOMToSysout(Document jdom) {
    try {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        BufferedOutputStream out = new BufferedOutputStream(System.out);
        xout.output(jdom, out);
        out.flush();
    } catch (IOException ioe) {
        if (LOGGER.isDebugEnabled()) {
            ioe.printStackTrace();
        } else {
            LOGGER.error("Can't write org.jdom2.Document to Sysout.");
        }
    }
}

From source file:org.mycore.common.MCRUtils.java

License:Open Source License

/**
 * Transforms the given {@link Document} into a String
 * /* w  ww . j a v a2s .c o  m*/
 * @return the xml document as {@link String} or null if an {@link Exception} occurs
 * @deprecated use {@link XMLOutputter} directly
 */
@Deprecated
public static String asString(Document doc) {
    return new XMLOutputter(Format.getPrettyFormat()).outputString(doc);
}

From source file:org.mycore.common.MCRUtils.java

License:Open Source License

/**
 * Transforms the given Element into a String
 * //  www.j av  a2  s  .  c o m
 * @return the xml element as {@link String}
 * @deprecated use {@link XMLOutputter} directly
 */
@Deprecated
public static String asString(Element elm) {
    return new XMLOutputter(Format.getPrettyFormat()).outputString(elm);
}

From source file:org.mycore.datamodel.metadata.MCRObjectMetadata.java

License:Open Source License

/**
 * This method adds MCRMetaElement's from a given MCRObjectMetadata to
 * this data set if there are any differences between the data sets.
 * // w ww .  ja va  2  s. c  o  m
 * @param input
 *            the MCRObjectMetadata, that should merged into this data set
 *            
 * @deprecated use {@link MCRObjectMerger#mergeMetadata(MCRObject, boolean)} instead
 */
public final void mergeMetadata(MCRObjectMetadata input) {

    for (MCRMetaElement metaElement : input) {
        int pos = -1;
        for (int j = 0; j < size(); j++) {
            if (meta_list.get(j).getTag().equals(metaElement.getTag())) {
                pos = j;
            }
        }
        if (pos != -1) {
            for (int j = 0; j < metaElement.size(); j++) {
                boolean found = false;
                for (MCRMetaInterface mcrMetaInterface : meta_list.get(pos)) {
                    Element xml = mcrMetaInterface.createXML();
                    Element xmlNEW = metaElement.getElement(j).createXML();
                    List<Element> childrenXML = xml.getChildren();
                    if (childrenXML.size() > 0 && xmlNEW.getChildren().size() > 0) {
                        int i = 0;
                        for (Element element : childrenXML) {
                            Element elementNew = xmlNEW.getChild(element.getName());

                            if (elementNew != null && element != null) {
                                if (element.getText().equals(elementNew.getText())) {
                                    i++;
                                }
                            }
                        }
                        if (i == childrenXML.size()) {
                            found = true;
                        }
                    } else {
                        if (xml.getText().equals(xmlNEW.getText())) {
                            found = true;
                        } else if (!found) {
                            int i = 0;
                            List<Attribute> attributes = xml.getAttributes();
                            for (Attribute attribute : attributes) {
                                Attribute attr = xmlNEW.getAttribute(attribute.getName());
                                if ((attr != null) && attr.equals(attribute)) {
                                    i++;
                                }
                            }
                            if (i == attributes.size()) {
                                found = true;
                            }

                        }
                    }
                }
                MCRMetaInterface obj = metaElement.getElement(j);
                if (!found) {
                    meta_list.get(pos).addMetaObject(obj);
                } else if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Found equal tags: \n\r"
                            + new XMLOutputter(Format.getPrettyFormat()).outputString(obj.createXML()));
                }
            }
        } else {
            meta_list.add(metaElement);
        }
    }
}