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