2016-07-21 16:40:26 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2016 Fernando Ramirez
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.mam.provider;
|
|
|
|
|
2016-07-23 15:45:45 +02:00
|
|
|
import java.io.IOException;
|
2016-07-21 16:40:26 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-02-15 23:21:30 +01:00
|
|
|
import org.jivesoftware.smack.packet.XmlEnvironment;
|
2016-07-21 16:40:26 +02:00
|
|
|
import org.jivesoftware.smack.provider.IQProvider;
|
2019-05-06 22:06:13 +02:00
|
|
|
import org.jivesoftware.smack.xml.XmlPullParser;
|
|
|
|
import org.jivesoftware.smack.xml.XmlPullParserException;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2016-07-21 16:40:26 +02:00
|
|
|
import org.jivesoftware.smackx.mam.element.MamPrefsIQ;
|
2016-07-23 15:45:45 +02:00
|
|
|
import org.jivesoftware.smackx.mam.element.MamPrefsIQ.DefaultBehavior;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2016-07-21 16:40:26 +02:00
|
|
|
import org.jxmpp.jid.Jid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MAM Preferences IQ Provider class.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2016-07-21 16:40:26 +02:00
|
|
|
* @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
|
|
|
|
* Archive Management</a>
|
|
|
|
* @author Fernando Ramirez
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2016-07-21 16:40:26 +02:00
|
|
|
*/
|
|
|
|
public class MamPrefsIQProvider extends IQProvider<MamPrefsIQ> {
|
|
|
|
|
|
|
|
@Override
|
2019-02-15 23:21:30 +01:00
|
|
|
public MamPrefsIQ parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
|
2016-07-21 16:40:26 +02:00
|
|
|
String iqType = parser.getAttributeValue("", "type");
|
2016-07-23 15:45:45 +02:00
|
|
|
String defaultBehaviorString = parser.getAttributeValue("", "default");
|
|
|
|
DefaultBehavior defaultBehavior = null;
|
|
|
|
if (defaultBehaviorString != null) {
|
|
|
|
defaultBehavior = DefaultBehavior.valueOf(defaultBehaviorString);
|
|
|
|
}
|
2016-07-21 16:40:26 +02:00
|
|
|
|
|
|
|
if (iqType == null) {
|
|
|
|
iqType = "result";
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Jid> alwaysJids = null;
|
|
|
|
List<Jid> neverJids = null;
|
|
|
|
|
|
|
|
outerloop: while (true) {
|
2019-05-06 22:06:13 +02:00
|
|
|
final XmlPullParser.Event eventType = parser.next();
|
2016-07-23 15:45:45 +02:00
|
|
|
final String name = parser.getName();
|
|
|
|
switch (eventType) {
|
2019-05-06 22:06:13 +02:00
|
|
|
case START_ELEMENT:
|
2016-07-23 15:45:45 +02:00
|
|
|
switch (name) {
|
|
|
|
case "always":
|
2016-07-21 16:40:26 +02:00
|
|
|
alwaysJids = iterateJids(parser);
|
2016-07-23 15:45:45 +02:00
|
|
|
break;
|
|
|
|
case "never":
|
2016-07-21 16:40:26 +02:00
|
|
|
neverJids = iterateJids(parser);
|
2016-07-23 15:45:45 +02:00
|
|
|
break;
|
2016-07-21 16:40:26 +02:00
|
|
|
}
|
2016-07-23 15:45:45 +02:00
|
|
|
break;
|
2019-05-06 22:06:13 +02:00
|
|
|
case END_ELEMENT:
|
2016-07-21 16:40:26 +02:00
|
|
|
if (parser.getDepth() == initialDepth) {
|
|
|
|
break outerloop;
|
|
|
|
}
|
2016-07-23 15:45:45 +02:00
|
|
|
break;
|
2019-05-06 22:06:13 +02:00
|
|
|
default:
|
|
|
|
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
|
|
|
|
break;
|
2016-07-21 16:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-23 15:45:45 +02:00
|
|
|
return new MamPrefsIQ(alwaysJids, neverJids, defaultBehavior);
|
2016-07-21 16:40:26 +02:00
|
|
|
}
|
|
|
|
|
2016-07-23 15:45:45 +02:00
|
|
|
private static List<Jid> iterateJids(XmlPullParser parser) throws XmlPullParserException, IOException {
|
2016-07-21 16:40:26 +02:00
|
|
|
List<Jid> jids = new ArrayList<>();
|
|
|
|
|
|
|
|
int initialDepth = parser.getDepth();
|
|
|
|
|
|
|
|
outerloop: while (true) {
|
2019-05-06 22:06:13 +02:00
|
|
|
final XmlPullParser.Event eventType = parser.next();
|
2016-07-23 15:45:45 +02:00
|
|
|
final String name = parser.getName();
|
|
|
|
switch (eventType) {
|
2019-05-06 22:06:13 +02:00
|
|
|
case START_ELEMENT:
|
2016-07-23 15:45:45 +02:00
|
|
|
switch (name) {
|
|
|
|
case "jid":
|
2016-07-21 16:40:26 +02:00
|
|
|
parser.next();
|
|
|
|
jids.add(JidCreate.from(parser.getText()));
|
2016-07-23 15:45:45 +02:00
|
|
|
break;
|
2016-07-21 16:40:26 +02:00
|
|
|
}
|
2016-07-23 15:45:45 +02:00
|
|
|
break;
|
2019-05-06 22:06:13 +02:00
|
|
|
case END_ELEMENT:
|
2016-07-21 16:40:26 +02:00
|
|
|
if (parser.getDepth() == initialDepth) {
|
|
|
|
break outerloop;
|
|
|
|
}
|
2016-07-23 15:45:45 +02:00
|
|
|
break;
|
2019-05-06 22:06:13 +02:00
|
|
|
default:
|
|
|
|
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
|
|
|
|
break;
|
2016-07-21 16:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jids;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|