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:com.dp2345.plugin.tenpayPartner.TenpayPartnerPlugin.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   w w  w  . j a v a2  s  .c o  m*/
public boolean verifyNotify(String sn, NotifyMethod notifyMethod, HttpServletRequest request) {
    PluginConfig pluginConfig = getPluginConfig();
    if (generateSign(request.getParameterMap()).equals(request.getParameter("sign"))
            && pluginConfig.getAttribute("partner").equals(request.getParameter("partner"))
            && sn.equals(request.getParameter("out_trade_no"))
            && "0".equals(request.getParameter("trade_state"))) {
        try {
            Map<String, Object> parameterMap = new HashMap<String, Object>();
            parameterMap.put("input_charset", "utf-8");
            parameterMap.put("sign_type", "MD5");
            parameterMap.put("partner", pluginConfig.getAttribute("partner"));
            parameterMap.put("notify_id", request.getParameter("notify_id"));
            String verifyUrl = "https://gw.tenpay.com/gateway/simpleverifynotifyid.xml?input_charset=utf-8&sign_type=MD5&partner="
                    + pluginConfig.getAttribute("partner") + "&notify_id=" + request.getParameter("notify_id")
                    + "&sign=" + generateSign(parameterMap);
            Document document = new SAXReader().read(new URL(verifyUrl));
            Node node = document.selectSingleNode("/root/retcode");
            if ("0".equals(node.getText().trim())) {
                return true;
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:com.dp2345.service.impl.LogConfigServiceImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Cacheable("logConfig")
public List<LogConfig> getAll() {
    try {/*ww  w.j  a v a  2  s  .  c om*/
        File dp2345XmlFile = new ClassPathResource(CommonAttributes.DP2345_XML_PATH).getFile();
        Document document = new SAXReader().read(dp2345XmlFile);
        List<org.dom4j.Element> elements = document.selectNodes("/dp2345/logConfig");
        List<LogConfig> logConfigs = new ArrayList<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);
        }
        return logConfigs;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.dp2345.service.impl.TemplateServiceImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Cacheable("template")
public List<Template> getAll() {
    try {/*from  ww w .  java2  s  . c om*/
        File dp2345XmlFile = new ClassPathResource(CommonAttributes.DP2345_XML_PATH).getFile();
        Document document = new SAXReader().read(dp2345XmlFile);
        List<Template> templates = new ArrayList<Template>();
        List<Element> elements = document.selectNodes("/dp2345/template");
        for (Element element : elements) {
            Template template = getTemplate(element);
            templates.add(template);
        }
        return templates;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.dp2345.service.impl.TemplateServiceImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Cacheable("template")
public List<Template> getList(Type type) {
    if (type != null) {
        try {//from  w w  w.ja  v  a 2s. c o m
            File dp2345XmlFile = new ClassPathResource(CommonAttributes.DP2345_XML_PATH).getFile();
            Document document = new SAXReader().read(dp2345XmlFile);
            List<Template> templates = new ArrayList<Template>();
            List<Element> elements = document.selectNodes("/dp2345/template[@type='" + type + "']");
            for (Element element : elements) {
                Template template = getTemplate(element);
                templates.add(template);
            }
            return templates;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    } else {
        return getAll();
    }
}

From source file:com.dp2345.service.impl.TemplateServiceImpl.java

License:Open Source License

@Cacheable("template")
public Template get(String id) {
    try {/*from   www  .jav  a2 s  .  com*/
        File dp2345XmlFile = new ClassPathResource(CommonAttributes.DP2345_XML_PATH).getFile();
        Document document = new SAXReader().read(dp2345XmlFile);
        Element element = (Element) document.selectSingleNode("/dp2345/template[@id='" + id + "']");
        Template template = getTemplate(element);
        return template;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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

License:Open Source License

/**
 * ?/*ww w.  j  ava  2s .c o m*/
 * 
 * @return 
 */
public static Setting get() {
    Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
    net.sf.ehcache.Element cacheElement = cache.get(Setting.CACHE_KEY);
    Setting setting;
    if (cacheElement != null) {
        setting = (Setting) cacheElement.getObjectValue();
    } else {
        setting = new Setting();
        try {
            File dp2345XmlFile = new ClassPathResource(CommonAttributes.DP2345_XML_PATH).getFile();
            Document document = new SAXReader().read(dp2345XmlFile);
            List<Element> elements = document.selectNodes("/dp2345/setting");
            for (Element element : elements) {
                String name = element.attributeValue("name");
                String value = element.attributeValue("value");
                try {
                    beanUtils.setProperty(setting, name, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    }
    return setting;
}

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

License:Open Source License

/**
 * /*from   ww  w .  j a va 2  s .co m*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File dp2345XmlFile = new ClassPathResource(CommonAttributes.DP2345_XML_PATH).getFile();
        Document document = new SAXReader().read(dp2345XmlFile);
        List<Element> elements = document.selectNodes("/dp2345/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(dp2345XmlFile);
            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.dsh105.nexus.command.module.information.WolframAlphaCommand.java

License:Open Source License

@Override
public boolean onCommand(CommandPerformEvent event) {
    if (event.getArgs().length == 0) {
        return false;
    }//from w w w. ja v a  2  s .  c  o m

    String apiKey = Nexus.getInstance().getConfig().getWolframAlphaKey();
    if (apiKey.isEmpty()) {
        event.errorWithPing("Failed to query WolframAlpha - API key has not been configured.");
        Nexus.LOGGER.warning("User attempted to access WolframAlpha - API key is invalid!");
        return true;
    }

    String input = StringUtil.combineSplit(0, event.getArgs(), " ");

    StringBuilder answer = new StringBuilder();
    try {
        String apiUrl = String.format(API_URL, URLEncoder.encode(input, "UTF-8"), apiKey);
        Nexus.LOGGER.info("Requesting WolframAlpha interpretation at " + apiUrl);

        SAXReader reader = new SAXReader();
        Document document = reader.read(Unirest.get(apiUrl).asBinary().getBody());

        Element root = document.getRootElement();

        if (Boolean.valueOf(root.attribute("success").getValue())) {
            for (Iterator pods = root.elementIterator("pod"); pods.hasNext();) {
                Element pod = (Element) pods.next();
                String primary = pod.attributeValue("primary");
                if (primary != null && Boolean.valueOf(primary)) {
                    for (Iterator subpods = pod.elementIterator("subpod"); subpods.hasNext();) {
                        Element subpod = (Element) subpods.next();
                        String result = subpod.element("plaintext").getText();
                        if (result != null && !result.isEmpty()) {
                            answer.append(result.replaceAll("\\n", " - ").replaceAll("\\s+", " "));
                        }
                    }
                }
            }
            if (answer.length() > 0) {
                event.respondWithPing(
                        answer + " (" + URLShortener.shorten(String.format(QUERY_URL, input)) + ")");
                return true;
            }
        }

        List<String> tips = new ArrayList<>();
        if (root.element("tips") != null) {
            for (Iterator tipElements = root.element("tips").elementIterator("tip"); tipElements.hasNext();) {
                if (tips.size() > 3) {
                    break;
                }
                Element tip = (Element) tipElements.next();
                String result = tip.attributeValue("text");
                if (result != null && !result.isEmpty()) {
                    tips.add(result.replaceAll("\\s+", " "));
                }
            }
        }
        event.errorWithPing("WolframAlpha could not interpret that!");
        if (!tips.isEmpty()) {
            event.errorWithPing(Colors.BOLD + "Tips" + Colors.BOLD + ": "
                    + StringUtil.combineSplit(0, tips.toArray(new String[tips.size()]), "; "));
        }
        return true;
    } catch (UnirestException e) {
        throw new WolframAlphaQueryException("Failed to execute WolframAlpha query: " + input, e);
    } catch (DocumentException e) {
        throw new WolframAlphaQueryException("Failed to execute WolframAlpha query: " + input, e);
    } catch (UnsupportedEncodingException e) {
        throw new WolframAlphaQueryException("Failed to encode URL", e);
    }
}

From source file:com.dtolabs.client.utils.WebserviceHttpClientChannel.java

License:Apache License

/**
 * called by makeRequest.  Evaluates the response to determine the state of the response and deserialize the content
 * (bean)./*from w ww .  j  a va  2s . c  om*/
 */
protected void postMakeRequest() {

    responseMessage = getResponseHeader(Constants.X_RUNDECK_RESULT_HEADER) != null
            ? getResponseHeader(Constants.X_RUNDECK_RESULT_HEADER).getValue()
            : getReasonCode();
    validResponse = getResultCode() >= 200 && getResultCode() < 300;
    errorResponse = !validResponse;
    String type = getResultContentType();
    if (type != null && type.indexOf(";") > 0) {
        type = type.substring(0, type.indexOf(";")).trim();
    }
    if (XML_CONTENT_TYPE.equals(type) || APPLICATION_XML_CONTENT_TYPE.equals(type)) {
        final SAXReader reader = new SAXReader();
        final Document document;
        try {
            document = reader.read(getResultStream());
            setResultDoc(document);
        } catch (DocumentException e) {
            logger.error("Unable to parse result document: " + e.getMessage(), e);
        }
    }
}

From source file:com.dtolabs.rundeck.core.cli.util.MvnPomInfoTool.java

License:Apache License

private void processFileResources(Iterator i) {
    SAXReader reader = new SAXReader();

    while (i.hasNext()) {
        FileResource f = (FileResource) i.next();
        try {//from  w w  w.  j  av  a 2 s. c  o  m
            Document doc = reader.read(f.getInputStream());
            //check packaging value
            if (null != doc.selectSingleNode("/project/packaging")) {
                Element e = (Element) doc.selectSingleNode("/project/packaging");
                if (!packaging.equals(e.getStringValue())) {
                    continue;
                }
            } else {
                continue;
            }
            logger.debug("processing pom: " + f.getFile());
            Map fdata = processDocument(doc);
            if (null != fdata) {
                data.put(f.getFile().getAbsolutePath(), fdata);
            }
        } catch (DocumentException e) {
            logger.error("Unable to read file: " + f.getFile() + ": " + e.getMessage());
        } catch (IOException e) {
            logger.error("Unable to read file: " + f.getFile() + ": " + e.getMessage());
        }
    }
}