com.google.research.ic.ferret.data.LogLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.google.research.ic.ferret.data.LogLoader.java

Source

/*******************************************************************************
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * 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.
 *******************************************************************************/
package com.google.research.ic.ferret.data;

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.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.research.ic.ferret.data.attributes.Attribute;
import com.google.research.ic.ferret.data.attributes.CategoricalAttribute;
import com.google.research.ic.ferret.data.attributes.DateTimeAttribute;
import com.google.research.ic.ferret.data.attributes.NumericalAttribute;
import com.google.research.ic.ferret.data.ext.alogger.AccessibilityLogParser;
import com.google.research.ic.ferret.test.Debug;

import java.io.File;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO: Insert description here. (generated by marknewman)
 */
public class LogLoader {

    public static final String REFLECTION_LOG = "ReflectionLog";
    public static final String ACCESSIBILITIY_LOG = "AccessibilityLog";

    private String logType = ACCESSIBILITIY_LOG;
    private boolean logsHaveBeenRead = false;

    private static LogLoader theLoader = null;
    private Parser parser = null;
    private Gson gson = null;
    private static Object loaderLock = new Object();

    private static Map<String, Integer> globalIdentifierIdMap = new HashMap<String, Integer>();
    private static Integer lastIDAssigned = 0;

    private LogLoader() {
        GsonBuilder gBuilder = new GsonBuilder();
        gBuilder.registerTypeAdapter(Event.class, new JsonEventDeserializer<Event>());
        gBuilder.registerTypeAdapter(Attribute.class, new JsonAttributeDeserializer<Attribute>());
        gson = gBuilder.create();
    }

    public String getLogType() {
        return logType;
    }

    public void setLogType(String logType) {
        synchronized (logType) {
            Debug.log("In LogLoader.setLogType, current logType= " + this.logType);
            if (!this.logType.equals(logType)) {
                Debug.log("So now setting it to" + logType);
                this.logType = logType;
                if (logsHaveBeenRead) {
                    loadLogs(); // reload
                }
            }
        }
    }

    public static LogLoader getLogLoader() {
        synchronized (loaderLock) {
            if (theLoader == null) {
                Debug.log("Created new LogLoader");
                theLoader = new LogLoader();
            }
        }
        return theLoader;
    }

    public List<Snippet> loadLogs() {
        return loadLogs(null);
    }

    public List<Snippet> loadLogs(String logDir) {

        List<Snippet> snippets = null;

        synchronized (logType) {
            snippets = getParser().readLogDirectory(logDir);
            logsHaveBeenRead = true;
        }
        return snippets;
    }

    public List<Snippet> loadLogFile(String logFileName) {
        List<Snippet> snippets = null;

        synchronized (logType) {
            snippets = getParser().readLogFile(logFileName);
            logsHaveBeenRead = true;
            for (Snippet s : snippets) {
                s.setSourceFilename(new File(logFileName).getName());
            }
        }
        return snippets;

    }

    public Parser getParser() {
        synchronized (logType) {
            if (logType.equals(REFLECTION_LOG)) {
                throw new IllegalArgumentException("Reflection logs not supported currently");
            } else if (logType.equals(ACCESSIBILITIY_LOG)) {
                parser = AccessibilityLogParser.getParser();
            }
        }
        return parser;
    }

    public int getOrCreateIdentifierId(String identifier) {
        Integer i = globalIdentifierIdMap.get(identifier);
        if (i == null) {
            i = ++lastIDAssigned;
            globalIdentifierIdMap.put(identifier, i);
        }
        return i;
    }

    public Gson getGson() {
        return gson;
    }

    private static final class JsonEventDeserializer<Event> implements JsonDeserializer, JsonSerializer {
        /* (non-Javadoc)
         * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
         */
        @Override
        public Object deserialize(JsonElement json, Type klass, JsonDeserializationContext jdContext)
                throws JsonParseException {
            return getLogLoader().getParser().deserializeEvent(json, klass, jdContext);
        }

        /* (non-Javadoc)
         * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type, com.google.gson.JsonSerializationContext)
         */
        @Override
        public JsonElement serialize(Object arg0, Type arg1, JsonSerializationContext arg2) {
            return arg2.serialize(arg0);
        }
    }

    static final class JsonAttributeDeserializer<Attribute> implements JsonDeserializer, JsonSerializer {
        /* (non-Javadoc)
         * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
         */
        @Override
        public Object deserialize(JsonElement json, Type klass, JsonDeserializationContext jdContext)
                throws JsonParseException {
            JsonObject jObj = json.getAsJsonObject();
            if (jObj == null) {
                Debug.log("JSON element was null");
            }
            if (jObj.get("type") == null) {
                Debug.log("JSON element has no type, it looks like this: " + jObj.toString());
                return null;
            }
            String type = jObj.get("type").getAsString();
            if (type.equals(CategoricalAttribute.TYPE)) {
                return jdContext.deserialize(json, CategoricalAttribute.class);
            }
            if (type.equals(DateTimeAttribute.TYPE)) {
                return jdContext.deserialize(json, DateTimeAttribute.class);
            }
            if (type.equals(NumericalAttribute.TYPE)) {
                return jdContext.deserialize(json, NumericalAttribute.class);
            }

            return null;
        }

        /* (non-Javadoc)
         * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type, com.google.gson.JsonSerializationContext)
         */
        @Override
        public JsonElement serialize(Object arg0, Type arg1, JsonSerializationContext arg2) {
            return arg2.serialize(arg0);
        }
    }
}