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

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

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

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.xml.namespace.QName;

import org.apache.commons.lang3.Validate;
import org.hisrc.jsonix.context.JsonixContext;
import org.jgrapht.DirectedGraph;
import org.jgrapht.traverse.BreadthFirstIterator;
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;

public class ModelInfoGraphAnalyzer {

	private final DirectedGraph, DependencyEdge> graph;

	private final Map packageInfoMap;
	private final Set packageNames;
	private final Collection packageInfos;
	private final Map> packageVertexMap;
	@SuppressWarnings("unused")
	private final Map>> packageVerticesMap;

	// private final ConnectivityInspector, DependencyEdge>
	// connectivityInspector;

	private final Map> typeInfoMap;
	private final Map> propertyInfoMap;
	private final Map> elementInfoMap;

	public ModelInfoGraphAnalyzer(JsonixContext context,
			MModelInfo modelInfo) {
		final DirectedGraph, DependencyEdge> graph = new ModelInfoGraphBuilder(
				context, modelInfo).build();

		this.packageVertexMap = createPackageVertexMap(graph);
		this.packageInfoMap = createPackageInfoMap(this.packageVertexMap);
		this.packageNames = this.packageInfoMap.keySet();
		this.packageInfos = this.packageInfoMap.values();
		this.packageVerticesMap = createPackageVerticesMap(graph);
		this.graph = graph;

		this.typeInfoMap = createTypeInfoMap(modelInfo);
		this.elementInfoMap = createElementInfoMap(modelInfo);
		this.propertyInfoMap = createPropertyInfoMap(modelInfo);
		// this.connectivityInspector = new ConnectivityInspector, DependencyEdge>(
		// this.graph);

		// for (PackageInfoVertex packageInfoVertex :
		// this.packageVertexMap
		// .values()) {
		// System.out
		// .println(packageInfoVertex
		// + " is connected to the following vertices in other packages:");
		// final Set> connectedVertices =
		// connectedSetOf(packageInfoVertex);
		// for (InfoVertex connectedVertex : connectedVertices) {
		// if (connectedVertex.getPackageInfo() != null
		// && connectedVertex.getPackageInfo() != packageInfoVertex
		// .getPackageInfo()) {
		// System.out.println("          " + connectedVertex);
		// }
		// }
		// }
	}

	private Map> createElementInfoMap(
			MModelInfo modelInfo) {
		final Map> elementInfoMap = new HashMap>();
		for (MElementInfo elementInfo : modelInfo.getElementInfos()) {
			final QName elementName = elementInfo.getElementName();
			if (elementInfo.getScope() == null) {
				elementInfoMap.put(elementName, elementInfo);
			} else {
				final MElementInfo existingElementinfo = elementInfoMap
						.get(elementName);
				if (existingElementinfo == null) {
					elementInfoMap.put(elementName, elementInfo);
				}
			}
		}
		return elementInfoMap;
	}

	private Map> createTypeInfoMap(
			MModelInfo modelInfo) {
		final Map> typeInfoMap = new HashMap>();
		for (MClassInfo classInfo : modelInfo.getClassInfos()) {
			typeInfoMap.put(classInfo.getName(), classInfo);
		}
		for (MEnumLeafInfo enumLeafInfo : modelInfo.getEnumLeafInfos()) {
			typeInfoMap.put(enumLeafInfo.getName(), enumLeafInfo);
		}
		return typeInfoMap;
	}

	private Map> createPropertyInfoMap(
			MModelInfo modelInfo) {
		final Map> propertyInfoMap = new HashMap>();
		for (MClassInfo classInfo : modelInfo.getClassInfos()) {
			for (MPropertyInfo propertyInfo : classInfo.getProperties()) {
				final String name = classInfo.getName() + "."
						+ propertyInfo.getPrivateName();
				propertyInfoMap.put(name, propertyInfo);
			}
		}
		return propertyInfoMap;
	}

	public DirectedGraph, DependencyEdge> getGraph() {
		return graph;
	}

	public Collection getPackageInfos() {
		return packageInfos;
	}

	public Set getPackageNames() {
		return packageNames;
	}

	public Map getPackageInfoMap() {
		return packageInfoMap;
	}

	private Map createPackageInfoMap(
			Map> packageVertexMap) {
		Map packageInfoMap = new HashMap();
		for (Entry> entry : packageVertexMap
				.entrySet()) {
			packageInfoMap.put(entry.getKey(), entry.getValue()
					.getPackageInfo());
		}
		return packageInfoMap;
	}

	private Map> createPackageVertexMap(
			DirectedGraph, DependencyEdge> graph) {
		final Map> packageVertexMap = new HashMap>();

		for (InfoVertex vertex : graph.vertexSet()) {

			vertex.accept(new DefaultInfoVertexVisitor() {
				@Override
				public Void visitPackageInfoVertex(
						PackageInfoVertex vertex) {
					final MPackageInfo packageInfo = vertex.getPackageInfo();
					packageVertexMap.put(packageInfo.getPackageName(), vertex);
					return null;
				}
			});
		}

		return packageVertexMap;
	}

	private Map>> createPackageVerticesMap(
			DirectedGraph, DependencyEdge> graph) {
		final Map>> packageVerticesMap = new HashMap>>();
		for (InfoVertex vertex : graph.vertexSet()) {
			final MPackageInfo packageInfo = vertex.getPackageInfo();

			if (packageInfo != null) {
				Set> packageVertices = packageVerticesMap
						.get(packageInfo.getPackageName());
				if (packageVertices == null) {
					packageVertices = new HashSet>();
					packageVerticesMap.put(packageInfo.getPackageName(),
							packageVertices);
				}
				packageVertices.add(vertex);
			}
		}
		return packageVerticesMap;
	}

	Map, Set>> vertexToConnectedSet = new HashMap, Set>>();

	public Set> connectedSetOf(InfoVertex vertex) {
		Set> connectedSet = vertexToConnectedSet.get(vertex);

		if (connectedSet == null) {
			connectedSet = new HashSet>();

			final BreadthFirstIterator, DependencyEdge> i = new BreadthFirstIterator, DependencyEdge>(
					graph, vertex);

			while (i.hasNext()) {
				connectedSet.add(i.next());
			}

			vertexToConnectedSet.put(vertex, connectedSet);
		}

		return connectedSet;
	}

	public MTypeInfo findTypeInfoByName(MPackageInfo packageInfo,
			String name) {
		Validate.notNull(packageInfo);
		Validate.notNull(name);
		final String typeInfoName = packageInfo.getPackagedName(name);
		return this.typeInfoMap.get(typeInfoName);
	}

	public MPropertyInfo findPropertyInfoByName(MPackageInfo packageInfo,
			String name) {
		Validate.notNull(packageInfo);
		Validate.notNull(name);
		final String propertyInfoName = packageInfo.getPackageName() + "."
				+ name;
		return this.propertyInfoMap.get(propertyInfoName);
	}

	public MElementInfo findElementInfoByQName(QName name) {
		Validate.notNull(name);
		return this.elementInfoMap.get(name);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy