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.olanto.myterm.export.JdomUtilities.java

License:Open Source License

/**
 * affiche le texte du xml dans la console
 *
 * @param document xml/*from ww w. j a  v  a  2 s .c  o  m*/
 */
static void showXML(Document document) {
    try {
        //On utilise ici un affichage classique avec getPrettyFormat()
        XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
        sortie.output(document, System.out);
    } catch (java.io.IOException e) {
    }
}

From source file:org.olanto.myterm.export.JdomUtilities.java

License:Open Source License

/**
 * copie le texte du xml dans un fichier
 *
 * @param document xml/*www. j  a  v  a  2 s.co m*/
 * @param namefile nom du fichier
 */
static void saveXML(Document document, String namefile) {
    try {
        msg("export file in: " + namefile);
        XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
        sortie.output(document, new FileOutputStream(namefile));
    } catch (java.io.IOException e) {
    }
}

From source file:org.onosproject.provider.netconf.device.impl.NetconfDevice.java

License:Apache License

private void printPrettyXML(String xmlstring) {
    try {/*from www  .j a va  2  s  . c  om*/
        Document doc = new SAXBuilder().build(new StringReader(xmlstring));
        XMLOutputter xmOut = new XMLOutputter(Format.getPrettyFormat());
        String outputString = xmOut.outputString(doc);
        log.debug(outputString);
    } catch (Exception e) {
        log.error("ERROR while parsing the XML " + xmlstring, e);

    }
}

From source file:org.onosproject.provider.netconf.flow.impl.XmlBuilder.java

License:Apache License

public String buildAclRequestXml(AccessList accessList) {
    Document doc = new Document();
    Namespace namespaceRpc = Namespace.getNamespace("urn:ietf:params:xml:ns:netconf:base:1.0");
    Namespace accessNamespaceRpc = Namespace.getNamespace("urn:ietf:params:xml:ns:yang:ietf-acl");
    doc.setRootElement(new Element("rpc", namespaceRpc).setAttribute("message-id", "101"));

    /**/*from w w  w .  j a  v a 2s .co m*/
     * Access list elements of given ACL model.
     */
    Element access = new Element("access-list", accessNamespaceRpc);
    access.addContent(new Element("acl-name", accessNamespaceRpc).setText(accessList.getAclName()));
    // access.addContent(accessEntries);

    if (!accessList.getAccessListEntries().isEmpty() && accessList.getAccessListEntries() != null) {
        for (int accessEntryIntVlu = 0; accessEntryIntVlu < accessList.getAccessListEntries()
                .size(); accessEntryIntVlu++) {
            access.addContent(getAccessEntries(accessEntryIntVlu, accessList, accessNamespaceRpc));
        }
    }

    /**
     * edit-config operation for given ACL model.
     */
    Element editConfig = new Element("edit-config", namespaceRpc);
    editConfig.addContent(new Element("target", namespaceRpc).addContent(new Element("running", namespaceRpc)));
    editConfig
            .addContent(new Element("config", Namespace.getNamespace("urn:ietf:params:xml:ns:netconf:base:1.0"))
                    .addContent(access));

    doc.getRootElement().addContent(editConfig);
    XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
    String outputString = xmlOutputter.outputString(doc);

    return outputString;
}

From source file:org.openflexo.foundation.fml.rm.ViewPointResourceImpl.java

License:Open Source License

public static void convertViewPoint(ViewPointResource viewPointResource) {

    File viewPointDirectory = ResourceLocator.retrieveResourceAsFile(viewPointResource.getDirectory());
    File xmlFile = (File) viewPointResource.getFlexoIODelegate().getSerializationArtefact();// getFile();

    logger.info("Converting " + viewPointDirectory.getAbsolutePath());

    File diagramSpecificationDir = new File(viewPointDirectory, "DiagramSpecification");
    diagramSpecificationDir.mkdir();// w  w  w  .j  av a2  s  .c  o  m

    logger.fine("Creating directory " + diagramSpecificationDir.getAbsolutePath());

    try {
        Document viewPointDocument = XMLUtils.readXMLFile(xmlFile);
        Document diagramSpecificationDocument = XMLUtils.readXMLFile(xmlFile);

        for (File f : viewPointDirectory.listFiles()) {
            if (!f.equals(xmlFile) && !f.equals(diagramSpecificationDir) && !f.getName().endsWith("~")) {
                if (f.getName().endsWith(".shema")) {
                    try {
                        File renamedExampleDiagramFile = new File(f.getParentFile(),
                                f.getName().substring(0, f.getName().length() - 6) + ".diagram");
                        FileUtils.rename(f, renamedExampleDiagramFile);
                        f = renamedExampleDiagramFile;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                File destFile = new File(diagramSpecificationDir, f.getName());
                FileUtils.rename(f, destFile);
                logger.fine("Moving file " + f.getAbsolutePath() + " to " + destFile.getAbsolutePath());
            }
            if (f.getName().endsWith("~")) {
                f.delete();
            }
        }

        Element diagramSpecification = XMLUtils.getElement(diagramSpecificationDocument, "ViewPoint");
        diagramSpecification.setName("DiagramSpecification");
        FileOutputStream fos = new FileOutputStream(
                new File(diagramSpecificationDir, "DiagramSpecification.xml"));
        Format prettyFormat = Format.getPrettyFormat();
        prettyFormat.setLineSeparator(LineSeparator.SYSTEM);
        XMLOutputter outputter = new XMLOutputter(prettyFormat);
        try {
            outputter.output(diagramSpecificationDocument, fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        fos.flush();
        fos.close();
    } catch (JDOMException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    ((ViewPointResourceImpl) viewPointResource).exploreVirtualModels(viewPointResource.getDirectory());

}

From source file:org.openflexo.foundation.utils.XMLUtils.java

License:Open Source License

public static boolean saveXMLFile(org.jdom2.Document document, OutputStream os) {
    try {/*from   www  .  j  av  a2s  .c  om*/
        Format prettyFormat = Format.getPrettyFormat();
        prettyFormat.setLineSeparator(LineSeparator.SYSTEM);
        XMLOutputter outputter = new XMLOutputter(prettyFormat);
        outputter.output(document, os);
        return true;
    } catch (Exception e) {
        // Warns about the exception
        if (logger.isLoggable(Level.WARNING)) {
            logger.warning("Exception raised: " + e.getClass().getName() + ". See console for details.");
        }
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:org.openflexo.foundation.viewpoint.rm.ViewPointResourceImpl.java

License:Open Source License

public static void convertViewPoint(ViewPointResource viewPointResource) {

    File viewPointDirectory = ResourceLocator.retrieveResourceAsFile(viewPointResource.getDirectory());
    File xmlFile = (File) viewPointResource.getFlexoIODelegate().getSerializationArtefact();//getFile();

    logger.info("Converting " + viewPointDirectory.getAbsolutePath());

    File diagramSpecificationDir = new File(viewPointDirectory, "DiagramSpecification");
    diagramSpecificationDir.mkdir();/* ww  w. j av  a2s .  c  o  m*/

    logger.fine("Creating directory " + diagramSpecificationDir.getAbsolutePath());

    try {
        Document viewPointDocument = XMLUtils.readXMLFile(xmlFile);
        Document diagramSpecificationDocument = XMLUtils.readXMLFile(xmlFile);

        for (File f : viewPointDirectory.listFiles()) {
            if (!f.equals(xmlFile) && !f.equals(diagramSpecificationDir) && !f.getName().endsWith("~")) {
                if (f.getName().endsWith(".shema")) {
                    try {
                        File renamedExampleDiagramFile = new File(f.getParentFile(),
                                f.getName().substring(0, f.getName().length() - 6) + ".diagram");
                        FileUtils.rename(f, renamedExampleDiagramFile);
                        f = renamedExampleDiagramFile;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                File destFile = new File(diagramSpecificationDir, f.getName());
                FileUtils.rename(f, destFile);
                logger.fine("Moving file " + f.getAbsolutePath() + " to " + destFile.getAbsolutePath());
            }
            if (f.getName().endsWith("~")) {
                f.delete();
            }
        }

        Element diagramSpecification = XMLUtils.getElement(diagramSpecificationDocument, "ViewPoint");
        diagramSpecification.setName("DiagramSpecification");
        FileOutputStream fos = new FileOutputStream(
                new File(diagramSpecificationDir, "DiagramSpecification.xml"));
        Format prettyFormat = Format.getPrettyFormat();
        prettyFormat.setLineSeparator(LineSeparator.SYSTEM);
        XMLOutputter outputter = new XMLOutputter(prettyFormat);
        try {
            outputter.output(diagramSpecificationDocument, fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        fos.flush();
        fos.close();
    } catch (JDOMException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    ((ViewPointResourceImpl) viewPointResource).exploreVirtualModels(viewPointResource.getDirectory());

}

From source file:org.openkennel.model.filemngr.XMLFileHandler.java

License:Open Source License

/**
 * Writes the XML representation in memory to the given file.
 *
 * @param file/*from   w w w .j a va2 s. co  m*/
 *            XML file to store in
 */
public void writeXMLToFile(File file) {
    if (xmlExists()) {
        XMLOutputter xMLOutput = null;
        try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) {
            xMLOutput = new XMLOutputter(Format.getPrettyFormat());
            xMLOutput.output(xmlFile, outputStream);
            outputStream.close();
        } catch (IOException e) {
            LOGGER.error(e);
            JOptionPane.showMessageDialog(UtilityClass.getActiveFrame(), ErrorLanguageContent.error_io, //
                    ErrorLanguageContent.error_title, JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:org.openmrs.module.dataaggregation.web.controller.DataAggregationManageController.java

License:Open Source License

private String convertToXML(String csvString) {
    String[] rows = csvString.split("\n");
    Element table = new Element("table");
    Document doc = new Document(table);
    int i = 0;/*w ww  .  j  a  va 2s  .  co  m*/
    while (i < rows.length) {
        Element row = new Element("row" + i);
        String[] cols = rows[i].split(":");
        int j = 0;
        for (String rowS : cols) {
            row.setAttribute(new Attribute("col" + j, rowS));
            j++;
        }

        doc.getRootElement().addContent(row);

        i++;
    }

    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());
    return xmlOutput.outputString(doc);
}

From source file:org.optaconf.benchmark.examples.common.persistence.AbstractXmlSolutionExporter.java

License:Apache License

public void writeSolution(Solution solution, File outputFile) {
    OutputStream out = null;/*from   w ww.ja  v a2s.c om*/
    try {
        out = new FileOutputStream(outputFile);
        Document document = new Document();
        XmlOutputBuilder xmlOutputBuilder = createXmlOutputBuilder();
        xmlOutputBuilder.setDocument(document);
        xmlOutputBuilder.setSolution(solution);
        xmlOutputBuilder.writeSolution();
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(document, out);
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not write the file (" + outputFile.getName() + ").", e);
    } catch (JDOMException e) {
        throw new IllegalArgumentException("Could not format the XML file (" + outputFile.getName() + ").", e);
    } finally {
        IOUtils.closeQuietly(out);
    }
    logger.info("Exported: {}", outputFile);
}