1
1
package org .hisrc .jsonix .compilation .jsonschema ;
2
2
3
+ import javax .json .JsonBuilderFactory ;
3
4
import javax .xml .namespace .QName ;
4
5
5
6
import org .apache .commons .lang3 .Validate ;
6
7
import 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 ;
7
11
import org .hisrc .jsonix .definition .Mapping ;
8
12
import org .hisrc .jsonix .definition .Module ;
9
13
import org .hisrc .jsonix .definition .Modules ;
17
21
18
22
public class JsonSchemaMappingCompiler <T , C extends T > {
19
23
20
- private JsonSchemaModuleCompiler <T , C > moduleCompiler ;
21
24
private final Modules <T , C > modules ;
22
25
private final Module <T , C > module ;
23
26
private final Mapping <T , C > mapping ;
27
+ private final JsonBuilderFactory jsonBuilderFactory ;
24
28
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 );
28
34
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 ;
32
38
this .mapping = mapping ;
33
39
}
34
40
35
- public JsonSchemaModuleCompiler <T , C > getModuleCompiler () {
36
- return moduleCompiler ;
37
- }
38
-
39
41
public Modules <T , C > getModules () {
40
42
return modules ;
41
43
}
@@ -48,13 +50,17 @@ public Mapping<T, C> getMapping() {
48
50
return mapping ;
49
51
}
50
52
53
+ public JsonBuilderFactory getJsonBuilderFactory () {
54
+ return jsonBuilderFactory ;
55
+ }
56
+
51
57
public JsonSchemaBuilder compile () {
52
58
final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
53
59
final String schemaId = mapping .getSchemaId ();
54
60
schema .addId (schemaId );
55
61
addElementInfos (schema );
56
62
addClassInfoSchemas (schema );
57
- addElementLeafInfoSchemas (schema );
63
+ addEnumLeafInfoSchemas (schema );
58
64
return schema ;
59
65
}
60
66
@@ -70,70 +76,51 @@ private void addElementInfos(final JsonSchemaBuilder schema) {
70
76
.addRef (XmlSchemaJsonSchemaConstants .QNAME_TYPE_INFO_SCHEMA_REF );
71
77
final JsonSchemaBuilder nameConstant = new JsonSchemaBuilder ();
72
78
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 ));
87
86
88
87
elementInfoSchema .addProperty (JsonixConstants .VALUE_PROPERTY_NAME ,
89
88
createTypeInfoSchemaRef (elementInfo , typeInfo ));
90
89
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 ()));
98
95
if (scope != null ) {
99
- elementInfoSchema .add (
100
- JsonixJsonSchemaConstants .SCOPE_PROPERTY_NAME ,
96
+ elementInfoSchema .add (JsonixJsonSchemaConstants .SCOPE_PROPERTY_NAME ,
101
97
createTypeInfoSchemaRef (scope , scope ));
102
98
}
103
99
schema .addAnyOf (elementInfoSchema );
104
100
}
105
101
}
106
102
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 ) {
110
104
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 ),
116
108
enumLeafInfoSchema );
117
109
}
118
110
}
119
111
120
112
private void addClassInfoSchemas (final JsonSchemaBuilder schema ) {
121
- final JsonSchemaClassInfoProducer <T , C > classInfoCompiler = new JsonSchemaClassInfoProducer <T , C >(
122
- this );
123
113
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 ),
129
117
classInfoSchema );
130
118
}
131
119
}
132
120
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 );
138
125
}
139
126
}
0 commit comments