PrivacyProvider.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  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.privacy.provider;

  18. import org.jivesoftware.smack.SmackException;
  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.jivesoftware.smack.util.ParserUtils;
  21. import org.jivesoftware.smackx.privacy.packet.Privacy;
  22. import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
  23. import org.xmlpull.v1.XmlPullParser;
  24. import org.xmlpull.v1.XmlPullParserException;

  25. import java.io.IOException;
  26. import java.util.ArrayList;

  27. /**
  28.  * The PrivacyProvider parses {@link Privacy} packets. {@link Privacy}
  29.  * Parses the <tt>query</tt> sub-document and creates an instance of {@link Privacy}.
  30.  * For each <tt>item</tt> in the <tt>list</tt> element, it creates an instance
  31.  * of {@link PrivacyItem}.
  32.  *
  33.  * @author Francisco Vives
  34.  */
  35. public class PrivacyProvider extends IQProvider<Privacy> {

  36.     @Override
  37.     public Privacy parse(XmlPullParser parser, int initialDepth)
  38.                     throws XmlPullParserException, IOException, SmackException {
  39.         Privacy privacy = new Privacy();
  40.         boolean done = false;
  41.         while (!done) {
  42.             int eventType = parser.next();
  43.             if (eventType == XmlPullParser.START_TAG) {
  44.                 if (parser.getName().equals("active")) {
  45.                     String activeName = parser.getAttributeValue("", "name");
  46.                     if (activeName == null) {
  47.                         privacy.setDeclineActiveList(true);
  48.                     } else {
  49.                         privacy.setActiveName(activeName);
  50.                     }
  51.                 }
  52.                 else if (parser.getName().equals("default")) {
  53.                     String defaultName = parser.getAttributeValue("", "name");
  54.                     if (defaultName == null) {
  55.                         privacy.setDeclineDefaultList(true);
  56.                     } else {
  57.                         privacy.setDefaultName(defaultName);
  58.                     }
  59.                 }
  60.                 else if (parser.getName().equals("list")) {
  61.                     parseList(parser, privacy);
  62.                 }
  63.             }
  64.             else if (eventType == XmlPullParser.END_TAG) {
  65.                 if (parser.getName().equals("query")) {
  66.                     done = true;
  67.                 }
  68.             }
  69.         }

  70.         return privacy;
  71.     }
  72.    
  73.     // Parse the list complex type
  74.     private static void parseList(XmlPullParser parser, Privacy privacy) throws XmlPullParserException, IOException, SmackException {
  75.         boolean done = false;
  76.         String listName = parser.getAttributeValue("", "name");
  77.         ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
  78.         while (!done) {
  79.             int eventType = parser.next();
  80.             if (eventType == XmlPullParser.START_TAG) {
  81.                 if (parser.getName().equals("item")) {
  82.                     items.add(parseItem(parser));
  83.                 }
  84.             }
  85.             else if (eventType == XmlPullParser.END_TAG) {
  86.                 if (parser.getName().equals("list")) {
  87.                     done = true;
  88.                 }
  89.             }
  90.         }

  91.         privacy.setPrivacyList(listName, items);
  92.     }
  93.    
  94.     // Parse the list complex type
  95.     private static PrivacyItem parseItem(XmlPullParser parser) throws XmlPullParserException, IOException, SmackException {
  96.         // Retrieves the required attributes
  97.         String actionValue = parser.getAttributeValue("", "action");
  98.         // Set the order number, this attribute is required
  99.         long order = ParserUtils.getLongAttribute(parser, "order");

  100.         // If type is not set, then it's the fall-through case
  101.         String type = parser.getAttributeValue("", "type");

  102.         /*
  103.          * According the action value it sets the allow status. The fall-through action is assumed
  104.          * to be "allow"
  105.          */
  106.         boolean allow;
  107.         switch (actionValue) {
  108.         case "allow":
  109.             allow = true;
  110.             break;
  111.         case "deny":
  112.             allow = false;
  113.             break;
  114.         default:
  115.             throw new SmackException("Unkown action value '" + actionValue + "'");
  116.         }

  117.         PrivacyItem item;
  118.         if (type != null) {
  119.             // If the type is not null, then we are dealing with a standard privacy item
  120.             String value = parser.getAttributeValue("", "value");
  121.             item = new PrivacyItem(PrivacyItem.Type.valueOf(type), value, allow, order);
  122.         }
  123.         else {
  124.             // If the type is null, then we are dealing with the fall-through privacy item.
  125.             item = new PrivacyItem(allow, order);
  126.         }
  127.         parseItemChildElements(parser, item);
  128.         return item;
  129.     }

  130.     private static void parseItemChildElements(XmlPullParser parser, PrivacyItem privacyItem) throws XmlPullParserException, IOException {
  131.         final int initialDepth = parser.getDepth();

  132.         outerloop: while (true) {
  133.             int eventType = parser.next();
  134.             switch (eventType) {
  135.             case XmlPullParser.START_TAG:
  136.                 String name = parser.getName();
  137.                 switch (name) {
  138.                 case "iq":
  139.                     privacyItem.setFilterIQ(true);
  140.                     break;
  141.                 case "message":
  142.                     privacyItem.setFilterMessage(true);
  143.                     break;
  144.                 case "presence-in":
  145.                     privacyItem.setFilterPresenceIn(true);
  146.                     break;
  147.                 case "presence-out":
  148.                     privacyItem.setFilterPresenceOut(true);
  149.                     break;
  150.                 }
  151.                 break;
  152.             case XmlPullParser.END_TAG:
  153.                 if (parser.getDepth() == initialDepth) {
  154.                     break outerloop;
  155.                 }
  156.             }
  157.         }
  158.     }
  159. }