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.jid.Jid; 034import org.jxmpp.jid.impl.JidCreate; 035 036/** 037 * MAM Preferences IQ Provider class. 038 * 039 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message 040 * Archive Management</a> 041 * @author Fernando Ramirez 042 * 043 */ 044public class MamPrefsIQProvider extends IqProvider<MamPrefsIQ> { 045 046 public static final MamPrefsIQProvider INSTANCE = new MamPrefsIQProvider(); 047 048 @Override 049 public MamPrefsIQ parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 050 MamElementFactory elementFactory = MamElementFactory.forParser(parser); 051 String defaultBehaviorString = parser.getAttributeValue("", "default"); 052 DefaultBehavior defaultBehavior = null; 053 if (defaultBehaviorString != null) { 054 defaultBehavior = DefaultBehavior.valueOf(defaultBehaviorString); 055 } 056 057 List<Jid> alwaysJids = null; 058 List<Jid> neverJids = null; 059 060 outerloop: while (true) { 061 final XmlPullParser.Event eventType = parser.next(); 062 switch (eventType) { 063 case START_ELEMENT: 064 final String name = parser.getName(); 065 switch (name) { 066 case "always": 067 alwaysJids = iterateJids(parser); 068 break; 069 case "never": 070 neverJids = iterateJids(parser); 071 break; 072 } 073 break; 074 case END_ELEMENT: 075 if (parser.getDepth() == initialDepth) { 076 break outerloop; 077 } 078 break; 079 default: 080 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 081 break; 082 } 083 } 084 085 return elementFactory.newPrefsIQ(alwaysJids, neverJids, defaultBehavior); 086 } 087 088 private static List<Jid> iterateJids(XmlPullParser parser) throws XmlPullParserException, IOException { 089 List<Jid> jids = new ArrayList<>(); 090 091 int initialDepth = parser.getDepth(); 092 093 outerloop: while (true) { 094 final XmlPullParser.Event eventType = parser.next(); 095 switch (eventType) { 096 case START_ELEMENT: 097 final String name = parser.getName(); 098 switch (name) { 099 case "jid": 100 parser.next(); 101 jids.add(JidCreate.from(parser.getText())); 102 break; 103 } 104 break; 105 case END_ELEMENT: 106 if (parser.getDepth() == initialDepth) { 107 break outerloop; 108 } 109 break; 110 default: 111 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 112 break; 113 } 114 } 115 116 return jids; 117 } 118 119}