Example usage for org.dom4j.io OutputFormat setIndentSize

List of usage examples for org.dom4j.io OutputFormat setIndentSize

Introduction

In this page you can find the example usage for org.dom4j.io OutputFormat setIndentSize.

Prototype

public void setIndentSize(int indentSize) 

Source Link

Document

This will set the indent String's size; an indentSize of 4 would result in the indention being equivalent to the String "    " (four space characters).

Usage

From source file:org.suren.autotest.web.framework.code.DefaultXmlSuiteRunnerGenerator.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//  www . ja va 2 s  .c om
public void generate(InputStream input, String outputDir, Callback callback) {
    this.outputDir = outputDir;

    //      String srcPath = srcCoding;
    //      srcPath = srcPath.replace(".xml", "");
    //      srcPath = srcPath + "_runner.xml";

    //      ClassLoader clsLoader = this.getClass().getClassLoader();
    //      URL url = clsLoader.getResource(srcPath);
    //      InputStream dsInput = null;
    //      try
    //      {
    //         if(url != null)
    //         {
    //            dsInput = url.openStream();
    //         }
    //      }
    //      catch (IOException e1)
    //      {
    //         e1.printStackTrace();
    //      }

    //      if(dsInput != null)
    //      {
    //         try
    //         {
    //            suiteRunnerDoc = new SAXReader().read(dsInput);
    //         }
    //         catch (DocumentException e)
    //         {
    //            e.printStackTrace();
    //         }
    //      }
    //      else
    //      {
    suiteRunnerDoc = DocumentHelper.createDocument();
    //      }

    //??
    try {
        read(input);
    } catch (DocumentException | IOException e) {
        logger.error(String.format("Main config [%s] parse process error.", srcCoding), e);
    } catch (SAXException e) {
        e.printStackTrace();
    }

    XMLWriter xmlWriter = null;
    //      if(dsInput != null)
    //      {
    //         try
    //         {
    //            dsInput.close();
    //         }
    //         catch (IOException e)
    //         {
    //            e.printStackTrace();
    //         }
    //      }

    //      String outputFileName = null;
    //      if(url != null)
    //      {
    //         outputFileName = new File(url.getFile()).getName();
    //      }
    //      else
    //      {
    //         outputFileName = new File(srcPath).getName();
    //      }

    File outputDirFile = new File(outputDir);
    if (!outputDirFile.isDirectory()) {
        outputDirFile.mkdirs();
    }

    File outputFile = new File(outputDir, System.currentTimeMillis() + ".xml");
    try (OutputStream dsOutput = new FileOutputStream(outputFile)) {
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setIndentSize(4);
        xmlWriter = new XMLWriter(dsOutput, outputFormat);

        xmlWriter.write(suiteRunnerDoc);

        if (callback != null) {
            callback.callback(outputFile);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.talend.updates.runtime.nexus.component.ComponentIndexManager.java

License:Open Source License

public boolean createIndexFile(File indexFile, List<ComponentIndexBean> newIndexList) throws IOException {
    if (newIndexList == null || newIndexList.isEmpty() || indexFile == null) {
        return false;
    }// w  ww.  ja v a2 s  .  c o  m

    XMLWriter xmlWriter = null;
    boolean created = false;
    try {
        // write to index
        final DocumentFactory docFactory = DocumentFactory.getInstance();
        final Element components = docFactory.createElement(ELEM_COMPONENTS);
        Document newDoc = docFactory.createDocument(components);
        for (ComponentIndexBean b : newIndexList) {
            final Element elem = createXmlElement(b);
            if (elem != null) {
                components.add(elem);
            }
        }

        // 4 spaces
        OutputFormat format = new OutputFormat();
        format.setEncoding("UTF-8"); //$NON-NLS-1$
        format.setIndentSize(4);
        format.setNewlines(true);
        xmlWriter = new XMLWriter(new FileOutputStream(indexFile), format);

        xmlWriter.write(newDoc);

        created = true;
        return true;
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException e) {
                //
            }
        }
        if (!created && indexFile.exists()) {
            indexFile.delete(); // remove the wrong file.
        }
    }
}

From source file:org.unitime.commons.ant.LowercaseTableNames.java

License:Open Source License

protected void write(String resource, Document document) throws IOException, DocumentException {
    File file = null;/* w ww.  ja va2 s.  c om*/
    if (iSource == null) {
        file = new File(getClass().getClassLoader().getResource(resource).getFile());
    } else {
        file = new File(iSource + File.separator + resource);
    }
    info("  -- writing " + file + " ...");
    FileOutputStream fos = new FileOutputStream(file);
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setIndentSize(4);
        format.setPadText(false);
        new MyXMLWriter(fos, format).write(document);
    } finally {
        fos.flush();
        fos.close();
    }
}

From source file:pt.webdetails.cda.exporter.HtmlExporter.java

License:Open Source License

@Override
public void export(OutputStream out, TableModel tableModel) throws ExporterException {
    final Document document = DocumentHelper.createDocument();
    Element table = null;//from   w ww  . ja  v a2  s. co m

    if (fullHtml) {
        final Element html = document.addElement("html");
        final Element head = html.addElement("head");
        head.addElement("title").addText(title);
        table = html.addElement("body").addElement("table");
    } else {
        table = document.addElement("table");
    }

    final int columnCount = tableModel.getColumnCount();

    //table headers
    final Element headerRow = table.addElement("tr");
    for (int i = 0; i < columnCount; i++) {
        String colName = tableModel.getColumnName(i);
        headerRow.addElement("th").addText(colName);
    }

    //table body
    for (int i = 0; i < tableModel.getRowCount(); i++) {
        Element row = table.addElement("tr");

        for (int j = 0; j < columnCount; j++) {
            Element tableCell = row.addElement("td");
            Object value = tableModel.getValueAt(i, j);
            tableCell.setText(valueToText(value));

            if (value instanceof Date) {
                tableCell.setText(format.format(value));
            } else if (value != null) {
                // numbers can be safely converted via toString, as they use a well-defined format there
                tableCell.setText(value.toString());
            }

        }
    }

    try {
        document.setXMLEncoding("UTF-8");

        OutputFormat outFormat = new OutputFormat();
        outFormat.setOmitEncoding(true);
        outFormat.setSuppressDeclaration(true);//otherwise msexcel/oocalc may not recognize content
        outFormat.setNewlines(true);
        outFormat.setIndentSize(columnCount);
        final Writer writer = new BufferedWriter(new OutputStreamWriter(out));
        XMLWriter xmlWriter = new XMLWriter(writer, outFormat);
        xmlWriter.write(document);
        xmlWriter.flush();
    } catch (IOException e) {
        throw new ExporterException("IO Exception converting to utf-8", e);
    }
}

From source file:se.vgregion.alfresco.repo.utils.VGRexporterComponent.java

License:Open Source License

/**
 * Create an XML Exporter that exports repository information to the specified
 * output stream in xml format./*from www .j  av  a 2s.  c o  m*/
 * 
 * @param viewWriter  the output stream to write to
 * @param referenceType  the format of references to export
 * @return  the xml exporter
 */
private Exporter createXMLExporter(OutputStream viewWriter, ReferenceType referenceType) {
    // Define output format
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(indentSize);
    format.setEncoding("UTF-8");

    // Construct an XML Exporter
    try {
        XMLWriter writer = new XMLWriter(viewWriter, format);
        VGRViewXMLExporter exporter = new VGRViewXMLExporter(namespaceService, nodeService, searchService,
                dictionaryService, permissionService, writer);
        exporter.setReferenceType(referenceType);
        return exporter;
    } catch (UnsupportedEncodingException e) {
        throw new ExporterException("Failed to create XML Writer for export", e);
    } catch (Exception e) {
        throw new ExporterException("Failed to create XML Writer for export", e);
    }
}

From source file:to.networld.fbtosemweb.AbstractRDFExporter.java

License:Open Source License

@Override
public String toString() {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {/*from w w  w . ja  v a 2  s . co m*/
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setIndentSize(INDENT_SIZE);
        format.setExpandEmptyElements(EXPAND_EMPTY_ELEMENTS);
        XMLWriter writer = new XMLWriter(os, format);
        writer.write(this.rdfDocument);
        return os.toString();
    } catch (IOException e) {
        return null;
    }
}

From source file:to.networld.semantic.contexthandler.common.RDFEntity.java

License:Open Source License

@Override
public String toString() {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {//from w ww  . ja v  a 2 s . c o  m
        OutputFormat format = OutputFormat.createPrettyPrint();
        if (this.config != null) {
            format.setIndentSize(this.config.getRDFIndentSize());
            format.setExpandEmptyElements(this.config.getRDFExpandEmptyElements());
        }
        XMLWriter writer = new XMLWriter(os, format);
        writer.write(this.document);
        return os.toString();
    } catch (IOException e) {
        return null;
    }
}