diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-09-09 16:30:45 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-09-09 16:30:45 +0000 |
commit | 401b2d71ebc8b452691967c5c79ac3719275b6f5 (patch) | |
tree | 9a2f1a67a927774b4710ea2a790fd47a17e7ea20 /java/sca/modules/common-xml/src/main/java | |
parent | df3251c5445b4e5efa48e20aab130157b873a6fc (diff) |
Fix the calculation of prefixes and add a test case
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@813042 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/common-xml/src/main/java')
-rw-r--r-- | java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelper.java | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelper.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelper.java index cbf8fa9f61..491837c7ee 100644 --- a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelper.java +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelper.java @@ -62,14 +62,27 @@ public class XPathHelper { public XPath newXPath() { return factory.newXPath(); } - + public XPathExpression compile(NamespaceContext context, String expression) throws XPathExpressionException { XPath path = newXPath(); - path.setNamespaceContext(getNamespaceContext(expression, context)); + context = getNamespaceContext(expression, context); + return compile(path, context, expression); + } + + public XPathExpression compile(XPath path, NamespaceContext context, String expression) + throws XPathExpressionException { + path.setNamespaceContext(context); return path.compile(expression); } - private NamespaceContext getNamespaceContext(String expression, NamespaceContext context) { + /** + * Take a snapshot of the given namespace context based on the prefixes found in the expression. + * In StAX, the prefix/namespace mapping in the namespace context can change as the event moves + * @param expression + * @param context + * @return + */ + public NamespaceContext getNamespaceContext(String expression, NamespaceContext context) { NamespaceContextImpl nsContext = new NamespaceContextImpl(null); for (String prefix : getPrefixes(expression)) { @@ -81,6 +94,11 @@ public class XPathHelper { return nsContext; } + /** + * Parse the XPath expression to collect all the prefixes for namespaces + * @param expression + * @return A collection of prefixes + */ private Collection<String> getPrefixes(String expression) { List<String> prefixes = new ArrayList<String>(); prefixes.add(""); @@ -95,14 +113,12 @@ public class XPathHelper { if (XMLCharHelper.isNCName(prefix.charAt(j))) { continue; } - j--; break; } + // j is before the first char of the prefix if (j != (prefix.length() - 1) && XMLCharHelper.isNCNameStart(prefix.charAt(j + 1))) { prefixes.add(prefix.substring(j + 1)); } - break; - } return prefixes; } |