<commit_before>package com.cube.storm.ui.lib.parser;

import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.cube.storm.UiSettings;
import com.cube.storm.ui.model.App;
import com.cube.storm.ui.model.page.Page;
import com.cube.storm.ui.model.page.TabbedPageCollection;
import com.cube.storm.ui.model.property.TextProperty;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Locale;

/**
 * View parser used to process the json files into models to be used with the list/grid adapters
 *
 * @author <NAME>
 * @project LightningUi
 */
public abstract class ViewBuilder
{
	private static Gson viewBuilder;

	/**
	 * Required to include view overrides
	 */
	public void rebuild()
	{
		viewBuilder = null;
		getGson();
	}

	/**
	 * Gets the gson object with the registered storm view type adapters. Use {@link #build(com.google.gson.JsonElement, Class)} or
	 * {@link #build(String, Class)} to build your page/view objects.
	 *
	 * @return The gson object
	 */
	private Gson getGson()
	{
		if (viewBuilder == null)
		{
			GsonBuilder builder = new GsonBuilder();

			for (Type instanceClass : UiSettings.getInstance().getViewProcessors().keySet())
			{
				builder.registerTypeAdapter(instanceClass, UiSettings.getInstance().getViewProcessors().get(instanceClass));
			}

			builder.registerTypeAdapter(TextProperty.class, new JsonDeserializer<TextProperty>()
			{
				@Override public TextProperty deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
				{
					JsonElement content = json.getAsJsonObject().get("content");
					if (content.isJsonPrimitive())
					{
						TextProperty text = new TextProperty();
						text.setContent(new HashMap<String, String>());
						text.getContent().put(Locale.getDefault().getLanguage(), content.getAsString());

						return text;
					}
					else
					{
						return new Gson().fromJson(json, TextProperty.class);
					}
				}
			});

			viewBuilder = builder.create();
		}

		return viewBuilder;
	}

	/**
	 * Builds an App object from a file Uri
	 *
	 * @param fileUri The file Uri to load from
	 *
	 * @return The app data or null
	 */
	@Nullable
	public App buildApp(@NonNull Uri fileUri)
	{
		InputStream appData = UiSettings.getInstance().getFileFactory().loadFromUri(fileUri);

		if (appData != null)
		{<commit_msg>Added public gson methods for view builder<commit_after>package com.cube.storm.ui.lib.parser;

import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.cube.storm.UiSettings;
import com.cube.storm.ui.model.App;
import com.cube.storm.ui.model.page.Page;
import com.cube.storm.ui.model.page.TabbedPageCollection;
import com.cube.storm.ui.model.property.TextProperty;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Locale;

/**
 * View parser used to process the json files into models to be used with the list/grid adapters
 *
 * @author <NAME>
 * @project LightningUi
 */
public abstract class ViewBuilder
{
	private static Gson viewGson;
	private static GsonBuilder viewBuilder;

	/**
	 * Required to include view overrides
	 */
	public void rebuild()
	{
		viewBuilder = null;
		getGson();
	}

	/**
	 * Creates a gson builder instance with all registered type adapters necessary for Ui models/views
	 * @return
	 */
	@NonNull
	public GsonBuilder getGsonBuilder()
	{
		GsonBuilder builder = new GsonBuilder();

		for (Type instanceClass : UiSettings.getInstance().getViewProcessors().keySet())
		{
			builder.registerTypeAdapter(instanceClass, UiSettings.getInstance().getViewProcessors().get(instanceClass));
		}

		builder.registerTypeAdapter(TextProperty.class, new JsonDeserializer<TextProperty>()
		{
			@Override public TextProperty deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
			{
				JsonElement content = json.getAsJsonObject().get("content");
				if (content.isJsonPrimitive())
				{
					TextProperty text = new TextProperty();
					text.setContent(new HashMap<String, String>());
					text.getContent().put(Locale.getDefault().getLanguage(), content.getAsString());

					return text;
				}
				else
				{
					return new Gson().fromJson(json, TextProperty.class);
				}
			}
		});

		return builder;
	}

	/**
	 * Gets the gson object with the registered storm view type adapters. Use {@link #build(com.google.gson.JsonElement, Class)} or
	 * {@link #build(String, Class)} to build your page/view objects.
	 *
	 * @return The gson object
	 */
	private Gson getGson()
	{
		if (viewGson == null)
		{
			GsonBuilder builder = getGsonBuilder();
			viewGson = builder.create();
		}

		return viewGson;
	}

	/**
	 * Builds an App object from a file Uri
	 *
	 * @param fileUri The file Uri to load from
	 *
	 * @return The app data or null
	 */
	@Nullable
	public App buildApp(@NonNull Uri fileUri)
	{
		InputStream appData = UiSettings.getInstance().getFileFactory().loadFromUri(fileUri);

		if (appData != null)
		{
{'fraction_non_alphanumeric': 0.11164383561643836, 'fraction_numerical': 0.0, 'mean_word_length': 4.205882352941177, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1792927', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1755, 'n_words': 436}