org.hisrc.jsonix.compilation.jsonschema.JsonSchemaMappingCompiler Maven / Gradle / Ivy
package org.hisrc.jsonix.compilation.jsonschema;
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.definition.Module;
import org.hisrc.jsonix.definition.Modules;
import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder;
import org.hisrc.jsonix.jsonschema.JsonSchemaConstants;
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.MTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.origin.MOriginated;
public class JsonSchemaMappingCompiler {
private JsonSchemaModuleCompiler moduleCompiler;
private final Modules modules;
private final Module module;
private final Mapping mapping;
public JsonSchemaMappingCompiler(
JsonSchemaModuleCompiler moduleCompiler, Mapping mapping) {
Validate.notNull(moduleCompiler);
Validate.notNull(mapping);
this.moduleCompiler = moduleCompiler;
this.modules = moduleCompiler.getModules();
this.module = moduleCompiler.getModule();
this.mapping = mapping;
}
public JsonSchemaModuleCompiler getModuleCompiler() {
return moduleCompiler;
}
public Modules getModules() {
return modules;
}
public Module getModule() {
return module;
}
public Mapping getMapping() {
return mapping;
}
public JsonSchemaBuilder compile() {
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
final String schemaId = mapping.getSchemaId();
schema.addId(schemaId);
addElementInfos(schema);
addClassInfoSchemas(schema);
addElementLeafInfoSchemas(schema);
return schema;
}
private void addElementInfos(final JsonSchemaBuilder schema) {
for (MElementInfo elementInfo : mapping.getElementInfos()) {
final QName elementName = elementInfo.getElementName();
final MTypeInfo typeInfo = elementInfo.getTypeInfo();
final MClassInfo scope = elementInfo.getScope();
final JsonSchemaBuilder elementInfoSchema = new JsonSchemaBuilder();
elementInfoSchema.addType(JsonSchemaConstants.OBJECT_TYPE);
final JsonSchemaBuilder qNameRef = new JsonSchemaBuilder()
.addRef(XmlSchemaJsonSchemaConstants.QNAME_TYPE_INFO_SCHEMA_REF);
final JsonSchemaBuilder nameConstant = new JsonSchemaBuilder();
nameConstant.addType(JsonSchemaConstants.OBJECT_TYPE);
nameConstant
.addProperty(
JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME,
new JsonSchemaBuilder().addEnum(elementName
.getLocalPart()));
nameConstant.addProperty(
JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME,
new JsonSchemaBuilder().addEnum(elementName
.getNamespaceURI()));
elementInfoSchema.addProperty(
JsonixConstants.NAME_PROPERTY_NAME,
new JsonSchemaBuilder().addAllOf(qNameRef).addAllOf(
nameConstant));
elementInfoSchema.addProperty(JsonixConstants.VALUE_PROPERTY_NAME,
createTypeInfoSchemaRef(elementInfo, typeInfo));
elementInfoSchema
.add(JsonixJsonSchemaConstants.ELEMENT_NAME_PROPERTY_NAME,
new JsonSchemaBuilder()
.add(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME,
elementName.getLocalPart())
.add(JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME,
elementName.getNamespaceURI()));
if (scope != null) {
elementInfoSchema.add(
JsonixJsonSchemaConstants.SCOPE_PROPERTY_NAME,
createTypeInfoSchemaRef(scope, scope));
}
schema.addAnyOf(elementInfoSchema);
}
}
private void addElementLeafInfoSchemas(final JsonSchemaBuilder schema) {
final JsonSchemaEnumLeafInfoProducer enumLeafInfoCompiler = new JsonSchemaEnumLeafInfoProducer(
this);
for (MEnumLeafInfo enumLeafInfo : mapping.getEnumLeafInfos()) {
final JsonSchemaBuilder enumLeafInfoSchema = enumLeafInfoCompiler
.produce(enumLeafInfo);
schema.addDefinition(
enumLeafInfo
.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER),
enumLeafInfoSchema);
}
}
private void addClassInfoSchemas(final JsonSchemaBuilder schema) {
final JsonSchemaClassInfoProducer classInfoCompiler = new JsonSchemaClassInfoProducer(
this);
for (MClassInfo classInfo : mapping.getClassInfos()) {
final JsonSchemaBuilder classInfoSchema = classInfoCompiler
.produce(classInfo);
schema.addDefinition(
classInfo
.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER),
classInfoSchema);
}
}
public , O> JsonSchemaBuilder createTypeInfoSchemaRef(
M originated, MTypeInfo typeInfo) {
return typeInfo
.acceptTypeInfoVisitor(new JsonSchemaRefTypeInfoProducerVisitor(
this, originated));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy