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:fr.gouv.culture.vitam.extract.ExtractInfo.java

License:Open Source License

public static void main(String[] args) {
    if (args.length == 0) {
        System.err.println("need file as target");
        return;/*from   w w w . ja v a 2 s . c  o m*/
    }
    StaticValues.initialize();
    Document document = DocumentFactory.getInstance().createDocument(StaticValues.CURRENT_OUTPUT_ENCODING);
    Element root = DocumentFactory.getInstance().createElement("extractkeywords");
    document.add(root);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(StaticValues.CURRENT_OUTPUT_ENCODING);
    XMLWriter writer = null;
    try {
        writer = new XMLWriter(System.out, format);
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        return;
    }
    for (String string : args) {
        File file = new File(string);
        recursive(file, string, root, StaticValues.config, writer);
    }
    if (StaticValues.config.argument.outputModel == VitamOutputModel.OneXML) {
        try {
            writer.write(document);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:fullyMeshedNet.FullyMeshedNet.java

License:Open Source License

@Override
public void exportToFile(File saveFile) {
    String extension = FrevoMain.getExtension(saveFile);
    if (extension.equals("net")) {
        System.out.println("Exporting Pajek network file to " + saveFile.getName());
        try {//from  www . j ava2 s  . c om
            // Create file
            FileWriter fstream = new FileWriter(saveFile);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("*Vertices " + this.nodes);
            out.newLine();
            // input neurons
            for (int i = 1; i < input_nodes + 1; i++) {
                out.write(i + " \"I" + i + "\"");
                out.newLine();
            }
            for (int h = input_nodes + 1; h < nodes - output_nodes + 1; h++) {
                out.write(h + " \"H" + (h - input_nodes) + "\"");
                out.newLine();
            }
            int a = 1;
            for (int o = nodes - output_nodes + 1; o < nodes + 1; o++) {
                out.write(o + " \"O" + a + "\"");
                out.newLine();
                a++;
            }

            // Edges
            out.write("*Edges");
            out.newLine();
            for (int n = 0; n < input_nodes - 1; n++) {
                for (int p = n + 1; p < input_nodes; p++) {
                    if (n != p) {
                        out.write((n + 1) + " " + (p + 1) + " " + 0);
                        out.newLine();
                    }
                }
            }

            for (int n = input_nodes; n < nodes; n++) {
                for (int from = 0; from < nodes; from++) {
                    out.write((n + 1) + " " + (from + 1) + " " + weight[from][n]);
                    out.newLine();
                }
            }

            // Close the output stream
            out.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    } else if (extension.equals("xml")) {
        System.out.println("Saving to XML");

        Document doc = DocumentHelper.createDocument();
        doc.addDocType("CompleteNetwork", null, System.getProperty("user.dir")
                + "//Components//Representations//CompleteNetwork//src//CompleteNetwork.dtd");
        Element cnetwork = doc.addElement("CompleteNetwork");
        this.exportToXmlElement(cnetwork);

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setLineSeparator(System.getProperty("line.separator"));

        try {
            saveFile.createNewFile();
            FileWriter out = new FileWriter(saveFile);
            BufferedWriter bw = new BufferedWriter(out);
            XMLWriter wr = new XMLWriter(bw, format);
            wr.write(doc);
            wr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:gestionecassa.backends.XmlDataBackend.java

License:Open Source License

@Override
public void saveArticlesList(ArticlesList lista) throws IOException {

    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("article_groups");
    Collection<ArticleGroup> groups = lista.getGroupsList();
    for (ArticleGroup group : groups) {
        Element tempGroup = root.addElement("group");

        tempGroup.addElement("name").addText(group.getName());
        tempGroup.addElement("id").addText(group.getId() + "");

        for (Article art : group.getList()) {
            Element tempArt = tempGroup.addElement("article");

            tempArt.addElement("name").addText(art.getName());
            tempArt.addElement("price").addText(art.getPrice() + "");
            tempArt.addElement("id").addText(art.getId() + "");

            if (art.hasOptions()) {
                tempArt.addAttribute("options", "true");
                Element tempOpzioni = tempArt.addElement("options");

                Collection<ArticleOption> options = art.getOptions();
                for (ArticleOption option : options) {
                    tempOpzioni.addElement("option").addText(option.getName());
                } //FIXME aggiungi altre parti di ArticleOption
            } else {
                tempArt.addAttribute("options", "false");
            }//  w  ww  .j a  v a  2 s  .com
        }
    }

    // for debug purposes
    OutputFormat format = OutputFormat.createPrettyPrint();

    // lets write to a file
    XMLWriter writer = new XMLWriter(new FileWriter(ArtListFile), format);
    //XMLWriter writer = new XMLWriter(new FileWriter(fileName));
    writer.write(document);
    writer.close();
}

From source file:gestionecassa.backends.XmlDataBackend.java

License:Open Source License

@Override
public void saveAdminsList(Collection<Admin> lista) throws IOException {

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

    for (Admin admin : lista) {
        Element tempAdmin = root.addElement("admin");
        tempAdmin.addElement("id").addText(admin.getId() + "");
        tempAdmin.addElement("username").addText(admin.getUsername());
        tempAdmin.addElement("password").addText(admin.getPassword());
    }/*from ww w .  j a  va 2 s  .c  om*/

    // for debug purposes
    OutputFormat format = OutputFormat.createPrettyPrint();

    // lets write to a file
    XMLWriter writer = new XMLWriter(new FileWriter(AdminListFile), format);
    //XMLWriter writer = new XMLWriter(new FileWriter(fileName));
    writer.write(document);
    writer.close();
}

From source file:gestionecassa.backends.XmlDataBackend.java

License:Open Source License

@Override
public void saveCassiereList(Collection<Cassiere> lista) throws IOException {

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

    for (Cassiere cassiere : lista) {
        Element tempCassiere = root.addElement("cassiere");
        tempCassiere.addElement("id").addText(cassiere.getId() + "");
        tempCassiere.addElement("username").addText(cassiere.getUsername());
        tempCassiere.addElement("password").addText(cassiere.getPassword());
    }//w  w w.java2  s. c  o m

    // for debug purposes
    OutputFormat format = OutputFormat.createPrettyPrint();

    // lets write to a file
    XMLWriter writer = new XMLWriter(new FileWriter(CassiereListFile), format);
    //XMLWriter writer = new XMLWriter(new FileWriter(fileName));
    writer.write(document);
    writer.close();
}

From source file:gestionecassa.backends.XmlDataBackend.java

License:Open Source License

@Override
public void saveListOfOrders(String id, Collection<Order> list) throws IOException {

    String fileName = xmlDataPath + id + ".xml";

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

    for (Order order : list) {
        addOrderToElement(root, order);/* w  ww  . j  a  va2  s  . co m*/
    }

    // for debug purposes
    OutputFormat format = OutputFormat.createPrettyPrint();

    // lets write to a file
    XMLWriter writer = new XMLWriter(new FileWriter(fileName), format);
    //XMLWriter writer = new XMLWriter(new FileWriter(fileName));
    writer.write(document);
    writer.close();
}

From source file:gestionecassa.XmlPreferencesHandler.java

License:Open Source License

/**
 * Saves options to a file given by the option type
 * @param preferences /*from www  .j  a v a2s .co  m*/
 * @throws IOException
 */
public void savePrefs(DataType preferences) throws IOException {

    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("config");
    root.addAttribute("version", preferences.getVersion());
    root.addAttribute("application", preferences.getApplication());

    for (Field field : preferences.getClass().getFields()) {
        try {
            root.addElement(field.getName()).addText(field.get(preferences) + "");
        } catch (IllegalArgumentException ex) {
            logger.warn("campo sbagliato", ex);
        } catch (IllegalAccessException ex) {
            logger.warn("campo sbagliato", ex);
        }
    }

    // lets write to a file
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(preferences.getFileName()), format);
    writer.write(document);
    writer.close();
}

From source file:gov.guilin.util.SettingUtils.java

License:Open Source License

/**
 * /*from w ww.j av  a2s  . c om*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File guilinXmlFile = new ClassPathResource(CommonAttributes.XML_PATH).getFile();
        Document document = new SAXReader().read(guilinXmlFile);
        List<Element> elements = document.selectNodes("/guilin/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(guilinXmlFile);
            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:gr.abiss.calipso.util.XmlUtils.java

License:Open Source License

/**
 * Override that accepts an XML DOM Document
 * @param document XML as DOM Document// w ww .j a  v  a2  s .c  o m
 */
public static String getAsPrettyXml(Document document) {
    OutputFormat format = new OutputFormat(" ", true);
    format.setSuppressDeclaration(true);
    StringWriter out = new StringWriter();
    XMLWriter writer = new XMLWriter(out, format);
    try {
        try {
            writer.write(document);
        } finally {
            writer.close();
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    return out.toString().trim();
}

From source file:gtu._work.etc.HotnoteMakerUI.java

License:Open Source License

private void initGUI() {
    try {//  w w  w.  j  a v  a2 s  .  c o  m
        ToolTipManager.sharedInstance().setInitialDelay(0);
        BorderLayout thisLayout = new BorderLayout();
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(thisLayout);
        {
            jTabbedPane1 = new JTabbedPane();
            getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
            {
                jPanel1 = new JPanel();
                BorderLayout jPanel1Layout = new BorderLayout();
                jPanel1.setLayout(jPanel1Layout);
                jTabbedPane1.addTab("hott notes - checklist", null, jPanel1, null);
                {
                    jScrollPane1 = new JScrollPane();
                    jPanel1.add(jScrollPane1, BorderLayout.CENTER);
                    jScrollPane1.setPreferredSize(new java.awt.Dimension(612, 348));
                    {
                        checkListArea = new JTextArea();
                        jScrollPane1.setViewportView(checkListArea);
                        checkListArea.addMouseListener(new MouseAdapter() {

                            String randomColor() {
                                StringBuilder sb = new StringBuilder().append("#");
                                for (int ii = 0; ii < 6; ii++) {
                                    sb.append(RandomUtil.randomChar('a', 'b', 'c', 'd', 'f', '0', '1', '2', '3',
                                            '4', '5', '6', '7', '8', '9'));
                                }
                                return sb.toString();
                            }

                            void saveXml(Document document, File file) {
                                OutputFormat format = OutputFormat.createPrettyPrint();
                                format.setEncoding("utf-16");
                                XMLWriter writer = null;
                                try {
                                    writer = new XMLWriter(new FileWriter(file), format);
                                    writer.write(document);
                                } catch (IOException e) {
                                    JCommonUtil.handleException(e);
                                } finally {
                                    if (writer != null) {
                                        try {
                                            writer.close();
                                        } catch (IOException e) {
                                            JCommonUtil.handleException(e);
                                        }
                                    }
                                }
                            }

                            public void mouseClicked(MouseEvent evt) {
                                if (!JMouseEventUtil.buttonLeftClick(2, evt)) {
                                    return;
                                }

                                if (StringUtils.isEmpty(checkListArea.getText())) {
                                    JCommonUtil
                                            ._jOptionPane_showMessageDialog_error("checklist area is empty!");
                                    return;
                                }

                                File file = JCommonUtil._jFileChooser_selectFileOnly_saveFile();
                                if (file == null) {
                                    JCommonUtil._jOptionPane_showMessageDialog_error("file is not correct!");
                                    return;
                                }

                                //XXX
                                StringTokenizer tok = new StringTokenizer(checkListArea.getText(), "\t\n\r\f");
                                List<String> list = new ArrayList<String>();
                                String tmp = null;
                                for (; tok.hasMoreElements();) {
                                    tmp = ((String) tok.nextElement()).trim();
                                    System.out.println(tmp);
                                    list.add(tmp);
                                }
                                //XXX

                                Document document = DocumentHelper.createDocument();
                                Element rootHot = document.addElement("hottnote");
                                rootHot.addAttribute("creationtime",
                                        new Timestamp(System.currentTimeMillis()).toString());
                                rootHot.addAttribute("lastmodified",
                                        new Timestamp(System.currentTimeMillis()).toString());
                                rootHot.addAttribute("type", "checklist");
                                //appearence
                                Element appearenceE = rootHot.addElement("appearence");
                                appearenceE.addAttribute("alpha", "204");
                                Element fontE = appearenceE.addElement("font");
                                fontE.addAttribute("face", "Default");
                                fontE.addAttribute("size", "0");
                                Element styleE = appearenceE.addElement("style");
                                styleE.addElement("bg2color").addAttribute("color", randomColor());
                                styleE.addElement("bgcolor").addAttribute("color", randomColor());
                                styleE.addElement("textcolor").addAttribute("color", randomColor());
                                styleE.addElement("titlecolor").addAttribute("color", randomColor());
                                //behavior
                                rootHot.addElement("behavior");
                                //content
                                Element contentE = rootHot.addElement("content");
                                Element checklistE = contentE.addElement("checklist");
                                for (String val : list) {
                                    checklistE.addElement("item").addCDATA(val);
                                }
                                //desktop
                                Element desktopE = rootHot.addElement("desktop");
                                desktopE.addElement("position").addAttribute("x", RandomUtil.numberStr(3))
                                        .addAttribute("y", RandomUtil.numberStr(3));
                                desktopE.addElement("size").addAttribute("height", "200").addAttribute("width",
                                        "200");
                                //title
                                Element titleE = rootHot.addElement("title");
                                titleE.addCDATA(StringUtils.defaultIfEmpty(checkListTitle.getText(),
                                        DateFormatUtils.format(System.currentTimeMillis(), "dd/MM/yyyy")));

                                if (!file.getName().toLowerCase().endsWith(".hottnote")) {
                                    file = new File(file.getParentFile(), file.getName() + ".hottnote");
                                }

                                saveXml(document, file);
                                JCommonUtil._jOptionPane_showMessageDialog_info("completed!\n" + file);
                            }
                        });
                    }
                }
                {
                    checkListTitle = new JTextField();
                    checkListTitle.setToolTipText("title");
                    jPanel1.add(checkListTitle, BorderLayout.NORTH);
                }
            }
        }
        pack();
        this.setSize(633, 415);
    } catch (Exception e) {
        //add your error handling code here
        e.printStackTrace();
    }
}