casualjim
11/3/2010 - 9:30 PM

gistfile1.scala

object Stream {
  sealed trait XMPPStreamError

  object BadFormatError {
    def apply( text: Option[String] = None, applicationCondition: Seq[Node]) = {
      <stream:error>
        <bad-format xmlns={XMPP_STREAMS_NS} />
        {text.map(t => <text xmlns={XMPP_STREAMS_NS}>{t}</text>) getOrElse Nil}
        {applicationCondition getOrElse Nil}
      </stream:error>
    }

    def unapply(stanza: NodeSeq) = stanza match {
      case err @ <stream:error>{ ch @ _*}</stream:error> if !(err \ "bad-format").isEmpty => {
        val txt = (err \ "text").text
        val text = if(txt.isNotBlank) Some(txt) else None
        val appCond = ch.filterNot(n => ("bad-format" :: "text" :: Nil).contains(n))
        Some((text, appCond))
      }
      case _ => None
    }
  }

}