Example usage for org.dom4j.io XMLWriter write

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

Introduction

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

Prototype

public void write(Object object) throws IOException 

Source Link

Document

Writes the given object which should be a String, a Node or a List of Nodes.

Usage

From source file:net.groupbuy.util.SettingUtils.java

License:Open Source License

/**
 * /* www. j a  v a  2  s. c  om*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/groupbuy/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(shopxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.nikr.eve.jeveasset.io.local.update.Update.java

License:Open Source License

void setVersion(final File xml, final int newVersion) throws DocumentException {
    SAXReader xmlReader = new SAXReader();
    Document doc = xmlReader.read(xml);

    XPath xpathSelector = DocumentHelper.createXPath("/settings");
    List<?> results = xpathSelector.selectNodes(doc);
    for (Iterator<?> iter = results.iterator(); iter.hasNext();) {
        Element element = (Element) iter.next();
        Attribute attr = element.attribute("version");
        if (attr == null) {
            element.add(new DefaultAttribute("version", String.valueOf(newVersion)));
        } else {// w w  w .  ja  va  2 s. c  o m
            attr.setText(String.valueOf(newVersion));
        }
    }

    try {
        FileOutputStream fos = new FileOutputStream(xml);
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        outformat.setEncoding("UTF-16");
        XMLWriter writer = new XMLWriter(fos, outformat);
        writer.write(doc);
        writer.flush();
    } catch (IOException ioe) {
        LOG.error("Failed to update the serttings.xml version number", ioe);
        throw new RuntimeException(ioe);
    }
}

From source file:net.nikr.eve.jeveasset.io.local.update.updates.Update1To2.java

License:Open Source License

@Override
public void performUpdate(final String path) {
    LOG.info("Performing update from v1 to v2");
    LOG.info(" - modifies files:");
    LOG.info("  - settings.xml");
    try {//  w ww  .  ja  va2s.  co m
        // We need to update the settings
        // current changes are:
        // 1. XPath: /settings/filters/filter/row[@mode]
        // changed from (e.g.) "Contains" to the enum value name in AssetFilter.Mode
        // 2. settings/marketstat[@defaultprice] --> another enum: Asset.PriceMode
        // 3. settings/columns/column --> settings/tables/table/column
        // settings/flags/flag --> removed two flags (now in settings/tables/table)
        SAXReader xmlReader = new SAXReader();
        Document doc = xmlReader.read(path);
        convertDefaultPriceModes(doc);
        convertModes(doc);
        convertTableSettings(doc);

        FileOutputStream fos = new FileOutputStream(path);
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        outformat.setEncoding("UTF-16");
        XMLWriter writer = new XMLWriter(fos, outformat);
        writer.write(doc);
        writer.flush();
    } catch (IOException ex) {
        LOG.error("", ex);
        throw new RuntimeException(ex);
    } catch (DocumentException ex) {
        LOG.error("", ex);
        throw new RuntimeException(ex);
    }
}

From source file:net.osxx.util.SettingUtils.java

License:Open Source License

/**
 * //from  w  w  w  . ja  v  a  2s .c o  m
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File osxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
        Document document = new SAXReader().read(osxxXmlFile);
        List<Element> elements = document.selectNodes("/osxx/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(osxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.sf.eclipsecs.core.util.XMLUtil.java

License:Open Source License

/**
 * Creates a pretty printed representation of the document as a byte array.
 * //  w  w  w. ja  va  2s  .  c  om
 * @param document the document
 * @return the document as a byte array (UTF-8)
 * @throws IOException Exception while serializing the document
 */
public static byte[] toByteArray(Document document) throws IOException {

    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(512);

    // Pretty print the document to System.out
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(byteOut, format);
    writer.write(document);

    return byteOut.toByteArray();
}

From source file:net.sf.ginp.config.Configuration.java

License:Open Source License

/**
 *  Saves current settings to Disk./*from  w  w w  .  j  a va2  s  .c  o m*/
 */
public static void writeConfig() {
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileWriter(configfilelocation), format);
        writer.write(document);
        writer.close();
        log.info("Writen config file to disk.");

        // check It
        try {
            FileInputStream fis = new FileInputStream(new File(configfilelocation));
            SetupManager service = ModelUtil.getSetupManager();
            document = service.testValidConfig(fis);
            configOK = true;
        } catch (Exception ex) {
            log.error("Error parsing new config file", ex);
        }
    } catch (Exception e) {
        log.error("Error writing config file to disk.", e);
    }

    readConfig();
}

From source file:net.sf.ginp.setup.SetupManagerImpl.java

License:Open Source License

/**
 * @param visit/* www.jav  a 2  s  . co m*/
 * @param outputDocument
 * @throws UnsupportedEncodingException
 * @throws IOException generally, and specifically
 *  FileNotFoundException and UnsupportedEncodingException
 */
public final boolean writeConfig(final SetupVisit visit, final Document outputDocument) throws IOException {
    FileOutputStream outputStream = null;
    XMLWriter write = null;
    boolean success = false;

    try {
        String confFileLocation = Configuration.getConfigfilelocation();

        if (confFileLocation == null) {
            log.warn("Configuration.getConfigfilelocation() is NULL!");
        }

        // make sure directory exists and be verbose about this to log.
        File configDir = new File(Configuration.getConfigfilelocationPath());

        if (!configDir.exists()) {
            if (configDir.mkdirs()) {
                log.info("Config directory created at: " + configDir.getAbsolutePath());
            } else {
                log.error("Can not create config directory at: " + configDir.getAbsolutePath());
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Config dir exists at:" + configDir.getAbsolutePath());
            }
        }

        // warn about overwrite
        File configFile = new File(confFileLocation);

        if (configFile.exists()) {
            log.warn("Overwriting config file at: " + configFile.getAbsolutePath());
        }

        outputStream = new FileOutputStream(configFile, false);

        OutputFormat format = OutputFormat.createPrettyPrint();
        write = new XMLWriter(outputStream, format);
        write.write(outputDocument);
        success = configFile.exists();
    } catch (Exception ex) {
        log.error("Error writing config.", ex);
    } finally {
        if (write != null) {
            write.flush();
            write.close();
        }

        if (outputStream != null) {
            outputStream.flush();
            outputStream.close();
        }
    }

    return success;
}

From source file:net.sf.jguard.core.util.XMLUtils.java

License:Open Source License

/**
 * write the updated configuration to the XML file in the UTF-8 format.
 *
 * @param url      URL of the file to write
 * @param document dom4j Document/*  www.  j a va  2 s . c  o m*/
 * @throws IOException
 */
public static void write(URL url, Document document) throws IOException {
    OutputFormat outFormat = OutputFormat.createPrettyPrint();
    if (document.getXMLEncoding() != null) {
        outFormat.setEncoding(document.getXMLEncoding());
    } else {
        outFormat.setEncoding(UTF_8);
    }
    XMLWriter out = new XMLWriter(new BufferedOutputStream(new FileOutputStream(url.getPath())), outFormat);
    out.write(document);
    out.flush();
    out.close();
}

From source file:net.sf.jguard.ext.authentication.manager.XmlAuthenticationManager.java

License:Open Source License

public void writeAsXML(OutputStream outputStream, String encodingScheme) throws IOException {
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setEncoding(encodingScheme);
    XMLWriter writer = new XMLWriter(outputStream, outformat);
    writer.write(this.document);
    writer.flush();//from  w  w w.  j  a  va2 s .  c o  m
}

From source file:net.sf.jguard.ext.authentication.manager.XmlAuthenticationManager.java

License:Open Source License

public void exportAsXMLFile(String fileName) throws IOException {
    FileWriter fileWriter = null;
    try {//from  www  . j  av  a 2  s . c o m
        fileWriter = new FileWriter(fileName);
        XMLWriter xmlWriter = new XMLWriter(fileWriter, OutputFormat.createPrettyPrint());
        xmlWriter.write(document);
        xmlWriter.close();
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
    }

}