Example usage for org.dom4j.io SAXReader SAXReader

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

Introduction

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

Prototype

public SAXReader() 

Source Link

Usage

From source file:cn.mario256.blog.util.SystemUtils.java

License:Open Source License

/**
 * /*ww  w  .  j a v  a 2 s  . com*/
 * 
 * @param setting
 *            
 */
@SuppressWarnings("unchecked")
public static void setSetting(Setting setting) {
    Assert.notNull(setting);

    try {
        File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile();
        Document document = new SAXReader().read(turingXmlFile);
        List<org.dom4j.Element> elements = document.selectNodes("/turing/setting");
        for (org.dom4j.Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = BEAN_UTILS.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e.getMessage(), e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }

        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            xmlWriter = new XMLWriter(new FileOutputStream(turingXmlFile), outputFormat);
            xmlWriter.write(document);
            xmlWriter.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            try {
                if (xmlWriter != null) {
                    xmlWriter.close();
                }
            } catch (IOException e) {
            }
        }
        Ehcache cache = CACHE_MANAGER.getEhcache(Setting.CACHE_NAME);
        String cacheKey = "setting";
        cache.put(new Element(cacheKey, setting));
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (DocumentException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:cn.mario256.blog.util.SystemUtils.java

License:Open Source License

/**
 * ???/*from  ww w .  j a  v  a 2s  .  co  m*/
 * 
 * @param id
 *            ID
 * @return ??
 */
public static TemplateConfig getTemplateConfig(String id) {
    Assert.hasText(id);

    Ehcache cache = CACHE_MANAGER.getEhcache(TemplateConfig.CACHE_NAME);
    String cacheKey = "templateConfig_" + id;
    Element cacheElement = cache.get(cacheKey);
    if (cacheElement == null) {
        TemplateConfig templateConfig = null;
        try {
            File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile();
            Document document = new SAXReader().read(turingXmlFile);
            org.dom4j.Element element = (org.dom4j.Element) document
                    .selectSingleNode("/turing/templateConfig[@id='" + id + "']");
            if (element != null) {
                templateConfig = getTemplateConfig(element);
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (DocumentException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        cache.put(new Element(cacheKey, templateConfig));
        cacheElement = cache.get(cacheKey);
    }
    return (TemplateConfig) cacheElement.getObjectValue();
}

From source file:cn.mario256.blog.util.SystemUtils.java

License:Open Source License

/**
 * ???//from  ww w.  j a  va2  s  .c  o  m
 * 
 * @param type
 *            
 * @return ??
 */
@SuppressWarnings("unchecked")
public static List<TemplateConfig> getTemplateConfigs(TemplateConfig.Type type) {
    Ehcache cache = CACHE_MANAGER.getEhcache(TemplateConfig.CACHE_NAME);
    String cacheKey = "templateConfigs_" + type;
    Element cacheElement = cache.get(cacheKey);
    if (cacheElement == null) {
        List<TemplateConfig> templateConfigs = new ArrayList<TemplateConfig>();
        try {
            File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile();
            Document document = new SAXReader().read(turingXmlFile);
            List<org.dom4j.Element> elements = document.selectNodes(
                    type != null ? "/turing/templateConfig[@type='" + type + "']" : "/turing/templateConfig");
            for (org.dom4j.Element element : elements) {
                templateConfigs.add(getTemplateConfig(element));
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (DocumentException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        cache.put(new Element(cacheKey, templateConfigs));
        cacheElement = cache.get(cacheKey);
    }
    return (List<TemplateConfig>) cacheElement.getObjectValue();
}

From source file:cn.mario256.blog.util.SystemUtils.java

License:Open Source License

/**
 * ??/*from  www .j a  v  a2 s . com*/
 * 
 * @return ?
 */
@SuppressWarnings("unchecked")
public static List<LogConfig> getLogConfigs() {
    Ehcache cache = CACHE_MANAGER.getEhcache(LogConfig.CACHE_NAME);
    String cacheKey = "logConfigs";
    Element cacheElement = cache.get(cacheKey);
    if (cacheElement == null) {
        List<LogConfig> logConfigs = new ArrayList<LogConfig>();
        try {
            File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile();
            Document document = new SAXReader().read(turingXmlFile);
            List<org.dom4j.Element> elements = document.selectNodes("/turing/logConfig");
            for (org.dom4j.Element element : elements) {
                String operation = element.attributeValue("operation");
                String urlPattern = element.attributeValue("urlPattern");
                LogConfig logConfig = new LogConfig();
                logConfig.setOperation(operation);
                logConfig.setUrlPattern(urlPattern);
                logConfigs.add(logConfig);
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (DocumentException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        cache.put(new Element(cacheKey, logConfigs));
        cacheElement = cache.get(cacheKey);
    }
    return (List<LogConfig>) cacheElement.getObjectValue();
}

From source file:cn.myloveqian.utils.XmlUtils.java

License:Open Source License

/**
 * @param fileName//w w  w.  j  a va2s.c  o m
 * @return
 * @throws DocumentException
 */
public static Document getFileDocument(String fileName) throws DocumentException {
    return new SAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
}

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);
    }//from w  ww.j  a  v  a 2  s  .  c  o  m
    return saxReader.read(file);
}

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

License:Apache License

private Document loadDocument() throws MojoFailureException {
    // FIXME: Building of File is strangely equivalent to method buildOutputFileName of NcssReportGenerator class...
    final File ncssXmlFile = new File(xmlOutputDirectory + File.separator + tempFileName);
    try {/*from  ww w.  j a  va 2 s  . c om*/
        return new SAXReader().read(ncssXmlFile);
    } catch (DocumentException de) {
        throw new MojoFailureException("Can't read javancss xml output file : " + ncssXmlFile);
    }
}

From source file:com.adobe.ac.pmd.metrics.maven.FlexMetricsMojo.java

License:Apache License

private Document loadDocument() throws MojoFailureException {
    final File ncssXmlFile = new File(xmlOutputDirectory + File.separator + tempFileName);
    try {/*from w w  w. j a va  2 s .  c  om*/
        return new SAXReader().read(ncssXmlFile);
    } catch (final DocumentException de) {
        throw new MojoFailureException("Can't read javancss xml output file : " + ncssXmlFile, de);
    }
}

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

private static List<StatConfig> readStatConfig() {
    if (stats != null && !stats.isEmpty()) {
        return stats;
    }/*  w w w.j  av a 2 s . com*/
    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;// w  ww .ja va2s. co  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();
            }
        }
    }

}