Example usage for org.dom4j.io OutputFormat OutputFormat

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

Introduction

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

Prototype

public OutputFormat() 

Source Link

Document

Creates an OutputFormat with no additional whitespace (indent or new lines) added.

Usage

From source file:org.snipsnap.util.XMLSnipRepair.java

License:Open Source License

public static void repair(File input, File output, File webAppDir) {
    System.err.println("STEP 1: parsing input file ...");
    Document document = null;/*ww w  . ja v  a  2  s  . c  om*/
    try {
        document = load(input);
    } catch (Exception e) {
        System.err.println("Unable to read input document: " + e);
        System.err.println(
                "This is usually the case for illegal XML characters, please manually edit the file and remove them.");
        System.exit(0);
    }

    System.err.println("STEP 2: checking SnipSpace consistency ...");
    Document repaired = repair(document, webAppDir);

    System.err.println("STEP 3: writing output file ...");
    OutputFormat outputFormat = new OutputFormat();
    outputFormat.setEncoding("UTF-8");
    outputFormat.setNewlines(true);
    try {
        XMLWriter xmlWriter = new XMLWriter(
                null == output ? System.out : (OutputStream) new FileOutputStream(output));
        xmlWriter.write(repaired);
        xmlWriter.flush();
        xmlWriter.close();
    } catch (Exception e) {
        System.err.println("Error: unable to write data: " + e);
    }
    System.err.println("Finished.");
}

From source file:org.soxmpp.server.xmpp.net.NIOConnection.java

License:Open Source License

/**
 * Delivers the packet to this connection (without checking the recipient).
 * /* w w  w.  j ava 2 s  . c  o  m*/
 * @param packet
 *            the packet to deliver
 */
public void deliver(Packet packet) {
    log.info("SENT: " + packet.toXML());
    if (!isClosed()) {
        IoBuffer buffer = IoBuffer.allocate(4096);
        buffer.setAutoExpand(true);

        boolean errorDelivering = false;
        try {
            XMLWriter xmlSerializer = new XMLWriter(new IoBufferWriter(buffer, (CharsetEncoder) encoder.get()),
                    new OutputFormat());
            xmlSerializer.write(packet.getElement());
            xmlSerializer.flush();
            buffer.flip();
            ioSession.write(buffer);
        } catch (Exception e) {
            log.debug("Connection: Error delivering packet" + "\n" + this.toString(), e);
            errorDelivering = true;
        }
        if (errorDelivering) {
            close();
        } else {
            session.incrementServerPacketCount();
        }
    }
}

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;
    }//www . j  ava  2s .  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.tsp.bws.BWSDocument.java

License:Open Source License

/** 
 * Prints the current document to the console via the dom4j XMLWriter 
 *//*from  ww  w  .  j  a  v a 2  s.  com*/
public void printDocumentSource() {
    // use pretty printing
    OutputFormat outformat = new OutputFormat();// = OutputFormat.createPrettyPrint();
    //      outformat.setEnconding();
    // use standard encoding, else: outformat.setEncoding(String encodingScheme)
    try {
        XMLWriter writer = new XMLWriter(System.out);
        writer.write(this.xmlDocument);
        writer.flush();
        // go to the next line
        System.out.println("\n");
    } catch (Exception e) {
        System.out.println("[Error] Exception printing the document, wrong encoding scheme?");
        e.printStackTrace();
    }
}

From source file:org.zenonpagetemplates.twoPhasesImpl.ZPTOutputFormat.java

License:Open Source License

public OutputFormat getOutputFormat() {

    OutputFormat result = new OutputFormat();

    result.setEncoding(this.encoding);
    //result.setOmitEncoding(true);
    result.setSuppressDeclaration(this.suppressDeclaration);
    //result.setExpandEmptyElements(expandEmptyElements)

    return result;
}

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;// www  . j  a  v a  2  s.  com

    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:util.mybatis.CodeGeneratorRuner.java

License:Apache License

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

    boolean overwrite = true;
    String generatorConfig = CodeGeneratorRuner.class.getClassLoader().getResource("generatorConfig.xml")
            .getFile();/*from  w  ww  .  j  a  va 2s .c  o  m*/
    URL url = Resources.getResource("generatorConfig.xml");
    URLConnection openConnection = url.openConnection();
    InputStream inputStream = openConnection.getInputStream();
    OutputFormat format = new OutputFormat();
    format.setEncoding("UTF-8");
    List<String> warnings = new ArrayList<String>();
    Log.print("???," + generatorConfig);
    //      File conifgFile = new File(url);

    ConfigurationParser configurationParser = new ConfigurationParser(warnings);
    Configuration configuration = configurationParser
            .parseConfiguration(new InputStreamReader(inputStream, "UTF-8"));
    DefaultShellCallback shellCallback = new DefaultShellCallback(overwrite);
    MyBatisGenerator batisGenerator = new MyBatisGenerator(configuration, shellCallback, warnings);
    batisGenerator.generate(new CodeProgressCallback());
}

From source file:util.mybatis.comment.CodetemplatesLoader.java

License:Apache License

private void loadAndResolveDocument() throws DocumentException, IOException {
    URL url = Resources.getResource("codetemplates.xml");
    URLConnection openConnection = url.openConnection();
    InputStream inputStream = openConnection.getInputStream();
    OutputFormat format = new OutputFormat();
    format.setEncoding("UTF-8");
    SAXReader reader = new SAXReader();
    Document document = reader.read(new BufferedReader(new InputStreamReader(inputStream, "UTF-8")));
    //      Document document = reader.read(url.getFile(), "UTF-8"));
    Element root = document.getRootElement();
    TemplateVisitor visitor = new TemplateVisitor();
    root.accept(visitor);//from  w  ww  .  j ava2  s  .co  m
    templates.putAll(visitor.getTemplates());
}