Search in sources :

Example 21 with XMLWriter

use of org.dom4j.io.XMLWriter in project Openfire by igniterealtime.

the class ArchiveIndexer method loadPropertiesFile.

/**
     * Loads a property manager for search properties if it isn't already
     * loaded. If an XML file for the search properties isn't already
     * created, it will attempt to make a file with default values.
     */
private void loadPropertiesFile(File searchDir) throws IOException {
    File indexPropertiesFile = new File(searchDir, "indexprops.xml");
    // If it doesn't exists we have to create it.
    if (!indexPropertiesFile.exists()) {
        org.dom4j.Document doc = DocumentFactory.getInstance().createDocument(DocumentFactory.getInstance().createElement("search"));
        // Now, write out to the file.
        Writer out = null;
        try {
            // Use JDOM's XMLOutputter to do the writing and formatting.
            out = new FileWriter(indexPropertiesFile);
            XMLWriter outputter = new XMLWriter(out, OutputFormat.createPrettyPrint());
            outputter.write(doc);
            outputter.flush();
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
            // Ignore.
            }
        }
    }
    indexProperties = new XMLProperties(indexPropertiesFile);
}
Also used : FileWriter(java.io.FileWriter) XMLProperties(org.jivesoftware.util.XMLProperties) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter) XMLWriter(org.dom4j.io.XMLWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) TimeoutException(java.util.concurrent.TimeoutException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 22 with XMLWriter

use of org.dom4j.io.XMLWriter in project randomizedtesting by randomizedtesting.

the class JUnit4Mojo method createTemporaryAntFile.

/**
   * Create a temporary ANT file for executing JUnit4 ANT task.
   */
private File createTemporaryAntFile(Document doc) throws IOException {
    Closer closer = Closer.create();
    try {
        File antFile = File.createTempFile("junit4-ant-", ".xml", dir);
        OutputStream os = closer.register(new FileOutputStream(antFile));
        XMLWriter xmlWriter = new XMLWriter(os, OutputFormat.createPrettyPrint());
        xmlWriter.write(doc);
        return antFile;
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}
Also used : Closer(com.google.common.io.Closer) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter)

Example 23 with XMLWriter

use of org.dom4j.io.XMLWriter in project reflections by ronmamo.

the class XmlSerializer method save.

public File save(final Reflections reflections, final String filename) {
    File file = Utils.prepareFile(filename);
    try {
        Document document = createDocument(reflections);
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(file), OutputFormat.createPrettyPrint());
        xmlWriter.write(document);
        xmlWriter.close();
    } catch (IOException e) {
        throw new ReflectionsException("could not save to file " + filename, e);
    } catch (Throwable e) {
        throw new RuntimeException("Could not save to file " + filename + ". Make sure relevant dependencies exist on classpath.", e);
    }
    return file;
}
Also used : ReflectionsException(org.reflections.ReflectionsException) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 24 with XMLWriter

use of org.dom4j.io.XMLWriter in project atlas by alibaba.

the class ManifestFileUtils method saveFile.

private static void saveFile(Document document, OutputFormat format, File file) throws IOException {
    // 声明写XML的对象
    XMLWriter writer = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        writer = new XMLWriter(fos, format);
        writer.write(document);
    } finally {
        if (null != writer) {
            writer.close();
        }
        IOUtils.closeQuietly(fos);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) XMLWriter(org.dom4j.io.XMLWriter)

Example 25 with XMLWriter

use of org.dom4j.io.XMLWriter in project atlas by alibaba.

the class ManifestFileUtils method minifyManifest.

public static void minifyManifest(File mainManifest, File destManifest) throws IOException, DocumentException {
    // 声明写XML的对象
    XMLWriter writer = null;
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    // 设置XML文件的编码格式
    format.setEncoding("UTF-8");
    FileOutputStream fos = new FileOutputStream(destManifest);
    if (mainManifest.exists()) {
        try {
            // 读取XML文件
            Document document = reader.read(mainManifest);
            //                removeComments(document);
            Element element = document.getRootElement();
            element.clearContent();
            writer = new XMLWriter(fos, format);
            writer.write(document);
        } finally {
            if (null != writer) {
                writer.close();
            }
            IOUtils.closeQuietly(fos);
        }
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) FileOutputStream(java.io.FileOutputStream) Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Aggregations

XMLWriter (org.dom4j.io.XMLWriter)42 Document (org.dom4j.Document)21 OutputFormat (org.dom4j.io.OutputFormat)17 IOException (java.io.IOException)15 Element (org.dom4j.Element)15 FileOutputStream (java.io.FileOutputStream)14 File (java.io.File)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 SAXReader (org.dom4j.io.SAXReader)9 FileWriter (java.io.FileWriter)6 StringWriter (java.io.StringWriter)5 BufferedWriter (java.io.BufferedWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 Writer (java.io.Writer)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TreeSet (java.util.TreeSet)3