Java XPath Create getXPath(String exp, NamespaceContext ctx)

Here you can find the source of getXPath(String exp, NamespaceContext ctx)

Description

get X Path

License

Open Source License

Declaration

public static XPathExpression getXPath(String exp, NamespaceContext ctx)
            throws XPathExpressionException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.HashMap;

import java.util.Map;

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;

import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Main {
    private static ThreadLocal<Map<String, XPathExpression>> sCache = new ThreadLocal<Map<String, XPathExpression>>();
    private static XPathFactory sFactory = XPathFactory.newInstance();

    public static XPathExpression getXPath(String exp)
            throws XPathExpressionException {
        Map<String, XPathExpression> cache = getCache();
        if (!cache.containsKey(exp)) {
            cache.put(exp, sFactory.newXPath().compile(exp));
        }/*  ww  w.j  a v  a2s. com*/
        return cache.get(exp);
    }

    public static XPathExpression getXPath(String exp, NamespaceContext ctx)
            throws XPathExpressionException {
        if (ctx == null) {
            return getXPath(exp);
        }
        XPath mRetVal = sFactory.newXPath();
        mRetVal.setNamespaceContext(ctx);
        return mRetVal.compile(exp);
    }

    private static Map<String, XPathExpression> getCache() {
        Map<String, XPathExpression> mRetVal = sCache.get();
        if (mRetVal == null) {
            mRetVal = new HashMap<String, XPathExpression>();
            sCache.set(mRetVal);
        }
        return mRetVal;
    }
}

Related

  1. getXPath(Node n)
  2. getXPath(Node node)
  3. getXPath(Node node)
  4. getXPath(Node node)
  5. getXPath(Node node, String xpath)
  6. getXPath(String expression)
  7. getXPathExpression(final Node node)
  8. getXPathExpression(String expression)
  9. getXPathExpression(String path)