2013-02-07 15:19:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-02-17 23:58:40 +01:00
|
|
|
package org.jivesoftware.smackx.privacy.provider;
|
|
|
|
|
2015-01-20 09:40:52 +01:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2014-02-15 22:35:38 +01:00
|
|
|
import org.jivesoftware.smack.provider.IQProvider;
|
2015-01-20 09:40:52 +01:00
|
|
|
import org.jivesoftware.smack.util.ParserUtils;
|
2014-02-15 22:35:38 +01:00
|
|
|
import org.jivesoftware.smackx.privacy.packet.Privacy;
|
|
|
|
import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
2014-10-07 21:15:20 +02:00
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
import java.io.IOException;
|
2014-02-17 23:58:40 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The PrivacyProvider parses {@link Privacy} packets. {@link Privacy}
|
|
|
|
* Parses the <tt>query</tt> sub-document and creates an instance of {@link Privacy}.
|
|
|
|
* For each <tt>item</tt> in the <tt>list</tt> element, it creates an instance
|
2014-03-10 14:45:17 +01:00
|
|
|
* of {@link PrivacyItem}.
|
2014-02-17 23:58:40 +01:00
|
|
|
*
|
|
|
|
* @author Francisco Vives
|
|
|
|
*/
|
2014-10-07 21:15:20 +02:00
|
|
|
public class PrivacyProvider extends IQProvider<Privacy> {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
@Override
|
|
|
|
public Privacy parse(XmlPullParser parser, int initialDepth)
|
2015-01-20 09:40:52 +01:00
|
|
|
throws XmlPullParserException, IOException, SmackException {
|
2014-02-17 23:58:40 +01:00
|
|
|
Privacy privacy = new Privacy();
|
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:OFF
|
2014-02-17 23:58:40 +01:00
|
|
|
if (parser.getName().equals("active")) {
|
2017-02-07 22:02:40 +01:00
|
|
|
String activeName = parser.getAttributeValue("", "name");
|
|
|
|
if (activeName == null) {
|
|
|
|
privacy.setDeclineActiveList(true);
|
|
|
|
} else {
|
|
|
|
privacy.setActiveName(activeName);
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
else if (parser.getName().equals("default")) {
|
2017-02-07 22:02:40 +01:00
|
|
|
String defaultName = parser.getAttributeValue("", "name");
|
|
|
|
if (defaultName == null) {
|
|
|
|
privacy.setDeclineDefaultList(true);
|
|
|
|
} else {
|
|
|
|
privacy.setDefaultName(defaultName);
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:ON
|
2014-02-17 23:58:40 +01:00
|
|
|
else if (parser.getName().equals("list")) {
|
|
|
|
parseList(parser, privacy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("query")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return privacy;
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
// Parse the list complex type
|
|
|
|
private static void parseList(XmlPullParser parser, Privacy privacy) throws XmlPullParserException, IOException, SmackException {
|
2014-02-17 23:58:40 +01:00
|
|
|
boolean done = false;
|
|
|
|
String listName = parser.getAttributeValue("", "name");
|
|
|
|
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
if (parser.getName().equals("item")) {
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:OFF
|
2017-02-07 22:02:40 +01:00
|
|
|
items.add(parseItem(parser));
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:ON
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("list")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
privacy.setPrivacyList(listName, items);
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:OFF
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
// Parse the list complex type
|
|
|
|
private static PrivacyItem parseItem(XmlPullParser parser) throws XmlPullParserException, IOException, SmackException {
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:ON
|
2014-02-17 23:58:40 +01:00
|
|
|
// Retrieves the required attributes
|
|
|
|
String actionValue = parser.getAttributeValue("", "action");
|
2015-01-20 09:40:52 +01:00
|
|
|
// Set the order number, this attribute is required
|
|
|
|
long order = ParserUtils.getLongAttribute(parser, "order");
|
|
|
|
|
|
|
|
// If type is not set, then it's the fall-through case
|
2014-02-17 23:58:40 +01:00
|
|
|
String type = parser.getAttributeValue("", "type");
|
2014-03-10 14:45:17 +01:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
/*
|
|
|
|
* According the action value it sets the allow status. The fall-through action is assumed
|
|
|
|
* to be "allow"
|
|
|
|
*/
|
2015-01-20 09:40:52 +01:00
|
|
|
boolean allow;
|
|
|
|
switch (actionValue) {
|
|
|
|
case "allow":
|
|
|
|
allow = true;
|
|
|
|
break;
|
|
|
|
case "deny":
|
|
|
|
allow = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new SmackException("Unkown action value '" + actionValue + "'");
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
2014-03-10 14:45:17 +01:00
|
|
|
PrivacyItem item;
|
|
|
|
if (type != null) {
|
|
|
|
// If the type is not null, then we are dealing with a standard privacy item
|
|
|
|
String value = parser.getAttributeValue("", "value");
|
|
|
|
item = new PrivacyItem(PrivacyItem.Type.valueOf(type), value, allow, order);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2014-03-10 14:45:17 +01:00
|
|
|
else {
|
|
|
|
// If the type is null, then we are dealing with the fall-through privacy item.
|
|
|
|
item = new PrivacyItem(allow, order);
|
|
|
|
}
|
2015-01-20 09:40:52 +01:00
|
|
|
parseItemChildElements(parser, item);
|
2014-02-17 23:58:40 +01:00
|
|
|
return item;
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:OFF
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:ON
|
2015-01-20 09:40:52 +01:00
|
|
|
|
|
|
|
private static void parseItemChildElements(XmlPullParser parser, PrivacyItem privacyItem) throws XmlPullParserException, IOException {
|
|
|
|
final int initialDepth = parser.getDepth();
|
|
|
|
|
|
|
|
outerloop: while (true) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
switch (eventType) {
|
|
|
|
case XmlPullParser.START_TAG:
|
|
|
|
String name = parser.getName();
|
|
|
|
switch (name) {
|
|
|
|
case "iq":
|
|
|
|
privacyItem.setFilterIQ(true);
|
|
|
|
break;
|
|
|
|
case "message":
|
|
|
|
privacyItem.setFilterMessage(true);
|
|
|
|
break;
|
|
|
|
case "presence-in":
|
|
|
|
privacyItem.setFilterPresenceIn(true);
|
|
|
|
break;
|
|
|
|
case "presence-out":
|
|
|
|
privacyItem.setFilterPresenceOut(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case XmlPullParser.END_TAG:
|
|
|
|
if (parser.getDepth() == initialDepth) {
|
|
|
|
break outerloop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|