1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-29 23:42:06 +01:00

implement a simple macro parser

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@11418 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Michael Will 2009-11-25 16:51:29 +00:00 committed by michael.will
parent f89e762f02
commit 0b7421eae3
3 changed files with 125 additions and 44 deletions

View file

@ -251,16 +251,16 @@ public class PacketParserUtils {
else if (eventType == XmlPullParser.END_TAG) { else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("property")) { if (parser.getName().equals("property")) {
if ("integer".equals(type)) { if ("integer".equals(type)) {
value = new Integer(valueText); value = Integer.valueOf(valueText);
} }
else if ("long".equals(type)) { else if ("long".equals(type)) {
value = new Long(valueText); value = Long.valueOf(valueText);
} }
else if ("float".equals(type)) { else if ("float".equals(type)) {
value = new Float(valueText); value = Float.valueOf(valueText);
} }
else if ("double".equals(type)) { else if ("double".equals(type)) {
value = new Double(valueText); value = Double.valueOf(valueText);
} }
else if ("boolean".equals(type)) { else if ("boolean".equals(type)) {
value = Boolean.valueOf(valueText); value = Boolean.valueOf(valueText);

View file

@ -29,16 +29,16 @@ import java.util.List;
* MacroGroup datamodel. * MacroGroup datamodel.
*/ */
public class MacroGroup { public class MacroGroup {
private List macros; private List<Macro> macros;
private List macroGroups; private List<MacroGroup> macroGroups;
// Define MacroGroup // Define MacroGroup
private String title; private String title;
public MacroGroup() { public MacroGroup() {
macros = new ArrayList(); macros = new ArrayList<Macro>();
macroGroups = new ArrayList(); macroGroups = new ArrayList<MacroGroup>();
} }
public void addMacro(Macro macro) { public void addMacro(Macro macro) {
@ -50,8 +50,8 @@ public class MacroGroup {
} }
public Macro getMacroByTitle(String title) { public Macro getMacroByTitle(String title) {
Collection col = Collections.unmodifiableList(macros); Collection<Macro> col = Collections.unmodifiableList(macros);
Iterator iter = col.iterator(); Iterator<Macro> iter = col.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
Macro macro = (Macro)iter.next(); Macro macro = (Macro)iter.next();
if (macro.getTitle().equalsIgnoreCase(title)) { if (macro.getTitle().equalsIgnoreCase(title)) {
@ -74,8 +74,8 @@ public class MacroGroup {
} }
public MacroGroup getMacroGroupByTitle(String title) { public MacroGroup getMacroGroupByTitle(String title) {
Collection col = Collections.unmodifiableList(macroGroups); Collection<MacroGroup> col = Collections.unmodifiableList(macroGroups);
Iterator iter = col.iterator(); Iterator<MacroGroup> iter = col.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
MacroGroup group = (MacroGroup)iter.next(); MacroGroup group = (MacroGroup)iter.next();
if (group.getTitle().equalsIgnoreCase(title)) { if (group.getTitle().equalsIgnoreCase(title)) {
@ -90,19 +90,19 @@ public class MacroGroup {
} }
public List getMacros() { public List<Macro> getMacros() {
return macros; return macros;
} }
public void setMacros(List macros) { public void setMacros(List<Macro> macros) {
this.macros = macros; this.macros = macros;
} }
public List getMacroGroups() { public List<MacroGroup> getMacroGroups() {
return macroGroups; return macroGroups;
} }
public void setMacroGroups(List macroGroups) { public void setMacroGroups(List<MacroGroup> macroGroups) {
this.macroGroups = macroGroups; this.macroGroups = macroGroups;
} }
@ -113,4 +113,31 @@ public class MacroGroup {
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
} }
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<macrogroup>");
buf.append("<title>" + getTitle() + "</title>");
buf.append("<macros>");
for (Macro macro : getMacros())
{
buf.append("<macro>");
buf.append("<title>" + macro.getTitle() + "</title>");
buf.append("<type>" + macro.getType() + "</type>");
buf.append("<description>" + macro.getDescription() + "</description>");
buf.append("<response>" + macro.getResponse() + "</response>");
buf.append("</macro>");
}
buf.append("</macros>");
if (getMacroGroups().size() > 0) {
buf.append("<macroGroups>");
for (MacroGroup groups : getMacroGroups()) {
buf.append(groups.toXML());
}
buf.append("</macroGroups>");
}
buf.append("</macrogroup>");
return buf.toString();
}
} }

View file

@ -19,9 +19,12 @@
package org.jivesoftware.smackx.workgroup.ext.macros; package org.jivesoftware.smackx.workgroup.ext.macros;
import java.io.StringReader;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider; import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.mxp1.MXParser;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
/** /**
@ -34,13 +37,6 @@ public class Macros extends IQ {
private boolean personal; private boolean personal;
private MacroGroup personalMacroGroup; private MacroGroup personalMacroGroup;
private static ClassLoader cl;
public static void setClassLoader(ClassLoader cloader) {
cl = cloader;
}
public MacroGroup getRootGroup() { public MacroGroup getRootGroup() {
return rootGroup; return rootGroup;
} }
@ -83,18 +79,11 @@ public class Macros extends IQ {
if (isPersonal()) { if (isPersonal()) {
buf.append("<personal>true</personal>"); buf.append("<personal>true</personal>");
} }
// TODO: REMOVE XSTREAM if (getPersonalMacroGroup() != null) {
// if (getPersonalMacroGroup() != null) { buf.append("<personalMacro>");
// final XStream xstream = new XStream(); buf.append(StringUtils.escapeForXML(getPersonalMacroGroup().toXML()));
// xstream.alias("macro", Macro.class); buf.append("</personalMacro>");
// xstream.alias("macrogroup", MacroGroup.class); }
//
// if (cl != null) {
// xstream.setClassLoader(cl);
// }
// String persitedGroup = StringUtils.escapeForXML(xstream.toXML(getPersonalMacroGroup()));
// buf.append("<personalMacro>").append(persitedGroup).append("</personalMacro>");
// }
buf.append("</").append(ELEMENT_NAME).append("> "); buf.append("</").append(ELEMENT_NAME).append("> ");
return buf.toString(); return buf.toString();
@ -120,15 +109,8 @@ public class Macros extends IQ {
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("model")) { if (parser.getName().equals("model")) {
String macros = parser.nextText(); String macros = parser.nextText();
// TODO: REMOVE XSTREAM MacroGroup group = parseMacroGroups(macros);
// XStream xstream = new XStream(); macroGroup.setRootGroup(group);
// if(cl != null){
// xstream.setClassLoader(cl);
// }
// xstream.alias("macro", Macro.class);
// xstream.alias("macrogroup", MacroGroup.class);
// MacroGroup group = (MacroGroup)xstream.fromXML(macros);
// macroGroup.setRootGroup(group);
} }
} }
else if (eventType == XmlPullParser.END_TAG) { else if (eventType == XmlPullParser.END_TAG) {
@ -140,5 +122,77 @@ public class Macros extends IQ {
return macroGroup; return macroGroup;
} }
public Macro parseMacro(XmlPullParser parser) throws Exception {
Macro macro = new Macro();
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("title")) {
parser.next();
macro.setTitle(parser.getText());
}
else if (parser.getName().equals("description")) {
macro.setDescription(parser.nextText());
}
else if (parser.getName().equals("response")) {
macro.setResponse(parser.nextText());
}
else if (parser.getName().equals("type")) {
macro.setType(Integer.valueOf(parser.nextText()).intValue());
}
}
else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("macro")) {
done = true;
}
}
}
return macro;
}
public MacroGroup parseMacroGroup(XmlPullParser parser) throws Exception {
MacroGroup group = new MacroGroup();
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("macrogroup")) {
group.addMacroGroup(parseMacroGroup(parser));
}
if (parser.getName().equals("title")) {
group.setTitle(parser.nextText());
}
if (parser.getName().equals("macro")) {
group.addMacro(parseMacro(parser));
}
}
else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("macrogroup")) {
done = true;
}
}
}
return group;
}
public MacroGroup parseMacroGroups(String macros) throws Exception {
MacroGroup group = null;
XmlPullParser parser = new MXParser();
parser.setInput(new StringReader(macros));
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("macrogroup")) {
group = parseMacroGroup(parser);
}
}
}
return group;
}
} }
} }