AMPExtensionProvider.java

  1. /**
  2.  *
  3.  * Copyright 2014 Vyacheslav Blinov
  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.amp.provider;

  18. import java.io.IOException;
  19. import java.util.logging.Logger;

  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.jivesoftware.smackx.amp.AMPDeliverCondition;
  22. import org.jivesoftware.smackx.amp.AMPExpireAtCondition;
  23. import org.jivesoftware.smackx.amp.AMPMatchResourceCondition;
  24. import org.jivesoftware.smackx.amp.packet.AMPExtension;
  25. import org.xmlpull.v1.XmlPullParser;
  26. import org.xmlpull.v1.XmlPullParserException;


  27. public class AMPExtensionProvider extends ExtensionElementProvider<AMPExtension> {
  28.     private static final Logger LOGGER = Logger.getLogger(AMPExtensionProvider.class.getName());

  29.     /**
  30.      * Parses a AMPExtension packet (extension sub-packet).
  31.      *
  32.      * @param parser the XML parser, positioned at the starting element of the extension.
  33.      * @return a PacketExtension.
  34.      * @throws IOException
  35.      * @throws XmlPullParserException
  36.      */
  37.     @Override
  38.     public AMPExtension parse(XmlPullParser parser, int initialDepth)
  39.                     throws XmlPullParserException, IOException {
  40.         final String from = parser.getAttributeValue(null, "from");
  41.         final String to = parser.getAttributeValue(null, "to");
  42.         final String statusString = parser.getAttributeValue(null, "status");
  43.         AMPExtension.Status status = null;
  44.         if (statusString != null) {
  45.             try {
  46.                 status = AMPExtension.Status.valueOf(statusString);
  47.             } catch (IllegalArgumentException ex) {
  48.                 LOGGER.severe("Found invalid amp status " + statusString);
  49.             }
  50.         }

  51.         AMPExtension ampExtension = new AMPExtension(from, to, status);

  52.         String perHopValue = parser.getAttributeValue(null, "per-hop");
  53.         if (perHopValue != null) {
  54.             boolean perHop = Boolean.parseBoolean(perHopValue);
  55.             ampExtension.setPerHop(perHop);
  56.         }

  57.         boolean done = false;
  58.         while (!done) {
  59.             int eventType = parser.next();
  60.             if (eventType == XmlPullParser.START_TAG) {
  61.                 if (parser.getName().equals(AMPExtension.Rule.ELEMENT)) {
  62.                     String actionString = parser.getAttributeValue(null, AMPExtension.Action.ATTRIBUTE_NAME);
  63.                     String conditionName = parser.getAttributeValue(null, AMPExtension.Condition.ATTRIBUTE_NAME);
  64.                     String conditionValue = parser.getAttributeValue(null, "value");

  65.                     AMPExtension.Condition condition = createCondition(conditionName, conditionValue);
  66.                     AMPExtension.Action action = null;
  67.                     if (actionString != null) {
  68.                         try {
  69.                             action = AMPExtension.Action.valueOf(actionString);
  70.                         } catch (IllegalArgumentException ex) {
  71.                             LOGGER.severe("Found invalid rule action value " + actionString);
  72.                         }
  73.                     }

  74.                     if (action == null || condition == null) {
  75.                         LOGGER.severe("Rule is skipped because either it's action or it's condition is invalid");
  76.                     } else {
  77.                         AMPExtension.Rule rule = new AMPExtension.Rule(action, condition);
  78.                         ampExtension.addRule(rule);
  79.                     }
  80.                 }
  81.             } else if (eventType == XmlPullParser.END_TAG) {
  82.                 if (parser.getName().equals(AMPExtension.ELEMENT)) {
  83.                     done = true;
  84.                 }
  85.             }
  86.         }

  87.         return ampExtension;
  88.     }

  89.     private AMPExtension.Condition createCondition(String name, String value) {
  90.         if (name == null || value == null) {
  91.             LOGGER.severe("Can't create rule condition from null name and/or value");
  92.             return null;
  93.         }


  94.         if (AMPDeliverCondition.NAME.equals(name)) {
  95.             try {
  96.                 return new AMPDeliverCondition(AMPDeliverCondition.Value.valueOf(value));
  97.             } catch (IllegalArgumentException ex) {
  98.                 LOGGER.severe("Found invalid rule delivery condition value " + value);
  99.                 return null;
  100.             }
  101.         } else if (AMPExpireAtCondition.NAME.equals(name)) {
  102.             return new AMPExpireAtCondition(value);
  103.         } else if (AMPMatchResourceCondition.NAME.equals(name)) {
  104.             try {
  105.                 return new AMPMatchResourceCondition(AMPMatchResourceCondition.Value.valueOf(value));
  106.             } catch (IllegalArgumentException ex) {
  107.                 LOGGER.severe("Found invalid rule match-resource condition value " + value);
  108.                 return null;
  109.             }
  110.         } else {
  111.             LOGGER.severe("Found unknown rule condition name " + name);
  112.             return null;
  113.         }
  114.     }
  115. }