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