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:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeSpecialTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) {
    XMLWriter output = null;

    ResultSet rsTable = null;//from w  w  w  .  j a  v  a  2s . c o  m

    Statement stTable = null;

    try {
        if (!(strPath.substring(strPath.length() - 1).equals(File.separator)
                || strPath.substring(strPath.length() - 1).equals("/"))) {
            strPath = strPath + File.separatorChar;
        }

        stTable = conTable.createStatement();

        //         String strSql = "select count(*) from " + strTableName
        //               + ", hm_domain where " + strTableName
        //               + ".owner=hm_domain.id and (" + strTableName + ".owner="
        //               + oDomain.getId().toString()
        //               + " or hm_domain.domainname='global')";
        //
        //         begin = System.currentTimeMillis();
        //
        //         rsTable = stTable.executeQuery(strSql);
        //
        //         dbtime += System.currentTimeMillis() - begin;
        //
        //         rsTable.next();
        //
        //         int iRowCount = rsTable.getInt(1);
        //
        //         rsTable.close();

        String strSql = "select id from " + strTableName + " where owner=" + oDomain.getId().toString()
                + " order by id asc limit 1";

        rsTable = stTable.executeQuery(strSql);

        long id = 0;
        if (rsTable.next())
            id = rsTable.getLong(1) - 1;
        //         else
        //            return;

        rsTable.close();

        int intFileCount = 0;
        String strId = String.valueOf(id);
        int intRecordNum;

        while (true) {
            intRecordNum = 0;
            //            strSql = "select * from " + strTableName
            //                  + " where owner=" + oDomain.getId().toString()
            //                  + " and id>" + strId +" order by id asc "+ " limit " + 5000;

            strSql = "select * from " + strTableName + " where id > " + strId + " and owner="
                    + oDomain.getId().toString() + " order by id asc " + " limit " + 5000;

            rsTable = stTable.executeQuery(strSql);

            Document document = DocumentHelper.createDocument();

            Element table = document.addElement("table").addAttribute("schema", strTableName);

            ResultSetMetaData rsmd = rsTable.getMetaData();

            int iCount = rsmd.getColumnCount();

            while (rsTable.next()) {
                intRecordNum++;
                Element row = table.addElement("row");

                for (int icol = 1; icol <= iCount; icol++) {
                    String newStr;

                    if (rsTable.getString(icol) == null) {
                        newStr = "NULL";
                    } else if ("null".equalsIgnoreCase(rsTable.getString(icol))) {
                        newStr = "_" + rsTable.getString(icol) + "_";
                    } else {
                        newStr = rsTable.getString(icol);
                    }

                    if ("id".equalsIgnoreCase(rsmd.getColumnName(icol))) {
                        strId = newStr;
                    }

                    if (1 == intRecordNum) {
                        row.addElement("field").addAttribute("name", rsmd.getColumnName(icol))
                                .addAttribute("value", newStr);
                    } else {
                        row.addElement("field").addAttribute("value", newStr);
                    }
                }
            }

            if (intRecordNum <= 0 && 0 != intFileCount)
                break;

            File file;

            if (intFileCount == 0) {
                file = new File(strPath + strTableName.toLowerCase() + ".xml");

            } else {
                file = new File(strPath + strTableName.toLowerCase() + "_" + intFileCount + ".xml");
            }
            intFileCount++;

            output = new XMLWriter(new FileOutputStream(file));

            output.write(document);

            output.close();

            if (5000 > intRecordNum)
                break;
        }

        rsTable.close();

        stTable.close();
    } catch (Exception ex) {
        BeLogTools.restoreLog(BeLogTools.ERROR, ex.getMessage());
    } finally {
        if (null != output) {
            try {
                output.close();
            } catch (Exception outex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, outex.getMessage());
            }
        }

        if (null != rsTable) {
            try {
                rsTable.close();
            } catch (Exception rsex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, rsex.getMessage());
            }
        }

        if (null != stTable) {
            try {
                stTable.close();
            } catch (Exception stex) {
                BeLogTools.restoreLog(BeLogTools.ERROR, stex.getMessage());
            }
        }
    }
}

From source file:com.amalto.workbench.utils.MDMServerHelper.java

License:Open Source License

private static boolean saveRootElement(Element rootElement) {
    try {//from w w w .  j  av a 2 s  .  c o  m
        XMLWriter writer = new XMLWriter(new FileWriter(MDMServerHelper.workbenchConfigFile));
        writer.write(rootElement.getDocument());
        writer.close();
        return true;
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        return false;
    }
}

From source file:com.amalto.workbench.utils.Util.java

License:Open Source License

public static String formatXsdSource(String xsdSource, boolean suppressDeclaration) {
    try {/*from   w w w .  java  2  s  . c o  m*/
        SAXReader reader = new SAXReader();
        org.dom4j.Document document = reader.read(new StringReader(xsdSource));
        StringWriter writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");//$NON-NLS-1$
        format.setSuppressDeclaration(suppressDeclaration);
        XMLWriter xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        String str = writer.toString();
        writer.close();
        xmlwriter.close();
        return str;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return xsdSource;

}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static void write(Document document, String filePath, String printMode, String encoding)
        throws IOException {

    OutputFormat format = null;//from w ww  .  j av a2  s.  co  m

    if (printMode.toLowerCase().equals("pretty")) {//$NON-NLS-1$
        // Pretty print the document
        format = OutputFormat.createPrettyPrint();
    } else if (printMode.toLowerCase().equals("compact")) {//$NON-NLS-1$
        // Compact format
        format = OutputFormat.createCompactFormat();
    }

    format.setEncoding(encoding);

    // lets write to a file
    XMLWriter writer = new XMLWriter(new FileOutputStream(filePath), format);

    // XMLWriter logger = new XMLWriter( System.out, format );

    writer.write(document);

    logger.info(Messages.bind(Messages.XmlUtil_Loginfo1, filePath));

    // logger.write( document );

    // logger.close();

    writer.close();
}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static String format(Document document, OutputFormat format, String encoding) {

    StringWriter writer = new StringWriter();

    format.setEncoding(encoding);//from  w  ww.  jav a  2  s .co  m

    format.setNewLineAfterDeclaration(false);
    // format.setSuppressDeclaration(suppressDeclaration);

    XMLWriter xmlwriter = new XMLWriter(writer, format);

    String result = ""; //$NON-NLS-1$

    try {

        xmlwriter.write(document);
        result = writer.toString().replaceAll("<\\?xml.*?\\?>", "").trim();//$NON-NLS-1$//$NON-NLS-2$
    } catch (Exception e) {
        log.error(e.getMessage(), e);

    } finally {
        try {
            if (xmlwriter != null)
                xmlwriter.close();

            if (writer != null)
                writer.close();
        } catch (IOException e) {

        }
    }

    return result;
}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static String formatXmlSource(String xmlSource) {

    SAXReader reader = new SAXReader();
    StringReader stringReader = new StringReader(xmlSource);
    StringWriter writer = null;//from   www. j  a v  a  2s . c o  m
    XMLWriter xmlwriter = null;
    String result = xmlSource;

    try {
        Document document = reader.read(stringReader);
        writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");//$NON-NLS-1$
        format.setIndentSize(4);
        format.setSuppressDeclaration(true);
        xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        result = writer.toString();
    } catch (Exception e) {

    } finally {

        try {
            if (stringReader != null)
                stringReader.close();

            if (xmlwriter != null)
                xmlwriter.close();

            if (writer != null)
                writer.close();

        } catch (Exception e) {

        }

    }
    return result;

}

From source file:com.app.util.SettingUtils.java

License:Open Source License

/**
 * //  w w w. ja  v a  2 s.  co  m
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File appXmlFile = new ClassPathResource(CommonAttributes.APP_XML_PATH).getFile();
        Document document = new SAXReader().read(appXmlFile);
        List<Element> elements = document.selectNodes("/app/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(appXmlFile);
            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:com.arthurh.xmergel.Xmergel.java

License:Open Source License

public void combine(List<Path> files)
        throws IOException, SAXException, TransformerException, DocumentException {
    Document finalDocument = this.saxReader.read(files.get(0).toFile());
    for (Path file : files) {
        finalDocument = this.xdtTransformer.transform(finalDocument, this.saxReader.read(file.toFile()));
    }/*from   w w w.j  av a2  s . c o m*/
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter xmlWriter = new XMLWriter(new FileWriter(this.resultFile.toFile()), format);
    xmlWriter.write(finalDocument);
    xmlWriter.close();
}

From source file:com.beetle.framework.resource.container.ContainerConfig.java

License:Apache License

/**
 * /*from   ww  w.j ava  2s. c o  m*/
 *
 * @param tagname --??
 * @param key     --??
 * @throws Exception
 */
public static void setContainValue(String tagname, String key, String value) throws Exception {
    Document doc = XMLReader.getXmlDoc(sysconfigFileName);
    Node node = doc.selectSingleNode(XMLReader.convertPath(tagname));
    if (node != null) {
        @SuppressWarnings("rawtypes")
        Iterator it = node.selectNodes("item").iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            String id = e.valueOf("@name");
            if (id != null && id.equals(key)) {
                e.addAttribute("value", value);
                break;
            }
        }
    }
    File f = new File(sysconfigFileName);
    if (f.exists()) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        FileOutputStream fos = new FileOutputStream(f);
        XMLWriter writer = new XMLWriter(fos, format);
        writer.write(doc);
        writer.close();
    } else {
        AppLogger.getInstance(ContainerConfig.class).error("??jarxml");
    }
}

From source file:com.beetle.framework.util.file.XMLReader.java

License:Apache License

public static void setTagContent(String xmlFileName, String TagPath, String value) {
    synchronized (writeLock) {
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;
        try {/*from  w  w w .ja  v  a 2s . c o  m*/
            Document doc = reader.read(new File(xmlFileName));
            Node node = doc.selectSingleNode(convertPath(TagPath));
            if (node != null) {
                node.setText(value);
            }
            writer = new XMLWriter(new FileWriter(xmlFileName));
            writer.write(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            reader = null;
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}