001/* 002 * 003 * Copyright 2016 Fernando Ramirez 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.mam.provider; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.jivesoftware.smack.packet.IqData; 024import org.jivesoftware.smack.packet.XmlEnvironment; 025import org.jivesoftware.smack.provider.IqProvider; 026import org.jivesoftware.smack.xml.XmlPullParser; 027import org.jivesoftware.smack.xml.XmlPullParserException; 028 029import org.jivesoftware.smackx.mam.element.MamElementFactory; 030import org.jivesoftware.smackx.mam.element.MamPrefsIQ; 031import org.jivesoftware.smackx.mam.element.MamPrefsIQ.DefaultBehavior; 032 033import org.jxmpp.JxmppContext; 034import org.jxmpp.jid.Jid; 035import org.jxmpp.jid.impl.JidCreate; 036 037/** 038 * MAM Preferences IQ Provider class. 039 * 040 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message 041 * Archive Management</a> 042 * @author Fernando Ramirez 043 * 044 */ 045public class MamPrefsIQProvider extends IqProvider<MamPrefsIQ> { 046 047 public static final MamPrefsIQProvider INSTANCE = new MamPrefsIQProvider(); 048 049 @Override 050 public MamPrefsIQ parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException { 051 MamElementFactory elementFactory = MamElementFactory.forParser(parser); 052 String defaultBehaviorString = parser.getAttributeValue("", "default"); 053 DefaultBehavior defaultBehavior = null; 054 if (defaultBehaviorString != null) { 055 defaultBehavior = DefaultBehavior.valueOf(defaultBehaviorString); 056 } 057 058 List<Jid> alwaysJids = null; 059 List<Jid> neverJids = null; 060 061 outerloop: while (true) { 062 final XmlPullParser.Event eventType = parser.next(); 063 switch (eventType) { 064 case START_ELEMENT: 065 final String name = parser.getName(); 066 switch (name) { 067 case "always": 068 alwaysJids = iterateJids(parser); 069 break; 070 case "never": 071 neverJids = iterateJids(parser); 072 break; 073 } 074 break; 075 case END_ELEMENT: 076 if (parser.getDepth() == initialDepth) { 077 break outerloop; 078 } 079 break; 080 default: 081 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 082 break; 083 } 084 } 085 086 return elementFactory.newPrefsIQ(alwaysJids, neverJids, defaultBehavior); 087 } 088 089 private static List<Jid> iterateJids(XmlPullParser parser) throws XmlPullParserException, IOException { 090 List<Jid> jids = new ArrayList<>(); 091 092 int initialDepth = parser.getDepth(); 093 094 outerloop: while (true) { 095 final XmlPullParser.Event eventType = parser.next(); 096 switch (eventType) { 097 case START_ELEMENT: 098 final String name = parser.getName(); 099 switch (name) { 100 case "jid": 101 parser.next(); 102 jids.add(JidCreate.from(parser.getText())); 103 break; 104 } 105 break; 106 case END_ELEMENT: 107 if (parser.getDepth() == initialDepth) { 108 break outerloop; 109 } 110 break; 111 default: 112 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 113 break; 114 } 115 } 116 117 return jids; 118 } 119 120}