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:de.tor.tribes.util.xml.JDomUtils.java

License:Apache License

public static void saveDocument(Document pDoc, String filename) {
    try {//from w  w w  .j  ava2 s .  c  o m
        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(pDoc, new FileWriter(filename));
    } catch (Exception e) {
        logger.warn("Unable to save document", e);
    }
}

From source file:de.tu_dortmund.ub.data.dswarm.Task.java

License:Open Source License

/**
 * upload a file and update an existing resource with it
 *
 * @param resourceUUID//from w w w  .  ja  v  a2  s  .  c o  m
 * @param filename
 * @param name
 * @param description
 * @return responseJson
 * @throws Exception
 */
private String uploadFileAndUpdateResource(String resourceUUID, String filename, String name,
        String description) throws Exception {

    if (null == resourceUUID)
        throw new Exception("ID of the resource to update was null.");

    String responseJson = null;

    String file = config.getProperty("resource.watchfolder") + File.separatorChar + filename;

    // ggf. Preprocessing: insert CDATA in XML and write new XML file to tmp folder
    if (Boolean.parseBoolean(config.getProperty("resource.preprocessing"))) {

        Document document = new SAXBuilder().build(new File(file));

        file = config.getProperty("preprocessing.folder") + File.separatorChar + UUID.randomUUID() + ".xml";

        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
        BufferedWriter bufferedWriter = null;
        try {

            bufferedWriter = new BufferedWriter(new FileWriter(file));

            out.output(new SAXBuilder().build(new StringReader(
                    XmlTransformer.xmlOutputter(document, config.getProperty("preprocessing.xslt"), null))),
                    bufferedWriter);
        } finally {
            if (bufferedWriter != null) {
                bufferedWriter.close();
            }
        }
    }

    // upload
    CloseableHttpClient httpclient = HttpClients.createDefault();

    try {
        HttpPut httpPut = new HttpPut(config.getProperty("engine.dswarm.api") + "resources/" + resourceUUID);

        FileBody fileBody = new FileBody(new File(file));
        StringBody stringBodyForName = new StringBody(name, ContentType.TEXT_PLAIN);
        StringBody stringBodyForDescription = new StringBody(description, ContentType.TEXT_PLAIN);

        HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", fileBody)
                .addPart("name", stringBodyForName).addPart("description", stringBodyForDescription).build();

        httpPut.setEntity(reqEntity);

        logger.info("[" + config.getProperty("service.name") + "] " + "request : " + httpPut.getRequestLine());

        CloseableHttpResponse httpResponse = httpclient.execute(httpPut);

        try {
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            HttpEntity httpEntity = httpResponse.getEntity();

            switch (statusCode) {

            case 200: {

                logger.info("[" + config.getProperty("service.name") + "] " + statusCode + " : "
                        + httpResponse.getStatusLine().getReasonPhrase());
                StringWriter writer = new StringWriter();
                IOUtils.copy(httpEntity.getContent(), writer, "UTF-8");
                responseJson = writer.toString();

                logger.info("[" + config.getProperty("service.name") + "] responseJson : " + responseJson);

                break;
            }
            default: {

                logger.error("[" + config.getProperty("service.name") + "] " + statusCode + " : "
                        + httpResponse.getStatusLine().getReasonPhrase());
            }
            }

            EntityUtils.consume(httpEntity);
        } finally {
            httpResponse.close();
        }
    } finally {
        httpclient.close();
    }

    return responseJson;
}

From source file:de.tu_dortmund.ub.data.util.XmlTransformer.java

License:Open Source License

public static void main(String[] args) throws Exception {

    Document document = new SAXBuilder().build(new File("data/project.mods.xml"));

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

    BufferedWriter bufferedWriter = null;
    try {//from  w w w . j  a v  a2  s .com

        bufferedWriter = new BufferedWriter(new FileWriter("data/cdata.project.mods.xml"));

        out.output(new SAXBuilder().build(new StringReader(xmlOutputter(document, "xslt/cdata.xsl", null))),
                bufferedWriter);
    } finally {
        if (bufferedWriter != null) {
            bufferedWriter.close();
        }
    }

}

From source file:delfos.Constants.java

License:Open Source License

/**
 * Devuelve el formato que se usa para guardar los archivos XML.
 *
 * @return/*from   w w w  .  j  a v  a2 s .  c o m*/
 */
public static Format getXMLFormat() {
    return Format.getPrettyFormat().setEncoding("ISO-8859-1");
}

From source file:delfos.rs.output.RecommendationsOutputStandardXML.java

License:Open Source License

@Override
public void writeRecommendations(Recommendations recommendations) {
    List<Recommendation> topNrecommendations = new ArrayList<>(recommendations.getRecommendations());

    if (getNumberOfRecommendations() > 0) {
        Collections.sort(topNrecommendations);
        topNrecommendations = topNrecommendations.subList(0,
                Math.min(topNrecommendations.size(), getNumberOfRecommendations()));
    }/*from  www  .  ja  v a  2s  . c  o m*/

    Recommendations recommendationsWithNewRanking = RecommendationsFactory
            .copyRecommendationsWithNewRanking(recommendations, topNrecommendations);

    Element recommendationsElement = RecommendationsToXML
            .getRecommendationsElement(recommendationsWithNewRanking);

    Document doc = new Document(recommendationsElement);
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setEncoding("ISO-8859-1"));
    try {
        outputter.output(doc, System.out);
    } catch (IOException ex) {
        ERROR_CODES.CANNOT_WRITE_RECOMMENDATIONS.exit(ex);
    }
}

From source file:devicemodel.conversions.XmlConversions.java

public static String document2XmlStringNoHeader(final Document doc) throws IOException {
    final StringWriter stringWriter = new StringWriter();
    final XMLOutputter xmlOutput = new XMLOutputter();
    final Format plainFormat = Format.getPrettyFormat();
    plainFormat.setOmitDeclaration(true);
    xmlOutput.setFormat(plainFormat);//from   w  ww  .  ja va2s. c o  m
    xmlOutput.output(doc, stringWriter);

    return stringWriter.toString();
}

From source file:dibl.diagrams.Template.java

License:Open Source License

public void write(final OutputStream out) throws IOException {
    final XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
    xmlOutputter.output(doc, out);/*ww  w  .  ja v  a 2 s .c  om*/
}

From source file:Domain.DataAccess.DatabaseHandler.java

public boolean bookAvailable(String pBookId) {

    boolean result = false;

    org.jdom2.Element book;//  ww  w . j  av  a  2 s .c  o m

    SAXBuilder builder = new SAXBuilder();
    org.jdom2.Document readDoc = null;
    try {
        readDoc = builder.build(new File("./src/Books.xml"));
        org.jdom2.Element root = readDoc.getRootElement();
        List children = root.getChildren();

        for (int i = 0; i < children.size(); i++) {

            book = (org.jdom2.Element) children.get(i);

            if (book.getChildText("bookId").equals(pBookId)) {

                String childText = book.getChildText("available");

                if (childText.equals("1")) {
                    result = true;
                }
                break;
            }
        }

        XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
        xmlOutput.output(readDoc, new FileOutputStream(new File("./src/Books.xml")));

    } catch (JDOMException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    System.out.println(result);
    return result;

}

From source file:Domain.DataAccess.DatabaseHandler.java

public void disableBook(String pBookId) {

    org.jdom2.Element book;/*from w  w  w.j  a  v a  2s.co  m*/

    SAXBuilder builder = new SAXBuilder();
    org.jdom2.Document readDoc = null;
    try {
        readDoc = builder.build(new File("./src/Books.xml"));
        org.jdom2.Element root = readDoc.getRootElement();
        List children = root.getChildren();

        for (int i = 0; i < children.size(); i++) {

            book = (org.jdom2.Element) children.get(i);

            if (book.getChildText("bookId").equals(pBookId)) {

                book.getChild("available").setText("0");
                break;
            }
        }

        XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
        xmlOutput.output(readDoc, new FileOutputStream(new File("./src/Books.xml")));

    } catch (JDOMException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Domain.DataAccess.DatabaseHandler.java

public void enableBook(String pBookId) {

    org.jdom2.Element book;// ww  w.java2s .  com

    SAXBuilder builder = new SAXBuilder();
    org.jdom2.Document readDoc = null;
    try {
        readDoc = builder.build(new File("./src/Books.xml"));
        org.jdom2.Element root = readDoc.getRootElement();
        List children = root.getChildren();

        for (int i = 0; i < children.size(); i++) {

            book = (org.jdom2.Element) children.get(i);

            if (book.getChildText("bookId").equals(pBookId)) {

                book.getChild("available").setText("1");
                break;
            }
        }

        XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
        xmlOutput.output(readDoc, new FileOutputStream(new File("./src/Books.xml")));

    } catch (JDOMException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}