11package org .hisrc .jsonix .compilation .jsonschema ;
22
3+ import javax .json .JsonBuilderFactory ;
34import javax .xml .namespace .QName ;
45
56import org .apache .commons .lang3 .Validate ;
67import org .hisrc .jsonix .JsonixConstants ;
8+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .ClassInfoProducer ;
9+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .CreateTypeInfoProducer ;
10+ import org .hisrc .jsonix .compilation .jsonschema .typeinfo .EnumLeafInfoProducer ;
711import org .hisrc .jsonix .definition .Mapping ;
812import org .hisrc .jsonix .definition .Module ;
913import org .hisrc .jsonix .definition .Modules ;
1721
1822public class JsonSchemaMappingCompiler <T , C extends T > {
1923
20- private JsonSchemaModuleCompiler <T , C > moduleCompiler ;
2124 private final Modules <T , C > modules ;
2225 private final Module <T , C > module ;
2326 private final Mapping <T , C > mapping ;
27+ private final JsonBuilderFactory jsonBuilderFactory ;
2428
25- public JsonSchemaMappingCompiler (
26- JsonSchemaModuleCompiler <T , C > moduleCompiler , Mapping <T , C > mapping ) {
27- Validate .notNull (moduleCompiler );
29+ public JsonSchemaMappingCompiler (JsonBuilderFactory jsonBuilderFactory , Modules <T , C > modules , Module <T , C > module ,
30+ Mapping <T , C > mapping ) {
31+ Validate .notNull (jsonBuilderFactory );
32+ Validate .notNull (modules );
33+ Validate .notNull (module );
2834 Validate .notNull (mapping );
29- this .moduleCompiler = moduleCompiler ;
30- this .modules = moduleCompiler . getModules () ;
31- this .module = moduleCompiler . getModule () ;
35+ this .jsonBuilderFactory = jsonBuilderFactory ;
36+ this .modules = modules ;
37+ this .module = module ;
3238 this .mapping = mapping ;
3339 }
3440
35- public JsonSchemaModuleCompiler <T , C > getModuleCompiler () {
36- return moduleCompiler ;
37- }
38-
3941 public Modules <T , C > getModules () {
4042 return modules ;
4143 }
@@ -48,13 +50,17 @@ public Mapping<T, C> getMapping() {
4850 return mapping ;
4951 }
5052
53+ public JsonBuilderFactory getJsonBuilderFactory () {
54+ return jsonBuilderFactory ;
55+ }
56+
5157 public JsonSchemaBuilder compile () {
5258 final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
5359 final String schemaId = mapping .getSchemaId ();
5460 schema .addId (schemaId );
5561 addElementInfos (schema );
5662 addClassInfoSchemas (schema );
57- addElementLeafInfoSchemas (schema );
63+ addEnumLeafInfoSchemas (schema );
5864 return schema ;
5965 }
6066
@@ -70,70 +76,51 @@ private void addElementInfos(final JsonSchemaBuilder schema) {
7076 .addRef (XmlSchemaJsonSchemaConstants .QNAME_TYPE_INFO_SCHEMA_REF );
7177 final JsonSchemaBuilder nameConstant = new JsonSchemaBuilder ();
7278 nameConstant .addType (JsonSchemaConstants .OBJECT_TYPE );
73- nameConstant
74- .addProperty (
75- JsonixJsonSchemaConstants .LOCAL_PART_PROPERTY_NAME ,
76- new JsonSchemaBuilder ().addEnum (elementName
77- .getLocalPart ()));
78- nameConstant .addProperty (
79- JsonixJsonSchemaConstants .NAMESPACE_URI_PROPERTY_NAME ,
80- new JsonSchemaBuilder ().addEnum (elementName
81- .getNamespaceURI ()));
82-
83- elementInfoSchema .addProperty (
84- JsonixConstants .NAME_PROPERTY_NAME ,
85- new JsonSchemaBuilder ().addAllOf (qNameRef ).addAllOf (
86- nameConstant ));
79+ nameConstant .addProperty (JsonixJsonSchemaConstants .LOCAL_PART_PROPERTY_NAME ,
80+ new JsonSchemaBuilder ().addEnum (elementName .getLocalPart ()));
81+ nameConstant .addProperty (JsonixJsonSchemaConstants .NAMESPACE_URI_PROPERTY_NAME ,
82+ new JsonSchemaBuilder ().addEnum (elementName .getNamespaceURI ()));
83+
84+ elementInfoSchema .addProperty (JsonixConstants .NAME_PROPERTY_NAME ,
85+ new JsonSchemaBuilder ().addAllOf (qNameRef ).addAllOf (nameConstant ));
8786
8887 elementInfoSchema .addProperty (JsonixConstants .VALUE_PROPERTY_NAME ,
8988 createTypeInfoSchemaRef (elementInfo , typeInfo ));
9089
91- elementInfoSchema
92- .add (JsonixJsonSchemaConstants .ELEMENT_NAME_PROPERTY_NAME ,
93- new JsonSchemaBuilder ()
94- .add (JsonixJsonSchemaConstants .LOCAL_PART_PROPERTY_NAME ,
95- elementName .getLocalPart ())
96- .add (JsonixJsonSchemaConstants .NAMESPACE_URI_PROPERTY_NAME ,
97- elementName .getNamespaceURI ()));
90+ elementInfoSchema .add (JsonixJsonSchemaConstants .ELEMENT_NAME_PROPERTY_NAME ,
91+ new JsonSchemaBuilder ()
92+ .add (JsonixJsonSchemaConstants .LOCAL_PART_PROPERTY_NAME , elementName .getLocalPart ()).add (
93+ JsonixJsonSchemaConstants .NAMESPACE_URI_PROPERTY_NAME ,
94+ elementName .getNamespaceURI ()));
9895 if (scope != null ) {
99- elementInfoSchema .add (
100- JsonixJsonSchemaConstants .SCOPE_PROPERTY_NAME ,
96+ elementInfoSchema .add (JsonixJsonSchemaConstants .SCOPE_PROPERTY_NAME ,
10197 createTypeInfoSchemaRef (scope , scope ));
10298 }
10399 schema .addAnyOf (elementInfoSchema );
104100 }
105101 }
106102
107- private void addElementLeafInfoSchemas (final JsonSchemaBuilder schema ) {
108- final JsonSchemaEnumLeafInfoProducer <T , C > enumLeafInfoCompiler = new JsonSchemaEnumLeafInfoProducer <T , C >(
109- this );
103+ private void addEnumLeafInfoSchemas (final JsonSchemaBuilder schema ) {
110104 for (MEnumLeafInfo <T , C > enumLeafInfo : mapping .getEnumLeafInfos ()) {
111- final JsonSchemaBuilder enumLeafInfoSchema = enumLeafInfoCompiler
112- .produce (enumLeafInfo );
113- schema .addDefinition (
114- enumLeafInfo
115- .getContainerLocalName (JsonixConstants .DEFAULT_SCOPED_NAME_DELIMITER ),
105+ final EnumLeafInfoProducer <T , C > enumLeafInfoCompiler = new EnumLeafInfoProducer <T , C >(enumLeafInfo );
106+ final JsonSchemaBuilder enumLeafInfoSchema = enumLeafInfoCompiler .compile (this );
107+ schema .addDefinition (enumLeafInfo .getContainerLocalName (JsonixConstants .DEFAULT_SCOPED_NAME_DELIMITER ),
116108 enumLeafInfoSchema );
117109 }
118110 }
119111
120112 private void addClassInfoSchemas (final JsonSchemaBuilder schema ) {
121- final JsonSchemaClassInfoProducer <T , C > classInfoCompiler = new JsonSchemaClassInfoProducer <T , C >(
122- this );
123113 for (MClassInfo <T , C > classInfo : mapping .getClassInfos ()) {
124- final JsonSchemaBuilder classInfoSchema = classInfoCompiler
125- .produce (classInfo );
126- schema .addDefinition (
127- classInfo
128- .getContainerLocalName (JsonixConstants .DEFAULT_SCOPED_NAME_DELIMITER ),
114+ final ClassInfoProducer <T , C > classInfoCompiler = new ClassInfoProducer <T , C >(classInfo );
115+ final JsonSchemaBuilder classInfoSchema = classInfoCompiler .compile (this );
116+ schema .addDefinition (classInfo .getContainerLocalName (JsonixConstants .DEFAULT_SCOPED_NAME_DELIMITER ),
129117 classInfoSchema );
130118 }
131119 }
132120
133- public <M extends MOriginated <O >, O > JsonSchemaBuilder createTypeInfoSchemaRef (
134- M originated , MTypeInfo <T , C > typeInfo ) {
135- return typeInfo
136- .acceptTypeInfoVisitor (new JsonSchemaRefTypeInfoProducerVisitor <T , C , O >(
137- this , originated ));
121+ public <M extends MOriginated <O >, O > JsonSchemaBuilder createTypeInfoSchemaRef (M originated ,
122+ MTypeInfo <T , C > typeInfo ) {
123+ return typeInfo .acceptTypeInfoVisitor (new CreateTypeInfoProducer <T , C , O >(originated ))
124+ .createTypeInfoSchemaRef (this );
138125 }
139126}
0 commit comments