handleResponse method

void handleResponse(
  1. String response
)

Implementation

void handleResponse(String response) {
  String fullResponse;
  if (_unparsedXmlResponse.isNotEmpty) {
    if (response.length > 12) {
      fullResponse = '$_unparsedXmlResponse${response.substring(12)}'; //
    } else {
      fullResponse = _unparsedXmlResponse;
    }
    Log.v(TAG, 'full response = $fullResponse');
    _unparsedXmlResponse = '';
  } else {
    fullResponse = response;
  }

  if (fullResponse.isNotEmpty) {
    xml.XmlNode? xmlResponse;
    try {
      xmlResponse = xml.XmlDocument.parse(
              fullResponse.replaceAll(RegExp(r'<\?(xml.+?)\>'), ''))
          .firstChild;
    } catch (e) {
      _unparsedXmlResponse += fullResponse.substring(
          0, fullResponse.length - 13); //remove  xmpp_stone end tag
      xmlResponse = xml.XmlElement(xml.XmlName('error'));
    }

    //TODO: Improve parser for children only
    xmlResponse!.descendants
        .whereType<xml.XmlElement>()
        .where((element) => startMatcher(element))
        .forEach((element) => processInitialStream(element));

    xmlResponse.childElements
        .where((element) => stanzaMatcher(element))
        .map((xmlElement) => StanzaParser.parseStanza(xmlElement))
        .forEach((stanza) => _inStanzaStreamController.add(stanza));

    xmlResponse.descendants
        .whereType<xml.XmlElement>()
        .where((element) => featureMatcher(element))
        .forEach((feature) =>
            connectionNegotatiorManager.negotiateFeatureList(feature));

    //TODO: Probably will introduce bugs!!!
    xmlResponse.childElements
        .where((element) => nonzaMatcher(element))
        .map((xmlElement) => Nonza.parse(xmlElement))
        .forEach((nonza) => _inNonzaStreamController.add(nonza));
  }
}