Example usage for org.dom4j.io XMLWriter XMLWriter

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

Introduction

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

Prototype

public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException 

Source Link

Usage

From source file:mesquite.lib.XMLUtil.java

License:Open Source License

public static String getDocumentAsXMLString(Document doc, boolean escapeText) {
    try {// w  ww  . ja va2 s.c o m
        String encoding = doc.getXMLEncoding();

        if (encoding == null)
            encoding = "UTF-8";

        Writer osw = new StringWriter();
        OutputFormat opf = new OutputFormat("  ", true, encoding);
        XMLWriter writer = new XMLWriter(osw, opf);
        writer.setEscapeText(escapeText);
        writer.write(doc);
        writer.close();
        return osw.toString();
    } catch (IOException e) {
        MesquiteMessage.warnProgrammer("XML Document could not be returned as string.");
    }
    return null;
}

From source file:mesquite.lib.XMLUtil.java

License:Open Source License

public static String getElementAsXMLString(Element doc, String encoding, boolean escapeText) {
    try {/*  w  ww  .  j  a va  2 s  . c  om*/

        Writer osw = new StringWriter();
        OutputFormat opf = new OutputFormat("  ", true, encoding);
        XMLWriter writer = new XMLWriter(osw, opf);
        writer.setEscapeText(escapeText);
        writer.write(doc);
        writer.close();
        return osw.toString();
    } catch (IOException e) {
        MesquiteMessage.warnProgrammer("XML Document could not be returned as string.");
    }
    return null;
}

From source file:mesquite.lib.XMLUtil.java

License:Open Source License

public static String getDocumentAsXMLString2(Document doc) {
    try {//from   w  w w .j  ava 2 s  .  com
        String encoding = doc.getXMLEncoding();

        //if (encoding == null)
        //   encoding = "UTF-8";

        Writer osw = new StringWriter();
        OutputFormat opf = new OutputFormat("  ", true);
        XMLWriter writer = new XMLWriter(osw, opf);
        writer.write(doc);
        writer.close();
        return osw.toString();
    } catch (IOException e) {
        MesquiteMessage.warnProgrammer("XML Document could not be returned as string.");
    }
    return null;
}

From source file:net.bpfurtado.ljcolligo.util.Util.java

License:Open Source License

public static void save(Element root, File file) {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setIndentSize(4);/*from ww w  .  j  a v a2  s .  c o  m*/
    format.setEncoding("UTF-8");
    format.setNewlines(true);
    format.setLineSeparator(System.getProperty("line.separator"));
    XMLWriter writer;
    try {
        writer = new XMLWriter(new FileWriter(file), format);
        writer.startDocument();
        writer.write(root);
        writer.close();
        logger.debug("All entries saved to file [" + file.getAbsolutePath() + "]");
    } catch (Exception e) {
        throw new LJColligoException("File [" + file.getAbsolutePath() + "]", e);
    }
}

From source file:net.bpfurtado.tas.model.persistence.XMLAdventureWriter.java

License:Open Source License

private File save(Document doc) {
    try {/*w w w .  j  a  v a  2  s  .c o  m*/
        if (!saveFile.getName().endsWith(".adv.xml")) {
            saveFile = new File(saveFile.getAbsolutePath() + ".adv.xml");
        }

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("ISO-8859-1");
        format.setNewlines(true);
        format.setLineSeparator(System.getProperty("line.separator"));
        XMLWriter writer = new XMLWriter(new FileWriter(saveFile), format);

        writer.write(doc);
        writer.close();

        return saveFile;
    } catch (Exception e) {
        throw new AdventureException("Error writing adventure", e);
    }
}

From source file:net.bpfurtado.tas.runner.savegame.SaveGamePersister.java

License:Open Source License

public static void write(Document xml, File saveGameFile, SaveGameListener saveGameListener) {
    try {/*from w ww .j a va  2  s  .com*/
        saveGameListener.log("Saving game to file: [" + saveGameFile + "]...");

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("ISO-8859-1");
        format.setNewlines(true);
        format.setLineSeparator(System.getProperty("line.separator"));
        XMLWriter writer = new XMLWriter(new FileWriter(saveGameFile), format);

        writer.write(xml);
        writer.flush();
        writer.close();

        saveGameListener.log("Game saved!");
    } catch (Exception e) {
        throw new AdventureException("Error writing Save Game", e);
    }
}

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.// w ww .  j  av  a2 s.co  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.erdfelt.android.sdkfido.project.FilteredFileUtil.java

License:Apache License

public static void write(Document pom, File outputFile) throws OutputProjectException {
    XMLWriter writer = null;//from   www. j a  v  a  2 s.c o m
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(outputFile);
        OutputFormat pretty = OutputFormat.createPrettyPrint();
        pretty.setIndentSize(2);
        writer = new XMLWriter(out, pretty);
        writer.write(pom);
        writer.flush();
    } catch (UnsupportedEncodingException e) {
        throw new OutputProjectException("Unable to write xml to file: " + outputFile, e);
    } catch (IOException e) {
        throw new OutputProjectException("Unable to write xml to file: " + outputFile, e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

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

License:Open Source License

/**
 * //from www  . ja v a  2s.  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 {//from  w w w  .  ja  va2s  .c  om
            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);
    }
}