Example usage for org.dom4j Document addElement

List of usage examples for org.dom4j Document addElement

Introduction

In this page you can find the example usage for org.dom4j Document addElement.

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:com.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeCommonTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) {
    XMLWriter output = null;/*from  w ww  .j  av  a  2  s.c o  m*/

    ResultSet rsTable = null;

    Statement stTable = null;

    try {
        stTable = conTable.createStatement();

        boolean bOwnerFlag = false;

        String strSql = "select * from " + strTableName + " limit 1";

        rsTable = stTable.executeQuery(strSql);

        rsTable.next();

        ResultSetMetaData rsmd = rsTable.getMetaData();

        int iColumnCount = rsmd.getColumnCount();

        for (int i = 1; i <= iColumnCount; ++i) {
            if (rsmd.getColumnName(i).equalsIgnoreCase("owner")) {
                bOwnerFlag = true;

                break;
            }
        }

        rsTable.close();

        //         if (!bOwnerFlag) {
        //            strSql = "select count(*) from " + strTableName;
        //         } else {
        //            strSql = "select count(*) from " + strTableName
        //                  + ", hm_domain where " + strTableName
        //                  + ".owner=hm_domain.id and (" + strTableName
        //                  + ".owner=" + oDomain.getId().toString()
        //                  + " or hm_domain.domainname='global')";
        //         }

        //         rsTable = stTable.executeQuery(strSql);

        //         rsTable.next();

        //         int iRowCount = rsTable.getInt(1);

        //         rsTable.close();

        int intFileCount = 0;
        int intRecordNum;

        strSql = "select id from hm_domain where domainname='global'";
        rsTable = stTable.executeQuery(strSql);
        long global_id = 0;

        if (rsTable.next())
            global_id = rsTable.getLong(1);

        String strId = String.valueOf(global_id);

        while (true) {

            intRecordNum = 0;
            //for (int i = 0; i < iRowCount || i == 0; i = i + 5000) {

            //            if (!bOwnerFlag) {
            //               strSql = "select * from " + strTableName + " limit " + 5000
            //                     + " offset " + i;
            //            } else {
            //               strSql = "select " + strTableName + ".* from "
            //                     + strTableName + ", hm_domain where "
            //                     + strTableName + ".owner=hm_domain.id and ("
            //                     + strTableName + ".owner="
            //                     + oDomain.getId().toString()
            //                     + " or hm_domain.domainname='global')" + " limit "
            //                     + 5000 + " offset " + i;
            //            }

            if (!bOwnerFlag) {
                strSql = "select * from " + strTableName + " limit " + 5000 + " offset " + intFileCount * 5000;
            } else {
                strSql = "select * from " + strTableName + " where owner=" + oDomain.getId().toString()
                        + " or owner=" + strId + " limit " + 5000 + " offset " + intFileCount * 5000;
            }

            rsTable = stTable.executeQuery(strSql);

            Document document = DocumentHelper.createDocument();

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

            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);
                    }

                    //                  row.addElement("field").addAttribute("name",
                    //                        rsmd.getColumnName(icol)).addAttribute("value",
                    //                        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.ah.be.admin.adminBackupUnit.AhBackupTool.java

private void storeSpecialTable(String strTableName, String strPath, HmDomain oDomain, Connection conTable) {
    XMLWriter output = null;/*w w w .j  a va2s  .c  o  m*/

    ResultSet rsTable = null;

    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.ah.be.ls.stat.StatManager.java

public Document stat() throws DocumentException, IOException, Exception {
    log.info("StatManager", "stats.size=" + stats.size());
    if (stats == null || stats.isEmpty()) {
        throw new Exception("no stat config");
    }//from  w w  w  .  j  a  v a2s  .c  o  m
    boolean isHMOL = NmsUtil.isProduction();
    boolean isHM = !NmsUtil.isHostedHMApplication();
    if (!isHMOL && !isHM) {
        log.warn("StatManager", "the server is not production HMOL or Stand alone HM, do not do data mining.");
        return null;
    }

    Document document = generateDocumentTemplate();

    Map<String, List<Element>> entity = new HashMap<String, List<Element>>();

    getTargetedHiveApVhmMapping();

    if (isHMOL) {// HMOL
        vhms = getHmolVhmList();
        if (vhms.isEmpty()) {
            log.warn("StatManager", "the server do not have any HMOL customer, do not do data mining.");
            return null;
        }
    }

    for (StatConfig stat : stats) {
        Map<String, Element> featureElements = buildOfDocumentFragment(stat, isHMOL);
        if (null == featureElements) {
            continue;
        }
        // store all document fragment
        for (String key : featureElements.keySet()) {
            List<Element> elements = entity.get(key);
            if (null == elements) {
                elements = new ArrayList<Element>();
                entity.put(key, elements);
            }
            elements.add(featureElements.get(key));
        }
    }

    // generate document
    if (isHMOL) {
        // create document root element
        Element root = document.addElement("stats");
        root.addAttribute("version", VERSION);
        for (String vhmId : entity.keySet()) {
            Element vhmRoot = createVhmStatElement(vhmId);
            List<Element> elements = entity.get(vhmId);
            for (Element element : elements) {
                vhmRoot.add(element);
            }
            root.add(vhmRoot);
        }
    } else {
        // fetch system id
        String systemId = "";
        LicenseInfo licenseInfo = HmBeLicenseUtil.getLicenseInfo();
        if (licenseInfo != null && null != licenseInfo.getSystemId()) {
            systemId = licenseInfo.getSystemId();
        }
        // create document root element
        Element root = document.addElement("stat");
        root.addAttribute("version", VERSION);
        root.addAttribute("system-id", systemId);
        List<Element> elements = entity.get("HM");
        if (null != elements) {
            for (Element element : elements) {
                root.add(element);
            }
        }
    }
    return document;
}

From source file:com.ah.ui.actions.config.USBModemAction.java

private Document generateExportDoc() {
    List<USBModem> usbModemLst = QueryUtil.executeQuery(USBModem.class, null, null, null, this);
    if (usbModemLst.isEmpty()) {
        return null;
    }/*w w  w  . ja  va  2s .  c om*/
    Document document = DocumentHelper.createDocument();
    Element modems = document.addElement("hiveos-modem-support-list").addElement("modems");
    for (USBModem usbModem : usbModemLst) {
        Element modem = modems.addElement("modem").addAttribute("id", usbModem.getModemName());
        modem.addElement("display").addAttribute("name", usbModem.getDisplayName()).addAttribute("type",
                usbModem.getDisplayType());
        modem.addElement("usb-info").addAttribute("vendor-id", usbModem.getUsbVendorId())
                .addAttribute("product-id", usbModem.getUsbProductId())
                .addAttribute("module", usbModem.getUsbModule());
        modem.addElement("hiveos-version").addAttribute("min", usbModem.getHiveOSVersionMin());
        //Element strengthCheck = modem.addElement("signal-strength-check");
        //if (usbModem.getUsbSignalStrengthCheckList() != null && usbModem.getUsbSignalStrengthCheckList().size()> 0) {
        //   strengthCheck.addAttribute("type", usbModem.getUsbSignalStrengthCheckList().get(0).getType());
        //   strengthCheck.addElement("serial-port").addAttribute("value", usbModem.getUsbSignalStrengthCheckList().get(0).getSerialPort());
        //   strengthCheck.addElement("check-cmd").addAttribute("value", usbModem.getUsbSignalStrengthCheckList().get(0).getCheckCmd());
        //}
        Element connect = modem.addElement("connect").addAttribute("type", usbModem.getConnectType());
        connect.addElement("serial-port").addAttribute("value", usbModem.getSerialPort());
        connect.addElement("dialstring").addAttribute("value", usbModem.getDailupNumber());
        connect.addElement("apn").addAttribute("value", usbModem.getApn());
        Element userAuth = connect.addElement("user-auth").addAttribute("type", usbModem.getAuthType());
        if ("password".equals(usbModem.getAuthType())) {
            userAuth.addElement("username").addAttribute("value", usbModem.getUserId());
            userAuth.addElement("password").addAttribute("value", usbModem.getPassword());
        }
        //modem.addElement("usepeerdns").addAttribute("value", usbModem.isUsePeerDns()?"true":"false");
    }

    return document;
}

From source file:com.alkacon.opencms.feeder.CmsFeedXmlContentHandler.java

License:Open Source License

/**
 * @see org.opencms.xml.content.CmsDefaultXmlContentHandler#validateAppinfoElement(org.dom4j.Element)
 *///from   ww  w .j  a  va2s. c o  m
protected void validateAppinfoElement(Element appinfoElement) throws CmsXmlException {

    // create a document to validate
    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement(APPINFO_APPINFO);
    // attach the default appinfo schema
    root.add(I_CmsXmlSchemaType.XSI_NAMESPACE);
    root.addAttribute(I_CmsXmlSchemaType.XSI_NAMESPACE_ATTRIBUTE_NO_SCHEMA_LOCATION,
            FEED_APPINFO_SCHEMA_SYSTEM_ID);
    // append the content from the appinfo node in the content definition 
    root.appendContent(appinfoElement);
    // now validate the document with the default appinfo schema
    CmsXmlUtils.validateXmlStructure(doc, CmsEncoder.ENCODING_UTF_8, new CmsXmlEntityResolver(null));
}

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

License:Open Source License

private static Element getRootElement() {
    Document logininfoDocument;
    Element rootElement;//w w  w.j a v  a 2s  . co  m
    if (new File(workbenchConfigFile).exists()) {
        try {
            SAXReader reader = new SAXReader();
            logininfoDocument = reader.read(new File(workbenchConfigFile));
        } catch (DocumentException e) {
            log.error(e.getMessage(), e);
            return null;
        }
        rootElement = logininfoDocument.getRootElement();
    } else {
        logininfoDocument = DocumentHelper.createDocument();
        rootElement = logininfoDocument.addElement(ROOT);
    }
    return rootElement;
}

From source file:com.apicloud.commons.model.Config.java

License:Open Source License

public static Document getDocument(Config config) {
    Document document = XMLUtil.createDocument();
    Element rootElement = document.addElement("widget");
    rootElement.addAttribute("id", config.getId());
    rootElement.addAttribute("version", config.getVersion());
    createGenralElement(rootElement, config);
    createPreferenceElement(rootElement, config);
    createPermissionElement(rootElement, config);
    createAccessElement(rootElement, config);
    createFeatureElement(rootElement, config);
    return document;
}

From source file:com.apicloud.commons.model.Config.java

License:Open Source License

public static File saveXml(Config config, File file) {
    if (!file.exists()) {
        try {/*  ww w.j a  va2 s . c o  m*/
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Document document = XMLUtil.createDocument();
    Element rootElement = document.addElement("widget");
    rootElement.addAttribute("id", config.getId());
    rootElement.addAttribute("version", config.getVersion());
    createGenralElement(rootElement, config);
    createPreferenceElement(rootElement, config);
    createPermissionElement(rootElement, config);
    createAccessElement(rootElement, config);
    createFeatureElement(rootElement, config);
    try {
        XMLUtil.saveXml(file, document);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}

From source file:com.apicloud.commons.model.Feature.java

License:Open Source License

public static File saveXml(List<Feature> features, File file) {
    if (!file.exists()) {
        try {//from w  ww . jav  a2  s.  com
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Document document = XMLUtil.createDocument();
    Element rootElement = document.addElement("Features");
    createFeatureElement(rootElement, features);
    try {
        XMLUtil.saveXml(file, document);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}

From source file:com.apicloud.commons.model.Feature.java

License:Open Source License

public static File saveXml2(List<Feature> features, File file) {
    if (!file.exists()) {
        try {/*from ww w .  j a v  a  2  s.c  o  m*/
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Document document = XMLUtil.createDocument();
    Element rootElement = document.addElement("Features");
    createFeatureElement2(rootElement, features);
    try {
        XMLUtil.saveXml(file, document);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}