Java 类org.jivesoftware.smack.packet.DefaultExtensionElement 实例源码

项目:Smack    文件:PacketParserUtils.java   
/**
 * Parses an extension element.
 *
 * @param elementName the XML element name of the extension element.
 * @param namespace the XML namespace of the stanza(/packet) extension.
 * @param parser the XML parser, positioned at the starting element of the extension.
 * @return an extension element.
 */
public static ExtensionElement parseExtensionElement(String elementName, String namespace,
                XmlPullParser parser) throws XmlPullParserException,
                IOException, SmackException {
    ParserUtils.assertAtStartTag(parser);
    // See if a provider is registered to handle the extension.
    ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getExtensionProvider(elementName, namespace);
    if (provider != null) {
            return provider.parse(parser);
    }

    final int initialDepth = parser.getDepth();
    // No providers registered, so use a default extension.
    DefaultExtensionElement extension = new DefaultExtensionElement(elementName, namespace);
    outerloop: while (true) {
        int eventType = parser.next();
        switch (eventType) {
        case XmlPullParser.START_TAG:
            String name = parser.getName();
            // If an empty element, set the value with the empty string.
            if (parser.isEmptyElementTag()) {
                extension.setValue(name,"");
            }
            // Otherwise, get the the element text.
            else {
                eventType = parser.next();
                if (eventType == XmlPullParser.TEXT) {
                    String value = parser.getText();
                    extension.setValue(name, value);
                }
            }
            break;
        case XmlPullParser.END_TAG:
            if (parser.getDepth() == initialDepth) {
                break outerloop;
            }
        }
    }
    return extension;
}
项目:Smack    文件:AgentSession.java   
/**
 * Sets the agent's current status with the workgroup. The presence mode affects how offers
 * are routed to the agent. The possible presence modes with their meanings are as follows:<ul>
 * <p/>
 * <li>Presence.Mode.AVAILABLE -- (Default) the agent is available for more chats
 * (equivalent to Presence.Mode.CHAT).
 * <li>Presence.Mode.DO_NOT_DISTURB -- the agent is busy and should not be disturbed.
 * However, special case, or extreme urgency chats may still be offered to the agent.
 * <li>Presence.Mode.AWAY -- the agent is not available and should not
 * have a chat routed to them (equivalent to Presence.Mode.EXTENDED_AWAY).</ul>
 * <p/>
 * The max chats value is the maximum number of chats the agent is willing to have routed to
 * them at once. Some servers may be configured to only accept max chat values in a certain
 * range; for example, between two and five. In that case, the maxChats value the agent sends
 * may be adjusted by the server to a value within that range.
 *
 * @param presenceMode the presence mode of the agent.
 * @param maxChats     the maximum number of chats the agent is willing to accept.
 * @param status       sets the status message of the presence update.
 * @throws XMPPErrorException 
 * @throws NoResponseException 
 * @throws NotConnectedException 
 * @throws IllegalStateException if the agent is not online with the workgroup.
 */
public void setStatus(Presence.Mode presenceMode, int maxChats, String status)
                throws NoResponseException, XMPPErrorException, NotConnectedException {
    if (!online) {
        throw new IllegalStateException("Cannot set status when the agent is not online.");
    }

    if (presenceMode == null) {
        presenceMode = Presence.Mode.available;
    }
    this.presenceMode = presenceMode;
    this.maxChats = maxChats;

    Presence presence = new Presence(Presence.Type.available);
    presence.setMode(presenceMode);
    presence.setTo(this.getWorkgroupJID());

    if (status != null) {
        presence.setStatus(status);
    }
    // Send information about max chats and current chats as a packet extension.
    DefaultExtensionElement agentStatus = new DefaultExtensionElement(AgentStatus.ELEMENT_NAME,
                    AgentStatus.NAMESPACE);
    agentStatus.setValue("max-chats", "" + maxChats);
    presence.addExtension(agentStatus);
    presence.addExtension(new MetaData(this.metaData));

    PacketCollector collector = this.connection.createPacketCollectorAndSend(new AndFilter(
                    new StanzaTypeFilter(Presence.class),
                    FromMatchesFilter.create(workgroupJID)), presence);

    collector.nextResultOrThrow();
}