001/** 002 * 003 * Copyright the original author or authors 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.privacy.provider; 018 019import org.jivesoftware.smack.SmackException; 020import org.jivesoftware.smack.provider.IQProvider; 021import org.jivesoftware.smack.util.ParserUtils; 022import org.jivesoftware.smackx.privacy.packet.Privacy; 023import org.jivesoftware.smackx.privacy.packet.PrivacyItem; 024import org.xmlpull.v1.XmlPullParser; 025import org.xmlpull.v1.XmlPullParserException; 026 027import java.io.IOException; 028import java.util.ArrayList; 029 030/** 031 * The PrivacyProvider parses {@link Privacy} packets. {@link Privacy} 032 * Parses the <tt>query</tt> sub-document and creates an instance of {@link Privacy}. 033 * For each <tt>item</tt> in the <tt>list</tt> element, it creates an instance 034 * of {@link PrivacyItem}. 035 * 036 * @author Francisco Vives 037 */ 038public class PrivacyProvider extends IQProvider<Privacy> { 039 040 @Override 041 public Privacy parse(XmlPullParser parser, int initialDepth) 042 throws XmlPullParserException, IOException, SmackException { 043 Privacy privacy = new Privacy(); 044 boolean done = false; 045 while (!done) { 046 int eventType = parser.next(); 047 if (eventType == XmlPullParser.START_TAG) { 048 // CHECKSTYLE:OFF 049 if (parser.getName().equals("active")) { 050 String activeName = parser.getAttributeValue("", "name"); 051 if (activeName == null) { 052 privacy.setDeclineActiveList(true); 053 } else { 054 privacy.setActiveName(activeName); 055 } 056 } 057 else if (parser.getName().equals("default")) { 058 String defaultName = parser.getAttributeValue("", "name"); 059 if (defaultName == null) { 060 privacy.setDeclineDefaultList(true); 061 } else { 062 privacy.setDefaultName(defaultName); 063 } 064 } 065 // CHECKSTYLE:ON 066 else if (parser.getName().equals("list")) { 067 parseList(parser, privacy); 068 } 069 } 070 else if (eventType == XmlPullParser.END_TAG) { 071 if (parser.getName().equals("query")) { 072 done = true; 073 } 074 } 075 } 076 077 return privacy; 078 } 079 080 // Parse the list complex type 081 private static void parseList(XmlPullParser parser, Privacy privacy) throws XmlPullParserException, IOException, SmackException { 082 boolean done = false; 083 String listName = parser.getAttributeValue("", "name"); 084 ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>(); 085 while (!done) { 086 int eventType = parser.next(); 087 if (eventType == XmlPullParser.START_TAG) { 088 if (parser.getName().equals("item")) { 089 // CHECKSTYLE:OFF 090 items.add(parseItem(parser)); 091 // CHECKSTYLE:ON 092 } 093 } 094 else if (eventType == XmlPullParser.END_TAG) { 095 if (parser.getName().equals("list")) { 096 done = true; 097 } 098 } 099 } 100 101 privacy.setPrivacyList(listName, items); 102 // CHECKSTYLE:OFF 103 } 104 105 // Parse the list complex type 106 private static PrivacyItem parseItem(XmlPullParser parser) throws XmlPullParserException, IOException, SmackException { 107 // CHECKSTYLE:ON 108 // Retrieves the required attributes 109 String actionValue = parser.getAttributeValue("", "action"); 110 // Set the order number, this attribute is required 111 long order = ParserUtils.getLongAttribute(parser, "order"); 112 113 // If type is not set, then it's the fall-through case 114 String type = parser.getAttributeValue("", "type"); 115 116 /* 117 * According the action value it sets the allow status. The fall-through action is assumed 118 * to be "allow" 119 */ 120 boolean allow; 121 switch (actionValue) { 122 case "allow": 123 allow = true; 124 break; 125 case "deny": 126 allow = false; 127 break; 128 default: 129 throw new SmackException("Unkown action value '" + actionValue + "'"); 130 } 131 132 PrivacyItem item; 133 if (type != null) { 134 // If the type is not null, then we are dealing with a standard privacy item 135 String value = parser.getAttributeValue("", "value"); 136 item = new PrivacyItem(PrivacyItem.Type.valueOf(type), value, allow, order); 137 } 138 else { 139 // If the type is null, then we are dealing with the fall-through privacy item. 140 item = new PrivacyItem(allow, order); 141 } 142 parseItemChildElements(parser, item); 143 return item; 144 // CHECKSTYLE:OFF 145 } 146 // CHECKSTYLE:ON 147 148 private static void parseItemChildElements(XmlPullParser parser, PrivacyItem privacyItem) throws XmlPullParserException, IOException { 149 final int initialDepth = parser.getDepth(); 150 151 outerloop: while (true) { 152 int eventType = parser.next(); 153 switch (eventType) { 154 case XmlPullParser.START_TAG: 155 String name = parser.getName(); 156 switch (name) { 157 case "iq": 158 privacyItem.setFilterIQ(true); 159 break; 160 case "message": 161 privacyItem.setFilterMessage(true); 162 break; 163 case "presence-in": 164 privacyItem.setFilterPresenceIn(true); 165 break; 166 case "presence-out": 167 privacyItem.setFilterPresenceOut(true); 168 break; 169 } 170 break; 171 case XmlPullParser.END_TAG: 172 if (parser.getDepth() == initialDepth) { 173 break outerloop; 174 } 175 } 176 } 177 } 178}