Class JXPathUtils


  • public class JXPathUtils
    extends Object
    Utility methods for working with JXPath.
    Author:
    Jerry Vos
    • Constructor Summary

      Constructors 
      Constructor Description
      JXPathUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.apache.commons.jxpath.JXPathContext createContext​(Object rootObj, boolean lenient)
      Creates a context with the specified object as its root and the specified leniency.
      static org.apache.commons.jxpath.JXPathContext createRootContext​(Object obj, boolean lenient)
      Creates a context for the root of a data file.
      static String fixPath​(String path)
      When doing xpath against beans we need to reference the root with /fileDef, but to be consistent with the DOM code we convert FilDef to fileDef.
      static org.apache.commons.jxpath.JXPathContext getContext​(org.apache.commons.jxpath.JXPathContext rootContext, org.apache.commons.jxpath.JXPathContext curContext, String path)
      If the path begins with a '/' then the root context is returned, otherwise curContext is.
      static Object getXPathNode​(org.apache.commons.jxpath.JXPathContext rootContext, org.apache.commons.jxpath.JXPathContext curContext, String path)
      Retrieves a value from a context (which depends on the path) based on the xpath expression and returns it.
      static List<?> getXPathNodes​(org.apache.commons.jxpath.JXPathContext rootContext, org.apache.commons.jxpath.JXPathContext curContext, String path)
      Retrieves a set of nodes from a context (which depends on the path) based on the xpath expression and returns it.
    • Field Detail

      • XPATH_FILE_DEF_CORRECT

        public static final String XPATH_FILE_DEF_CORRECT
        The correct representation of the root FileDef tag (/fileDef) according to what JXPath requires.
      • XPATH_FILE_DEF_WRONG

        public static final String XPATH_FILE_DEF_WRONG
        The normal (default) FileDef string.
    • Constructor Detail

      • JXPathUtils

        public JXPathUtils()
    • Method Detail

      • createContext

        public static org.apache.commons.jxpath.JXPathContext createContext​(Object rootObj,
                                                                            boolean lenient)
        Creates a context with the specified object as its root and the specified leniency.
        Parameters:
        rootObj - the object at the root of the context
        lenient - whether the context is lenient about xpath errrors or not
        Returns:
        a newly created context
      • createRootContext

        public static org.apache.commons.jxpath.JXPathContext createRootContext​(Object obj,
                                                                                boolean lenient)
        Creates a context for the root of a data file. This wraps the specified object in a FileDef object so that xpath strings like "/FileDef/..." work correctly.
        Parameters:
        obj - the object to return from "/FileDef" xpath strings
        lenient - whether to be lenient about xpath errors
        Returns:
        the newly created context
      • getContext

        public static org.apache.commons.jxpath.JXPathContext getContext​(org.apache.commons.jxpath.JXPathContext rootContext,
                                                                         org.apache.commons.jxpath.JXPathContext curContext,
                                                                         String path)
        If the path begins with a '/' then the root context is returned, otherwise curContext is. Hopefully in the future this method will also handle higher nested levels of contexts and relative paths in contexts, but as of now it does not.
        Parameters:
        rootContext - the context to be returned for finding from root
        curContext - the context to be returned for normal (non-rooted) checking
        path - the xpath expression
        Returns:
        the context as specified
      • fixPath

        public static String fixPath​(String path)
        When doing xpath against beans we need to reference the root with /fileDef, but to be consistent with the DOM code we convert FilDef to fileDef.
        Parameters:
        path - the path to possibly convert
        Returns:
        the result of replacing XPATH_FILE_DEF_WRONG with XPATH_FILE_DEF_CORRECT
      • getXPathNode

        public static Object getXPathNode​(org.apache.commons.jxpath.JXPathContext rootContext,
                                          org.apache.commons.jxpath.JXPathContext curContext,
                                          String path)
        Retrieves a value from a context (which depends on the path) based on the xpath expression and returns it.
        Parameters:
        rootContext - the root context
        curContext - the current context
        path - the xpath expression
        Returns:
        the result of applying the xpath expression to the context returned by getContext(JXPathContext, JXPathContext, String)
        See Also:
        getContext(JXPathContext, JXPathContext, String)
      • getXPathNodes

        public static List<?> getXPathNodes​(org.apache.commons.jxpath.JXPathContext rootContext,
                                            org.apache.commons.jxpath.JXPathContext curContext,
                                            String path)
        Retrieves a set of nodes from a context (which depends on the path) based on the xpath expression and returns it.
        Parameters:
        rootContext - the root context
        curContext - the current context
        path - the xpath expression
        Returns:
        the result of applying the xpath expression to the context returned by getContext(JXPathContext, JXPathContext, String)
        See Also:
        getContext(JXPathContext, JXPathContext, String)