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