From 0b7421eae3af59024164f4522a308a195afc4486 Mon Sep 17 00:00:00 2001 From: Michael Will Date: Wed, 25 Nov 2009 16:51:29 +0000 Subject: [PATCH] implement a simple macro parser git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@11418 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smack/util/PacketParserUtils.java | 8 +- .../workgroup/ext/macros/MacroGroup.java | 51 ++++++-- .../smackx/workgroup/ext/macros/Macros.java | 110 +++++++++++++----- 3 files changed, 125 insertions(+), 44 deletions(-) diff --git a/source/org/jivesoftware/smack/util/PacketParserUtils.java b/source/org/jivesoftware/smack/util/PacketParserUtils.java index aa3d9b942..ecaa4c42a 100644 --- a/source/org/jivesoftware/smack/util/PacketParserUtils.java +++ b/source/org/jivesoftware/smack/util/PacketParserUtils.java @@ -251,16 +251,16 @@ public class PacketParserUtils { else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("property")) { if ("integer".equals(type)) { - value = new Integer(valueText); + value = Integer.valueOf(valueText); } else if ("long".equals(type)) { - value = new Long(valueText); + value = Long.valueOf(valueText); } else if ("float".equals(type)) { - value = new Float(valueText); + value = Float.valueOf(valueText); } else if ("double".equals(type)) { - value = new Double(valueText); + value = Double.valueOf(valueText); } else if ("boolean".equals(type)) { value = Boolean.valueOf(valueText); diff --git a/source/org/jivesoftware/smackx/workgroup/ext/macros/MacroGroup.java b/source/org/jivesoftware/smackx/workgroup/ext/macros/MacroGroup.java index 87382807f..0742b3d26 100644 --- a/source/org/jivesoftware/smackx/workgroup/ext/macros/MacroGroup.java +++ b/source/org/jivesoftware/smackx/workgroup/ext/macros/MacroGroup.java @@ -29,16 +29,16 @@ import java.util.List; * MacroGroup datamodel. */ public class MacroGroup { - private List macros; - private List macroGroups; + private List macros; + private List macroGroups; // Define MacroGroup private String title; public MacroGroup() { - macros = new ArrayList(); - macroGroups = new ArrayList(); + macros = new ArrayList(); + macroGroups = new ArrayList(); } public void addMacro(Macro macro) { @@ -50,8 +50,8 @@ public class MacroGroup { } public Macro getMacroByTitle(String title) { - Collection col = Collections.unmodifiableList(macros); - Iterator iter = col.iterator(); + Collection col = Collections.unmodifiableList(macros); + Iterator iter = col.iterator(); while (iter.hasNext()) { Macro macro = (Macro)iter.next(); if (macro.getTitle().equalsIgnoreCase(title)) { @@ -74,8 +74,8 @@ public class MacroGroup { } public MacroGroup getMacroGroupByTitle(String title) { - Collection col = Collections.unmodifiableList(macroGroups); - Iterator iter = col.iterator(); + Collection col = Collections.unmodifiableList(macroGroups); + Iterator iter = col.iterator(); while (iter.hasNext()) { MacroGroup group = (MacroGroup)iter.next(); if (group.getTitle().equalsIgnoreCase(title)) { @@ -90,19 +90,19 @@ public class MacroGroup { } - public List getMacros() { + public List getMacros() { return macros; } - public void setMacros(List macros) { + public void setMacros(List macros) { this.macros = macros; } - public List getMacroGroups() { + public List getMacroGroups() { return macroGroups; } - public void setMacroGroups(List macroGroups) { + public void setMacroGroups(List macroGroups) { this.macroGroups = macroGroups; } @@ -113,4 +113,31 @@ public class MacroGroup { public void setTitle(String title) { this.title = title; } + + public String toXML() { + StringBuilder buf = new StringBuilder(); + buf.append(""); + buf.append("" + getTitle() + ""); + buf.append(""); + for (Macro macro : getMacros()) + { + buf.append(""); + buf.append("" + macro.getTitle() + ""); + buf.append("" + macro.getType() + ""); + buf.append("" + macro.getDescription() + ""); + buf.append("" + macro.getResponse() + ""); + buf.append(""); + } + buf.append(""); + + if (getMacroGroups().size() > 0) { + buf.append(""); + for (MacroGroup groups : getMacroGroups()) { + buf.append(groups.toXML()); + } + buf.append(""); + } + buf.append(""); + return buf.toString(); + } } diff --git a/source/org/jivesoftware/smackx/workgroup/ext/macros/Macros.java b/source/org/jivesoftware/smackx/workgroup/ext/macros/Macros.java index 509bd69c7..b658bf965 100644 --- a/source/org/jivesoftware/smackx/workgroup/ext/macros/Macros.java +++ b/source/org/jivesoftware/smackx/workgroup/ext/macros/Macros.java @@ -19,9 +19,12 @@ package org.jivesoftware.smackx.workgroup.ext.macros; +import java.io.StringReader; + import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.provider.IQProvider; import org.jivesoftware.smack.util.StringUtils; +import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParser; /** @@ -34,13 +37,6 @@ public class Macros extends IQ { private boolean personal; private MacroGroup personalMacroGroup; - private static ClassLoader cl; - - public static void setClassLoader(ClassLoader cloader) { - cl = cloader; - } - - public MacroGroup getRootGroup() { return rootGroup; } @@ -83,18 +79,11 @@ public class Macros extends IQ { if (isPersonal()) { buf.append("true"); } - // TODO: REMOVE XSTREAM -// if (getPersonalMacroGroup() != null) { -// final XStream xstream = new XStream(); -// xstream.alias("macro", Macro.class); -// xstream.alias("macrogroup", MacroGroup.class); -// -// if (cl != null) { -// xstream.setClassLoader(cl); -// } -// String persitedGroup = StringUtils.escapeForXML(xstream.toXML(getPersonalMacroGroup())); -// buf.append("").append(persitedGroup).append(""); -// } + if (getPersonalMacroGroup() != null) { + buf.append(""); + buf.append(StringUtils.escapeForXML(getPersonalMacroGroup().toXML())); + buf.append(""); + } buf.append(" "); return buf.toString(); @@ -120,15 +109,8 @@ public class Macros extends IQ { if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("model")) { String macros = parser.nextText(); - // TODO: REMOVE XSTREAM -// XStream xstream = new XStream(); -// 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); + MacroGroup group = parseMacroGroups(macros); + macroGroup.setRootGroup(group); } } else if (eventType == XmlPullParser.END_TAG) { @@ -140,5 +122,77 @@ public class Macros extends IQ { 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; + } } } \ No newline at end of file