From: <fg...@us...> - 2011-08-08 10:32:09
|
Revision: 3586 http://openutils.svn.sourceforge.net/openutils/?rev=3586&view=rev Author: fgiust Date: 2011-08-08 10:32:03 +0000 (Mon, 08 Aug 2011) Log Message: ----------- CRIT-40 escaping of valid elements in base path 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/jcr/query/xpath/impl/MagnoliaCriteriaTest.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 2011-08-08 10:31:44 UTC (rev 3585) +++ trunk/openutils-mgnlcriteria/src/main/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/utils/XPathTextUtils.java 2011-08-08 10:32:03 UTC (rev 3586) @@ -121,7 +121,8 @@ StringBuilder encodedPath = new StringBuilder(path.length()); - boolean inXpathCondition = false; + int inXpathCondition = 0; + boolean xpathWithFunction = false; // TODO maybe a more robust check is needed for (int i = 0; i < path.length(); ++i) @@ -132,11 +133,11 @@ { encodedPath.append("_x" + StringUtils.leftPad(Integer.toHexString(ch), 4, '0') + "_"); } - else if (!inXpathCondition && ch == ' ') + else if (inXpathCondition <= 0 && ch == ' ') { encodedPath.append("_x0020_"); } - else if (!inXpathCondition && ch == ',') + else if (inXpathCondition <= 0 && ch == ',') { encodedPath.append("_x002c_"); } @@ -145,12 +146,27 @@ if (ch == '[') { - inXpathCondition = true; + inXpathCondition++; } - else if (inXpathCondition && ch == ']') + else if (ch == '(') { - inXpathCondition = false; + // "(" is the beginning of an expression only when used with the element() function + // not really a clean check, actually... + if (encodedPath.indexOf("element") > 0) + { + inXpathCondition++; + xpathWithFunction = true; + } } + else if (inXpathCondition > 0 && ch == ']') + { + inXpathCondition--; + } + else if (inXpathCondition > 0 && xpathWithFunction && ch == ')') + { + inXpathCondition--; + xpathWithFunction = false; + } encodedPath.append(ch); } } Modified: trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaTest.java =================================================================== --- trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaTest.java 2011-08-08 10:31:44 UTC (rev 3585) +++ trunk/openutils-mgnlcriteria/src/test/java/net/sourceforge/openutils/mgnlcriteria/jcr/query/xpath/impl/MagnoliaCriteriaTest.java 2011-08-08 10:32:03 UTC (rev 3586) @@ -86,4 +86,56 @@ Assert.assertEquals("//site//*[( (@property='test') )] ", xpathExpression); } + + /** + * Test for CRIT-37 + */ + @Test + public void testEscapeComma() + { + Criteria criteria = JCRCriteriaFactory.createCriteria().setBasePath("/one/two/3three/fo,ur/"); + criteria.add(Restrictions.eq("@property", "test")); + String xpathExpression = criteria.toXpathExpression(); + + Assert.assertEquals(xpathExpression, "//one/two/_x0033_three/fo_x002c_ur//*[( (@property='test') )] "); + } + + /** + * Test for CRIT-40 + */ + @Test + public void testDontEscapeBasePathWithParenthesis() + { + Criteria criteria = JCRCriteriaFactory.createCriteria().setBasePath("/path/with(paren,thesis)/test"); + criteria.add(Restrictions.eq("@property", "test")); + String xpathExpression = criteria.toXpathExpression(); + + Assert.assertEquals(xpathExpression, "//path/with(paren_x002c_thesis)/test//*[( (@property='test') )] "); + } + + /** + * Test for CRIT-40 + */ + @Test + public void testDontEscapeExpressions() + { + Criteria criteria = JCRCriteriaFactory.createCriteria().setBasePath("/jcr:root///element(* , mgnl:media)"); + criteria.add(Restrictions.eq("@property", "test")); + String xpathExpression = criteria.toXpathExpression(); + + Assert.assertEquals(xpathExpression, "/jcr:root///element(* , mgnl:media)[( (@property='test') )] "); + } + + /** + * Test for CRIT-40 + */ + @Test + public void testDontEscapeExpressions2() + { + Criteria criteria = JCRCriteriaFactory.createCriteria().setBasePath("/jcr:root//*[@jcr:uuid='xxxx-xxxx']//*"); + criteria.add(Restrictions.eq("@property", "test")); + String xpathExpression = criteria.toXpathExpression(); + + Assert.assertEquals(xpathExpression, "/jcr:root//*[@jcr:uuid='xxxx-xxxx']//*[( (@property='test') )] "); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |