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; 021import java.io.StringReader; 022 023import org.jivesoftware.smack.packet.IQ; 024import org.jivesoftware.smack.provider.IQProvider; 025import org.jivesoftware.smack.util.StringUtils; 026import org.xmlpull.v1.XmlPullParserException; 027import org.xmlpull.v1.XmlPullParserFactory; 028import org.xmlpull.v1.XmlPullParser; 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(/packet) extension. 038 */ 039 public static final String ELEMENT_NAME = "macros"; 040 041 /** 042 * Namespace of the stanza(/packet) 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 // CHECKSTYLE:OFF 087 buf.append("<personalMacro>"); 088 buf.append(StringUtils.escapeForXmlText(getPersonalMacroGroup().toXML())); 089 buf.append("</personalMacro>"); 090 // CHECKSTYLE:ON 091 } 092 093 return buf; 094 } 095 096 /** 097 * An IQProvider for Macro packets. 098 * 099 * @author Derek DeMoro 100 */ 101 public static class InternalProvider extends IQProvider<Macros> { 102 103 @Override 104 public Macros parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { 105 Macros macroGroup = new Macros(); 106 107 boolean done = false; 108 while (!done) { 109 int eventType = parser.next(); 110 if (eventType == XmlPullParser.START_TAG) { 111 if (parser.getName().equals("model")) { 112 String macros = parser.nextText(); 113 MacroGroup group = parseMacroGroups(macros); 114 macroGroup.setRootGroup(group); 115 } 116 } 117 else if (eventType == XmlPullParser.END_TAG) { 118 if (parser.getName().equals(ELEMENT_NAME)) { 119 done = true; 120 } 121 } 122 } 123 124 return macroGroup; 125 } 126 127 public Macro parseMacro(XmlPullParser parser) throws XmlPullParserException, IOException { 128 // CHECKSTYLE:OFF 129 Macro macro = new Macro(); 130 boolean done = false; 131 while (!done) { 132 int eventType = parser.next(); 133 if (eventType == XmlPullParser.START_TAG) { 134 if (parser.getName().equals("title")) { 135 parser.next(); 136 macro.setTitle(parser.getText()); 137 } 138 else if (parser.getName().equals("description")) { 139 macro.setDescription(parser.nextText()); 140 } 141 else if (parser.getName().equals("response")) { 142 macro.setResponse(parser.nextText()); 143 } 144 else if (parser.getName().equals("type")) { 145 macro.setType(Integer.valueOf(parser.nextText()).intValue()); 146 } 147 } 148 else if (eventType == XmlPullParser.END_TAG) { 149 if (parser.getName().equals("macro")) { 150 done = true; 151 } 152 } 153 } 154 return macro; 155 // CHECKSTYLE:ON 156 } 157 158 public MacroGroup parseMacroGroup(XmlPullParser parser) throws XmlPullParserException, IOException { 159 // CHECKSTYLE:OFF 160 MacroGroup group = new MacroGroup(); 161 162 boolean done = false; 163 while (!done) { 164 int eventType = parser.next(); 165 if (eventType == XmlPullParser.START_TAG) { 166 if (parser.getName().equals("macrogroup")) { 167 group.addMacroGroup(parseMacroGroup(parser)); 168 } 169 if (parser.getName().equals("title")) { 170 group.setTitle(parser.nextText()); 171 } 172 if (parser.getName().equals("macro")) { 173 group.addMacro(parseMacro(parser)); 174 } 175 } 176 else if (eventType == XmlPullParser.END_TAG) { 177 if (parser.getName().equals("macrogroup")) { 178 done = true; 179 } 180 } 181 } 182 return group; 183 // CHECKSTYLE:ON 184 } 185 186 public MacroGroup parseMacroGroups(String macros) throws XmlPullParserException, IOException { 187 // CHECKSTYLE:OFF 188 MacroGroup group = null; 189 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); 190 parser.setInput(new StringReader(macros)); 191 int eventType = parser.getEventType(); 192 while (eventType != XmlPullParser.END_DOCUMENT) { 193 eventType = parser.next(); 194 if (eventType == XmlPullParser.START_TAG) { 195 if (parser.getName().equals("macrogroup")) { 196 group = parseMacroGroup(parser); 197 } 198 } 199 } 200 return group; 201 // CHECKSTYLE:ON 202 } 203 } 204}