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

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

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

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.commons.lang3.Validate;
import org.hisrc.jsonix.JsonixConstants;
import org.hisrc.jsonix.definition.Mapping;
import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder;
import org.hisrc.jsonix.jsonschema.JsonSchemaConstants;
import org.hisrc.jsonix.naming.StandardNaming;
import org.hisrc.jsonix.xml.xsom.ParticleMultiplicityCounter;
import org.hisrc.xml.xsom.XSFunctionApplier;
import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MClassTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfo;

import com.sun.tools.xjc.model.Multiplicity;

public class JsonSchemaClassInfoProducer implements
		JsonSchemaTypeInfoProducer, T, C> {

	private final JsonSchemaMappingCompiler mappingCompiler;
	private final Mapping mapping;
	private final XSFunctionApplier multiplicityCounter = new XSFunctionApplier(
			ParticleMultiplicityCounter.INSTANCE);

	public JsonSchemaClassInfoProducer(
			JsonSchemaMappingCompiler mappingCompiler) {
		Validate.notNull(mappingCompiler);
		this.mappingCompiler = mappingCompiler;
		this.mapping = mappingCompiler.getMapping();
	}

	public JsonSchemaMappingCompiler getMappingCompiler() {
		return mappingCompiler;
	}

	@Override
	public JsonSchemaBuilder produce(MClassInfo classInfo) {
		final JsonSchemaBuilder classInfoSchema = new JsonSchemaBuilder();
		classInfoSchema.addType(JsonSchemaConstants.OBJECT_TYPE);
		final String localName = classInfo
				.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER);
		classInfoSchema.addTitle(localName);
		final MClassTypeInfo baseTypeInfo = classInfo.getBaseTypeInfo();
		final JsonSchemaBuilder typeInfoSchema;
		if (baseTypeInfo != null) {
			final JsonSchemaBuilder baseTypeInfoSchema = mappingCompiler
					.createTypeInfoSchemaRef(baseTypeInfo, baseTypeInfo);
			typeInfoSchema = new JsonSchemaBuilder();
			typeInfoSchema.addAllOf(baseTypeInfoSchema);
			typeInfoSchema.addAllOf(classInfoSchema);
		} else {
			typeInfoSchema = classInfoSchema;
		}

		final Map propertyInfoSchemas = compilePropertyInfos(classInfo);
		final List propertiesOrder = new ArrayList(
				propertyInfoSchemas.size());
		propertiesOrder.addAll(propertyInfoSchemas.keySet());
		classInfoSchema.addProperties(propertyInfoSchemas);
		for (MPropertyInfo propertyInfo : classInfo.getProperties()) {
			final Multiplicity multiplicity = multiplicityCounter
					.apply(propertyInfo.getOrigin());
			if (multiplicity != null && multiplicity.min != null
					&& multiplicity.min.compareTo(BigInteger.ZERO) > 0) {
				typeInfoSchema.addRequired(propertyInfo.getPrivateName());
			}
		}
		typeInfoSchema.add(JsonixJsonSchemaConstants.TYPE_TYPE_PROPERTY_NAME,
				StandardNaming.CLASS_INFO);
		final QName typeName = classInfo.getTypeName();
		if (typeName != null) {
			typeInfoSchema
					.add(JsonixJsonSchemaConstants.TYPE_NAME_PROPERTY_NAME,
							new JsonSchemaBuilder()
									.add(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME,
											typeName.getLocalPart())
									.add(JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME,
											typeName.getNamespaceURI()));
		}
		if (!propertiesOrder.isEmpty()) {
			typeInfoSchema.add("propertiesOrder", propertiesOrder);
		}
		return typeInfoSchema;
	}

	private Map compilePropertyInfos(
			MClassInfo classInfo) {
		final Map propertyInfoSchemas = new LinkedHashMap(
				classInfo.getProperties().size());
		for (MPropertyInfo propertyInfo : classInfo.getProperties()) {
			if (mapping.getPropertyInfos().contains(propertyInfo)) {
				propertyInfoSchemas
						.put(propertyInfo.getPrivateName(),
								propertyInfo
										.acceptPropertyInfoVisitor(new JsonSchemaPropertyInfoProducerVisitor(
												this)));
			}
		}
		return propertyInfoSchemas;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy