Author: thorsten Date: Mon Sep 29 05:38:19 2008 New Revision: 700103 URL: http://svn.apache.org/viewvc?rev=700103&view=rev Log: WSCOMMONS-389 Creating nodes as well based on xpath expression that like [EMAIL PROTECTED]'test']. This will provoke that we will create an attribute "id" with the value "test"
Modified: forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/helper/AXIOMXPathPatched.java Modified: forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/helper/AXIOMXPathPatched.java URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/helper/AXIOMXPathPatched.java?rev=700103&r1=700102&r2=700103&view=diff ============================================================================== --- forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/helper/AXIOMXPathPatched.java (original) +++ forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/helper/AXIOMXPathPatched.java Mon Sep 29 05:38:19 2008 @@ -16,8 +16,11 @@ */ package org.apache.forrest.dispatcher.impl.helper; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; @@ -124,13 +127,10 @@ private void calculateNsElements(LinkedHashSet<OMElement> pathNodes, String pathPart) { - /* - * FIXME: Need to test xpath expression such as [EMAIL PROTECTED]'example'] I think - * their are NOT generated correctly! - */ OMElement element = null; OMNamespaceImpl localSpace = null; - String localName = pathPart; + HashMap<String, String> map = new HashMap<String, String>(); + String localName = cleanFromExpressions(pathPart,map); String[] nameSpacedNode = localName.split(":"); if (nameSpacedNode.length == 2) { final String prefix = nameSpacedNode[0]; @@ -139,9 +139,41 @@ localName = nameSpacedNode[1]; } element = factory.createOMElement(localName, localSpace); + for (String key : map.keySet()) { + element.addAttribute(key, map.get(key), null); + } pathNodes.add(element); } + private String cleanFromExpressions(String pathPart, HashMap<String, String> map) { + Pattern pattern = Pattern.compile("(.*)\\[(.*)\\]"); + Matcher matcher = pattern.matcher(pathPart); + if (matcher.matches()){ + pathPart=matcher.group(1); + String expression = matcher.group(2); + String [] parts = expression.split("([\\s]*and[\\s]*)"); + for (String part : parts) { + pattern = Pattern.compile("@([a-zA-Z0-9]*)='(.*)'"); + matcher = pattern.matcher(part); + if (matcher.matches()){ + String key = matcher.group(1); + String value = matcher.group(2); + int end = value.indexOf("'"); + if (end>-1){ + /* + * if we have a ' in the expression it means that we have + * a xpath expression with operators other then "and". + * We will use only the first one to create an attribute from it. + */ + value= value.substring(0, end); + } + map.put(key, value); + } + } + } + return pathPart; + } + /** * @param path * @return