|
| 1 | +package org.hisrc.jsonix.compilation.jsonschema; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import javax.json.JsonValue; |
| 6 | + |
| 7 | +import org.hisrc.jsonix.compilation.jsonschema.typeinfo.TypeInfoProducer; |
| 8 | +import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder; |
| 9 | +import org.hisrc.jsonix.xml.xsom.CollectEnumerationValuesVisitor; |
| 10 | +import org.hisrc.xml.xsom.SchemaComponentAware; |
| 11 | +import org.jvnet.jaxb2_commons.xml.bind.model.MBuiltinLeafInfo; |
| 12 | +import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo; |
| 13 | +import org.jvnet.jaxb2_commons.xml.bind.model.origin.MOriginated; |
| 14 | +import org.jvnet.jaxb2_commons.xml.bind.model.util.DefaultTypeInfoVisitor; |
| 15 | + |
| 16 | +import com.sun.xml.xsom.XSComponent; |
| 17 | +import com.sun.xml.xsom.XmlString; |
| 18 | + |
| 19 | +public class CreateTypeInfoSchema<T, C extends T, M extends MOriginated<O>, O> |
| 20 | + extends DefaultTypeInfoVisitor<T, C, JsonSchemaBuilder> { |
| 21 | + private final M info; |
| 22 | + private JsonSchemaMappingCompiler<T, C> mappingCompiler; |
| 23 | + |
| 24 | + public CreateTypeInfoSchema(JsonSchemaMappingCompiler<T, C> mappingCompiler, M info) { |
| 25 | + this.mappingCompiler = mappingCompiler; |
| 26 | + this.info = info; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public JsonSchemaBuilder visitTypeInfo(MTypeInfo<T, C> typeInfo) { |
| 31 | + final TypeInfoProducer<T, C> typeInfoCompiler = mappingCompiler.getTypeInfoProducer(info, typeInfo); |
| 32 | + return typeInfoCompiler.createTypeInfoSchemaRef(mappingCompiler); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public JsonSchemaBuilder visitBuiltinLeafInfo(MBuiltinLeafInfo<T, C> typeInfo) { |
| 37 | + final TypeInfoProducer<T, C> typeInfoCompiler = mappingCompiler.getTypeInfoProducer(info, typeInfo); |
| 38 | + final JsonSchemaBuilder typeInfoSchemaRef = typeInfoCompiler.createTypeInfoSchemaRef(mappingCompiler); |
| 39 | + |
| 40 | + if (info instanceof MOriginated) { |
| 41 | + MOriginated<?> originated = (MOriginated<?>) info; |
| 42 | + Object origin = originated.getOrigin(); |
| 43 | + if (origin instanceof SchemaComponentAware) { |
| 44 | + final XSComponent component = ((SchemaComponentAware) origin).getSchemaComponent(); |
| 45 | + if (component != null) { |
| 46 | + |
| 47 | + final CollectEnumerationValuesVisitor collectEnumerationValuesVisitor = new CollectEnumerationValuesVisitor(); |
| 48 | + component.visit(collectEnumerationValuesVisitor); |
| 49 | + final List<XmlString> enumerationValues = collectEnumerationValuesVisitor.getValues(); |
| 50 | + if (enumerationValues != null && !enumerationValues.isEmpty()) { |
| 51 | + final JsonSchemaBuilder values = new JsonSchemaBuilder(); |
| 52 | + boolean valueSupported = true; |
| 53 | + for (XmlString enumerationValue : enumerationValues) { |
| 54 | + final JsonValue value = typeInfoCompiler.createValue(this.mappingCompiler, |
| 55 | + enumerationValue); |
| 56 | + if (value == null) { |
| 57 | + valueSupported = false; |
| 58 | + break; |
| 59 | + } else { |
| 60 | + values.addEnum(value); |
| 61 | + } |
| 62 | + } |
| 63 | + if (valueSupported) { |
| 64 | + final JsonSchemaBuilder typeInfoSchemaRefWithEnums = new JsonSchemaBuilder(); |
| 65 | + typeInfoSchemaRefWithEnums.addAllOf(typeInfoSchemaRef); |
| 66 | + typeInfoSchemaRefWithEnums.addAllOf(values); |
| 67 | + return typeInfoSchemaRefWithEnums; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return typeInfoSchemaRef; |
| 74 | + } |
| 75 | +} |
0 commit comments