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:net.daimonin.client3d.editor.main.Editor3D.java

License:Open Source License

/**
* Builds image groups/categories by file name, creates and writes the XML
* containing all images with their attributes.
* 
* @param fileNameImageSet Name of resulting PNG.
* @param fileNameXML Name of the XML.//from   ww  w .j a  v  a  2 s .c o  m
* @throws IOException IOException.
*/
private static void writeXML(final String fileNameImageSet, final String fileNameXML) throws IOException {

    // group ImageSetImages by name/category.
    // the left side of the image file name up to the '_' is taken as the
    // category
    Hashtable<String, List<ImageSetImage>> names = new Hashtable<String, List<ImageSetImage>>();
    for (int i = 0; i < images.size(); i++) {
        if (!names.containsKey(images.get(i).getName())) {
            List<ImageSetImage> values = new ArrayList<ImageSetImage>();
            values.add(images.get(i));
            names.put(images.get(i).getName(), values);
        } else {
            names.get(images.get(i).getName()).add(images.get(i));
        }
    }

    // check if every category has a default state (except FontExtensions)
    Enumeration<String> keys2 = names.keys();
    while (keys2.hasMoreElements()) {
        List<ImageSetImage> img2 = names.get(keys2.nextElement());
        if (!"fontextensions".equals(img2.get(0).getName())) {
            boolean hasDefault = false;
            for (int i = 0; i < img2.size(); i++) {
                if ("default".equals(img2.get(i).getState().toLowerCase())) {
                    hasDefault = true;
                }
            }
            if (!hasDefault) {
                printError("WARNING: image category '" + img2.get(0).getName()
                        + "' has no image with a default state!");
            }
        }
    }

    // create the XML structure
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("ImageSet").addAttribute("file", fileNameImageSet);

    List<ImageSetImage> fntex = names.get("fontextensions");
    if (fntex != null) {
        Element category = root.addElement("ImageFntExt").addAttribute("name", fntex.get(0).getName());
        for (int i = 0; i < fntex.size(); i++) {
            category.addElement("State").addAttribute("name", fntex.get(i).getState())
                    .addAttribute("posX", String.valueOf(fntex.get(i).getPosX() + fntex.get(i).getBorderSize()))
                    .addAttribute("posY", String.valueOf(fntex.get(i).getPosY() + fntex.get(i).getBorderSize()))
                    .addAttribute("width", String.valueOf(fntex.get(i).getWidth()))
                    .addAttribute("height", String.valueOf(fntex.get(i).getHeight()));
        }
        names.remove("fontextensions");
    }

    List<ImageSetImage> mouse = names.get("mousecursor");
    if (mouse != null) {
        Element category = root.addElement("Image").addAttribute("name", mouse.get(0).getName())
                .addAttribute("width", String.valueOf(mouse.get(0).getImage().getWidth()))
                .addAttribute("height", String.valueOf(mouse.get(0).getImage().getHeight()))
                .addAttribute("alpha", mouse.get(0).getImage().getColorModel().hasAlpha() ? String.valueOf(1)
                        : String.valueOf(0));

        for (int i = 0; i < mouse.size(); i++) {
            checkImageSameDimension(mouse.get(0), mouse.get(i),
                    "Images of same category have different dimension");
            checkImageSameAlpha(mouse.get(0), mouse.get(i), "Images of same category have different alpha");
            category.addElement("State").addAttribute("name", mouse.get(i).getState())
                    .addAttribute("posX", String.valueOf(mouse.get(i).getPosX() + mouse.get(i).getBorderSize()))
                    .addAttribute("posY",
                            String.valueOf(mouse.get(i).getPosY() + mouse.get(i).getBorderSize()));
        }
        names.remove("mousecursor");
    }

    Enumeration<String> keys = names.keys();
    while (keys.hasMoreElements()) {
        List<ImageSetImage> img = names.get(keys.nextElement());
        Element category = root.addElement("Image").addAttribute("name", img.get(0).getName())
                .addAttribute("width", String.valueOf(img.get(0).getImage().getWidth()))
                .addAttribute("height", String.valueOf(img.get(0).getImage().getHeight()))
                .addAttribute("alpha", img.get(0).getImage().getColorModel().hasAlpha() ? String.valueOf(1)
                        : String.valueOf(0));

        for (int i = 0; i < img.size(); i++) {
            checkImageSameDimension(img.get(0), img.get(i), "Images of same category have different dimension");
            checkImageSameAlpha(img.get(0), img.get(i), "Images of same category have different alpha");
            category.addElement("State").addAttribute("name", img.get(i).getState())
                    .addAttribute("posX", String.valueOf(img.get(i).getPosX() + img.get(i).getBorderSize()))
                    .addAttribute("posY", String.valueOf(img.get(i).getPosY() + img.get(i).getBorderSize()));
        }
    }

    // write the XML
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(fileNameXML), format);
    writer.write(document);
    writer.close();
    printInfo(System.getProperty("user.dir") + "/" + imagesetxml + " created");
}

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

License:Open Source License

/**
 * //from   w w  w  . ja v a 2s . co m
 * 
 * @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.osxx.util.SettingUtils.java

License:Open Source License

/**
 * //from w w w .j a  v a  2  s.  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.ginp.config.Configuration.java

License:Open Source License

/**
 *  Saves current settings to Disk.//from   ww w.ja v  a  2s . 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// w  w w  .  ja v a 2  s. c  om
 * @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//from  w  w w  . j a  va 2s . 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 exportAsXMLFile(String fileName) throws IOException {
    FileWriter fileWriter = null;
    try {//from   ww  w.jav  a  2s  .  c om
        fileWriter = new FileWriter(fileName);
        XMLWriter xmlWriter = new XMLWriter(fileWriter, OutputFormat.createPrettyPrint());
        xmlWriter.write(document);
        xmlWriter.close();
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
    }

}

From source file:net.sf.jguard.ext.authorization.manager.XmlAuthorizationManager.java

License:Open Source License

public void exportAsXMLFile(String fileName) throws IOException {
    XMLWriter xmlWriter = null;
    FileWriter fileWriter = null;
    try {//from  w w  w.  j a  va  2  s .com
        fileWriter = new FileWriter(fileName);
        xmlWriter = new XMLWriter(fileWriter, OutputFormat.createPrettyPrint());
        xmlWriter.write(document);

    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
        if (xmlWriter != null) {
            xmlWriter.close();
        }
    }
}

From source file:net.sf.jvifm.model.AppStatus.java

License:Open Source License

public static boolean writeAppStatus() {

    try {/*from   ww w  . java2s.  c  om*/

        String[][] currentPath = Main.fileManager.getAllCurrentPath();
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("tabs");

        for (int i = 0; i < currentPath.length; i++) {
            Element tabElement = root.addElement("tab");
            tabElement.addAttribute("left", currentPath[i][0]);
            tabElement.addAttribute("right", currentPath[i][1]);
        }

        FileOutputStream fos = new FileOutputStream(appStatusPath);
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        outformat.setEncoding("UTF-8");
        BufferedOutputStream out = new BufferedOutputStream(fos);
        XMLWriter writer = new XMLWriter(out, outformat);
        writer.write(document);
        writer.flush();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;

}

From source file:net.sf.jvifm.model.BookmarkManager.java

License:Open Source License

@SuppressWarnings("unchecked")
public void store() {

    try {/*from  w w  w  . j  ava2  s .c o  m*/

        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("bookmarks");

        for (Iterator it = bookmarkList.iterator(); it.hasNext();) {

            Bookmark bm = (Bookmark) it.next();

            Element bookmarkElement = root.addElement("bookmark");

            bookmarkElement.addElement("name").addText(bm.getName());
            bookmarkElement.addElement("path").addText(bm.getPath());
            if (bm.getKey() != null)
                bookmarkElement.addElement("key").addText(bm.getKey());

        }

        FileOutputStream fos = new FileOutputStream(storePath);
        OutputFormat outformat = OutputFormat.createPrettyPrint();

        outformat.setEncoding("UTF-8");
        BufferedOutputStream out = new BufferedOutputStream(fos);
        XMLWriter writer = new XMLWriter(out, outformat);

        writer.write(document);
        writer.flush();
        writer.close();
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}