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

org.hisrc.jsonix.compilation.jsonschema.JsonSchemaModuleCompiler Maven / Gradle / Ivy

There is a newer version: 2.3.9
Show newest version
package org.hisrc.jsonix.compilation.jsonschema;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.Validate;
import org.hisrc.jsonix.definition.JsonSchema;
import org.hisrc.jsonix.definition.Mapping;
import org.hisrc.jsonix.definition.Module;
import org.hisrc.jsonix.definition.Modules;
import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder;

public class JsonSchemaModuleCompiler {

	private final JsonSchemaModulesGenerator modulesCompiler;
	private final Modules modules;
	private final Module module;

	// private final JsonSchema jsonSchema;

	public JsonSchemaModuleCompiler(
			JsonSchemaModulesGenerator modulesCompiler,
			Module module, JsonSchema jsonSchema) {
		Validate.notNull(modulesCompiler);
		Validate.notNull(module);
		Validate.notNull(jsonSchema);
		this.modulesCompiler = modulesCompiler;
		this.modules = modulesCompiler.getModules();
		this.module = module;
		// this.jsonSchema = jsonSchema;
	}

	public JsonSchemaModulesGenerator getModulesCompiler() {
		return modulesCompiler;
	}

	public Modules getModules() {
		return modules;
	}

	public Module getModule() {
		return module;
	}

	public JsonSchemaBuilder compile() {
		final Map, JsonSchemaBuilder> mappingSchemas = new LinkedHashMap, JsonSchemaBuilder>(
				this.module.getMappings().size());
		for (Mapping mapping : this.module.getMappings()) {
			if (!mapping.isEmpty()) {
				final JsonSchemaMappingCompiler mappingCompiler = new JsonSchemaMappingCompiler(
						this, mapping);
				mappingSchemas.put(mapping, mappingCompiler.compile());

			}
		}

		final JsonSchemaBuilder schema;
		if (mappingSchemas.size() == 1) {
			schema = mappingSchemas.values().iterator().next();
		} else {
			schema = new JsonSchemaBuilder();
			schema.addId(getModule().getSchemaId());
			for (Entry, JsonSchemaBuilder> entry : mappingSchemas
					.entrySet()) {
				final Mapping mapping = entry.getKey();
				final JsonSchemaBuilder mappingSchema = entry.getValue();
				schema.addDefinition(mapping.getMappingName(), mappingSchema);
				schema.addAnyOf(new JsonSchemaBuilder().addRef(mapping
						.getSchemaId()));
			}
		}

		return schema;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy