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

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

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

import javax.xml.namespace.QName;

import org.apache.commons.lang3.Validate;
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyAttributePropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyElementPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MAttributePropertyInfo;
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.MElementPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefsPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementsPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MModelInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfoVisitor;
import org.jvnet.jaxb2_commons.xml.bind.model.MTyped;
import org.jvnet.jaxb2_commons.xml.bind.model.MValuePropertyInfo;

public final class PropertyInfoGraphBuilder implements
		MPropertyInfoVisitor> {

	private final ModelInfoGraphBuilder modelInfoGraphBuilder;
	private final MModelInfo modelInfo;
	private final PropertyInfoVertex vertex;

	public PropertyInfoGraphBuilder(
			ModelInfoGraphBuilder modelInfoGraphBuilder,
			MModelInfo modelInfo, PropertyInfoVertex vertex) {
		Validate.notNull(modelInfoGraphBuilder);
		Validate.notNull(modelInfo);
		Validate.notNull(vertex);
		this.modelInfoGraphBuilder = modelInfoGraphBuilder;
		this.modelInfo = modelInfo;
		this.vertex = vertex;
	}

	private void addHardDependencyOnType(
			PropertyInfoVertex propertyInfoVertex, MTyped info) {
		final InfoVertex typeInfoVertex = modelInfoGraphBuilder.typeInfo(
				propertyInfoVertex.getPackageInfo(), info.getTypeInfo());
		modelInfoGraphBuilder.addHardDependency(propertyInfoVertex,
				typeInfoVertex);
	}

	private void addSoftDependencyOnType(
			PropertyInfoVertex propertyInfoVertex, MTyped info) {
		final InfoVertex typeInfoVertex = modelInfoGraphBuilder.typeInfo(
				propertyInfoVertex.getPackageInfo(), info.getTypeInfo());
		modelInfoGraphBuilder.addSoftDependency(propertyInfoVertex,
				typeInfoVertex);
	}

	@Override
	public PropertyInfoVertex visitValuePropertyInfo(
			MValuePropertyInfo info) {
		// Value property depends on its type
		addHardDependencyOnType(vertex, info);
		return vertex;
	}

	@Override
	public PropertyInfoVertex visitAttributePropertyInfo(
			MAttributePropertyInfo info) {
		// Attribute property depends on its type
		addHardDependencyOnType(vertex, info);
		return vertex;
	}

	@Override
	public PropertyInfoVertex visitAnyAttributePropertyInfo(
			MAnyAttributePropertyInfo info) {
		return vertex;
	}

	@Override
	public PropertyInfoVertex visitAnyElementPropertyInfo(
			MAnyElementPropertyInfo info) {
		return vertex;
	}

	@Override
	public PropertyInfoVertex visitElementPropertyInfo(
			MElementPropertyInfo info) {
		// Element property depends on its type
		addHardDependencyOnType(vertex, info);
		return vertex;
	}

	@Override
	public PropertyInfoVertex visitElementsPropertyInfo(
			MElementsPropertyInfo info) {
		// Elements property has a "soft" dependency types of its elements
		// That is, some of these types may be excluded
		for (MElementTypeInfo elementTypeInfo : info
				.getElementTypeInfos()) {
			addSoftDependencyOnType(vertex, elementTypeInfo);
		}
		return vertex;
	}

	@Override
	public PropertyInfoVertex visitElementRefPropertyInfo(
			MElementRefPropertyInfo info) {
		// Element reference property depends on the type of its element
		addHardDependencyOnType(vertex, info);
		addSubstitutionHeadDependencies(info, vertex);
		return vertex;
	}

	@Override
	public PropertyInfoVertex visitElementRefsPropertyInfo(
			MElementRefsPropertyInfo info) {
		for (MElementTypeInfo elementTypeInfo : info
				.getElementTypeInfos()) {
			// Element references property has "soft" dependencies on the
			// types
			// of its elements
			addSoftDependencyOnType(vertex, elementTypeInfo);
			addSubstitutionHeadDependencies(elementTypeInfo, vertex);
		}
		return vertex;
	}

	private void addSubstitutionHeadDependencies(
			MElementTypeInfo elementTypeInfo,
			PropertyInfoVertex propertyInfoVertex) {
		final MClassInfo classInfo = propertyInfoVertex.getClassInfo();
		final QName elementName = elementTypeInfo.getElementName();
		for (MElementInfo elementInfo : this.modelInfo.getElementInfos()) {
			if (elementName.equals(elementInfo.getSubstitutionHead())
					&& (elementInfo.getScope() == null || elementInfo
							.getScope() == classInfo)) {
				final InfoVertex elementInfoVertex = modelInfoGraphBuilder
						.elementInfo(elementInfo);
				modelInfoGraphBuilder.addSoftDependency(elementInfoVertex,
						propertyInfoVertex);
				modelInfoGraphBuilder.addSoftDependency(propertyInfoVertex, elementInfoVertex);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy