001/** 002 * 003 * Copyright 2003-2007 Jive Software. 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 */ 017 018package org.jivesoftware.smackx.workgroup.ext.macros; 019 020import java.io.IOException; 021 022import org.jivesoftware.smack.packet.IQ; 023import org.jivesoftware.smack.packet.XmlEnvironment; 024import org.jivesoftware.smack.provider.IQProvider; 025import org.jivesoftware.smack.util.PacketParserUtils; 026import org.jivesoftware.smack.util.StringUtils; 027import org.jivesoftware.smack.xml.XmlPullParser; 028import org.jivesoftware.smack.xml.XmlPullParserException; 029 030/** 031 * Macros iq is responsible for handling global and personal macros in the a Live Assistant 032 * Workgroup. 033 */ 034public class Macros extends IQ { 035 036 /** 037 * Element name of the stanza extension. 038 */ 039 public static final String ELEMENT_NAME = "macros"; 040 041 /** 042 * Namespace of the stanza extension. 043 */ 044 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup"; 045 046 private MacroGroup rootGroup; 047 private boolean personal; 048 private MacroGroup personalMacroGroup; 049 050 public Macros() { 051 super(ELEMENT_NAME, NAMESPACE); 052 } 053 054 public MacroGroup getRootGroup() { 055 return rootGroup; 056 } 057 058 public void setRootGroup(MacroGroup rootGroup) { 059 this.rootGroup = rootGroup; 060 } 061 062 public boolean isPersonal() { 063 return personal; 064 } 065 066 public void setPersonal(boolean personal) { 067 this.personal = personal; 068 } 069 070 public MacroGroup getPersonalMacroGroup() { 071 return personalMacroGroup; 072 } 073 074 public void setPersonalMacroGroup(MacroGroup personalMacroGroup) { 075 this.personalMacroGroup = personalMacroGroup; 076 } 077 078 @Override 079 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 080 buf.rightAngleBracket(); 081 082 if (isPersonal()) { 083 buf.append("<personal>true</personal>"); 084 } 085 if (getPersonalMacroGroup() != null) { 086 buf.append("<personalMacro>"); 087 buf.append(StringUtils.escapeForXmlText(getPersonalMacroGroup().toXML())); 088 buf.append("</personalMacro>"); 089 } 090 091 return buf; 092 } 093 094 /** 095 * An IQProvider for Macro packets. 096 * 097 * @author Derek DeMoro 098 */ 099 public static class InternalProvider extends IQProvider<Macros> { 100 101 @Override 102 public Macros parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 103 Macros macroGroup = new Macros(); 104 105 boolean done = false; 106 while (!done) { 107 XmlPullParser.Event eventType = parser.next(); 108 if (eventType == XmlPullParser.Event.START_ELEMENT) { 109 if (parser.getName().equals("model")) { 110 String macros = parser.nextText(); 111 MacroGroup group = parseMacroGroups(macros); 112 macroGroup.setRootGroup(group); 113 } 114 } 115 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 116 if (parser.getName().equals(ELEMENT_NAME)) { 117 done = true; 118 } 119 } 120 } 121 122 return macroGroup; 123 } 124 125 public Macro parseMacro(XmlPullParser parser) throws XmlPullParserException, IOException { 126 Macro macro = new Macro(); 127 boolean done = false; 128 while (!done) { 129 XmlPullParser.Event eventType = parser.next(); 130 if (eventType == XmlPullParser.Event.START_ELEMENT) { 131 if (parser.getName().equals("title")) { 132 parser.next(); 133 macro.setTitle(parser.getText()); 134 } 135 else if (parser.getName().equals("description")) { 136 macro.setDescription(parser.nextText()); 137 } 138 else if (parser.getName().equals("response")) { 139 macro.setResponse(parser.nextText()); 140 } 141 else if (parser.getName().equals("type")) { 142 macro.setType(Integer.valueOf(parser.nextText()).intValue()); 143 } 144 } 145 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 146 if (parser.getName().equals("macro")) { 147 done = true; 148 } 149 } 150 } 151 return macro; 152 } 153 154 public MacroGroup parseMacroGroup(XmlPullParser parser) throws XmlPullParserException, IOException { 155 MacroGroup group = new MacroGroup(); 156 157 boolean done = false; 158 while (!done) { 159 XmlPullParser.Event eventType = parser.next(); 160 if (eventType == XmlPullParser.Event.START_ELEMENT) { 161 if (parser.getName().equals("macrogroup")) { 162 group.addMacroGroup(parseMacroGroup(parser)); 163 } 164 if (parser.getName().equals("title")) { 165 group.setTitle(parser.nextText()); 166 } 167 if (parser.getName().equals("macro")) { 168 group.addMacro(parseMacro(parser)); 169 } 170 } 171 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 172 if (parser.getName().equals("macrogroup")) { 173 done = true; 174 } 175 } 176 } 177 return group; 178 } 179 180 public MacroGroup parseMacroGroups(String macros) throws XmlPullParserException, IOException { 181 MacroGroup group = null; 182 XmlPullParser parser = PacketParserUtils.getParserFor(macros); 183 184 XmlPullParser.Event eventType = parser.getEventType(); 185 while (eventType != XmlPullParser.Event.END_DOCUMENT) { 186 eventType = parser.next(); 187 if (eventType == XmlPullParser.Event.START_ELEMENT) { 188 if (parser.getName().equals("macrogroup")) { 189 group = parseMacroGroup(parser); 190 } 191 } 192 } 193 return group; 194 } 195 } 196}