blob: 869ec57775cfcf3c1c5a030e6feefd9aef0427be [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * $Revision$
3 * $Date$
4 *
5 * Copyright 2003-2007 Jive Software.
6 *
7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20package org.jivesoftware.smackx.workgroup.ext.macros;
21
22import java.io.StringReader;
23
24import org.jivesoftware.smack.packet.IQ;
25import org.jivesoftware.smack.provider.IQProvider;
26import org.jivesoftware.smack.util.StringUtils;
27import org.xmlpull.v1.XmlPullParserFactory;
28import org.xmlpull.v1.XmlPullParser;
29
30/**
31 * Macros iq is responsible for handling global and personal macros in the a Live Assistant
32 * Workgroup.
33 */
34public class Macros extends IQ {
35
36 private MacroGroup rootGroup;
37 private boolean personal;
38 private MacroGroup personalMacroGroup;
39
40 public MacroGroup getRootGroup() {
41 return rootGroup;
42 }
43
44 public void setRootGroup(MacroGroup rootGroup) {
45 this.rootGroup = rootGroup;
46 }
47
48 public boolean isPersonal() {
49 return personal;
50 }
51
52 public void setPersonal(boolean personal) {
53 this.personal = personal;
54 }
55
56 public MacroGroup getPersonalMacroGroup() {
57 return personalMacroGroup;
58 }
59
60 public void setPersonalMacroGroup(MacroGroup personalMacroGroup) {
61 this.personalMacroGroup = personalMacroGroup;
62 }
63
64
65 /**
66 * Element name of the packet extension.
67 */
68 public static final String ELEMENT_NAME = "macros";
69
70 /**
71 * Namespace of the packet extension.
72 */
73 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
74
75 public String getChildElementXML() {
76 StringBuilder buf = new StringBuilder();
77
78 buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">");
79 if (isPersonal()) {
80 buf.append("<personal>true</personal>");
81 }
82 if (getPersonalMacroGroup() != null) {
83 buf.append("<personalMacro>");
84 buf.append(StringUtils.escapeForXML(getPersonalMacroGroup().toXML()));
85 buf.append("</personalMacro>");
86 }
87 buf.append("</").append(ELEMENT_NAME).append("> ");
88
89 return buf.toString();
90 }
91
92 /**
93 * An IQProvider for Macro packets.
94 *
95 * @author Derek DeMoro
96 */
97 public static class InternalProvider implements IQProvider {
98
99 public InternalProvider() {
100 super();
101 }
102
103 public IQ parseIQ(XmlPullParser parser) throws Exception {
104 Macros macroGroup = new Macros();
105
106 boolean done = false;
107 while (!done) {
108 int eventType = parser.next();
109 if (eventType == XmlPullParser.START_TAG) {
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.END_TAG) {
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 Exception {
127 Macro macro = new Macro();
128 boolean done = false;
129 while (!done) {
130 int eventType = parser.next();
131 if (eventType == XmlPullParser.START_TAG) {
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.END_TAG) {
147 if (parser.getName().equals("macro")) {
148 done = true;
149 }
150 }
151 }
152 return macro;
153 }
154
155 public MacroGroup parseMacroGroup(XmlPullParser parser) throws Exception {
156 MacroGroup group = new MacroGroup();
157
158 boolean done = false;
159 while (!done) {
160 int eventType = parser.next();
161 if (eventType == XmlPullParser.START_TAG) {
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.END_TAG) {
173 if (parser.getName().equals("macrogroup")) {
174 done = true;
175 }
176 }
177 }
178 return group;
179 }
180
181 public MacroGroup parseMacroGroups(String macros) throws Exception {
182
183 MacroGroup group = null;
184 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
185 parser.setInput(new StringReader(macros));
186 int eventType = parser.getEventType();
187 while (eventType != XmlPullParser.END_DOCUMENT) {
188 eventType = parser.next();
189 if (eventType == XmlPullParser.START_TAG) {
190 if (parser.getName().equals("macrogroup")) {
191 group = parseMacroGroup(parser);
192 }
193 }
194 }
195 return group;
196 }
197 }
198}