From: <die...@us...> - 2010-03-19 09:15:55
|
Revision: 2157 http://openutils.svn.sourceforge.net/openutils/?rev=2157&view=rev Author: diego_schivo Date: 2010-03-19 09:15:47 +0000 (Fri, 19 Mar 2010) Log Message: ----------- CRIT-9 escapeIllegalXpathSearchChars reimplemented to fix unit-tests Modified Paths: -------------- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/utils/XPathTextUtils.java trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/JcrContainsCriteriaSearchTest.java Modified: trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/utils/XPathTextUtils.java =================================================================== --- trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/utils/XPathTextUtils.java 2010-03-19 08:53:51 UTC (rev 2156) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/utils/XPathTextUtils.java 2010-03-19 09:15:47 UTC (rev 2157) @@ -90,6 +90,22 @@ return str.replaceAll("'", "\""); } + private static String[] JCR_SEARCH_EXP_UNESCAPED = new String[] { + "'", + "\"", + "-", + "\\", + "?" + }; + + private static String[] JCR_SEARCH_EXP_ESCAPED = new String[] { + "\\'", + "\\\"", + "\\-", + "\\\\", + "\\?" + }; + /** * Convert a string to a JCR search expression literal, suitable for use in jcr:contains() (inside XPath queries). * The characters - and " have special meaning, and may be escaped with a backslash to obtain their literal value. @@ -107,9 +123,14 @@ // Escape ' and \ everywhere, preceding them with \ except when \ // appears // in one of the combinations \" or \- - return escapeIllegalXpathSearchChars(stringToXPathLiteral(str - .replaceAll("\\\\(?![-\"])", "\\\\\\\\") - .replaceAll("'", "\\\\'"))); + + // CRIT-9: if str=="\"milano\"" then only the trailing " is escaped, resulting in a JCRQueryException + // return escapeIllegalXpathSearchChars(stringToXPathLiteral(str + // .replaceAll("\\\\(?![-\"])", "\\\\\\\\") + // .replaceAll("'", "\\\\'"))); + + // if you change this implementation, please add unit tests + return StringUtils.replaceEach(str, JCR_SEARCH_EXP_UNESCAPED, JCR_SEARCH_EXP_ESCAPED); } /** Modified: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/JcrContainsCriteriaSearchTest.java =================================================================== --- trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/JcrContainsCriteriaSearchTest.java 2010-03-19 08:53:51 UTC (rev 2156) +++ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/advanced/JcrContainsCriteriaSearchTest.java 2010-03-19 09:15:47 UTC (rev 2157) @@ -133,8 +133,8 @@ { String textEnteredByUser = "te?st"; Criteria criteria = criteria(textEnteredByUser, true); -// Assert.assertEquals("//*[((@jcr:primaryType='mgnl:content')and(jcr:contains(@title,'te\\?st')))]", StringUtils -// .remove(criteria.toXpathExpression(), ' ')); + Assert.assertEquals("//*[((@jcr:primaryType='mgnl:content')and(jcr:contains(@title,'te\\?st')))]", StringUtils + .remove(criteria.toXpathExpression(), ' ')); AdvancedResult advResult = null; try { @@ -155,9 +155,11 @@ public void testMilano() throws Exception { String textEnteredByUser = "\"Milano\""; - Criteria criteria = criteria(encode(textEnteredByUser), true); -// Assert.assertEquals("//*[((@jcr:primaryType='mgnl:content')and(jcr:contains(@title,'\\\"Milano\\\"')))]", StringUtils -// .remove(criteria.toXpathExpression(), ' ')); + Criteria criteria = criteria(textEnteredByUser, true); + System.out.println("> " + criteria.toXpathExpression()); + Assert.assertEquals( + "//*[((@jcr:primaryType='mgnl:content')and(jcr:contains(@title,'\\\"Milano\\\"')))]", + StringUtils.remove(criteria.toXpathExpression(), ' ')); AdvancedResult advResult = null; try { @@ -171,18 +173,9 @@ Assert.assertEquals(1, advResult.getTotalSize()); ResultIterator<AdvancedResultItem> items = advResult.getItems(); AdvancedResultItem item = items.next(); - Assert.assertEquals("hello \\\"Milano\\\" world", item.getTitle()); + Assert.assertEquals("hello \"Milano\" world", item.getTitle()); } - private String encode(String textEnteredByUser) - { - return StringUtils.replaceEach(textEnteredByUser, new String[]{ - "\"", "-" - }, new String[]{ - "\\\"", "\\-" - }); - } - private Criteria criteria(String titleSearch, boolean escape) { Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |