diff options
author | Mili Verma <mili.verma@isode.com> | 2012-01-06 15:00:55 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2012-01-09 15:54:52 (GMT) |
commit | cc760bfd15caadb56bfef477cb54dc94c25f7fa7 (patch) | |
tree | 05f5918b97488b05ca1266c0644a3874adb98129 /src/com/isode/stroke/parser/payloadparsers/CommandParser.java | |
parent | 12b1d667965556002ea0fd300a71bcdf57634e90 (diff) | |
download | stroke-cc760bfd15caadb56bfef477cb54dc94c25f7fa7.zip stroke-cc760bfd15caadb56bfef477cb54dc94c25f7fa7.tar.bz2 |
Port Adhoc commands to Stroke
This patch ports the Adhoc commands from Swiften to Stroke.
It also ports their unit tests.
Test-information:
Unit tests pass.
MLC able to use the ad-hoc command fine.
Diffstat (limited to 'src/com/isode/stroke/parser/payloadparsers/CommandParser.java')
-rw-r--r-- | src/com/isode/stroke/parser/payloadparsers/CommandParser.java | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/src/com/isode/stroke/parser/payloadparsers/CommandParser.java b/src/com/isode/stroke/parser/payloadparsers/CommandParser.java new file mode 100644 index 0000000..b94acfb --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/CommandParser.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2012 Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.Command; +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.Command.Action; +import com.isode.stroke.elements.Command.Note; +import com.isode.stroke.elements.Command.Status; +import com.isode.stroke.elements.Command.Note.Type; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +/** + * Parser for {@link Command} element. + */ +public class CommandParser extends GenericPayloadParser<Command> { + private static final int TopLevel = 0; + private static final int PayloadLevel = 1; + private static final int FormOrNoteOrActionsLevel = 2; + private static final int ActionsActionLevel = 3; + + private int level_; + private boolean inNote_; + private boolean inActions_; + private Type noteType_; + private FormParserFactory formParserFactory_; + private FormParser formParser_; + private String currentText_; + + /** + * Constructor + */ + public CommandParser() { + super(new Command()); + + level_ = TopLevel; + inNote_ = false; + inActions_ = false; + noteType_ = Type.INFO; + formParser_ = null; + + formParserFactory_ = new FormParserFactory(); + } + + public void handleStartElement(String element, String ns, + AttributeMap attributes) { + ++level_; + Command command = getPayloadInternal(); + + if (level_ == PayloadLevel) { + Action action = parseAction(attributes + .getAttribute(Command.COMMAND_ATTRIBUTE_ACTION)); + command.setAction(action); + + String status = attributes + .getAttribute(Command.COMMAND_ATTRIBUTE_STATUS); + command.setStatus(Status.getStatus(status)); + + command.setNode(attributes + .getAttribute(Command.COMMAND_ATTRIBUTE_NODE)); + command.setSessionID(attributes + .getAttribute(Command.COMMAND_ATTRIBUTE_SESSION_ID)); + } else if (level_ == FormOrNoteOrActionsLevel) { + assert (formParser_ == null); + if (formParserFactory_.canParse(element, ns, attributes)) { + formParser_ = (FormParser) (formParserFactory_ + .createPayloadParser()); + assert (formParser_ != null); + } else if (element.equals(Command.COMMAND_ELEMENT_NOTE)) { + inNote_ = true; + currentText_ = ""; + String noteType = attributes + .getAttribute(Command.COMMAND_ATTRIBUTE_TYPE); + noteType_ = Type.getType(noteType); + } else if (element.equals(Command.COMMAND_ELEMENT_ACTIONS)) { + inActions_ = true; + Action action = parseAction(attributes + .getAttribute(Command.COMMAND_ATTRIBUTE_EXECUTE)); + command.setExecuteAction(action); + } + } else if (level_ == ActionsActionLevel) { + } + + if (formParser_ != null) { + formParser_.handleStartElement(element, ns, attributes); + } + } + + public void handleEndElement(String element, String ns) { + if (formParser_ != null) { + formParser_.handleEndElement(element, ns); + } + + Command command = getPayloadInternal(); + + if (level_ == FormOrNoteOrActionsLevel) { + if (formParser_ != null) { + Form form = (Form) (formParser_.getPayload()); + assert (form != null); + command.setForm(form); + formParser_ = null; + } else if (inNote_) { + inNote_ = false; + command.addNote(new Note(currentText_, noteType_)); + } else if (inActions_) { + inActions_ = false; + } + } else if ((level_ == ActionsActionLevel) && inActions_) { + Action action = parseAction(element); + command.addAvailableAction(action); + } + + --level_; + } + + public void handleCharacterData(String data) { + if (formParser_ != null) { + formParser_.handleCharacterData(data); + } else { + currentText_ += data; + } + } + + private static Action parseAction(String action) { + return Action.getAction(action); + } + + @Override + public String toString() { + return CommandParser.class.getSimpleName() + "\nlevel: " + level_ + + "\ncurrent text: " + currentText_ + "\nnote: " + inNote_ + + "\nactions: " + inActions_ + "\nnote type: " + noteType_; + } +} |