StorageHelper.PathParser.java Source code

Java tutorial

Introduction

Here is the source code for StorageHelper.PathParser.java

Source

package StorageHelper;

import org.apache.commons.lang.WordUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
 Copyright 2016 Alianza Inc.
    
 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.
    
 */

public class PathParser {

    private JSONObject storedData;

    public PathParser(JSONObject data) {
        storedData = data;
    }

    /**
     * find all the paths of the key, this searches recursively, searches in JSon object from constructor
     * @param searchKey key we are searching for
     * @return list of arrays, array being the path list of all the paths
     */
    public ArrayList<String[]> findPaths(String searchKey) {

        //the search takes a long time, dont' do it if  you don't have to
        if (storedData.toString().contains(searchKey))
            return findPaths(searchKey, storedData);
        else
            return new ArrayList<>();
    }

    /**
     * find all the paths in the json Object that will match the key
     * @param searchKey key searching for
     * @param searching object searching in
     * @return list of arrays, array being the path list of all the paths
     */
    private ArrayList<String[]> findPaths(String searchKey, JSONObject searching) {

        Iterator<String> keys = searching.keys();
        ArrayList<String[]> allPaths = new ArrayList<>();

        //adding found key to array
        if (searching.has(WordUtils.capitalize(searchKey)) || searching.has(WordUtils.uncapitalize(searchKey))) {
            allPaths.add(new String[] { searchKey });
        }

        while (keys.hasNext()) {
            ArrayList<String[]> foundPaths;
            String next = keys.next();
            //get the next one, either as json object or json array
            Object nextJson = null;
            try {
                nextJson = searching.get(next);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            foundPaths = findPathsRecurser(searchKey, nextJson);

            //add the path to the main collection of paths
            for (String[] paths : foundPaths) {
                ArrayList<String> pathFinder = new ArrayList<>();

                pathFinder.add(next);
                pathFinder.addAll(Arrays.asList(paths));

                allPaths.add(pathFinder.toArray(new String[pathFinder.size()]));
            }
        }

        return allPaths;
    }

    /**
     * find all the paths in the json Array that will match the key
     * @param searchKey key searching for
     * @param searching object searching in
     * @return list of arrays, array being the path list of all the paths
     */
    private ArrayList<String[]> findPaths(String searchKey, JSONArray searching) {
        ArrayList<String[]> allPaths = new ArrayList<>();

        for (int i = 0; i < searching.length(); ++i) {
            ArrayList<String[]> foundPaths;

            Object nextJson = null;
            try {
                nextJson = searching.get(i);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            foundPaths = findPathsRecurser(searchKey, nextJson);

            //add the path to the main collection of paths
            for (String[] paths : foundPaths) {
                ArrayList<String> pathFinder = new ArrayList<>();

                pathFinder.add(String.valueOf(i));
                pathFinder.addAll(Arrays.asList(paths));

                allPaths.add(pathFinder.toArray(new String[pathFinder.size()]));
            }
        }
        return allPaths;
    }

    /**
     * recruse throught he object to find a path
     * @param searchKey
     * @param searching
     * @return
     */
    private ArrayList<String[]> findPathsRecurser(String searchKey, Object searching) {
        ArrayList<String[]> foundPaths = new ArrayList<>();

        if (searching.getClass().equals(JSONObject.class))
            foundPaths = findPaths(searchKey, (JSONObject) searching);
        else if (searching.getClass().equals(JSONArray.class))
            foundPaths = findPaths(searchKey, (JSONArray) searching);

        return foundPaths;
    }

}