001/**
002 *
003 * Copyright 2014 Vyacheslav Blinov
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.amp.provider;
018
019import java.io.IOException;
020import java.util.logging.Logger;
021
022import org.jivesoftware.smack.provider.ExtensionElementProvider;
023import org.jivesoftware.smackx.amp.AMPDeliverCondition;
024import org.jivesoftware.smackx.amp.AMPExpireAtCondition;
025import org.jivesoftware.smackx.amp.AMPMatchResourceCondition;
026import org.jivesoftware.smackx.amp.packet.AMPExtension;
027import org.xmlpull.v1.XmlPullParser;
028import org.xmlpull.v1.XmlPullParserException;
029
030
031public class AMPExtensionProvider extends ExtensionElementProvider<AMPExtension> {
032    private static final Logger LOGGER = Logger.getLogger(AMPExtensionProvider.class.getName());
033
034    /**
035     * Parses a AMPExtension stanza(/packet) (extension sub-packet).
036     *
037     * @param parser the XML parser, positioned at the starting element of the extension.
038     * @return a PacketExtension.
039     * @throws IOException 
040     * @throws XmlPullParserException 
041     */
042    @Override
043    public AMPExtension parse(XmlPullParser parser, int initialDepth)
044                    throws XmlPullParserException, IOException {
045        final String from = parser.getAttributeValue(null, "from");
046        final String to = parser.getAttributeValue(null, "to");
047        final String statusString = parser.getAttributeValue(null, "status");
048        AMPExtension.Status status = null;
049        if (statusString != null) {
050            try {
051                status = AMPExtension.Status.valueOf(statusString);
052            } catch (IllegalArgumentException ex) {
053                LOGGER.severe("Found invalid amp status " + statusString);
054            }
055        }
056
057        AMPExtension ampExtension = new AMPExtension(from, to, status);
058
059        String perHopValue = parser.getAttributeValue(null, "per-hop");
060        if (perHopValue != null) {
061            boolean perHop = Boolean.parseBoolean(perHopValue);
062            ampExtension.setPerHop(perHop);
063        }
064
065        boolean done = false;
066        while (!done) {
067            int eventType = parser.next();
068            if (eventType == XmlPullParser.START_TAG) {
069                if (parser.getName().equals(AMPExtension.Rule.ELEMENT)) {
070                    String actionString = parser.getAttributeValue(null, AMPExtension.Action.ATTRIBUTE_NAME);
071                    String conditionName = parser.getAttributeValue(null, AMPExtension.Condition.ATTRIBUTE_NAME);
072                    String conditionValue = parser.getAttributeValue(null, "value");
073
074                    AMPExtension.Condition condition = createCondition(conditionName, conditionValue);
075                    AMPExtension.Action action = null;
076                    if (actionString != null) {
077                        try {
078                            action = AMPExtension.Action.valueOf(actionString);
079                        } catch (IllegalArgumentException ex) {
080                            LOGGER.severe("Found invalid rule action value " + actionString);
081                        }
082                    }
083
084                    if (action == null || condition == null) {
085                        LOGGER.severe("Rule is skipped because either it's action or it's condition is invalid");
086                    } else {
087                        AMPExtension.Rule rule = new AMPExtension.Rule(action, condition);
088                        ampExtension.addRule(rule);
089                    }
090                }
091            } else if (eventType == XmlPullParser.END_TAG) {
092                if (parser.getName().equals(AMPExtension.ELEMENT)) {
093                    done = true;
094                }
095            }
096        }
097
098        return ampExtension;
099    }
100
101    private AMPExtension.Condition createCondition(String name, String value) {
102        if (name == null || value == null) {
103            LOGGER.severe("Can't create rule condition from null name and/or value");
104            return null;
105        }
106
107
108        if (AMPDeliverCondition.NAME.equals(name)) {
109            try {
110                return new AMPDeliverCondition(AMPDeliverCondition.Value.valueOf(value));
111            } catch (IllegalArgumentException ex) {
112                LOGGER.severe("Found invalid rule delivery condition value " + value);
113                return null;
114            }
115        } else if (AMPExpireAtCondition.NAME.equals(name)) {
116            return new AMPExpireAtCondition(value);
117        } else if (AMPMatchResourceCondition.NAME.equals(name)) {
118            try {
119                return new AMPMatchResourceCondition(AMPMatchResourceCondition.Value.valueOf(value));
120            } catch (IllegalArgumentException ex) {
121                LOGGER.severe("Found invalid rule match-resource condition value " + value);
122                return null;
123            }
124        } else {
125            LOGGER.severe("Found unknown rule condition name " + name);
126            return null;
127        }
128    }
129}