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

org.hisrc.jsonix.analysis.ModelInfoGraphBuilder Maven / Gradle / Ivy

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

import java.text.MessageFormat;

import org.apache.commons.lang3.Validate;
import org.hisrc.jsonix.context.JsonixContext;
import org.jgrapht.DirectedGraph;
import org.jgrapht.EdgeFactory;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MModelInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPackageInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;
import org.slf4j.Logger;

public class ModelInfoGraphBuilder {

	private final Logger logger;
	private final MModelInfo modelInfo;

	private final EdgeFactory, DependencyEdge> edgeFactory = new EdgeFactory, DependencyEdge>() {
		@Override
		public DependencyEdge createEdge(InfoVertex sourceVertex,
				InfoVertex targetVertex) {
			return new DependencyEdge(DependencyType.HARD);
		}
	};

	private final DirectedGraph, DependencyEdge> graph;

	public ModelInfoGraphBuilder(JsonixContext context,
			MModelInfo modelInfo) {
		Validate.notNull(modelInfo);
		this.logger = Validate.notNull(context).getLoggerFactory()
				.getLogger(ModelInfoGraphAnalyzer.class.getName());
		this.modelInfo = modelInfo;
		this.graph = new DefaultDirectedGraph, DependencyEdge>(
				this.edgeFactory);
	}

	public DirectedGraph, DependencyEdge> build() {
		for (final MClassInfo info : modelInfo.getClassInfos()) {
			typeInfo(info.getPackageInfo(), info);
		}
		for (final MEnumLeafInfo info : modelInfo.getEnumLeafInfos()) {
			typeInfo(info.getPackageInfo(), info);
		}
		for (final MElementInfo info : modelInfo.getElementInfos()) {
			elementInfo(info);
		}
		return this.graph;
	}

	public PackageInfoVertex packageInfo(MPackageInfo info) {
		final PackageInfoVertex packageInfoVertex = new PackageInfoVertex(
				info);
		addInfoVertex(packageInfoVertex);
		return packageInfoVertex;
	}

	public ElementInfoVertex elementInfo(MElementInfo info) {
		final ElementInfoVertex elementInfoVertex = new ElementInfoVertex(
				info);
		if (addInfoVertex(elementInfoVertex)) {
			final TypeInfoVertex typeInfoVertex = typeInfo(
					info.getPackageInfo(), info.getTypeInfo());
			addHardDependency(elementInfoVertex, typeInfoVertex);

			final PackageInfoVertex packageInfoVertex = packageInfo(info
					.getPackageInfo());
			addSoftDependency(packageInfoVertex, elementInfoVertex);
		}
		return elementInfoVertex;

	}

	public TypeInfoVertex typeInfo(MPackageInfo packageInfo,
			MTypeInfo info) {
		return info.acceptTypeInfoVisitor(new TypeInfoGraphBuilder(this,
				packageInfo));
	}

	public PropertyInfoVertex propertyInfo(MPropertyInfo info) {
		final PropertyInfoVertex propertyInfoVertex = new PropertyInfoVertex(
				info);
		if (addInfoVertex(propertyInfoVertex)) {
			final MClassInfo classInfo = info.getClassInfo();
			final MPackageInfo packageInfo = classInfo.getPackageInfo();
			final TypeInfoVertex classInfoVertex = new TypeInfoVertex(
					packageInfo, classInfo);
			addHardDependency(propertyInfoVertex, classInfoVertex);
			info.acceptPropertyInfoVisitor(new PropertyInfoGraphBuilder(
					this, this.modelInfo, propertyInfoVertex));
		}
		return propertyInfoVertex;
	}

	public boolean addInfoVertex(InfoVertex vertex) {
		Validate.notNull(vertex);
		final boolean added = this.graph.addVertex(vertex);
		if (added) {
			logger.trace(MessageFormat.format("Added ({0}).", vertex.toString()));
		}
		return added;
	}

	public boolean addHardDependency(InfoVertex source,
			InfoVertex target) {
		Validate.notNull(source);
		Validate.notNull(target);
		final boolean added = this.graph.addEdge(source, target,
				new DependencyEdge(DependencyType.HARD));
		if (added) {
			logger.trace(MessageFormat.format(
					"Added hard dependency ({0})->({1}).", source.toString(),
					target.toString()));
		}
		return added;
	}

	public boolean addSoftDependency(InfoVertex source,
			InfoVertex target) {
		Validate.notNull(source);
		Validate.notNull(target);
		final boolean added = this.graph.addEdge(source, target,
				new DependencyEdge(DependencyType.SOFT));
		if (added) {
			logger.trace(MessageFormat.format(
					"Added soft dependency ({0})->({1}).", source.toString(),
					target.toString()));
		}
		return added;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy