Example usage for org.dom4j Element attributeValue

List of usage examples for org.dom4j Element attributeValue

Introduction

In this page you can find the example usage for org.dom4j Element attributeValue.

Prototype

String attributeValue(QName qName);

Source Link

Document

This returns the attribute value for the attribute with the given fully qualified name or null if there is no such attribute or the empty string if the attribute value is empty.

Usage

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 ww  w  .ja v a 2  s  . co  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.easyjf.generator.AllGenerator.java

License:Apache License

@SuppressWarnings("unchecked")
private Element findBean(Document document, String beanName) {
    Element beans = (Element) document.selectSingleNode("/beans");
    Element bean = null;//from  www  .  jav a  2s  .c o  m
    if (beans != null) {
        List e = beans.elements("bean");
        for (int i = 0; i < e.size(); i++) {
            Element n = (Element) e.get(i);
            if (beanName.equals(n.attributeValue("id")))
                bean = n;
        }
    }
    return bean;
}

From source file:com.easyjf.web.ajax.AjaxConfigManager.java

License:Apache License

public void parseServices(Element serviceRoot) {
    String allowName = serviceRoot.attributeValue("allowName");
    if (allowName != null) {
        String[] names = allowName.split(",");
        for (int i = 0; i < names.length; i++) {
            allowNames.add(formatRegx(names[i]));
        }//from ww  w  .j a  v a2s  .co  m
    }
    String denyName = serviceRoot.attributeValue("denyName");
    if (denyName != null) {
        String[] names = denyName.split(",");
        for (int i = 0; i < names.length; i++) {
            denyNames.add(formatRegx(names[i]));
        }
    }
    List list = XmlElementUtil.findElements("service", serviceRoot);
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            Element service = (Element) list.get(i);
            RemoteService remote = new RemoteService();
            String name = service.attributeValue("name");
            if (StringUtils.hasText(name)) {
                remote.setName(name);
                // ?include
                String attrInclude = service.attributeValue("include");
                if (attrInclude != null) {
                    String[] is = StringUtils.tokenizeToStringArray(attrInclude, ",");
                    if (is != null) {
                        for (int t = 0; t < is.length; t++)
                            remote.addAllowName(formatRegx(is[t]));
                    }
                }
                // ?indlue
                List includes = XmlElementUtil.findElements("include", service);
                if (includes != null) {
                    for (int j = 0; j < includes.size(); j++) {
                        Element e = (Element) includes.get(j);
                        remote.addAllowName(formatRegx(e.attributeValue("method")));
                    }
                }
                String attrExclude = service.attributeValue("exclude");
                if (attrExclude != null) {
                    String[] is = StringUtils.tokenizeToStringArray(attrExclude, ",");
                    if (is != null) {
                        for (int t = 0; t < is.length; t++)
                            remote.addDenyName(formatRegx(is[t]));
                    }
                }
                List excludes = XmlElementUtil.findElements("exclude", service);
                if (excludes != null) {
                    for (int j = 0; j < excludes.size(); j++) {
                        Element e = (Element) excludes.get(j);
                        remote.addDenyName(formatRegx(e.attributeValue("method")));
                    }
                }
                allowNames.add(formatRegx(remote.getName()));
                services.put(remote.getName(), remote);
            }
        }
    }
}

From source file:com.easyjf.web.ajax.AjaxConfigManager.java

License:Apache License

public void parseConvert(List list) {
    for (int i = 0; i < list.size(); i++) {
        Element convert = (Element) list.get(i);
        String name = convert.attributeValue("name");
        RemoteService remote = new RemoteService();
        if (StringUtils.hasText(name)) {
            remote.setName(name);/* ww  w.j av a 2s .  c  om*/
            List includes = XmlElementUtil.findElements("include", convert);
            if (includes != null) {
                for (int j = 0; j < includes.size(); j++) {
                    Element e = (Element) includes.get(j);
                    remote.addAllowName(formatRegx(e.attributeValue("property")));
                }
            }
            List excludes = XmlElementUtil.findElements("exclude", convert);
            if (excludes != null) {
                for (int j = 0; j < excludes.size(); j++) {
                    Element e = (Element) excludes.get(j);
                    remote.addDenyName(formatRegx(e.attributeValue("property")));
                }
            }
        }
        convertBeans.put(remote.getName(), remote);
    }
}

From source file:com.easyjf.web.config.BeanConfigReader.java

License:Apache License

public static List parseBeansFromDocument(Document doc) {
    List ret = new ArrayList();
    Element beans = XmlElementUtil.findElement("beans", doc.getRootElement());
    if (beans == null)
        return ret;
    List list = beans.elements();
    try {//from w w  w. jav  a2  s.  c o  m
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                Element e = (Element) list.get(i);
                BeanDefinitionImpl definition = new BeanDefinitionImpl();
                // ?
                definition.setBeanName(e.attributeValue("name"));
                Class clz = ClassUtils.forName(e.attributeValue("class"));
                definition.setBeanClass(clz);
                if (e.attributeValue("scope") != null)
                    definition.setScope(e.attributeValue("scope"));
                if (e.attributeValue("factory-method") != null)
                    definition.setFactoryMethod(e.attributeValue("factory-method"));
                // ???

                List ps = XmlElementUtil.findElements("property", e);
                MutablePropertyValues mpv = parsePropertyValues(ps);
                definition.setPropertyValues(mpv);
                // ????
                String autoInject = e.attributeValue("auto");// auto?
                if (autoInject == null)
                    autoInject = e.attributeValue("inject");
                handleAutoInject(definition, autoInject);// ?
                ps = XmlElementUtil.findElements("constructor-arg", e);
                ps = e.selectNodes("constructor-arg");
                definition.setConstructorArguments(parseConstructorArguments(ps));
                ret.add(definition);
            }
        }
    } catch (Exception e) {
        logger.error(I18n.getLocaleMessage("core.web.loading.Bean.configuration.information.error") + e);
        throw new com.easyjf.web.exception.FrameworkException(
                I18n.getLocaleMessage("core.web.loading.Bean.configuration.information.error"), e);
    }
    return ret;
}

From source file:com.easyjf.web.config.BeanConfigReader.java

License:Apache License

public static MutablePropertyValues parsePropertyValues(List ps) {
    MutablePropertyValues mpv = new MutablePropertyValues();
    for (int j = 0; j < ps.size(); j++) {
        Element pe = (Element) ps.get(j);
        mpv.addPropertyValue(pe.attributeValue("name"), parsePropertyValue(pe));
    }//w w  w  .  j a  v a2s. c  o m
    return mpv;
}

From source file:com.easyjf.web.config.BeanConfigReader.java

License:Apache License

public static ConstructorArguments parseConstructorArguments(List<Element> ps) {
    ConstructorArguments crgs = new ConstructorArguments();
    int i = 0;/* w  ww  .ja  va  2s .  co  m*/
    for (Iterator<Element> it = ps.iterator(); it.hasNext(); i++) {
        try {
            Element arg = it.next();
            Class type = ClassUtils.forName(arg.attributeValue("type"));
            Integer index = arg.attributeValue("index") == null ? i : new Integer(arg.attributeValue("index"));
            Object value = parsePropertyValue(arg);
            if (!(value instanceof com.easyjf.container.BeanDefinition))
                value = BeanUtils.convertType(value, type);
            crgs.addArgument(index, type, value);

        } catch (ClassNotFoundException e) {
            logger.error(I18n.getLocaleMessage("core.web.construction.of.loading.into.the.wrong.type") + e);
        }

    }
    return crgs;
}

From source file:com.easyjf.web.config.BeanConfigReader.java

License:Apache License

public static Object parsePropertyValue(Element e) {
    // value/*from   w ww. j a  va 2s . c o  m*/
    Object value = e.attributeValue("value");
    if (value == null && XmlElementUtil.findElement("value", e) != null)
        value = XmlElementUtil.findElement("value", e).getText();

    // ref
    if (value == null) {
        String ref = e.attributeValue("ref");
        if (ref == null && XmlElementUtil.findElement("ref", e) != null) {
            ref = XmlElementUtil.findElement("ref", e).attributeValue("value") != null
                    ? XmlElementUtil.findElement("ref", e).attributeValue("value")
                    : XmlElementUtil.findElement("ref", e).getText();
        }
        if (ref != null) {
            BeanDefinitionImpl innerBean = new BeanDefinitionImpl();
            innerBean.setBeanName(ref);
            value = innerBean;
        }
    }
    // ?
    if (value == null) {
        value = parseCollectionValue(e);
    }
    // System.out.println(e.asXML());
    // ?Bean??
    if (value == null) {
        value = parseInnerBean(e);
    }
    return value;
}

From source file:com.easyjf.web.config.BeanConfigReader.java

License:Apache License

public static Object parseInnerBean(Element e) {
    BeanDefinitionImpl innerBean = new BeanDefinitionImpl();
    innerBean.setBeanName(e.attributeValue("name"));
    try {//w  w  w.j a  v  a  2  s .co  m
        innerBean.setBeanClass(Class.forName(e.attributeValue("class")));
    } catch (ClassNotFoundException e1) {
        logger.error(I18n.getLocaleMessage("core.web.module.information.into.beanc.ause.the.errors") + e);
    }
    return innerBean;
}

From source file:com.easyjf.web.config.XMLConfigFactory.java

License:Apache License

public void initForm(Map forms) {
    Element root = findElement("forms", getRootElement());
    if (root == null)
        return;//from www . j  av a  2 s. c  o  m
    List nodes = root.elements();
    // ??forms
    // formsform??
    // FromConfig?forms
    for (int i = 0; i < nodes.size(); i++) {
        Element e = (Element) nodes.get(i);
        FormConfig fc = new FormConfig();
        // ???js?page????EasyJWeb??
        fc.setAlertType(e.attributeValue("alertType"));
        // Formbean??WebForm
        fc.setBean(e.attributeValue("bean"));
        // ???
        fc.setClientValidate(e.attributeValue("clientValidate"));
        // form??
        fc.setName(e.attributeValue("name"));
        // ????
        fc.setServerValidate(e.attributeValue("serverValidate"));
        // Form
        String events = e.attributeValue("event");
        if (events != null && (!events.equals(""))) {
            String[] s = events.split(",");
            if (s != null) {
                List event = new ArrayList();
                for (int t = 0; t < s.length; t++) {
                    if (!s[t].equals(""))
                        event.add(s[t]);
                }
                fc.setEvent(event);
            }
        }
        // form
        List lPage = findElements("property", e);
        Map pages = new HashMap();
        for (int j = 0; j < lPage.size(); j++) {

            Element p = (Element) lPage.get(j);
            FormProperty prop = new FormProperty();
            prop.setInitial(p.attributeValue("initial"));
            prop.setName(p.attributeValue("name"));
            prop.setNotNull(p.attributeValue("notNull"));
            prop.setSize(p.attributeValue("size"));
            prop.setType(p.attributeValue("type"));
            events = p.attributeValue("event");
            if (events != null && (!events.equals(""))) {
                String[] s = events.split(",");
                if (s != null) {
                    List event = new ArrayList();
                    for (int t = 0; t < s.length; t++) {
                        if (!s[t].equals(""))
                            event.add(s[t]);
                    }
                    prop.setEvent(event);
                }
            }
            pages.put(prop.getName(), prop);
        }
        fc.setPropertys(pages);
        forms.put(fc.getName(), fc);
    }
}