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