Macros.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  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.workgroup.ext.macros;

  18. import java.io.IOException;
  19. import java.io.StringReader;

  20. import org.jivesoftware.smack.packet.IQ;
  21. import org.jivesoftware.smack.provider.IQProvider;
  22. import org.jivesoftware.smack.util.StringUtils;
  23. import org.xmlpull.v1.XmlPullParserException;
  24. import org.xmlpull.v1.XmlPullParserFactory;
  25. import org.xmlpull.v1.XmlPullParser;

  26. /**
  27.  * Macros iq is responsible for handling global and personal macros in the a Live Assistant
  28.  * Workgroup.
  29.  */
  30. public class Macros extends IQ {

  31.     /**
  32.      * Element name of the packet extension.
  33.      */
  34.     public static final String ELEMENT_NAME = "macros";

  35.     /**
  36.      * Namespace of the packet extension.
  37.      */
  38.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  39.     private MacroGroup rootGroup;
  40.     private boolean personal;
  41.     private MacroGroup personalMacroGroup;

  42.     public Macros() {
  43.         super(ELEMENT_NAME, NAMESPACE);
  44.     }

  45.     public MacroGroup getRootGroup() {
  46.         return rootGroup;
  47.     }

  48.     public void setRootGroup(MacroGroup rootGroup) {
  49.         this.rootGroup = rootGroup;
  50.     }

  51.     public boolean isPersonal() {
  52.         return personal;
  53.     }

  54.     public void setPersonal(boolean personal) {
  55.         this.personal = personal;
  56.     }

  57.     public MacroGroup getPersonalMacroGroup() {
  58.         return personalMacroGroup;
  59.     }

  60.     public void setPersonalMacroGroup(MacroGroup personalMacroGroup) {
  61.         this.personalMacroGroup = personalMacroGroup;
  62.     }

  63.     @Override
  64.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  65.         buf.rightAngleBracket();

  66.         if (isPersonal()) {
  67.             buf.append("<personal>true</personal>");
  68.         }
  69.         if (getPersonalMacroGroup() != null) {          
  70.             buf.append("<personalMacro>");
  71.             buf.append(StringUtils.escapeForXML(getPersonalMacroGroup().toXML()));
  72.             buf.append("</personalMacro>");
  73.         }

  74.         return buf;
  75.     }

  76.     /**
  77.      * An IQProvider for Macro packets.
  78.      *
  79.      * @author Derek DeMoro
  80.      */
  81.     public static class InternalProvider extends IQProvider<Macros> {

  82.         @Override
  83.         public Macros parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  84.             Macros macroGroup = new Macros();

  85.             boolean done = false;
  86.             while (!done) {
  87.                 int eventType = parser.next();
  88.                 if (eventType == XmlPullParser.START_TAG) {
  89.                     if (parser.getName().equals("model")) {
  90.                         String macros = parser.nextText();
  91.                         MacroGroup group = parseMacroGroups(macros);
  92.                         macroGroup.setRootGroup(group);
  93.                     }
  94.                 }
  95.                 else if (eventType == XmlPullParser.END_TAG) {
  96.                     if (parser.getName().equals(ELEMENT_NAME)) {
  97.                         done = true;
  98.                     }
  99.                 }
  100.             }

  101.             return macroGroup;
  102.         }
  103.        
  104.         public Macro parseMacro(XmlPullParser parser) throws XmlPullParserException, IOException {
  105.             Macro macro = new Macro();
  106.              boolean done = false;
  107.             while (!done) {
  108.                 int eventType = parser.next();
  109.                 if (eventType == XmlPullParser.START_TAG) {
  110.                     if (parser.getName().equals("title")) {
  111.                         parser.next();
  112.                         macro.setTitle(parser.getText());
  113.                     }
  114.                     else if (parser.getName().equals("description")) {
  115.                         macro.setDescription(parser.nextText());
  116.                     }
  117.                     else if (parser.getName().equals("response")) {
  118.                         macro.setResponse(parser.nextText());
  119.                     }
  120.                     else if (parser.getName().equals("type")) {
  121.                         macro.setType(Integer.valueOf(parser.nextText()).intValue());
  122.                     }
  123.                 }
  124.                 else if (eventType == XmlPullParser.END_TAG) {
  125.                     if (parser.getName().equals("macro")) {
  126.                         done = true;
  127.                     }
  128.                 }
  129.             }
  130.             return macro;
  131.         }
  132.        
  133.         public MacroGroup parseMacroGroup(XmlPullParser parser) throws XmlPullParserException, IOException {
  134.             MacroGroup group = new MacroGroup();
  135.            
  136.             boolean done = false;
  137.             while (!done) {
  138.                 int eventType = parser.next();
  139.                 if (eventType == XmlPullParser.START_TAG) {
  140.                     if (parser.getName().equals("macrogroup")) {
  141.                         group.addMacroGroup(parseMacroGroup(parser));
  142.                     }
  143.                     if (parser.getName().equals("title")) {
  144.                         group.setTitle(parser.nextText());
  145.                     }
  146.                     if (parser.getName().equals("macro")) {
  147.                         group.addMacro(parseMacro(parser));
  148.                     }
  149.                 }
  150.                 else if (eventType == XmlPullParser.END_TAG) {
  151.                     if (parser.getName().equals("macrogroup")) {
  152.                         done = true;
  153.                     }
  154.                 }
  155.             }
  156.             return group;
  157.         }
  158.        
  159.         public MacroGroup parseMacroGroups(String macros) throws XmlPullParserException, IOException {

  160.             MacroGroup group = null;
  161.             XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
  162.             parser.setInput(new StringReader(macros));
  163.             int eventType = parser.getEventType();
  164.             while (eventType != XmlPullParser.END_DOCUMENT) {      
  165.                 eventType = parser.next();
  166.                  if (eventType == XmlPullParser.START_TAG) {
  167.                         if (parser.getName().equals("macrogroup")) {
  168.                             group = parseMacroGroup(parser);
  169.                         }
  170.                  }
  171.             }
  172.             return group;
  173.         }
  174.     }
  175. }