Example usage for org.dom4j.io XMLWriter close

List of usage examples for org.dom4j.io XMLWriter close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the underlying Writer

Usage

From source file:org.waarp.common.xml.XmlUtil.java

License:Open Source License

/**
 * Save the document into the file/*from  ww w .  j  ava  2  s .  co m*/
 * 
 * @param file
 * @param document
 * @throws IOException
 */
static public void saveDocument(File file, Document document) throws IOException {
    if (file.exists() && (!file.canWrite())) {
        throw new IOException("File is not writable: " + file.getPath());
    }
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(WaarpStringUtils.UTF8.name());
    XMLWriter writer = new XMLWriter(new FileWriter(file), format);
    writer.write(document);
    writer.flush();
    writer.close();
}

From source file:org.waarp.common.xml.XmlUtil.java

License:Open Source License

/**
 * Save the branch from element into the file
 * //from www  .  j a va 2 s. c o m
 * @param file
 * @param element
 * @throws IOException
 */
static public void saveElement(File file, Element element) throws IOException {
    if (file.exists() && (!file.canWrite())) {
        throw new IOException("File is not writable: " + file.getPath());
    }
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(WaarpStringUtils.UTF8.name());
    XMLWriter writer = new XMLWriter(new FileWriter(file), format);
    writer.write(element);
    writer.flush();
    writer.close();
}

From source file:org.waarp.common.xml.XmlUtil.java

License:Open Source License

/**
 * Write the given XML document to filename using the encoding
 * //  w w  w.java  2  s . co  m
 * @param filename
 * @param encoding
 *            if null, default encoding UTF-8 will be used
 * @param document
 * @throws IOException
 */
public static void writeXML(String filename, String encoding, Document document) throws IOException {
    OutputFormat format = OutputFormat.createPrettyPrint();
    if (encoding != null) {
        format.setEncoding(encoding);
    } else {
        format.setEncoding(WaarpStringUtils.UTF8.name());
    }
    XMLWriter writer = null;
    writer = new XMLWriter(new FileWriter(filename), format);
    writer.write(document);
    try {
        writer.close();
    } catch (IOException e) {
    }
}

From source file:org.waarp.openr66.database.data.DbTaskRunner.java

License:Open Source License

/**
 * Write selected TaskRunners to an XML file using an XMLWriter
 * /*from   w  ww. j  ava 2 s .c om*/
 * @param preparedStatement
 * @param filename
 * @return the NbAndSpecialId for the number of transfer and higher rank found
 * @throws WaarpDatabaseNoConnectionException
 * @throws WaarpDatabaseSqlException
 * @throws OpenR66ProtocolBusinessException
 */
public static NbAndSpecialId writeXMLWriter(DbPreparedStatement preparedStatement, String filename)
        throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException, OpenR66ProtocolBusinessException {
    NbAndSpecialId nbAndSpecialId = null;
    OutputStream outputStream = null;
    XMLWriter xmlWriter = null;
    try {
        outputStream = new FileOutputStream(filename);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("ISO-8859-1");
        xmlWriter = new XMLWriter(outputStream, format);
        preparedStatement.executeQuery();
        nbAndSpecialId = writeXML(preparedStatement, xmlWriter);
    } catch (FileNotFoundException e) {
        logger.error("Cannot write XML file", e);
        throw new OpenR66ProtocolBusinessException("File not found");
    } catch (UnsupportedEncodingException e) {
        logger.error("Cannot write XML file", e);
        throw new OpenR66ProtocolBusinessException("Unsupported Encoding");
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.endDocument();
                xmlWriter.flush();
                xmlWriter.close();
            } catch (SAXException e) {
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                File file = new File(filename);
                file.delete();
                logger.error("Cannot write XML file", e);
                throw new OpenR66ProtocolBusinessException("Unsupported Encoding");
            } catch (IOException e) {
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                File file = new File(filename);
                file.delete();
                logger.error("Cannot write XML file", e);
                throw new OpenR66ProtocolBusinessException("Unsupported Encoding");
            }
        } else if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
            }
            File file = new File(filename);
            file.delete();
        }
    }
    return nbAndSpecialId;
}

From source file:org.waarp.openr66.database.data.DbTaskRunner.java

License:Open Source License

/**
 * Method to write the current DbTaskRunner for NoDb client instead of updating DB. 'setToArray'
 * must be called priorly to be able to store the values.
 * // w  w  w  . j  a v a 2 s  .  c  om
 * @throws OpenR66ProtocolBusinessException
 */
public void writeXmlWorkNoDb() throws OpenR66ProtocolBusinessException {
    String filename = backendXmlFilename();
    OutputStream outputStream = null;
    XMLWriter xmlWriter = null;
    try {
        outputStream = new FileOutputStream(filename);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("ISO-8859-1");
        xmlWriter = new XMLWriter(outputStream, format);
        Element root = new DefaultElement(XMLRUNNERS);
        try {
            xmlWriter.writeOpen(root);
            Element node;
            node = DbTaskRunner.getElementFromRunner(this);
            xmlWriter.write(node);
            xmlWriter.flush();
            xmlWriter.writeClose(root);
        } catch (IOException e) {
            logger.error("Cannot write XML file", e);
            throw new OpenR66ProtocolBusinessException("Cannot write file: " + e.getMessage());
        } catch (WaarpDatabaseSqlException e) {
            logger.error("Cannot write Data", e);
            throw new OpenR66ProtocolBusinessException("Cannot write Data: " + e.getMessage());
        }
    } catch (FileNotFoundException e) {
        logger.error("Cannot write XML file", e);
        throw new OpenR66ProtocolBusinessException("File not found");
    } catch (UnsupportedEncodingException e) {
        logger.error("Cannot write XML file", e);
        throw new OpenR66ProtocolBusinessException("Unsupported Encoding");
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.endDocument();
                xmlWriter.flush();
                xmlWriter.close();
            } catch (SAXException e) {
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                File file = new File(filename);
                file.delete();
                logger.error("Cannot write XML file", e);
                throw new OpenR66ProtocolBusinessException("Unsupported Encoding");
            } catch (IOException e) {
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                File file = new File(filename);
                file.delete();
                logger.error("Cannot write XML file", e);
                throw new OpenR66ProtocolBusinessException("Unsupported Encoding");
            }
        } else if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
            }
            File file = new File(filename);
            file.delete();
        }
    }
}

From source file:org.xmldb.core.xml.XmlHelper.java

License:Apache License

public synchronized void save() {
    try {/*from w  w w. j  av a2  s  . c  o  m*/
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        outformat.setEncoding(codifica);

        XMLWriter writer = new XMLWriter(new FileWriter(file), outformat);
        writer.write(document);
        writer.close();

        LogHelper.info("Documento salvato correttamente");
    } catch (Exception e) {
        throw new XmlDBRuntimeException(e);
    }
}

From source file:org.xwiki.tool.xar.FormatMojo.java

License:Open Source License

private void format(File file, String defaultLanguage) throws Exception {
    SAXReader reader = new SAXReader();
    Document domdoc = reader.read(file);
    format(file.getName(), domdoc, defaultLanguage);

    XMLWriter writer;
    if (this.pretty) {
        OutputFormat format = new OutputFormat("  ", true, "UTF-8");
        format.setExpandEmptyElements(false);
        writer = new XWikiXMLWriter(new FileOutputStream(file), format);
    } else {//from w w  w . jav a  2  s.co m
        writer = new XWikiXMLWriter(new FileOutputStream(file));
    }
    writer.write(domdoc);
    writer.close();

    String parentName = file.getParentFile().getName();
    getLog().info(String.format("  Formatting [%s/%s]... ok", parentName, file.getName()));
}

From source file:org.xwiki.tool.xar.XARMojo.java

License:Open Source License

private void performTransformations() throws Exception {
    if (this.transformations == null) {
        return;/*from w  w  w.  j  a  v a 2 s. c om*/
    }

    // Copy XML pages from dependent XAR if we modify them.
    unpackTransformedXARs();

    SAXReader reader = new SAXReader();

    // For each defined file, perform the transformation asked
    for (Transformation transformation : this.transformations) {
        File file = new File(this.project.getBuild().getOutputDirectory(), transformation.getFile());
        Document document = reader.read(file);
        Node node = document.selectSingleNode(transformation.getXpath());
        String value = transformation.getValue();
        if (!StringUtils.isEmpty(value)) {
            // Get the current value at node and replace $1 with it (if any)
            String currentValue = node.getText();
            node.setText(value.replace("$1", currentValue));
        }
        // Write the modified file to disk
        XMLWriter writer = new XMLWriter(new FileOutputStream(file));
        writer.write(document);
        writer.flush();
        writer.close();
    }
}

From source file:org.xwiki.tool.xar.XARMojo.java

License:Open Source License

/**
 * Create and add package configuration file to the package.
 * //from   w  w w.  j a v a  2 s.c o  m
 * @param packageFile the package when to add configuration file.
 * @param files the files in the package.
 * @throws Exception error when writing the configuration file.
 */
private void generatePackageXml(File packageFile, Collection<ArchiveEntry> files) throws Exception {
    getLog().info(String.format("Generating package.xml descriptor at [%s]", packageFile.getPath()));

    OutputFormat outputFormat = new OutputFormat("", true);
    outputFormat.setEncoding(this.encoding);
    OutputStream out = new FileOutputStream(packageFile);
    XMLWriter writer = new XMLWriter(out, outputFormat);
    writer.write(toXML(files));
    writer.close();
    out.close();
}

From source file:org.youi.framework.util.Dom4jUtils.java

License:Apache License

/**
 * ?xml?//from  w w w . ja va 2  s  .com
 * @param doc
 * @param os
 * @param encoding
 */
public static void formatXml(Document doc, OutputStream os, String encoding) {
    if (os == null)
        return;
    OutputFormat format = null;
    XMLWriter output = null;
    try {
        format = OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);
        format.setOmitEncoding(false);
        output = new XMLWriter(new BufferedWriter(new OutputStreamWriter(os, "UTF-8")), format);
        output.write(doc);
    } catch (IOException e) {
        log.error("xmlIO:" + e.getMessage());
    } finally {
        try {
            output.close();
        } catch (IOException e) {
            log.info("" + e.getMessage());
        }
    }
}