summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/isode/stroke/parser/XMPPParser.java')
-rw-r--r--src/com/isode/stroke/parser/XMPPParser.java119
1 files changed, 59 insertions, 60 deletions
diff --git a/src/com/isode/stroke/parser/XMPPParser.java b/src/com/isode/stroke/parser/XMPPParser.java
index 85d0e1f..8fd7763 100644
--- a/src/com/isode/stroke/parser/XMPPParser.java
+++ b/src/com/isode/stroke/parser/XMPPParser.java
@@ -9,95 +9,90 @@ import java.util.logging.Logger;
public class XMPPParser implements XMLParserClient {
- private final XMLParser xmlParser_;
- private final XMPPParserClient client_;
- private final PayloadParserFactoryCollection payloadParserFactories_;
- private int currentDepth_ = 0;
+ private XMLParser xmlParser_;
+ private XMPPParserClient client_;
+ private PayloadParserFactoryCollection payloadParserFactories_;
+ private int level_ = 0;
private ElementParser currentElementParser_ = null;
private boolean parseErrorOccurred_ = false;
private Logger logger_ = Logger.getLogger(this.getClass().getName());
-
+ private final int TopLevel = 0;
+ private final int StreamLevel = 1;
+ private final int ElementLevel = 2;
+
public XMPPParser(XMPPParserClient parserClient,
PayloadParserFactoryCollection payloadParserFactories) {
+ xmlParser_ = null;
client_ = parserClient;
payloadParserFactories_ = payloadParserFactories;
+ level_ = 0;
+ currentElementParser_ = null;
+ parseErrorOccurred_ = false;
xmlParser_ = PlatformXMLParserFactory.createXMLParser(this);
}
public boolean parse(String data) {
- parseErrorOccurred_ = false;
- boolean xmlParseResult = false;
- try {
- xmlParseResult = xmlParser_.parse(data);
- } catch (Exception e) {
- parseErrorOccurred_ = true;
- logger_.log(java.util.logging.Level.WARNING, "Data " + data + " caused:\n" + e.getMessage(), e);
- }
- if (parseErrorOccurred_ || !xmlParseResult) {
- logger_.warning(String.format("When parsing, %b and %b",
- parseErrorOccurred_, xmlParseResult));
- if (data != null) {
- logger_.warning("xml that caused failure: " + data);
- }
- }
+ boolean xmlParseResult = xmlParser_.parse(data);
return xmlParseResult && !parseErrorOccurred_;
}
public void handleStartElement(String element, String ns,
AttributeMap attributes) {
- if (!inStream()) {
- if (element.equals("stream")
- && ns.equals("http://etherx.jabber.org/streams")) {
- ProtocolHeader header = new ProtocolHeader();
- header.setFrom(attributes.getAttribute("from"));
- header.setTo(attributes.getAttribute("to"));
- header.setID(attributes.getAttribute("id"));
- header.setVersion(attributes.getAttribute("version"));
- client_.handleStreamStart(header);
- } else {
- parseErrorOccurred_ = true;
+ if (!parseErrorOccurred_) {
+ if (level_ == TopLevel) {
+ if (element.equals("stream") && ns.equals("http://etherx.jabber.org/streams")) {
+ ProtocolHeader header = new ProtocolHeader();
+ header.setFrom(attributes.getAttribute("from"));
+ header.setTo(attributes.getAttribute("to"));
+ header.setID(attributes.getAttribute("id"));
+ header.setVersion(attributes.getAttribute("version"));
+ client_.handleStreamStart(header);
+ }
+ else {
+ parseErrorOccurred_ = true;
+ }
}
- } else {
- if (!inElement()) {
- assert currentElementParser_ == null;
- currentElementParser_ = createElementParser(element, ns);
+ else {
+ if (level_ == StreamLevel) {
+ assert(currentElementParser_ == null);
+ currentElementParser_ = createElementParser(element, ns);
+ }
+ currentElementParser_.handleStartElement(element, ns, attributes);
}
- currentElementParser_.handleStartElement(element, ns, attributes);
}
- ++currentDepth_;
+ ++level_;
}
public void handleEndElement(String element, String ns) {
- assert (inStream());
- if (inElement()) {
- assert currentElementParser_ != null;
- currentElementParser_.handleEndElement(element, ns);
- --currentDepth_;
- if (!inElement()) {
- client_.handleElement(currentElementParser_.getElement());
- currentElementParser_ = null;
+ assert(level_ > TopLevel);
+ --level_;
+ if (!parseErrorOccurred_) {
+ if (level_ == TopLevel) {
+ assert(element.equals("stream"));
+ client_.handleStreamEnd();
+ }
+ else {
+ assert(currentElementParser_ != null);
+ currentElementParser_.handleEndElement(element, ns);
+ if (level_ == StreamLevel) {
+ client_.handleElement(currentElementParser_.getElement());
+ currentElementParser_ = null;
+ }
}
- } else {
- assert (element.equals("stream"));
- --currentDepth_;
- client_.handleStreamEnd();
}
}
public void handleCharacterData(String data) {
- if (currentElementParser_ != null) {
- currentElementParser_.handleCharacterData(data);
+ if (!parseErrorOccurred_) {
+ if (currentElementParser_ != null) {
+ currentElementParser_.handleCharacterData(data);
+ }
+ //else {
+ // std::cerr << "XMPPParser: Ignoring stray character data: " << data << std::endl;
+ //}
}
}
-
- private boolean inStream() {
- return currentDepth_ > 0;
- }
-
- private boolean inElement() {
- return currentDepth_ > 1;
- }
-
+
private ElementParser createElementParser(String element, String xmlns) {
if (element.equals("presence")) {
return new PresenceParser(payloadParserFactories_);
@@ -108,6 +103,8 @@ public class XMPPParser implements XMLParserClient {
} else if (element.equals("features")
&& xmlns.equals("http://etherx.jabber.org/streams")) {
return new StreamFeaturesParser();
+ } else if (element.equals("error") && xmlns.equals("http://etherx.jabber.org/streams")) {
+ return new StreamErrorParser();
} else if (element.equals("auth")) {
return new AuthRequestParser();
} else if (element.equals("success")) {
@@ -149,6 +146,8 @@ public class XMPPParser implements XMLParserClient {
return new StanzaAckParser();
} else if (element.equals("r") && xmlns.equals("urn:xmpp:sm:2")) {
return new StanzaAckRequestParser();
+ } else if (element.equals("handshake")) {
+ return new ComponentHandshakeParser();
}
return new UnknownElementParser();