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