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

com.oath.maven.plugin.freemarker.JsonPropertiesProvider Maven / Gradle / Ivy

// Copyright 2018, Oath Inc.
// Licensed under the terms of the Apache 2.0 license. See the LICENSE file in the project root for terms.

package com.oath.maven.plugin.freemarker;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import org.fugerit.java.core.cfg.ConfigRuntimeException;

public class JsonPropertiesProvider implements OutputGeneratorPropertiesProvider {
	private final Gson gson;
	private final Type stringObjectMap;
	private final File dataDir;
	private final File templateDir;
	private final File outputDir;

	private JsonPropertiesProvider(File dataDir, File templateDir, File outputDir) {
		this.dataDir = dataDir;
		this.templateDir = templateDir;
		this.outputDir = outputDir;
		gson = new GsonBuilder().setLenient().create();
		stringObjectMap = new TypeToken>() { } .getType();
	}

	public static JsonPropertiesProvider create(File dataDir, File templateDir, File outputDir) {
		return new JsonPropertiesProvider(dataDir, templateDir, outputDir);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void providePropertiesFromFile(Path path, OutputGenerator.OutputGeneratorBuilder builder) {
		File jsonDataFile = path.toFile();
		Map data = parseJson(jsonDataFile);

		Object obj = data.get("dataModel");
		if (obj != null) {
			builder.addDataModel((Map) obj);
		} else {
			builder.addDataModel(new HashMap<>());
		}

		obj = data.get("templateName");
		if (obj == null) {
			throw new ConfigRuntimeException("Require json data property not found: templateName");
		}
		builder.addTemplateLocation(templateDir.toPath().resolve(obj.toString()));

		String dataDirName = dataDir.getAbsolutePath();
		String jsonFileName = jsonDataFile.getAbsolutePath();
		if (!jsonFileName.startsWith(dataDirName)) {
			throw new IllegalStateException("visitFile() given file not in sourceDirectory: " + jsonDataFile);
		}

		String outputFileName = jsonFileName.substring(dataDirName.length()+1);
		outputFileName = outputFileName.substring(0, outputFileName.length() - 5);
		Path outputPath = outputDir.toPath();
		Path resolved = outputPath.resolve(outputFileName);
		builder.addOutputLocation(resolved);
	}

	private Map parseJson(File jsonDataFile) {
		try (JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonDataFile), StandardCharsets.UTF_8))) {
			return gson.fromJson(reader, stringObjectMap);
		} catch (Exception t) {
			throw new ConfigRuntimeException("Could not parse json data file: " + jsonDataFile, t);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy