/** * Performs a case sensitive comparison of the given substrings. * * @param a * the first sequence * @param b * the second sequence * @return true, if both character sequences are exactly equal, including the case */ public static boolean checkEqualCheckCase(CharSequence a, CharSequence b) { return checkEqualCheckCase(a, 0, a.length(), b, 0, b.length()); }
/** * Performs a case insensitive comparison of the given substrings. * * @param a * the first sequence * @param b * the second sequence * @return true, if both character sequences are equal except for the case */ public static boolean checkEqualIgnoreCase(CharSequence a, CharSequence b) { return checkEqualIgnoreCase(a, 0, a.length(), b, 0, b.length()); } }
/** * Checks if the substring of the source text starting at the carets position starts with the * give String. Case-Insensitive Comparison is used. * * @param strToMatch * the string to match * @return true if matched */ public boolean startsWithIgnoreCase(String strToMatch) { int len = strToMatch.length(); if (len > wayToEnd()) { return false; } return CharSequenceUtils.checkEqualIgnoreCase(src, offset, len, strToMatch, 0, len); }
/** * Checks if the substring of the source text starting at the carets position starts with the * give String. Case-Sensitive Comparison is used. * * @param strToMatch * the string to match * @return true if matched */ public boolean startsWithCheckCase(String strToMatch) { int len = strToMatch.length(); if (len > wayToEnd()) { return false; } return CharSequenceUtils.checkEqualCheckCase(src, offset, len, strToMatch, 0, len); }
@Test public void testNonMatching() { String a = "abcd"; String b = "efgh"; assertThat(CharSequenceUtils.checkEqualIgnoreCase(a, b), equalTo(false)); assertThat(CharSequenceUtils.checkEqualIgnoreCase(a, 0, 4, b, 0, 4), equalTo(false)); }
@Test public void testNonEqualCase() { String a = "abcd"; String b = "aBcd"; assertThat(CharSequenceUtils.checkEqualCheckCase(a, b), equalTo(false)); assertThat(CharSequenceUtils.checkEqualCheckCase(a, 0, 4, b, 0, 4), equalTo(false)); }
@Test public void testNonEqualCaseNoOffset() { String a = "abcd"; String b = "aBcd"; assertThat(CharSequenceUtils.checkEqualIgnoreCase(a, b), equalTo(true)); assertThat(CharSequenceUtils.checkEqualIgnoreCase(a, 0, 4, b, 0, 4), equalTo(true)); }
@Test public void testEqualCaseNoOffset() { String a = "abcd"; String b = "abcd"; assertThat(CharSequenceUtils.checkEqualCheckCase(a, b), equalTo(true)); assertThat(CharSequenceUtils.checkEqualCheckCase(a, 0, 4, b, 0, 4), equalTo(true)); }
@Test public void testEqualCaseWithOffset() { String a = "12abcd34"; String b = "34abcd56"; assertThat(CharSequenceUtils.checkEqualIgnoreCase(a, 2, 4, b, 2, 4), equalTo(true)); }
@Test public void testInvalidLengths() { String a1 = "abcd"; String b1 = "abcde"; String a2 = "abcde"; String b2 = "abcd"; assertThat(CharSequenceUtils.checkEqualCheckCase(a1, 0, 5, b1, 0, 5), equalTo(false)); assertThat(CharSequenceUtils.checkEqualCheckCase(a2, 0, 5, b2, 0, 5), equalTo(false)); }
@Test public void testEqualCaseNoOffset() { String a = "abcd"; String b = "abcd"; assertThat(CharSequenceUtils.checkEqualIgnoreCase(a, b), equalTo(true)); assertThat(CharSequenceUtils.checkEqualIgnoreCase(a, 0, 4, b, 0, 4), equalTo(true)); }
@Test public void testNonMatching() { String a = "abcd"; String b = "efgh"; assertThat(CharSequenceUtils.checkEqualCheckCase(a, b), equalTo(false)); assertThat(CharSequenceUtils.checkEqualCheckCase(a, 0, 4, b, 0, 4), equalTo(false)); }
@Test public void testInvalidLengths() { String a1 = "abcd"; String b1 = "abcde"; String a2 = "abcde"; String b2 = "abcd"; assertThat(CharSequenceUtils.checkEqualIgnoreCase(a1, 0, 5, b1, 0, 5), equalTo(false)); assertThat(CharSequenceUtils.checkEqualIgnoreCase(a2, 0, 5, b2, 0, 5), equalTo(false)); }
@Test public void testEqualCaseWithOffset() { String a = "12abcd34"; String b = "34abcd56"; assertThat(CharSequenceUtils.checkEqualCheckCase(a, 2, 4, b, 2, 4), equalTo(true)); }
@Test public void testNonEqualCaseWithOffset() { String a = "12abcd34"; String b = "34abCD56"; assertThat(CharSequenceUtils.checkEqualIgnoreCase(a, 2, 4, b, 2, 4), equalTo(true)); }
/** * Scans for an opening html tag, skipping preamble tags like or !DOCTYPE. */ private void scanHtmlPreamble() { // Preamble checking based on the information on this page // http://wiki.selfhtml.org/wiki/HTML/Dokumentstruktur_und_Aufbau#HTML5 // we also allow html without preamble, directly starting with the <html> tag if (CharSequenceUtils.checkEqualIgnoreCase(tokenParser.getTagType(), "!DOCTYPE")) { // Doctypes are formated as opening tags if (tokenParser.getParsedTokenType() != Token.START_TAG) { abortInjectionPointSearch(); return; } // we accept any doctype starting with "html" if (!CharSequenceUtils.checkEqualIgnoreCase(tokenParser.getTagArguments(), 0, 4, "html", 0, 4)) { abortInjectionPointSearch(); return; } // DOCTYPE okay, proceed with the next tag scanning for the html tag status = Status.SCAN_FOR_HTML_TAG; } else { // no preamble tag found, we assume the html is starting immediately status = Status.SCAN_FOR_HTML_TAG; processToken(); } }
/** * Same as {@link #scanHtmlPreamble()}, however does not allow to ommit the preamble. */ private void scanRequiredXHtmlPreamble() { // Preamble checking based on the information on this page // http://wiki.selfhtml.org/wiki/HTML/Dokumentstruktur_und_Aufbau#HTML5 // we also allow html without preamble, directly starting with the <html> tag if (CharSequenceUtils.checkEqualIgnoreCase(tokenParser.getTagType(), "!DOCTYPE")) { // Doctypes are formated as opening tags if (tokenParser.getParsedTokenType() != Token.START_TAG) { abortInjectionPointSearch(); return; } // we accept any doctype starting with "html" if (!CharSequenceUtils.checkEqualIgnoreCase(tokenParser.getTagArguments(), 0, 4, "html", 0, 4)) { abortInjectionPointSearch(); return; } // DOCTYPE okay, proceed with the next tag scanning for the html tag status = Status.SCAN_FOR_HTML_TAG; } else { // no preamble tag found, it however is required for xhtml abortInjectionPointSearch(); return; } }
/** * Tries to find an opening html tag. */ private void scanForHtmlTag() { if (CharSequenceUtils.checkEqualIgnoreCase(tokenParser.getTagType(), "html")) { if (tokenParser.getParsedTokenType() != Token.START_TAG) { abortInjectionPointSearch(); return; } status = Status.SCAN_FOR_HEAD_TAG; } else { // current token is not the html tag, we assume the document starts immediately with the // head status = Status.SCAN_FOR_HEAD_TAG; processToken(); } }
/** * Tries to find an opening body tag. Omitting both the head and the body tag is currently not * supported. */ private void scanForBodyTag() { if (CharSequenceUtils.checkEqualIgnoreCase(tokenParser.getTagType(), "body")) { if (tokenParser.getParsedTokenType() != Token.START_TAG) { abortInjectionPointSearch(); return; } // Perform injection after start of the head tag status = Status.INJECTION_POINT_FOUND; } else { // current token is not the head tag, we assume it must be the body tag (head is empty) abortInjectionPointSearch(); } }
/** * Tries to find an opening head tag. Omitting both the head and the body tag is currently not * supported. */ private void scanForHeadTag() { if (CharSequenceUtils.checkEqualIgnoreCase(tokenParser.getTagType(), "head")) { if (tokenParser.getParsedTokenType() != Token.START_TAG) { abortInjectionPointSearch(); return; } // Perform injection after start of the head tag status = Status.INJECTION_POINT_FOUND; } else { // current token is not the head tag, we assume it must be the body tag (head is empty) status = Status.SCAN_FOR_BODY_TAG; processToken(); } }