Example usage for org.dom4j Element elementText

List of usage examples for org.dom4j Element elementText

Introduction

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

Prototype

String elementText(QName qname);

Source Link

Usage

From source file:com.jfinal.weixin.sdk.msg.InMsgParser.java

License:Apache License

private static InMsg parseInShortVideoMsg(Element root, String toUserName, String fromUserName,
        Integer createTime, String msgType) {
    InShortVideoMsg msg = new InShortVideoMsg(toUserName, fromUserName, createTime, msgType);
    msg.setMediaId(root.elementText("MediaId"));
    msg.setThumbMediaId(root.elementText("ThumbMediaId"));
    msg.setMsgId(root.elementText("MsgId"));
    return msg;/*  w  w w  .  j av  a  2  s  .c o m*/
}

From source file:com.jfinal.weixin.sdk.msg.InMsgParser.java

License:Apache License

private static InMsg parseInEvent(Element root, String toUserName, String fromUserName, Integer createTime,
        String msgType) {/*ww w  . j  av a  2 s. c o m*/
    String event = root.elementText("Event");
    String eventKey = root.elementText("EventKey");

    // /???????????
    if (("subscribe".equals(event) || "unsubscribe".equals(event)) && StrKit.isBlank(eventKey)) {
        return new InFollowEvent(toUserName, fromUserName, createTime, msgType, event);
    }

    // ????      1: ??
    String ticket = root.elementText("Ticket");
    if ("subscribe".equals(event) && StrKit.notBlank(eventKey) && eventKey.startsWith("qrscene_")) {
        InQrCodeEvent e = new InQrCodeEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setEventKey(eventKey);
        e.setTicket(ticket);
        return e;
    }
    // ????      2: ?
    if ("SCAN".equals(event)) {
        InQrCodeEvent e = new InQrCodeEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setEventKey(eventKey);
        e.setTicket(ticket);
        return e;
    }

    // ??
    if ("LOCATION".equals(event)) {
        InLocationEvent e = new InLocationEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setLatitude(root.elementText("Latitude"));
        e.setLongitude(root.elementText("Longitude"));
        e.setPrecision(root.elementText("Precision"));
        return e;
    }

    // ??         1?????
    if ("CLICK".equals(event)) {
        InMenuEvent e = new InMenuEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setEventKey(eventKey);
        return e;
    }
    // ??         2???
    if ("VIEW".equals(event)) {
        InMenuEvent e = new InMenuEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setEventKey(eventKey);
        return e;
    }
    // ?????
    if ("TEMPLATESENDJOBFINISH".equals(event)) {
        InTemplateMsgEvent e = new InTemplateMsgEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setMsgId(root.elementText("MsgID"));
        e.setStatus(root.elementText("Status"));
        return e;
    }
    // ?????
    if ("MASSSENDJOBFINISH".equals(event)) {
        InMassEvent e = new InMassEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setMsgId(root.elementText("MsgID"));
        e.setStatus(root.elementText("Status"));
        e.setTotalCount(root.elementText("TotalCount"));
        e.setFilterCount(root.elementText("FilterCount"));
        e.setSentCount(root.elementText("SentCount"));
        e.setErrorCount(root.elementText("ErrorCount"));
        return e;
    }
    // ??
    if ("kf_create_session".equals(event)) {
        InCustomEvent e = new InCustomEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setKfAccount(root.elementText("KfAccount"));
        return e;
    }
    // ??
    if ("kf_close_session".equals(event)) {
        InCustomEvent e = new InCustomEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setKfAccount(root.elementText("KfAccount"));
        return e;
    }
    // ??
    if ("kf_switch_session".equals(event)) {
        InCustomEvent e = new InCustomEvent(toUserName, fromUserName, createTime, msgType, event);
        e.setKfAccount(root.elementText("KfAccount"));
        e.setToKfAccount(root.elementText("ToKfAccount"));
        return e;
    }

    throw new RuntimeException(
            "" + event + "??");
}

From source file:com.jpsycn.wggl.mobile.androidpn.xmpp.handler.IQAuthHandler.java

License:Open Source License

/**
 * Handles the received IQ packet.//from  w w  w .  j  a  v  a2 s .  co m
 * 
 * @param packet the packet
 * @return the response to send back
 * @throws UnauthorizedException if the user is not authorized
 */
public IQ handleIQ(IQ packet) throws UnauthorizedException {
    IQ reply = null;

    ClientSession session = sessionManager.getSession(packet.getFrom());
    if (session == null) {
        log.error("Session not found for key " + packet.getFrom());
        reply = IQ.createResultIQ(packet);
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.internal_server_error);
        return reply;
    }

    try {
        Element iq = packet.getElement();
        Element query = iq.element("query");
        Element queryResponse = probeResponse.createCopy();

        if (IQ.Type.get == packet.getType()) { // get query
            String username = query.elementText("username");
            if (username != null) {
                queryResponse.element("username").setText(username);
            }
            reply = IQ.createResultIQ(packet);
            reply.setChildElement(queryResponse);
            if (session.getStatus() != Session.STATUS_AUTHENTICATED) {
                reply.setTo((JID) null);
            }
        } else { // set query
            String resource = query.elementText("resource");
            String username = query.elementText("username");
            String password = query.elementText("password");
            String digest = null;
            if (query.element("digest") != null) {
                digest = query.elementText("digest").toLowerCase();
            }

            // Verify the resource
            if (resource != null) {
                try {
                    resource = JID.resourceprep(resource);
                } catch (StringprepException e) {
                    throw new UnauthorizedException("Invalid resource: " + resource, e);
                }
            } else {
                throw new IllegalArgumentException("Invalid resource (empty or null).");
            }

            // Verify the username
            if (username == null || username.trim().length() == 0) {
                throw new UnauthorizedException("Invalid username (empty or null).");
            }
            try {
                Stringprep.nodeprep(username);
            } catch (StringprepException e) {
                throw new UnauthorizedException("Invalid username: " + username, e);
            }
            username = username.toLowerCase();

            // Verify that username and password are correct
            AuthToken token = null;
            if (password != null && AuthManager.isPlainSupported()) {
                token = AuthManager.authenticate(username, password);
            } else if (digest != null && AuthManager.isDigestSupported()) {
                token = AuthManager.authenticate(username, session.getStreamID().toString(), digest);
            }

            if (token == null) {
                throw new UnauthenticatedException();
            }

            // Set the session authenticated successfully
            session.setAuthToken(token, resource);
            packet.setFrom(session.getAddress());
            reply = IQ.createResultIQ(packet);
        }
    } catch (Exception ex) {
        log.error(ex);
        reply = IQ.createResultIQ(packet);
        reply.setChildElement(packet.getChildElement().createCopy());
        if (ex instanceof IllegalArgumentException) {
            reply.setError(PacketError.Condition.not_acceptable);
        } else if (ex instanceof UnauthorizedException) {
            reply.setError(PacketError.Condition.not_authorized);
        } else if (ex instanceof UnauthenticatedException) {
            reply.setError(PacketError.Condition.not_authorized);
        } else {
            reply.setError(PacketError.Condition.internal_server_error);
        }
    }

    // Send the response directly to the session
    if (reply != null) {
        session.process(reply);
    }
    return null;
}

From source file:com.jpsycn.wggl.mobile.androidpn.xmpp.handler.IQRegisterHandler.java

License:Open Source License

/**
 * Handles the received IQ packet.//  w w  w.  j  a  v  a  2s .  c o m
 * 
 * @param packet the packet
 * @return the response to send back
 * @throws UnauthorizedException if the user is not authorized
 */
public IQ handleIQ(IQ packet) throws UnauthorizedException {
    IQ reply = null;

    ClientSession session = sessionManager.getSession(packet.getFrom());
    if (session == null) {
        log.error("Session not found for key " + packet.getFrom());
        reply = IQ.createResultIQ(packet);
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.internal_server_error);
        return reply;
    }

    if (IQ.Type.get.equals(packet.getType())) {
        reply = IQ.createResultIQ(packet);
        if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
            // TODO
        } else {
            reply.setTo((JID) null);
            reply.setChildElement(probeResponse.createCopy());
        }
    } else if (IQ.Type.set.equals(packet.getType())) {
        try {
            Element query = packet.getChildElement();
            if (query.element("remove") != null) {
                if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
                    // TODO
                } else {
                    throw new UnauthorizedException();
                }
            } else {
                String username = query.elementText("username");
                String password = query.elementText("password");
                String email = query.elementText("email");
                String name = query.elementText("name");

                // Verify the username
                if (username != null) {
                    Stringprep.nodeprep(username);
                }

                // Deny registration of users with no password
                if (password == null || password.trim().length() == 0) {
                    reply = IQ.createResultIQ(packet);
                    reply.setChildElement(packet.getChildElement().createCopy());
                    reply.setError(PacketError.Condition.not_acceptable);
                    return reply;
                }

                if (email != null && email.matches("\\s*")) {
                    email = null;
                }

                if (name != null && name.matches("\\s*")) {
                    name = null;
                }

                User user;
                if (session.getStatus() == Session.STATUS_AUTHENTICATED) {
                    user = userService.getUser(session.getUsername());
                } else {
                    user = userService.getUserByUsername(username);
                    if (null == user) {
                        user = new User();
                        user.setUsername(username);
                        user.setPassword(password);
                        user.setEmail(email);
                        user.setName(name);
                        user.setCreatedDate(new Date());
                        user.setUpdatedDate(new Date());
                        user.setOnline(true);
                        userService.saveUser(user);
                    } else {
                        user.setOnline(true);
                        userService.update(user);
                    }
                }

                reply = IQ.createResultIQ(packet);
            }
        } catch (Exception ex) {
            log.error(ex);
            reply = IQ.createResultIQ(packet);
            reply.setChildElement(packet.getChildElement().createCopy());
            if (ex instanceof StringprepException) {
                reply.setError(PacketError.Condition.jid_malformed);
            } else if (ex instanceof IllegalArgumentException) {
                reply.setError(PacketError.Condition.not_acceptable);
            } else {
                reply.setError(PacketError.Condition.internal_server_error);
            }
        }
    }

    // Send the response directly to the session
    if (reply != null) {
        session.process(reply);
    }
    return null;
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected Element getElementByName(List<Element> elements, String name) {
    for (Element element : elements) {
        if (name.equals(element.elementText("name"))) {
            return element;
        }//  www .  j  a  v a2s .c  om
    }

    return null;
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected Document mergeXMLAttributes(Document doc1, Document doc2) {
    Element doc2Root = doc2.getRootElement();
    Element doc1Root = doc1.getRootElement();
    Element docRoot = doc2Root.createCopy();

    docRoot.clearContent();/*from w w w  . j  av  a 2s  .c  o  m*/

    if (doc1Root != null) {
        Iterator<Object> attributesIterator = doc1Root.attributeIterator();

        while (attributesIterator.hasNext()) {
            org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

            if (attribute.getName().equals("extends")) {
                continue;
            }

            docRoot.addAttribute(attribute.getName(), attribute.getValue());
        }

        Element descriptionElement = doc1Root.element("description");

        if (descriptionElement != null) {
            docRoot.add(descriptionElement.createCopy());
        }
    }

    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    Document doc = factory.createDocument();
    doc.setRootElement(docRoot);

    List<Element> doc2Components = doc2Root.elements(_COMPONENT);

    for (Element doc2Component : doc2Components) {
        Element component = doc2Component.createCopy();

        String name = doc2Component.attributeValue("name");

        Element doc1Component = getComponentNode(doc1, name);

        if (doc1Component != null) {
            Element doc1ComponentDescriptionElement = doc1Component.element("description");

            if (doc1ComponentDescriptionElement != null) {
                Element descriptionElement = component.element("description");

                if (descriptionElement != null) {
                    component.remove(descriptionElement);
                }

                component.add(doc1ComponentDescriptionElement.createCopy());
            }

            Iterator<Object> attributesIterator = doc1Component.attributeIterator();

            while (attributesIterator.hasNext()) {
                org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

                component.addAttribute(attribute.getName(), attribute.getValue());
            }

            Element doc1AttributesNode = doc1Component.element(_ATTRIBUTES);

            Element attributesNode = component.element(_ATTRIBUTES);

            if ((doc1AttributesNode != null) && (attributesNode != null)) {
                List<Element> doc1Attributes = doc1AttributesNode.elements(_ATTRIBUTE);

                List<Element> attributes = attributesNode.elements(_ATTRIBUTE);

                for (Element doc1Attribute : doc1Attributes) {
                    Element attribute = getElementByName(attributes, doc1Attribute.elementText("name"));

                    if (attribute != null) {
                        attributesNode.remove(attribute);
                    }

                    attributesNode.add(doc1Attribute.createCopy());
                }
            }

            Element doc1EventsNode = doc1Component.element(_EVENTS);

            Element eventsNode = component.element(_EVENTS);

            if ((doc1EventsNode != null) && (eventsNode != null)) {
                List<Element> doc1Events = doc1EventsNode.elements(_EVENT);

                List<Element> events = eventsNode.elements(_EVENT);

                for (Element doc1Event : doc1Events) {
                    Element event = getElementByName(events, doc1Event.elementText("name"));

                    if (event != null) {
                        eventsNode.add(event);
                    }

                    eventsNode.add(doc1Event.createCopy());
                }
            }
        }

        doc.getRootElement().add(component);
    }

    if (doc1Root != null) {
        List<Element> doc1Components = doc1Root.elements(_COMPONENT);

        for (Element doc1Component : doc1Components) {
            Element component = doc1Component.createCopy();

            String name = doc1Component.attributeValue("name");

            Element doc2Component = getComponentNode(doc2, name);

            if (doc2Component == null) {
                doc.getRootElement().add(component);
            }
        }
    }

    return doc;
}

From source file:com.liferay.alloy.tools.builder.faces.model.FacesAttribute.java

License:Open Source License

@Override
public void initialize(Element facesAttributeElement, Component component) {
    super.initialize(facesAttributeElement, component);

    _defaultToComponentLabel = Convert.toBoolean(facesAttributeElement.elementText("defaultToComponentLabel"),
            true);// w  w  w . jav  a2s.  c  om
    String defaultValue = Convert.toString(facesAttributeElement.elementText("defaultValue"), "null");

    setDefaultValue(defaultValue);

    String type = Convert.toString(facesAttributeElement.elementText("type"), DEFAULT_TYPE);
    _methodSignature = facesAttributeElement.elementText("method-signature");

    if ((_methodSignature != null) && type.equals(DEFAULT_TYPE)) {
        type = "javax.el.MethodExpression";
    }

    setType(type);

    _inherited = Convert.toBoolean(facesAttributeElement.elementText("inherited"), false);
    _methodSignature = facesAttributeElement.elementText("method-signature");
    _override = Convert.toBoolean(facesAttributeElement.elementText("override"), false);
    _yui = Convert.toBoolean(facesAttributeElement.elementText("yui"), false);
    _yuiName = Convert.toString(facesAttributeElement.elementText("yuiName"), getName());
    _yuiType = Convert.toString(facesAttributeElement.elementText("yuiType"), getJavaWrapperType());
}

From source file:com.liferay.alloy.tools.builder.taglib.model.TagAttribute.java

License:Open Source License

@Override
public void initialize(Element attributeElement, Component component) {
    super.initialize(attributeElement, component);

    String javaScriptType = getJavaScriptType();
    String defaultInputType = getType();
    String defaultOutputType = getType();

    if (javaScriptType != null) {
        defaultInputType = TypeUtil.getInputJavaType(javaScriptType, true);
        defaultOutputType = TypeUtil.getOutputJavaType(javaScriptType, true);
    }//w  ww .  j  a va  2  s  .  co  m

    _inputType = Convert.toString(attributeElement.elementText("inputType"), defaultInputType);
    _outputType = Convert.toString(attributeElement.elementText("outputType"), defaultOutputType);
}

From source file:com.liferay.alloy.tools.builder.taglib.TagBuilder.java

License:Open Source License

protected Document mergeTlds(Document sourceDoc, Document targetDoc) {
    Element targetRoot = targetDoc.getRootElement();

    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    XPath xpathTags = factory.createXPath("//tld:tag");

    Map<String, String> namespaceContextMap = new HashMap<String, String>();

    namespaceContextMap.put(_TLD_XPATH_PREFIX, _TLD_XPATH_URI);

    NamespaceContext namespaceContext = new AlloyGeneratorNamespaceContext(namespaceContextMap);

    xpathTags.setNamespaceContext(namespaceContext);

    List<Node> sources = xpathTags.selectNodes(sourceDoc);

    for (Node source : sources) {
        Element sourceElement = (Element) source;

        String sourceName = sourceElement.elementText("name");

        String xpathTagValue = "//tld:tag[tld:name='" + sourceName + "']";

        XPath xpathTag = factory.createXPath(xpathTagValue);

        xpathTag.setNamespaceContext(namespaceContext);

        List<Node> targets = xpathTag.selectNodes(targetDoc);

        if (targets.size() > 0) {
            Element targetElement = (Element) targets.get(0);

            XPath xpathAttributes = factory.createXPath(xpathTagValue + "//tld:attribute");

            Map<String, String> namespaces = new HashMap<String, String>();

            namespaces.put("tld", StringPool.EMPTY);

            xpathAttributes.setNamespaceURIs(namespaces);

            List<Node> sourceAttributes = xpathAttributes.selectNodes(source);

            for (Node sourceAttribute : sourceAttributes) {
                Element sourceAttributeElement = (Element) sourceAttribute;

                String attributeName = sourceAttributeElement.elementText("name");

                String xpathAttributeValue = "//tld:attribute[tld:name='" + attributeName + "']";

                XPath xpathAttribute = factory.createXPath(xpathTagValue + xpathAttributeValue);

                xpathAttribute.setNamespaceContext(namespaceContext);

                Node targetAttribute = xpathAttribute.selectSingleNode(targetElement);

                if (targetAttribute != null) {
                    targetAttribute.detach();
                }//w ww.  j av  a  2s.c  om

                targetElement.add(sourceAttributeElement.createCopy());
            }

            Element dynamicAttrElement = targetElement.element("dynamic-attributes");

            if (dynamicAttrElement != null) {
                targetElement.add(dynamicAttrElement.detach());
            }
        } else {
            targetRoot.add(sourceElement.createCopy());
        }
    }

    return targetDoc;
}

From source file:com.liferay.alloy.tools.model.Attribute.java

License:Open Source License

public void initialize(Element attributeElement, Component component) {
    setComponent(component);// w w  w .  ja  va 2 s.  c  o  m
    String description = attributeElement.elementText("description");
    setDescription(description);
    String name = attributeElement.elementText("name");
    setName(name);
    _defaultValue = attributeElement.elementText("defaultValue");
    _javaScriptType = Convert.toString(attributeElement.elementText("javaScriptType"), null);

    String defaultType = DEFAULT_TYPE;

    if (_javaScriptType != null) {
        defaultType = TypeUtil.getJavaScriptType(_javaScriptType);
        defaultType = TypeUtil.getInputJavaType(defaultType, true);
    }

    _type = Convert.toString(attributeElement.elementText("type"), defaultType);
    boolean generateJava = Convert.toBoolean(attributeElement.elementText("generateJava"), true);
    setGenerateJava(generateJava);

    _gettable = Convert.toBoolean(attributeElement.elementText("gettable"), true);
    _required = Convert.toBoolean(attributeElement.elementText("required"), false);
    _settable = Convert.toBoolean(attributeElement.elementText("settable"), true);
    _readOnly = Convert.toBoolean(attributeElement.elementText("readOnly"), false);
}