Example usage for org.dom4j.io SAXReader read

List of usage examples for org.dom4j.io SAXReader read

Introduction

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

Prototype

public Document read(InputSource in) throws DocumentException 

Source Link

Document

Reads a Document from the given InputSource using SAX

Usage

From source file:cn.kee.engine.common.SystemInitServlet.java

private Document readerDom(File flie) {
    Document doc = null;//from  w ww  .  ja v a2  s .c o  m
    SAXReader reader = new SAXReader();
    reader.setValidation(false);
    reader.setEntityResolver(new NoOpEntityResolver());
    try {
        doc = reader.read(flie);
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return doc;
}

From source file:com.adobe.ac.maven.ncss.NcssReportMojo.java

License:Apache License

private Document loadDocument(final File file, final String encoding) throws DocumentException {
    final SAXReader saxReader = new SAXReader();
    if (encoding != null) {
        saxReader.setEncoding(encoding);
        getLog().debug("Loading xml file with encoding : " + encoding);
    }// w  w  w. j  ava  2s .  c o  m
    return saxReader.read(file);
}

From source file:com.adspore.splat.xep0060.PublishedItem.java

License:Open Source License

/**
 * Returns the payload included when publishing the item. A published item may or may not
 * have a payload. Transient nodes that are configured to not broadcast payloads may allow
 * published items to have no payload.//from   w w  w.  j a v a  2  s .  c  o  m
 *
 * @return the payload included when publishing the item or <tt>null</tt> if none was found.
 */
public Element getPayload() {
    if (payload == null && payloadXML != null) {
        synchronized (this) {
            if (payload == null) {
                // payload initialized as XML string from DB
                SAXReader xmlReader = null;
                try {
                    xmlReader = xmlReaders.take();
                    payload = xmlReader.read(new StringReader(payloadXML)).getRootElement();
                } catch (Exception ex) {
                    log.error("Failed to parse payload XML", ex);
                } finally {
                    if (xmlReader != null) {
                        xmlReaders.add(xmlReader);
                    }
                }
            }
        }
    }
    return payload;
}

From source file:com.ah.be.ls.stat.StatManager.java

private static List<StatConfig> readStatConfig() {
    if (stats != null && !stats.isEmpty()) {
        return stats;
    }/*from   w ww .  j av a 2 s.c  om*/
    stats = new ArrayList<StatConfig>();
    // read from stat-config.xml
    SAXReader reader = new SAXReader();
    File ff = new File(System.getenv("HM_ROOT") + "/resources/stat-config.xml");
    if (!ff.exists()) {
        // for test
        ff = new File("stat-config.xml");
    }
    try {
        Document doc = reader.read(ff);

        Element roota = doc.getRootElement();
        log.info("StatManager", "roota..nodcount=" + roota.nodeCount());

        Iterator<?> iters = roota.elementIterator("feature");
        while (iters.hasNext()) {
            StatConfig stat = new StatConfig();
            Element foo = (Element) iters.next();
            if (foo.attribute("ignore") != null) {
                continue;
            }
            stat.setFeatureId(Integer.valueOf(foo.attributeValue("id")));
            stat.setFeatureName(foo.attributeValue("name"));

            Element e2 = foo.element("bo-class");
            Element e4 = foo.element("search-rule");
            stat.setBoClassName(e2.attributeValue("name"));
            stat.setSearchRule(e4.attributeValue("value"));
            stat.setSearchType(e4.attributeValue("type"));

            stats.add(stat);
        }

        return stats;
    } catch (ClassNotFoundException e) {
        log.error("StatManager", "readStatConfig: ClassNotFoundException", e);
    } catch (Exception e) {
        log.error("StatManager", "readStatConfig: Exception", e);
    }

    return null;
}

From source file:com.ah.be.parameter.BeParaModuleDefImpl.java

private void changeOsVersionFile() {
    String fingerprints = ImportTextFileAction.OS_VERSION_FILE_PATH + ImportTextFileAction.OS_VERSION_FILE_NAME;
    String fingerprintsChg = ImportTextFileAction.OS_VERSION_FILE_PATH
            + ImportTextFileAction.OS_VERSION_FILE_NAME_CHG;
    FileWriter fWriter = null;//from www  .ja v  a2s. c o  m
    try {
        if (new File(fingerprints).exists() && new File(fingerprintsChg).exists()) {
            List<String> lines = NmsUtil.readFileByLines(fingerprints);
            List<String> replaceOsName = new ArrayList<>();
            List<String> replaceOption55 = new ArrayList<>();
            String preHmVer = NmsUtil.getHiveOSVersion(NmsUtil
                    .getVersionInfo(BeAdminCentOSTools.ahBackupdir + File.separatorChar + "hivemanager.ver"));

            // parse os_dhcp_fingerprints_changes.xml
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(fingerprintsChg));
            Element root = document.getRootElement();
            List<?> fingerprintElems = root.elements();
            for (Object obj : fingerprintElems) {
                Element fingerprintElem = (Element) obj;
                String osName = fingerprintElem.attributeValue("osname");
                for (Iterator<?> iterator = fingerprintElem.elementIterator(); iterator.hasNext();) {
                    Element option55Elem = (Element) iterator.next();
                    String node_option55_text = option55Elem.getText();
                    Attribute version = option55Elem.attribute("version");
                    String version_text = version.getText();
                    if (NmsUtil.compareSoftwareVersion(preHmVer, version_text) <= 0) {
                        if (!replaceOption55.contains(node_option55_text)) {
                            replaceOsName.add(osName);
                            replaceOption55.add(node_option55_text);
                        }
                    }
                }
            }

            if (replaceOption55.isEmpty()) {
                log.debug("No need to modify os_dhcp_fingerprints.txt.");
                FileUtils.deleteQuietly(new File(fingerprintsChg));
                return;
            }

            for (String option55 : replaceOption55) {
                int size = lines.size();
                boolean remove = false;
                for (int i = size - 1; i >= 0; i--) {
                    if (remove) {
                        lines.remove(i);
                        remove = false;
                    } else {
                        if (option55.equals(lines.get(i))) {
                            if (i < size - 1 && i > 0
                                    && lines.get(i - 1).startsWith(ImportTextFileAction.OS_STR)
                                    && lines.get(i + 1).equals(ImportTextFileAction.END_STR)) {
                                lines.remove(i + 1);
                                lines.remove(i);
                                remove = true;
                            } else {
                                lines.remove(i);
                            }
                        }
                    }
                }
            }

            //insert
            for (int i = 0; i < replaceOption55.size(); i++) {
                String option55 = replaceOption55.get(i);
                String osName = ImportTextFileAction.OS_STR + replaceOsName.get(i);

                if (!lines.contains(option55)) {
                    if (lines.contains(osName)) {
                        List<String> temp = lines.subList(lines.indexOf(osName), lines.size());
                        int index = lines.indexOf(osName) + temp.indexOf(ImportTextFileAction.END_STR);
                        lines.add(index, option55);

                    } else {
                        lines.add(osName);
                        lines.add(option55);
                        lines.add(ImportTextFileAction.END_STR);
                    }
                }
            }

            fWriter = new FileWriter(fingerprints, false);
            for (String line : lines) {
                if (line != null && line.startsWith(ImportTextFileAction.VERSION_STR)) {
                    String version = line.substring(line.indexOf(ImportTextFileAction.VERSION_STR)
                            + ImportTextFileAction.VERSION_STR.length());
                    BigDecimal b1 = new BigDecimal(version);
                    BigDecimal b2 = new BigDecimal("0.1");
                    float fVer = b1.add(b2).floatValue();
                    String versionStr = ImportTextFileAction.VERSION_STR + String.valueOf(fVer) + "\r\n";
                    fWriter.write(versionStr);
                } else {
                    fWriter.write(line + "\r\n");
                }
            }
            fWriter.close();

            //compress file
            String strCmd = "";
            StringBuffer strCmdBuf = new StringBuffer();
            strCmdBuf.append("tar zcvf ");
            strCmdBuf.append(
                    ImportTextFileAction.OS_VERSION_FILE_PATH + ImportTextFileAction.OS_VERSION_FILE_NAME_TAR);
            strCmdBuf.append(" -C ");
            strCmdBuf.append(ImportTextFileAction.OS_VERSION_FILE_PATH);
            strCmdBuf.append(" " + ImportTextFileAction.OS_VERSION_FILE_NAME);
            strCmd = strCmdBuf.toString();
            boolean compressResult = BeAdminCentOSTools.exeSysCmd(strCmd);
            if (!compressResult) {
                log.error("compress os_dhcp_fingerprints.txt error.");
                return;
            }

            FileUtils.deleteQuietly(new File(fingerprintsChg));
        } else {
            if (new File(fingerprintsChg).exists()) {
                FileUtils.deleteQuietly(new File(fingerprintsChg));
            }
        }
    } catch (Exception e) {
        setDebugMessage("change OsVersionFile error: ", e);
    } finally {
        if (fWriter != null) {
            try {
                fWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

From source file:com.ah.be.parameter.BeParaModuleDefImpl.java

public void insertDefaultCustomReportField() {
    try {/*  w  w w  . j  a v a  2  s  .  c  o m*/
        long rowCount = QueryUtil.findRowCount(AhCustomReportField.class, null);

        if (rowCount > 0) {
            return;
        }

        List<AhCustomReportField> reportFields = new ArrayList<>();

        SAXReader reader = new SAXReader();
        String docName = AhDirTools.getHmRoot() + "resources" + File.separator + "customReport" + File.separator
                + "custom_report_table.xml";
        Document doc = reader.read(new File(docName));
        Element root = doc.getRootElement();
        List<?> rootLst = root.elements();

        for (Object obj : rootLst) {
            List<?> rowlst = ((Element) obj).elements();
            AhCustomReportField reportField = new AhCustomReportField();
            for (int j = 0; j < rowlst.size(); j++) {
                Element elm = (Element) rowlst.get(j);
                //               String name = elm.attributeValue("name");
                String value = elm.attributeValue("value");
                if (j == 0) {
                    reportField.setId(Long.parseLong(value));
                } else if (j == 1) {
                    reportField.setType(Integer.parseInt(value));
                } else if (j == 2) {
                    reportField.setDetailType(Integer.parseInt(value));
                } else if (j == 3) {
                    reportField.setTableName(value);
                } else if (j == 4) {
                    reportField.setTableField(value);
                } else if (j == 5) {
                    reportField.setFieldString(value);
                } else if (j == 6) {
                    reportField.setStrUnit(value);
                } else if (j == 7) {
                    reportField.setDescription(value);
                }
            }
            reportFields.add(reportField);
        }

        root.clearContent();
        doc.clearContent();

        QueryUtil.bulkCreateBos(reportFields);
    } catch (Exception e) {
        setDebugMessage("insert default custom report field: ", e);
    }
}

From source file:com.ah.be.performance.db.TablePartitionProcessor.java

private void init() {
    try {// w ww.java  2 s.c  om
        File f = new File(TABLE_PARTITION_CONF_FILE);
        SAXReader reader = new SAXReader();
        Document doc = reader.read(f);
        Element root = doc.getRootElement();
        Element cata = null;

        int catalog = 1;
        for (Iterator<?> i = root.elementIterator(); i.hasNext();) {
            //for catalog
            cata = (Element) i.next();
            String enable = cata.elementText("enable");
            if (enable != null && enable.equalsIgnoreCase("false"))
                continue;
            int default_maxtime = Integer.parseInt(cata.elementTextTrim("default_maxtime"));
            int default_interval = Integer.parseInt(cata.elementTextTrim("default_interval"));
            int table_partition_number = Integer.parseInt(cata.elementTextTrim("table_partition_number"));
            int default_max_record = Integer.parseInt(cata.elementTextTrim("default_max_record"));
            int default_max_record_per_partition = Integer
                    .parseInt(cata.elementTextTrim("default_max_record_per_partition"));
            int maxtime_policy = Integer.parseInt(cata.elementTextTrim("maxtime_policy"));
            int interval_policy = Integer.parseInt(cata.elementTextTrim("interval_policy"));
            int max_record_policy = Integer.parseInt(cata.elementTextTrim("max_record_policy"));

            addTableCatalogInfo(catalog, default_maxtime, default_interval, table_partition_number,
                    default_max_record, default_max_record_per_partition, maxtime_policy, interval_policy,
                    max_record_policy);

            List<?> tableElements = cata.elements("table");
            //for table in catalog
            for (int j = 0; j < tableElements.size(); j++) {
                Element table = (Element) tableElements.get(j);
                String tableName = table.attributeValue("name");
                String schemaName = table.elementTextTrim("schemaname");
                String timeField = table.elementTextTrim("timefield");
                addTableInfo(schemaName, tableName, timeField, catalog);
            }

            catalog++;
        }
    } catch (Exception e) {
        BeLogTools.error(HmLogConst.M_PERFORMANCE_TABLEPARTITION, "Fail to init table partition configure file",
                e);
    }
}

From source file:com.ah.ui.actions.hiveap.HiveApFileAction.java

/**
 * Get the image product name and version from the header xml string
 *
 *@param lineStr -//from  ww w. j a  va  2 s. com
 *@return HiveApImageInfo
 */
private HiveApImageInfo getImageInfoHead(String lineStr) {
    try {
        if (!lineStr.startsWith("#!/bin/bash") || !lineStr.contains("<Image-Header")
                || !lineStr.contains("</Image-Header>") || !lineStr.contains("<Firmware>")
                || !lineStr.contains("</Firmware>")) {
            return null;
        }
        HiveApImageInfo info = new HiveApImageInfo();
        SAXReader reader = new SAXReader();
        String docName = lineStr.substring(lineStr.indexOf("<Firmware>"),
                lineStr.indexOf("</Firmware>") + "</Firmware>".length());
        Document doc = reader.read(new StringReader(docName));
        Element roota = doc.getRootElement();
        Iterator<?> iter = roota.elementIterator();
        Element foo;
        while (iter.hasNext()) {
            foo = (Element) iter.next();
            // get product name
            if (foo.getName().equalsIgnoreCase("Product")) {
                info.setProductName(foo.getStringValue());
                // get image version
            } else if (foo.getName().equalsIgnoreCase("Version")) {
                iter = foo.elementIterator();
                while (iter.hasNext()) {
                    foo = (Element) iter.next();
                    if (foo.getName().equalsIgnoreCase("External")) {
                        iter = foo.elementIterator();
                        while (iter.hasNext()) {
                            foo = (Element) iter.next();
                            // get major version
                            if (foo.getName().equalsIgnoreCase("Major")) {
                                info.setMajorVersion(foo.getStringValue());

                                // get minor version
                            } else if (foo.getName().equalsIgnoreCase("Minor")) {
                                info.setMinorVersion(foo.getStringValue());

                                // get release version
                            } else if (foo.getName().equalsIgnoreCase("Release")) {
                                info.setRelVersion(foo.getStringValue());

                                // get patch string
                            } else if (foo.getName().equalsIgnoreCase("Patch")) {
                                try {
                                    info.setImageUid(Integer.parseInt(foo.getStringValue()));
                                } catch (NumberFormatException nfe) {
                                    info.setImageUid(0);
                                }
                            }
                        }
                    }
                }
            }
        }
        String regex = "^\\d+\\.+\\d+r\\d+\\w*$";
        // check the product name and version format
        if ("".equals(info.getProductName()) || !Pattern.matches(regex, info.getImageVersion().trim())) {
            return null;
        }
        com.ah.be.config.image.ImageManager.updateHiveApImageInfo(info);
        return info;
    } catch (Exception ex) {
        log.error("checkImageInfo : ", ex.getMessage());
        return null;
    }
}

From source file:com.ai.tools.generator.util.DocumentUtil.java

License:Open Source License

public static Document readDocumentFromFile(File file, boolean validate) throws DocumentException {
    SAXReader reader = SAXReaderFactory.getInstance(validate);

    return reader.read(file);
}

From source file:com.ai.tools.generator.util.DocumentUtil.java

License:Open Source License

public static Document readDocumentFromStream(InputStream is, boolean validate) throws DocumentException {
    SAXReader reader = SAXReaderFactory.getInstance(validate);

    return reader.read(is);
}