<commit_before>/*
 * Copyright 2006-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.consol.citrus.xml.xpath;

import java.util.*;
import java.util.Map.Entry;

import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.xpath.*;

import org.springframework.util.StringUtils;
import org.springframework.xml.namespace.SimpleNamespaceContext;
import org.w3c.dom.Node;

import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.util.XMLUtils;

/**
 * XPath utility class providing static utility methods
 * dealing with XPath expression evaluation.
 *
 * Class is abstract to prevent instantiation.
 *
 * @author <NAME>
 */
public abstract class XPathUtils {

    /** XPath expression factory */
    private static XPathFactory xPathFactory;
    
    /** Dynamic namespace prefix suffix */
    public static final String DYNAMIC_NS_START = "{";
    public static final String DYNAMIC_NS_END = "}";
    
    /** Dynamic namespace prefix */
    private static final String DYNAMIC_NS_PREFIX = "dns";

    static {
        xPathFactory = XPathFactory.newInstance();
    }
    
    /**
     * Prevent instantiation.
     */
    private XPathUtils() {
    }
    
    public static Map<String, String> getDynamicNamespaces(String expression) {
        Map<String, String> namespaces = new HashMap<String, String>();
        
        if (expression.contains(DYNAMIC_NS_START) && expression.contains(DYNAMIC_NS_END)) {
            String[] tokens = expression.split("\\" + DYNAMIC_NS_START);
            
            for (int i = 1; i < tokens.length; i++) {
                String namespace = tokens[i].substring(0, tokens[i].indexOf(DYNAMIC_NS_END));
                
                if (!namespaces.containsValue(namespace)) {
                    namespaces.put(DYNAMIC_NS_PREFIX + i, namespace);
                }
            }
        }
        
        return namespaces;
    }
    
    /**
     * Replaces all dynamic namespaces in a XPath expression with respective prefixes 
     * in namespace map.
     * 
     * XPath: //{http://sample.org/foo}foo/{http://sample.org/bar}bar results in //ns1:foo/ns2:bar where
     * the namespace map contains ns1 and ns2.
     * 
     * @param expression
     * @param namespaces<commit_msg>Completed java doc in utility class<commit_after>/*
 * Copyright 2006-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.consol.citrus.xml.xpath;

import java.util.*;
import java.util.Map.Entry;

import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.xpath.*;

import org.springframework.util.StringUtils;
import org.springframework.xml.namespace.SimpleNamespaceContext;
import org.w3c.dom.Node;

import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.util.XMLUtils;

/**
 * XPath utility class providing static utility methods
 * dealing with XPath expression evaluation.
 *
 * Class is abstract to prevent instantiation.
 *
 * @author <NAME>
 */
public abstract class XPathUtils {

    /** XPath expression factory */
    private static XPathFactory xPathFactory;
    
    /** Dynamic namespace prefix suffix */
    public static final String DYNAMIC_NS_START = "{";
    public static final String DYNAMIC_NS_END = "}";
    
    /** Dynamic namespace prefix */
    private static final String DYNAMIC_NS_PREFIX = "dns";

    static {
        xPathFactory = XPathFactory.newInstance();
    }
    
    /**
     * Prevent instantiation.
     */
    private XPathUtils() {
    }
    
    /**
     * Extracts dynamic namespaces that are inline inside a XPath expression. Example:
     * <code>/{http://sample.org/foo}foo/{http://sample.org/bar}bar</code>
     * @param expression
     * @return
     */
    public static Map<String, String> getDynamicNamespaces(String expression) {
        Map<String, String> namespaces = new HashMap<String, String>();
        
        if (expression.contains(DYNAMIC_NS_START) && expression.contains(DYNAMIC_NS_END)) {
            String[] tokens = expression.split("\\" + DYNAMIC_NS_START);
            
            for (int i = 1; i < tokens.length; i++) {
                String namespace = tokens[i].substring(0, tokens[i].indexOf(DYNAMIC_NS_END));
                
                if (!namespaces.containsValue(namespace)) {
                    namespaces.put(DYNAMIC_NS_PREFIX + i, namespace);
                }
            }
        }
        
        return namespaces;
    }
    
    /**
     * Replaces all dynamic namespaces in a XPath expression with respective prefixes 
     * in namespace map.
     * 
     * XPath: <code>/{http://sample.org/foo}foo/{http://sample.org/bar}bar</code> 
     * results in <code>/ns1:foo/ns2:bar</code> where the namespace map contains ns1 and ns2.
     * 
     * @param expression
     * @param namespaces
{'fraction_non_alphanumeric': 0.10018428547495394, 'fraction_numerical': 0.0063662254984084435, 'mean_word_length': 2.7570799244808057, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2407524', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1636, 'n_words': 493}