MamPrefsIQProvider.java

  1. /**
  2.  *
  3.  * Copyright 2016 Fernando Ramirez
  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.mam.provider;

  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;

  21. import org.jivesoftware.smack.packet.IqData;
  22. import org.jivesoftware.smack.packet.XmlEnvironment;
  23. import org.jivesoftware.smack.provider.IqProvider;
  24. import org.jivesoftware.smack.xml.XmlPullParser;
  25. import org.jivesoftware.smack.xml.XmlPullParserException;

  26. import org.jivesoftware.smackx.mam.element.MamElementFactory;
  27. import org.jivesoftware.smackx.mam.element.MamPrefsIQ;
  28. import org.jivesoftware.smackx.mam.element.MamPrefsIQ.DefaultBehavior;

  29. import org.jxmpp.jid.Jid;
  30. import org.jxmpp.jid.impl.JidCreate;

  31. /**
  32.  * MAM Preferences IQ Provider class.
  33.  *
  34.  * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
  35.  *      Archive Management</a>
  36.  * @author Fernando Ramirez
  37.  *
  38.  */
  39. public class MamPrefsIQProvider extends IqProvider<MamPrefsIQ> {

  40.     public static final MamPrefsIQProvider INSTANCE = new MamPrefsIQProvider();

  41.     @Override
  42.     public MamPrefsIQ parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  43.         MamElementFactory elementFactory =  MamElementFactory.forParser(parser);
  44.         String defaultBehaviorString = parser.getAttributeValue("", "default");
  45.         DefaultBehavior defaultBehavior = null;
  46.         if (defaultBehaviorString != null) {
  47.             defaultBehavior = DefaultBehavior.valueOf(defaultBehaviorString);
  48.         }

  49.         List<Jid> alwaysJids = null;
  50.         List<Jid> neverJids = null;

  51.         outerloop: while (true) {
  52.             final XmlPullParser.Event eventType = parser.next();
  53.             switch (eventType) {
  54.             case START_ELEMENT:
  55.                 final String name = parser.getName();
  56.                 switch (name) {
  57.                 case "always":
  58.                     alwaysJids = iterateJids(parser);
  59.                     break;
  60.                 case "never":
  61.                     neverJids = iterateJids(parser);
  62.                     break;
  63.                 }
  64.                 break;
  65.             case END_ELEMENT:
  66.                 if (parser.getDepth() == initialDepth) {
  67.                     break outerloop;
  68.                 }
  69.                 break;
  70.             default:
  71.                 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  72.                 break;
  73.             }
  74.         }

  75.         return elementFactory.newPrefsIQ(alwaysJids, neverJids, defaultBehavior);
  76.     }

  77.     private static List<Jid> iterateJids(XmlPullParser parser) throws XmlPullParserException, IOException {
  78.         List<Jid> jids = new ArrayList<>();

  79.         int initialDepth = parser.getDepth();

  80.         outerloop: while (true) {
  81.             final XmlPullParser.Event eventType = parser.next();
  82.             switch (eventType) {
  83.             case START_ELEMENT:
  84.                 final String name = parser.getName();
  85.                 switch (name) {
  86.                 case "jid":
  87.                     parser.next();
  88.                     jids.add(JidCreate.from(parser.getText()));
  89.                     break;
  90.                 }
  91.                 break;
  92.             case  END_ELEMENT:
  93.                 if (parser.getDepth() == initialDepth) {
  94.                     break outerloop;
  95.                 }
  96.                 break;
  97.             default:
  98.                 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  99.                 break;
  100.             }
  101.         }

  102.         return jids;
  103.     }

  104. }