MUCParserUtils.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.muc.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.util.ParserUtils;
  20. import org.jivesoftware.smackx.muc.MUCAffiliation;
  21. import org.jivesoftware.smackx.muc.MUCRole;
  22. import org.jivesoftware.smackx.muc.packet.Destroy;
  23. import org.jivesoftware.smackx.muc.packet.MUCItem;
  24. import org.jxmpp.jid.BareJid;
  25. import org.jxmpp.jid.Jid;
  26. import org.jxmpp.jid.parts.Resourcepart;
  27. import org.xmlpull.v1.XmlPullParser;
  28. import org.xmlpull.v1.XmlPullParserException;

  29. public class MUCParserUtils {
  30.     public static MUCItem parseItem(XmlPullParser parser) throws XmlPullParserException, IOException {
  31.         int initialDepth = parser.getDepth();
  32.         MUCAffiliation affiliation = MUCAffiliation.fromString(parser.getAttributeValue("", "affiliation"));
  33.         Resourcepart nick = ParserUtils.getResourcepartAttribute(parser, "nick");
  34.         MUCRole role = MUCRole.fromString(parser.getAttributeValue("", "role"));
  35.         Jid jid = ParserUtils.getJidAttribute(parser);
  36.         Jid actor = null;
  37.         String reason = null;
  38.         outerloop: while (true) {
  39.             int eventType = parser.next();
  40.             switch (eventType) {
  41.             case XmlPullParser.START_TAG:
  42.                 String name = parser.getName();
  43.                 switch (name) {
  44.                 case "actor":
  45.                     actor = ParserUtils.getJidAttribute(parser);
  46.                     break;
  47.                 case "reason":
  48.                     reason = parser.nextText();
  49.                     break;
  50.                 }
  51.             case XmlPullParser.END_TAG:
  52.                 if (parser.getDepth() == initialDepth) {
  53.                     break outerloop;
  54.                 }
  55.                 break;
  56.             }
  57.         }
  58.         return new MUCItem(affiliation, role, actor, reason, jid, nick);
  59.     }

  60.     public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserException, IOException {
  61.         final int initialDepth = parser.getDepth();
  62.         final BareJid jid = ParserUtils.getBareJidAttribute(parser);
  63.         String reason = null;
  64.         outerloop: while (true) {
  65.             int eventType = parser.next();
  66.             switch (eventType) {
  67.             case XmlPullParser.START_TAG:
  68.                 final String name = parser.getName();
  69.                 switch (name) {
  70.                 case "reason":
  71.                     reason = parser.nextText();
  72.                     break;
  73.                 }
  74.                 break;
  75.             case XmlPullParser.END_TAG:
  76.                 if (initialDepth == parser.getDepth()) {
  77.                     break outerloop;
  78.                 }
  79.                 break;
  80.             }
  81.         }
  82.         return new Destroy(jid, reason);
  83.     }
  84. }