Example usage for org.apache.commons.lang3 StringUtils endsWithAny

List of usage examples for org.apache.commons.lang3 StringUtils endsWithAny

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils endsWithAny.

Prototype

public static boolean endsWithAny(final CharSequence string, final CharSequence... searchStrings) 

Source Link

Document

Check if a CharSequence ends with any of an array of specified strings.

 StringUtils.endsWithAny(null, null)      = false StringUtils.endsWithAny(null, new String[] {"abc"})  = false StringUtils.endsWithAny("abcxyz", null)     = false StringUtils.endsWithAny("abcxyz", new String[] {""}) = true StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true 

Usage

From source file:org.maodian.flyingcat.netty.handler.XMLFragmentDecoder.java

@Override
protected Object decode(ChannelHandlerContext ctx, String msg) throws Exception {
    if (StringUtils.endsWithAny(msg, INVALID_ELEMENT_TAILS)) {
        throw new XmppException(StreamError.RESTRICTED_XML).set("xml", msg);
    }/*from  w ww. java 2s  .  c o  m*/

    if (StringUtils.endsWith(msg, PROCESS_INSTRUCTION_TAIL) && !StringUtils.startsWith(msg, "<?xml ")) {
        throw new XmppException(StreamError.RESTRICTED_XML).set("xml", msg);
    }

    if (StringUtils.startsWithAny(msg, INVALID_ELEMENT_HEADS)) {
        throw new XmppException(StreamError.RESTRICTED_XML).set("xml", msg);
    }

    // always accept xml declaration to improve compability with some client
    if (StringUtils.startsWith(msg, "<?xml ")) {
        return msg;
        /*if (acceptXMLDeclaration) {
          acceptXMLDeclaration = false;
          return msg;
        } else {
          throw new XmppException("XML declaration has been already received before", StreamError.NOT_WELL_FORMED).set("xml", msg);
        }*/
    }

    // deal with stream tag
    if (StringUtils.contains(msg, ":stream")
            && (StringUtils.contains(msg, "<") || StringUtils.contains(msg, "</"))) {
        if (depth != 0) {
            throw new XmppException("Stream Open/Close Tag can only be root element", StreamError.INVALID_XML);
        }
        return msg;
    }

    // deal with empty element at first level
    if (depth == 0 && StringUtils.endsWith(msg, "/>")) {
        return msg;
    }

    if (xml == null) {
        xml = new StringBuilder(msg);
    } else {
        xml.append(msg);
    }

    // deal with nested empty element
    if (StringUtils.endsWith(msg, "/>")) {
        return null;
    }

    if (StringUtils.contains(msg, "</")) {
        depth--;
    } else if (StringUtils.contains(msg, "<")) {
        depth++;
    }

    if (depth == 0) {
        String fragment = xml.toString();
        xml = null;
        return fragment;
    }
    return null;
}