diff options
Diffstat (limited to 'src/com/isode/stroke/parser')
8 files changed, 392 insertions, 1 deletions
diff --git a/src/com/isode/stroke/parser/StanzaParser.java b/src/com/isode/stroke/parser/StanzaParser.java index cb7647d..6b0ccdf 100644 --- a/src/com/isode/stroke/parser/StanzaParser.java +++ b/src/com/isode/stroke/parser/StanzaParser.java @@ -25,7 +25,7 @@ public abstract class StanzaParser implements ElementParser { void handleStanzaAttributes(AttributeMap map) { } - Stanza getStanza() { + public Stanza getStanza() { return (Stanza) getElement(); } diff --git a/src/com/isode/stroke/parser/payloadparsers/DelayParser.java b/src/com/isode/stroke/parser/payloadparsers/DelayParser.java new file mode 100644 index 0000000..44d21ce --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/DelayParser.java @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import java.util.Date; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Delay; +import com.isode.stroke.jid.JID; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +public class DelayParser extends GenericPayloadParser<Delay> { + + public DelayParser() { + super(new Delay()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 0) { + Date stamp = DateTime.stringToDate(attributes.getAttribute("stamp")); + getPayloadInternal().setStamp(stamp); + if (!attributes.getAttribute("from").isEmpty()) { + String from = attributes.getAttribute("from"); + getPayloadInternal().setFrom(new JID(from)); + } + } + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + } + + public void handleCharacterData(String data) { + } + + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/ForwardedParser.java b/src/com/isode/stroke/parser/payloadparsers/ForwardedParser.java new file mode 100644 index 0000000..b7a023e --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/ForwardedParser.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2014 Kevin Smith and Remko Tronçon + * All rights reserved. + */ + +/* + * Copyright (c) 2014, Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.Delay; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.IQParser; +import com.isode.stroke.parser.MessageParser; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.PresenceParser; +import com.isode.stroke.parser.StanzaParser; + +public class ForwardedParser extends GenericPayloadParser<Forwarded> { + public ForwardedParser(PayloadParserFactoryCollection factories) { + super(new Forwarded()); + factories_ = factories; + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 1) { + if (element == "iq") { /* begin parsing a nested stanza? */ + childParser_ = new IQParser(factories_); + } else if (element == "message") { + childParser_ = new MessageParser(factories_); + } else if (element == "presence") { + childParser_ = new PresenceParser(factories_); + } else if (element == "delay" && ns == "urn:xmpp:delay") { + delayParser_ = new DelayParser(); + } + } + if (childParser_ != null) { /* parsing a nested stanza? */ + childParser_.handleStartElement(element, ns, attributes); + } + if (delayParser_ != null) { /* parsing a nested delay payload? */ + delayParser_.handleStartElement(element, ns, attributes); + } + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + if (childParser_ != null && level_ >= 1) { + childParser_.handleEndElement(element, ns); + } + if (childParser_ != null && level_ == 1) { /* done parsing nested stanza? */ + getPayloadInternal().setStanza(childParser_.getStanza()); + childParser_ = null; + } + if (delayParser_ != null && level_ >= 1) { + delayParser_.handleEndElement(element, ns); + } + if (delayParser_ != null && level_ == 1) { /* done parsing nested delay payload? */ + getPayloadInternal().setDelay((Delay) delayParser_.getPayload()); + delayParser_ = null; + } + } + + public void handleCharacterData(String data) { + if (childParser_ != null) { + childParser_.handleCharacterData(data); + } + if (delayParser_ != null) { + delayParser_.handleCharacterData(data); + } + } + + private PayloadParserFactoryCollection factories_; + private StanzaParser childParser_; + private DelayParser delayParser_; + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index cf41338..41f44f5 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -64,6 +64,13 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory2<PubSubEventParser>("event", "http://jabber.org/protocol/pubsub#event", this, PubSubEventParser.class)); addFactory(new PubSubErrorParserFactory()); + addFactory(new GenericPayloadParserFactory<DelayParser>("delay", "urn:xmpp:delay", DelayParser.class)); + addFactory(new GenericPayloadParserFactory<ResultSetParser>("set", "http://jabber.org/protocol/rsm", ResultSetParser.class)); + addFactory(new GenericPayloadParserFactory2<ForwardedParser>("forwarded", "urn:xmpp:forward:0", this, ForwardedParser.class)); + addFactory(new GenericPayloadParserFactory2<MAMResultParser>("result", "urn:xmpp:mam:0", this, MAMResultParser.class)); + addFactory(new GenericPayloadParserFactory<MAMQueryParser>("query", "urn:xmpp:mam:0", MAMQueryParser.class)); + addFactory(new GenericPayloadParserFactory<MAMArchivedParser>("archived", "urn:xmpp:mam:0", MAMArchivedParser.class)); + //addFactory(new NicknameParserFactory()); PayloadParserFactory defaultFactory = new RawXMLPayloadParserFactory(); diff --git a/src/com/isode/stroke/parser/payloadparsers/MAMArchivedParser.java b/src/com/isode/stroke/parser/payloadparsers/MAMArchivedParser.java new file mode 100644 index 0000000..5547a87 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/MAMArchivedParser.java @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.MAMArchived; +import com.isode.stroke.jid.JID; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +public class MAMArchivedParser extends GenericPayloadParser<MAMArchived> { + public MAMArchivedParser() { + super(new MAMArchived()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 0) { + String attributeValue = attributes.getAttributeValue("by"); + if (attributeValue != null) { + getPayloadInternal().setBy(JID.fromString(attributeValue)); + } + attributeValue = attributes.getAttributeValue("id"); + if (attributeValue != null) { + getPayloadInternal().setID(attributeValue); + } + } + + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + } + + public void handleCharacterData(String data) { + } + + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/MAMQueryParser.java b/src/com/isode/stroke/parser/payloadparsers/MAMQueryParser.java new file mode 100644 index 0000000..fcfd5e4 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/MAMQueryParser.java @@ -0,0 +1,82 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.MAMQuery; +import com.isode.stroke.elements.ResultSet; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +public class MAMQueryParser extends GenericPayloadParser<MAMQuery> { + + public MAMQueryParser() { + super(new MAMQuery()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 0) { + String attributeValue = attributes.getAttributeValue("queryid"); + if (attributeValue != null) { + getPayloadInternal().setQueryID(attributeValue); + } + } else if (level_ == 1) { + if (element == "x" && ns == "jabber:x:data") { + formParser_ = new FormParser(); + } else if (element == "set" && ns == "http://jabber.org/protocol/rsm") { + resultSetParser_ = new ResultSetParser(); + } + } + + if (formParser_ != null) { /* parsing a nested Form */ + formParser_.handleStartElement(element, ns, attributes); + } + + if (resultSetParser_ != null) { /* parsing a nested ResultSet */ + resultSetParser_.handleStartElement(element, ns, attributes); + } + + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + + if (formParser_ != null && level_>= 1) { + formParser_.handleEndElement(element, ns); + } + if (formParser_ != null && level_==1) { /* done parsing nested Form? */ + getPayloadInternal().setForm((Form)formParser_.getPayload()); + formParser_ = null; + } + + if (resultSetParser_ != null && level_>= 1) { + resultSetParser_.handleEndElement(element, ns); + } + if (resultSetParser_ != null && level_==1) { /* done parsing nested ResultSet? */ + getPayloadInternal().setResultSet((ResultSet)resultSetParser_.getPayload()); + resultSetParser_ = null; + } + } + + public void handleCharacterData(String data) { + if (formParser_ != null) { + formParser_.handleCharacterData(data); + } + if (resultSetParser_ != null) { + resultSetParser_.handleCharacterData(data); + } + } + + private FormParser formParser_; + private ResultSetParser resultSetParser_; + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/MAMResultParser.java b/src/com/isode/stroke/parser/payloadparsers/MAMResultParser.java new file mode 100644 index 0000000..cb3d7fd --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/MAMResultParser.java @@ -0,0 +1,68 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.MAMResult; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.PayloadParserFactoryCollection; + +public class MAMResultParser extends GenericPayloadParser<MAMResult> { + public MAMResultParser(PayloadParserFactoryCollection factories) { + super(new MAMResult()); + factories_ = factories; + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (level_ == 0) { + String attributeValue = attributes.getAttributeValue("id"); + if (attributeValue != null) { + getPayloadInternal().setID(attributeValue); + } + attributeValue = attributes.getAttributeValue("queryid"); + if (attributeValue != null) { + getPayloadInternal().setQueryID(attributeValue); + } + } else if (level_ == 1) { + if (element == "forwarded" && ns == "urn:xmpp:forward:0") { + payloadParser_ = new ForwardedParser(factories_); + } + } + + if (payloadParser_ != null) { /* parsing a nested payload? */ + payloadParser_.handleStartElement(element, ns, attributes); + } + + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + if (payloadParser_ != null && level_ >= 1) { + payloadParser_.handleEndElement(element, ns); + } + if (payloadParser_ != null && level_ == 1) { /* done parsing nested stanza? */ + getPayloadInternal().setPayload((Forwarded)payloadParser_.getPayload()); + payloadParser_ = null; + } + } + + public void handleCharacterData(String data) { + if (payloadParser_ != null) { + payloadParser_.handleCharacterData(data); + } + } + + private ForwardedParser payloadParser_; + private PayloadParserFactoryCollection factories_; + private int level_; +} diff --git a/src/com/isode/stroke/parser/payloadparsers/ResultSetParser.java b/src/com/isode/stroke/parser/payloadparsers/ResultSetParser.java new file mode 100644 index 0000000..26343a0 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/ResultSetParser.java @@ -0,0 +1,61 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.ResultSet; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +public class ResultSetParser extends GenericPayloadParser<ResultSet> { + + public ResultSetParser() { + super(new ResultSet()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + currentText_ = ""; + if (level_ == 1) { + if (element == "first" && ns == "http://jabber.org/protocol/rsm") { + String attributeValue = attributes.getAttributeValue("index"); + if (attributeValue != null) { + getPayloadInternal().setFirstIDIndex(Long.parseLong(attributeValue)); + } + } + } + ++level_; + } + + public void handleEndElement(String element, String ns) { + --level_; + if (level_ == 1) { + if (element == "max") { + getPayloadInternal().setMaxItems(Long.parseLong(currentText_)); + } else if (element == "count") { + getPayloadInternal().setCount(Long.parseLong(currentText_)); + } else if (element == "first") { + getPayloadInternal().setFirstID(currentText_); + } else if (element == "last") { + getPayloadInternal().setLastID(currentText_); + } else if (element == "after") { + getPayloadInternal().setAfter(currentText_); + } else if (element == "before") { + getPayloadInternal().setBefore(currentText_); + } + } + } + + public void handleCharacterData(String data) { + currentText_ += data; + } + + private String currentText_; + private int level_; +} |