All Downloads are FREE. Search and download functionalities are using the official Maven repository.

StorageHelper.CreatorHelper Maven / Gradle / Ivy

package StorageHelper;

import Data.Storage;
import DataTools.Utils;
import com.google.gson.internal.LinkedTreeMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 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 CreatorHelper {

    /**
     * create a data storage from given data using the right type it really is
     * method finds out the type of the object and then adds it to collection the right way for that type
     * @param data Object of data
     * @return DataStorage with data populated
     */
    public static Storage createDataStorage(Object data) {
        boolean alreadyEntered = false;
        Storage store = new Storage();
        //if it is a default type then don't let it match anything else
        Class[] dataTypes = {Integer.class, String.class, int.class, Double.class, double.class, boolean.class, Boolean.class};

        //if data is null we can return an empty data storage
        if (data == null) {
            return store;
        }

        //skip all of these things if it is a data type not collection
        if (!Arrays.asList(dataTypes).contains(data.getClass())) {

            //find what type the Object data is and cast it to that
            //add to collection as the right type it is
            if (data.getClass().getTypeName().contains("ArrayList")) {
                store.addArray((ArrayList) data);
                alreadyEntered = true;
            } else if (data.getClass().getTypeName().contains("HashMap")) {
                store.addMap((HashMap) data);
                alreadyEntered = true;
            } else if (data.getClass().getTypeName().contains("JSONArray")) {
                store.addJson((JSONArray) data);
                alreadyEntered = true;
            } else if (data.getClass().getTypeName().contains("JSONObject")) {
                store.addJson((JSONObject) data);
                alreadyEntered = true;
            }

            //gson uses this linked tree map type, this will convert it to Data Storage
            else {
                if (data.getClass().getTypeName().contains("LinkedTreeMap")) {
                    LinkedTreeMap treeData = (LinkedTreeMap) data;
                    for (String key : treeData.keySet())
                        store.put(key, treeData.get(key));
                }
                //this could be many things, but not a string
                else if (!data.getClass().getTypeName().contains("String"))
                    if (!data.toString().isEmpty()) store.addModel(data);
                alreadyEntered = true;
            }
        }

        //if nothing is added just add the object at index 0
        if (store.size() < 1 && !data.toString().isEmpty() && !alreadyEntered)
            store.put("0", data);

        return store;
    }

    /**
     * constructor that populates the collections with given data
     * @param data String of data or json String
     */
    public static Storage createDataStorage(String data) {
        data = data.trim();
        Storage storage = new Storage();
        boolean isXml = data.startsWith("<") && data.endsWith(">");
        boolean isJson = data.startsWith("{") && data.endsWith("}");
        boolean isArray = data.startsWith("[") && data.endsWith("]");

        if (isJson)
            try {
                storage.addJson(new JSONObject(data));
            } catch (JSONException e) {
                Utils.debug("not valid XML, or json not added to system\n" + data, "error");
            }
        else if (isArray)
            try {
                storage.addJson(new JSONArray(data));
            } catch (JSONException e) {
                Utils.debug("not valid XML, or json not added to system\n" + data, "error");
            }
        else if (isXml) {
            String cleanData = Converter.cleanXmlHeader(data);
            //not supporting html xml, saving it as a string
            if (cleanData.startsWith("") && cleanData.endsWith(""))
                storage.put("html", cleanData);
            else{
                storage.addXml(Converter.cleanXml(cleanData));
            }
        }
        else
            Utils.debug("not valid XML, or json not added to system\n" + data, "error");

        return storage;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy