Could that be re-written more functionally as:

    def tolerantXml(maxLength: Int): BodyParser[NodeSeq] =
      tolerantBodyParser[NodeSeq]("xml", maxLength, "Invalid XML") { (request, bytes) =>
        // Encoding notes: RFC 3023 is the RFC for XML content types.  Comments below reflect what it says.

        // An externally declared charset takes precedence
        val charset = request.charset.getOrElse(
          // If omitted, maybe select a default charset, based on the media type.
          request.mediaType.collect {
            // According to RFC 3023, the default encoding for text/xml is us-ascii. This contradicts RFC 2616, which
            // states that the default for text/* is ISO-8859-1.  An RFC 3023 conforming client will send US-ASCII,
            // in that case it is safe for us to use US-ASCII or ISO-8859-1.  But a client that knows nothing about
            // XML, and therefore nothing about RFC 3023, but rather conforms to RFC 2616, will send ISO-8859-1.
            // Since decoding as ISO-8859-1 works for both clients that conform to RFC 3023, and clients that conform
            // to RFC 2616, we use that.
            case mt if mt.mediaType == "text" => "iso-8859-1"
            // Otherwise, there should be no default, it will be detected by the XML parser.
           }
        )
        val inputSource = new InputSource(new InputStreamReader(new ByteArrayInputStream(bytes), charset))
        XML.load(inputSource)