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

org.hisrc.jsonix.definition.Modules Maven / Gradle / Ivy

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

import java.text.MessageFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.hisrc.jsonix.configuration.exception.AmbiguousPackageMappingNameException;
import org.hisrc.jsonix.configuration.exception.AmbiguousPackageSchemaIdException;
import org.hisrc.jsonix.context.JsonixContext;
import org.jvnet.jaxb2_commons.xml.bind.model.MModelInfo;
import org.slf4j.Logger;

public class Modules {

	private final Logger logger;
	private final Collection> modules;
	private final Map packageMappingNameMap = new HashMap();
	private final Map packageSchemaIdMap = new HashMap();
	private final MModelInfo modelInfo;

	public Modules(JsonixContext context, MModelInfo modelInfo,
			Collection> modules) {
		Validate.notNull(modelInfo);
		Validate.noNullElements(modules);
		this.logger = Validate.notNull(context).getLoggerFactory()
				.getLogger(Modules.class.getName());
		this.modelInfo = modelInfo;
		this.modules = modules;

		for (Module module : modules) {
			for (Mapping mapping : module.getMappings()) {

				final String packageName = mapping.getPackageName();
				final String mappingName = mapping.getMappingName();
				{
					final String knownMappingName = packageMappingNameMap
							.get(packageName);
					if (knownMappingName == null) {
						this.packageMappingNameMap
								.put(packageName, mappingName);
					} else if (!knownMappingName.equals(mappingName)) {
						logger.warn(MessageFormat
								.format("Package [{0}] is mapped using at least two different mapping names [{1}] and [{2}]. Packages may be mapped by several mappings but they have to have equal names.",
										packageName, knownMappingName,
										mappingName));
						throw new AmbiguousPackageMappingNameException(
								packageName, knownMappingName, mappingName);
					}
				}
				{
					final String mappingSchemaId = mapping.getSchemaId();
					final String knownSchemaId = packageSchemaIdMap
							.get(packageName);
					if (knownSchemaId == null) {
						this.packageSchemaIdMap.put(packageName,
								mappingSchemaId);
					} else if (!knownSchemaId.equals(mappingSchemaId)) {
						logger.warn(MessageFormat
								.format("Package [{0}] is mapped using at least two different schema ids [{1}] and [{2}]. "
										+ "Packages may be mapped by several mappings but they have to have equal schema ids. "
										+ "Please use the [jsonix:module/@schemaId] or [jsonix:mapping/@schemaId] attribute and specify the same schema id in both mappings.",
										packageName, knownSchemaId,
										mappingSchemaId));
						throw new AmbiguousPackageSchemaIdException(
								packageName, knownSchemaId, mappingSchemaId);
					}
				}
			}
		}
	}

	public MModelInfo getModelInfo() {
		return modelInfo;
	}

	public String getMappingName(String packageName) {
		return this.packageMappingNameMap.get(packageName);
	}

	public String getSchemaId(String packageName) {
		return this.packageSchemaIdMap.get(packageName);
	}

	public Collection> getModules() {
		return modules;
	}

	@Override
	public String toString() {
		return this.modules.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy