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 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            // 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            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        }
156
157        public MacroGroup parseMacroGroup(XmlPullParser parser) throws XmlPullParserException, IOException {
158            MacroGroup group = new MacroGroup();
159
160            boolean done = false;
161            while (!done) {
162                int eventType = parser.next();
163                if (eventType == XmlPullParser.START_TAG) {
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.END_TAG) {
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 = XmlPullParserFactory.newInstance().newPullParser();
186            parser.setInput(new StringReader(macros));
187            int eventType = parser.getEventType();
188            while (eventType != XmlPullParser.END_DOCUMENT) {
189                eventType = parser.next();
190                 if (eventType == XmlPullParser.START_TAG) {
191                        if (parser.getName().equals("macrogroup")) {
192                            group = parseMacroGroup(parser);
193                        }
194                 }
195            }
196            return group;
197        }
198    }
199}