1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 22:32: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) {
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);

View file

@ -29,16 +29,16 @@ import java.util.List;
* MacroGroup datamodel.
*/
public class MacroGroup {
private List macros;
private List macroGroups;
private List<Macro> macros;
private List<MacroGroup> macroGroups;
// Define MacroGroup
private String title;
public MacroGroup() {
macros = new ArrayList();
macroGroups = new ArrayList();
macros = new ArrayList<Macro>();
macroGroups = new ArrayList<MacroGroup>();
}
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<Macro> col = Collections.unmodifiableList(macros);
Iterator<Macro> 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<MacroGroup> col = Collections.unmodifiableList(macroGroups);
Iterator<MacroGroup> 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<Macro> getMacros() {
return macros;
}
public void setMacros(List macros) {
public void setMacros(List<Macro> macros) {
this.macros = macros;
}
public List getMacroGroups() {
public List<MacroGroup> getMacroGroups() {
return macroGroups;
}
public void setMacroGroups(List macroGroups) {
public void setMacroGroups(List<MacroGroup> 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("<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;
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("<personal>true</personal>");
}
// 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("<personalMacro>").append(persitedGroup).append("</personalMacro>");
// }
if (getPersonalMacroGroup() != null) {
buf.append("<personalMacro>");
buf.append(StringUtils.escapeForXML(getPersonalMacroGroup().toXML()));
buf.append("</personalMacro>");
}
buf.append("</").append(ELEMENT_NAME).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;
}
}
}