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
031import org.jxmpp.JxmppContext;
032
033/**
034 * Macros iq is responsible for handling global and personal macros in the a Live Assistant
035 * Workgroup.
036 */
037public class Macros extends IQ {
038
039    /**
040     * Element name of the stanza extension.
041     */
042    public static final String ELEMENT_NAME = "macros";
043
044    /**
045     * Namespace of the stanza extension.
046     */
047    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
048
049    private MacroGroup rootGroup;
050    private boolean personal;
051    private MacroGroup personalMacroGroup;
052
053    public Macros() {
054        super(ELEMENT_NAME, NAMESPACE);
055    }
056
057    public MacroGroup getRootGroup() {
058        return rootGroup;
059    }
060
061    public void setRootGroup(MacroGroup rootGroup) {
062        this.rootGroup = rootGroup;
063    }
064
065    public boolean isPersonal() {
066        return personal;
067    }
068
069    public void setPersonal(boolean personal) {
070        this.personal = personal;
071    }
072
073    public MacroGroup getPersonalMacroGroup() {
074        return personalMacroGroup;
075    }
076
077    public void setPersonalMacroGroup(MacroGroup personalMacroGroup) {
078        this.personalMacroGroup = personalMacroGroup;
079    }
080
081    @Override
082    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
083        buf.rightAngleBracket();
084
085        if (isPersonal()) {
086            buf.append("<personal>true</personal>");
087        }
088        if (getPersonalMacroGroup() != null) {
089            buf.append("<personalMacro>");
090            buf.append(StringUtils.escapeForXmlText(getPersonalMacroGroup().toXML()));
091            buf.append("</personalMacro>");
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, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
106            Macros macroGroup = new Macros();
107
108            boolean done = false;
109            while (!done) {
110                XmlPullParser.Event eventType = parser.next();
111                if (eventType == XmlPullParser.Event.START_ELEMENT) {
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.Event.END_ELEMENT) {
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            Macro macro = new Macro();
130             boolean done = false;
131            while (!done) {
132                XmlPullParser.Event eventType = parser.next();
133                if (eventType == XmlPullParser.Event.START_ELEMENT) {
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.Event.END_ELEMENT) {
149                    if (parser.getName().equals("macro")) {
150                        done = true;
151                    }
152                }
153            }
154            return macro;
155        }
156
157        public MacroGroup parseMacroGroup(XmlPullParser parser) throws XmlPullParserException, IOException {
158            MacroGroup group = new MacroGroup();
159
160            boolean done = false;
161            while (!done) {
162                XmlPullParser.Event eventType = parser.next();
163                if (eventType == XmlPullParser.Event.START_ELEMENT) {
164                    if (parser.getName().equals("macrogroup")) {
165                        group.addMacroGroup(parseMacroGroup(parser));
166                    }
167                    if (parser.getName().equals("title")) {
168                        group.setTitle(parser.nextText());
169                    }
170                    if (parser.getName().equals("macro")) {
171                        group.addMacro(parseMacro(parser));
172                    }
173                }
174                else if (eventType == XmlPullParser.Event.END_ELEMENT) {
175                    if (parser.getName().equals("macrogroup")) {
176                        done = true;
177                    }
178                }
179            }
180            return group;
181        }
182
183        public MacroGroup parseMacroGroups(String macros) throws XmlPullParserException, IOException {
184            MacroGroup group = null;
185            XmlPullParser parser = PacketParserUtils.getParserFor(macros);
186
187            XmlPullParser.Event eventType = parser.getEventType();
188            while (eventType != XmlPullParser.Event.END_DOCUMENT) {
189                eventType = parser.next();
190                 if (eventType == XmlPullParser.Event.START_ELEMENT) {
191                        if (parser.getName().equals("macrogroup")) {
192                            group = parseMacroGroup(parser);
193                        }
194                 }
195            }
196            return group;
197        }
198    }
199}