MessageEventProvider.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.xevent.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.XmlEnvironment;
  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.jivesoftware.smack.xml.XmlPullParser;
  22. import org.jivesoftware.smack.xml.XmlPullParserException;

  23. import org.jivesoftware.smackx.xevent.packet.MessageEvent;

  24. /**
  25.  *
  26.  * The MessageEventProvider parses Message Event packets.
  27. *
  28.  * @author Gaston Dombiak
  29.  */
  30. public class MessageEventProvider extends ExtensionElementProvider<MessageEvent> {

  31.     /**
  32.      * Parses a MessageEvent stanza (extension sub-packet).
  33.      *
  34.      * @param parser the XML parser, positioned at the starting element of the extension.
  35.      * @return a PacketExtension.
  36.      * @throws IOException if an I/O error occurred.
  37.      * @throws XmlPullParserException if an error in the XML parser occurred.
  38.      */
  39.     @Override
  40.     public MessageEvent parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
  41.                     throws XmlPullParserException, IOException {
  42.         MessageEvent messageEvent = new MessageEvent();
  43.         boolean done = false;
  44.         while (!done) {
  45.             XmlPullParser.Event eventType = parser.next();
  46.             if (eventType == XmlPullParser.Event.START_ELEMENT) {
  47.                 if (parser.getName().equals("id"))
  48.                     messageEvent.setStanzaId(parser.nextText());
  49.                 if (parser.getName().equals(MessageEvent.COMPOSING))
  50.                     messageEvent.setComposing(true);
  51.                 if (parser.getName().equals(MessageEvent.DELIVERED))
  52.                     messageEvent.setDelivered(true);
  53.                 if (parser.getName().equals(MessageEvent.DISPLAYED))
  54.                     messageEvent.setDisplayed(true);
  55.                 if (parser.getName().equals(MessageEvent.OFFLINE))
  56.                     messageEvent.setOffline(true);
  57.             } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  58.                 if (parser.getName().equals("x")) {
  59.                     done = true;
  60.                 }
  61.             }
  62.         }

  63.         return messageEvent;
  64.     }

  65. }