LuceneUtils { public static String extractLanguageOrNullFromStatement(String statement) { int langIndex = statement.indexOf(Constants.JCR_LANGUAGE); String languageCandidate = null; while (langIndex >= 0) { langIndex = langIndex + Constants.JCR_LANGUAGE.length(); int nextLangIndex = statement.indexOf(Constants.JCR_LANGUAGE, langIndex); String constraint = nextLangIndex < 0 ? statement.substring(langIndex) : statement.substring(langIndex, nextLangIndex); int equalsIndex = constraint.indexOf('='); if (equalsIndex >= 0) { char quoteChar = '\''; boolean hasQuote = false; int begLang = constraint.indexOf(quoteChar); if (begLang < 0) { begLang = constraint.indexOf('\"'); if (begLang >= 0) { quoteChar = '\"'; hasQuote = true; } } else { hasQuote = true; } if (hasQuote) { begLang = begLang + 1; int endConstraint = getEndOfConstraintIndex(constraint); int endLang = constraint.indexOf(quoteChar, begLang); if (endLang > 0 && endLang <= endConstraint) { if (languageCandidate != null) { return null; } languageCandidate = constraint.substring(begLang, endLang).trim(); } } } langIndex = nextLangIndex; } return languageCandidate; }  static String getFullTextFieldName(String siteName, String language); static String extractLanguageOrNullFrom(String fieldName); static String extractLanguageOrNullFromStatement(String statement); static final String DASH; }
@Test public void testExtractLanguageFromStatement() { String statement = "foo bar jcr:language baz blah \t \n jcr:language = ' en \t' asdf asdn "; Assert.assertEquals("en", LuceneUtils.extractLanguageOrNullFromStatement(statement)); statement = "foo bar jcr:language baz blah \t \n jcr:language = \" en \t\" asdf asdn "; Assert.assertEquals("en", LuceneUtils.extractLanguageOrNullFromStatement(statement)); statement = "jcr:language='en'"; Assert.assertEquals("en", LuceneUtils.extractLanguageOrNullFromStatement(statement)); statement = "jcr:language=\"en\""; Assert.assertEquals("en", LuceneUtils.extractLanguageOrNullFromStatement(statement)); statement = " foo bar jcr:language is null baz asdf"; Assert.assertNull(LuceneUtils.extractLanguageOrNullFromStatement(statement)); statement = "foo bar jcr:language is null and jcr:language = 'en' asdaf "; Assert.assertEquals("en", LuceneUtils.extractLanguageOrNullFromStatement(statement)); statement = " foo bar jcr:language < > 'en' baz asdf"; Assert.assertNull(LuceneUtils.extractLanguageOrNullFromStatement(statement)); statement = " foo bar jcr:language = 'fr' or jcr:language='en' baz asdf"; Assert.assertNull(LuceneUtils.extractLanguageOrNullFromStatement(statement)); statement = " faijdsfsd asdfasd ([jcr:language] = \"en\" or [jcr:language] is null) and [someOtherProperty] = \"test\" foo bar "; Assert.assertEquals("en", LuceneUtils.extractLanguageOrNullFromStatement(statement)); }