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.packet.DefaultPacketExtension;
020import org.jivesoftware.smack.packet.IQ;
021import org.jivesoftware.smack.provider.IQProvider;
022import org.jivesoftware.smackx.privacy.packet.Privacy;
023import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
024import org.xmlpull.v1.XmlPullParser;
025
026import java.util.ArrayList;
027
028/**
029 * The PrivacyProvider parses {@link Privacy} packets. {@link Privacy}
030 * Parses the <tt>query</tt> sub-document and creates an instance of {@link Privacy}.
031 * For each <tt>item</tt> in the <tt>list</tt> element, it creates an instance 
032 * of {@link PrivacyItem}.
033 * 
034 * @author Francisco Vives
035 */
036public class PrivacyProvider implements IQProvider {
037
038        public PrivacyProvider() {
039        }
040
041        public IQ parseIQ(XmlPullParser parser) throws Exception {
042        Privacy privacy = new Privacy();
043        /* privacy.addExtension(PacketParserUtils.parsePacketExtension(parser
044                .getName(), parser.getNamespace(), parser)); */
045        privacy.addExtension(new DefaultPacketExtension(parser.getName(), parser.getNamespace()));
046        boolean done = false;
047        while (!done) {
048            int eventType = parser.next();
049            if (eventType == XmlPullParser.START_TAG) {
050                if (parser.getName().equals("active")) {
051                        String activeName = parser.getAttributeValue("", "name");
052                        if (activeName == null) {
053                                privacy.setDeclineActiveList(true);
054                        } else {
055                                privacy.setActiveName(activeName);
056                        }
057                }
058                else if (parser.getName().equals("default")) {
059                        String defaultName = parser.getAttributeValue("", "name");
060                        if (defaultName == null) {
061                                privacy.setDeclineDefaultList(true);
062                        } else {
063                                privacy.setDefaultName(defaultName);
064                        }
065                }
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        public void parseList(XmlPullParser parser, Privacy privacy) throws Exception {
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                        items.add(parseItem(parser));
090                }
091            }
092            else if (eventType == XmlPullParser.END_TAG) {
093                if (parser.getName().equals("list")) {
094                    done = true;
095                }
096            }
097        }
098
099        privacy.setPrivacyList(listName, items);
100        }
101        
102        // Parse the list complex type
103        public PrivacyItem parseItem(XmlPullParser parser) throws Exception {
104        boolean done = false;
105        // Retrieves the required attributes
106        String actionValue = parser.getAttributeValue("", "action");
107        String orderValue = parser.getAttributeValue("", "order");
108        String type = parser.getAttributeValue("", "type");
109
110        /* 
111         * According the action value it sets the allow status. The fall-through action is assumed 
112         * to be "allow"
113         */
114        boolean allow = true;
115        if ("allow".equalsIgnoreCase(actionValue)) {
116                allow = true;
117        } else if ("deny".equalsIgnoreCase(actionValue)) {
118                allow = false;
119        }
120        // Set the order number
121        int order = Integer.parseInt(orderValue);
122
123        PrivacyItem item;
124        if (type != null) {
125            // If the type is not null, then we are dealing with a standard privacy item
126            String value = parser.getAttributeValue("", "value");
127            item = new PrivacyItem(PrivacyItem.Type.valueOf(type), value, allow, order);
128
129            while (!done) {
130                int eventType = parser.next();
131                if (eventType == XmlPullParser.START_TAG) {
132                    if (parser.getName().equals("iq")) {
133                        item.setFilterIQ(true);
134                    }
135                    if (parser.getName().equals("message")) {
136                        item.setFilterMessage(true);
137                    }
138                    if (parser.getName().equals("presence-in")) {
139                        item.setFilterPresenceIn(true);
140                    }
141                    if (parser.getName().equals("presence-out")) {
142                        item.setFilterPresenceOut(true);
143                    }
144                }
145                else if (eventType == XmlPullParser.END_TAG) {
146                    if (parser.getName().equals("item")) {
147                        done = true;
148                    }
149                }
150            }
151        }
152        else {
153            // If the type is null, then we are dealing with the fall-through privacy item.
154            item = new PrivacyItem(allow, order);
155        }
156        return item;
157        }
158}