Class CopyPathsAction

  • All Implemented Interfaces:
    JSONNavigateAction, NavigateAction<JSONObject,​JSONArray>

    public class CopyPathsAction
    extends java.lang.Object
    implements JSONNavigateAction
    Creates a copy of a JSONObject containing just the nodes on the paths specified.

    Specified paths that do not exist in the source object are ignored silently. Specifying an empty list of paths to navigate or only non-existent paths will result in an empty object being returned.

    See package-info for more details

    Example:

    To copy the branch k1.k2 from {k1:{k2:v1}, k3:{k4:v2}} instantiate the copier like so: new JSONObjectCopier("k1.k2") The resulting copy would be {k1:{k2:v1}}

    See unit tests for more examples

    Since:
    15 March 2016.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected JSONObject destBranch
      The current destination branch
      protected java.util.Stack<java.lang.Object> destNodeStack
      Stack for tracking destination nodes
      protected JSONObject destTree
      The destination tree for copied paths
    • Constructor Summary

      Constructors 
      Constructor Description
      CopyPathsAction()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void end()
      called after all navigation ends, and just before the navigation method exits
      boolean failFast​(java.lang.String path, java.lang.Exception e)
      called if navigation of a path throws an exception
      boolean failSilently​(java.lang.String path, java.lang.Exception e)
      called if navigation of a path throws an exception
      void foundLeafBeforePathEnd​(TreePath jp, java.lang.Object obj)
      reached leaf node (not a container) in source but specified path expects children - the specified path does not exist in the source
      void handleLeaf​(TreePath jp, int arrIndex, java.lang.Object o)
      called when a leaf in a L is reached.
      void handleLeaf​(TreePath jp, java.lang.Object o)
      called when a leaf node is reached in a M.
      private void handleNewNode​(TreePath jp, java.lang.Object node)  
      void pathEnd​(java.lang.String path)
      called after the navigation of a path ends
      boolean pathStart​(java.lang.String path)
      called before navigation of a new path starts
      void pathTailNotFound​(TreePath jp, java.lang.Object source)
      reached end of branch in source before end of specified path - the specified path does not exist in the source
      void recurEnd​(TreePath jp, JSONArray ja)
      called when navigation of an NavigateAction type object ends
      void recurEnd​(TreePath jp, JSONObject jo)
      called when navigation of an NavigateAction type object ends
      boolean recurInto​(TreePath jp, JSONArray o)
      called when an array node is encountered on the path
      boolean recurInto​(TreePath jp, JSONObject o)
      called when an object node is encountered on the path
      java.lang.Object result()
      holds the result of the navigation, as assigned by the action implementing this interface
      boolean start​(JSONObject source, java.util.Collection<java.lang.String> pathsToCopy)
      called before any navigation of the NavigateAction starts
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • destTree

        protected JSONObject destTree
        The destination tree for copied paths
      • destBranch

        protected JSONObject destBranch
        The current destination branch
      • destNodeStack

        protected java.util.Stack<java.lang.Object> destNodeStack
        Stack for tracking destination nodes
    • Constructor Detail

      • CopyPathsAction

        public CopyPathsAction()
    • Method Detail

      • start

        public boolean start​(JSONObject source,
                             java.util.Collection<java.lang.String> pathsToCopy)
        Description copied from interface: NavigateAction
        called before any navigation of the NavigateAction starts
        Specified by:
        start in interface NavigateAction<JSONObject,​JSONArray>
        Parameters:
        source - TODO
        pathsToCopy - TODO
        Returns:
        true if navigation should start at all
      • handleNewNode

        private void handleNewNode​(TreePath jp,
                                   java.lang.Object node)
      • foundLeafBeforePathEnd

        public void foundLeafBeforePathEnd​(TreePath jp,
                                           java.lang.Object obj)
        Description copied from interface: NavigateAction
        reached leaf node (not a container) in source but specified path expects children - the specified path does not exist in the source
        Specified by:
        foundLeafBeforePathEnd in interface NavigateAction<JSONObject,​JSONArray>
        Parameters:
        jp - TODO
        obj - TODO
      • pathTailNotFound

        public void pathTailNotFound​(TreePath jp,
                                     java.lang.Object source)
        Description copied from interface: NavigateAction
        reached end of branch in source before end of specified path - the specified path does not exist in the source
        Specified by:
        pathTailNotFound in interface NavigateAction<JSONObject,​JSONArray>
        Parameters:
        jp - TODO
        source - TODO
      • handleLeaf

        public void handleLeaf​(TreePath jp,
                               java.lang.Object o)
        Description copied from interface: NavigateAction
        called when a leaf node is reached in a M. a leaf in a M is a key-value pair where the value is not a container itself (it is not a M nor a L)
        Specified by:
        handleLeaf in interface NavigateAction<JSONObject,​JSONArray>
        Parameters:
        jp - - the JsonPath pointing to the leaf
        o - TODO
      • handleLeaf

        public void handleLeaf​(TreePath jp,
                               int arrIndex,
                               java.lang.Object o)
        Description copied from interface: NavigateAction
        called when a leaf in a L is reached. a leaf in a L is a non-container item (it is not a M nor a L)
        Specified by:
        handleLeaf in interface NavigateAction<JSONObject,​JSONArray>
        Parameters:
        jp - -
        arrIndex - -
        o - - the item
      • pathStart

        public boolean pathStart​(java.lang.String path)
        Description copied from interface: NavigateAction
        called before navigation of a new path starts
        Specified by:
        pathStart in interface NavigateAction<JSONObject,​JSONArray>
        Parameters:
        path - TODO
        Returns:
        true if the specified path should be navigated
      • failSilently

        public boolean failSilently​(java.lang.String path,
                                    java.lang.Exception e)
        Description copied from interface: NavigateAction
        called if navigation of a path throws an exception
        Specified by:
        failSilently in interface NavigateAction<JSONObject,​JSONArray>
        Parameters:
        path - TODO
        e - TODO
        Returns:
        true if the failure on this path should not abort the rest of the navigation
      • failFast

        public boolean failFast​(java.lang.String path,
                                java.lang.Exception e)
        Description copied from interface: NavigateAction
        called if navigation of a path throws an exception
        Specified by:
        failFast in interface NavigateAction<JSONObject,​JSONArray>
        Parameters:
        path - TODO
        e - TODO
        Returns:
        true if the failure on this path should abort the rest of the navigation
      • result

        public java.lang.Object result()
        Description copied from interface: NavigateAction
        holds the result of the navigation, as assigned by the action implementing this interface
        Specified by:
        result in interface NavigateAction<JSONObject,​JSONArray>
        Returns:
        - result