11package org .hisrc .jsonix .compilation .jsc ;
22
3+ import java .util .ArrayList ;
34import java .util .List ;
45
56import javax .xml .namespace .QName ;
@@ -45,13 +46,14 @@ public JsonSchemaBuilder visitElementPropertyInfo(
4546 final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
4647 addPropertyInfoSchema (info , schema );
4748 addPropertyInfoTypeSchema (StandardNaming .ELEMENT , schema );
48- addWrappableSchema (info , schema );
4949 addElementNameSchema (info .getElementName (), schema );
50+ addWrappableSchema (info , schema );
51+
5052 final JsonSchemaBuilder itemTypeSchema = createTypeSchema (info
5153 .getTypeInfo ());
5254 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
5355 info .isCollection (), itemTypeSchema );
54- schema .addAnyOf (typeSchema );
56+ schema .addAllOf (typeSchema );
5557 return schema ;
5658 }
5759
@@ -62,39 +64,43 @@ public JsonSchemaBuilder visitElementsPropertyInfo(
6264 addPropertyInfoSchema (info , schema );
6365 addPropertyInfoTypeSchema (StandardNaming .ELEMENTS , schema );
6466 addWrappableSchema (info , schema );
67+
6568 final JsonSchemaBuilder itemTypeSchema = createElementTypeInfosSchema (info );
6669 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
6770 info .isCollection (), itemTypeSchema );
68- schema .addAnyOf (typeSchema );
71+ schema .addAllOf (typeSchema );
6972 return schema ;
7073 }
7174
7275 @ Override
7376 public JsonSchemaBuilder visitElementRefPropertyInfo (
7477 MElementRefPropertyInfo <T , C > info ) {
78+
7579 final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
7680 addPropertyInfoSchema (info , schema );
7781 addPropertyInfoTypeSchema (StandardNaming .ELEMENT_REF , schema );
78- addWrappableSchema (info , schema );
7982 addElementNameSchema (info .getElementName (), schema );
83+ addWrappableSchema (info , schema );
8084
81- final JsonSchemaBuilder itemTypeSchema = new JsonSchemaBuilder ();
85+ final List <JsonSchemaBuilder > itemTypeSchemas = new ArrayList <JsonSchemaBuilder >(
86+ 3 );
8287 if (info .isMixed ()) {
83- itemTypeSchema
84- .addAnyOf (new JsonSchemaBuilder ()
88+ itemTypeSchemas
89+ .add (new JsonSchemaBuilder ()
8590 .addType (XmlSchemaJsonSchemaConstants .STRING_TYPE_INFO_SCHEMA_REF ));
8691 }
8792 if (info .isDomAllowed ()) {
88- itemTypeSchema
89- .addAnyOf (new JsonSchemaBuilder ()
93+ itemTypeSchemas
94+ .add (new JsonSchemaBuilder ()
9095 .addType (JsonixJsonSchemaConstants .DOM_TYPE_INFO_SCHEMA_REF ));
9196 }
9297 if (info .isTypedObjectAllowed ()) {
93- itemTypeSchema . addAnyOf (createElementRefSchema (info ));
98+ itemTypeSchemas . add (createElementRefSchema (info ));
9499 }
95100 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
96- info .isCollection (), itemTypeSchema );
97- schema .addAnyOf (typeSchema );
101+ info .isCollection (),
102+ createPossiblyAnyOfTypeSchema (itemTypeSchemas ));
103+ schema .addAllOf (typeSchema );
98104 return schema ;
99105 }
100106
@@ -106,23 +112,25 @@ public JsonSchemaBuilder visitElementRefsPropertyInfo(
106112 addPropertyInfoTypeSchema (StandardNaming .ELEMENT_REFS , schema );
107113 addWrappableSchema (info , schema );
108114
109- final JsonSchemaBuilder itemTypeSchema = new JsonSchemaBuilder ();
115+ final List <JsonSchemaBuilder > itemTypeSchemas = new ArrayList <JsonSchemaBuilder >(
116+ 2 + info .getElementTypeInfos ().size ());
110117 if (info .isMixed ()) {
111- itemTypeSchema
112- .addAnyOf (new JsonSchemaBuilder ()
118+ itemTypeSchemas
119+ .add (new JsonSchemaBuilder ()
113120 .addType (XmlSchemaJsonSchemaConstants .STRING_TYPE_INFO_SCHEMA_REF ));
114121 }
115122 if (info .isDomAllowed ()) {
116- itemTypeSchema
117- .addAnyOf (new JsonSchemaBuilder ()
123+ itemTypeSchemas
124+ .add (new JsonSchemaBuilder ()
118125 .addType (JsonixJsonSchemaConstants .DOM_TYPE_INFO_SCHEMA_REF ));
119126 }
120127 if (info .isTypedObjectAllowed ()) {
121- itemTypeSchema . addAnyOf (createElementRefsSchema (info ));
128+ itemTypeSchemas . addAll (createElementRefsSchema (info ));
122129 }
123130 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
124- info .isCollection (), itemTypeSchema );
125- schema .addAnyOf (typeSchema );
131+ info .isCollection (),
132+ createPossiblyAnyOfTypeSchema (itemTypeSchemas ));
133+ schema .addAllOf (typeSchema );
126134 return schema ;
127135 }
128136
@@ -132,30 +140,34 @@ public JsonSchemaBuilder visitValuePropertyInfo(
132140 final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
133141 addPropertyInfoSchema (info , schema );
134142 addPropertyInfoTypeSchema (StandardNaming .VALUE , schema );
143+
135144 final JsonSchemaBuilder itemTypeSchema = createTypeSchema (info
136145 .getTypeInfo ());
137146 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
138147 info .isCollection (), itemTypeSchema );
139- schema .addAnyOf (typeSchema );
148+ schema .addAllOf (typeSchema );
140149 return schema ;
141150 }
142151
143152 @ Override
144153 public JsonSchemaBuilder visitAnyElementPropertyInfo (
145154 MAnyElementPropertyInfo <T , C > info ) {
155+
146156 final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
147157 addPropertyInfoSchema (info , schema );
148158 addPropertyInfoTypeSchema (StandardNaming .ANY_ELEMENT , schema );
149159
150- final JsonSchemaBuilder itemTypeSchema = new JsonSchemaBuilder ();
160+ final List <JsonSchemaBuilder > itemTypeSchemas = new ArrayList <JsonSchemaBuilder >(
161+ 3 );
162+
151163 if (info .isMixed ()) {
152- itemTypeSchema
153- .addAnyOf (new JsonSchemaBuilder ()
164+ itemTypeSchemas
165+ .add (new JsonSchemaBuilder ()
154166 .addType (XmlSchemaJsonSchemaConstants .STRING_TYPE_INFO_SCHEMA_REF ));
155167 }
156168 if (info .isDomAllowed ()) {
157- itemTypeSchema
158- .addAnyOf (new JsonSchemaBuilder ()
169+ itemTypeSchemas
170+ .add (new JsonSchemaBuilder ()
159171 .addType (JsonixJsonSchemaConstants .DOM_TYPE_INFO_SCHEMA_REF ));
160172 }
161173 if (info .isTypedObjectAllowed ()) {
@@ -167,11 +179,12 @@ public JsonSchemaBuilder visitAnyElementPropertyInfo(
167179 .addRef (XmlSchemaJsonSchemaConstants .QNAME_TYPE_INFO_SCHEMA_REF ))
168180 .addProperty (JsonixConstants .VALUE_PROPERTY_NAME ,
169181 new JsonSchemaBuilder ());
170- itemTypeSchema . addAnyOf (anyElementSchema );
182+ itemTypeSchemas . add (anyElementSchema );
171183 }
172184 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
173- info .isCollection (), itemTypeSchema );
174- schema .addAnyOf (typeSchema );
185+ info .isCollection (),
186+ createPossiblyAnyOfTypeSchema (itemTypeSchemas ));
187+ schema .addAllOf (typeSchema );
175188 return schema ;
176189 }
177190
@@ -186,7 +199,7 @@ public JsonSchemaBuilder visitAttributePropertyInfo(
186199 .getTypeInfo ());
187200 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
188201 info .isCollection (), itemTypeSchema );
189- schema .addAnyOf (typeSchema );
202+ schema .addAllOf (typeSchema );
190203 return schema ;
191204 }
192205
@@ -200,44 +213,56 @@ public JsonSchemaBuilder visitAnyAttributePropertyInfo(
200213 JsonSchemaConstants .OBJECT_TYPE ).addAdditionalProperties (
201214 new JsonSchemaBuilder ()
202215 .addType (JsonSchemaConstants .STRING_TYPE ));
203- schema .addAnyOf (typeSchema );
216+ schema .addAllOf (typeSchema );
204217 return schema ;
205218 }
206219
207220 private void addPropertyInfoTypeSchema (String string ,
208221 JsonSchemaBuilder schema ) {
209- // TODO
210-
222+ schema . add ( JsonixJsonSchemaConstants . PROPERTY_TYPE_PROPERTY_NAME ,
223+ string );
211224 }
212225
213226 private void addPropertyInfoSchema (MPropertyInfo <T , C > propertyInfo ,
214227 JsonSchemaBuilder schemaBuilder ) {
215228 schemaBuilder .addTitle (propertyInfo .getPrivateName ());
216- // TODO
217- // if (propertyInfo.isCollection()) {
218- // options.append(naming.collection(), this.codeModel._boolean(true));
219- // }
220229 }
221230
222- private void addWrappableSchema (MWrappable info ,
223- JsonSchemaBuilder schemaBuilder ) {
231+ private void addWrappableSchema (MWrappable info , JsonSchemaBuilder schema ) {
224232 final QName wrapperElementName = info .getWrapperElementName ();
225233 if (wrapperElementName != null ) {
226- // TODO add wrapper element name
227- // options.append(naming.wrapperElementName(), mappingCompiler
228- // .createElementNameExpression(wrapperElementName));
234+ addNameSchema (
235+ schema ,
236+ JsonixJsonSchemaConstants .WRAPPER_ELEMENT_NAME_PROPERTY_NAME ,
237+ wrapperElementName );
229238 }
230239 }
231240
241+ private void addNameSchema (JsonSchemaBuilder schema , final String key ,
242+ final QName name ) {
243+ schema .add (key , createNameSchema (name ));
244+ }
245+
246+ private JsonSchemaBuilder createNameSchema (final QName elementName ) {
247+ return new JsonSchemaBuilder ().add (
248+ JsonixJsonSchemaConstants .LOCAL_PART_PROPERTY_NAME ,
249+ elementName .getLocalPart ()).add (
250+ JsonixJsonSchemaConstants .NAMESPACE_URI_PROPERTY_NAME ,
251+ elementName .getNamespaceURI ());
252+ }
253+
232254 private void addElementNameSchema (QName elementName ,
233- JsonSchemaBuilder schemaBuilder ) {
234- // TODO add element name
255+ JsonSchemaBuilder schema ) {
256+ addNameSchema (schema ,
257+ JsonixJsonSchemaConstants .ELEMENT_NAME_PROPERTY_NAME ,
258+ elementName );
235259 }
236260
237261 private void addAttributeNameSchema (QName attributeName ,
238262 JsonSchemaBuilder schema ) {
239- // TODO Auto-generated method stub
240-
263+ addNameSchema (schema ,
264+ JsonixJsonSchemaConstants .ATTRIBUTE_NAME_PROPERTY_NAME ,
265+ attributeName );
241266 }
242267
243268 private JsonSchemaBuilder createElementTypeInfosSchema (
@@ -265,20 +290,19 @@ private JsonSchemaBuilder createElementTypeInfoSchema(
265290 return elementTypeInfoSchema ;
266291 }
267292
268- private JsonSchemaBuilder createElementRefsSchema (
293+ private List < JsonSchemaBuilder > createElementRefsSchema (
269294 MElementTypeInfos <T , C > info ) {
270295
271- final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
272-
273296 final List <MElementTypeInfo <T , C >> elementTypeInfos = info
274297 .getElementTypeInfos ();
275- if (!elementTypeInfos .isEmpty ()) {
276- for (MElementTypeInfo <T , C > elementTypeInfo : elementTypeInfos ) {
277- final JsonSchemaBuilder elementTypeInfoSchema = createElementRefSchema (elementTypeInfo );
278- schema .addAnyOf (elementTypeInfoSchema );
279- }
298+ final List <JsonSchemaBuilder > schemas = new ArrayList <JsonSchemaBuilder >(
299+ elementTypeInfos .size ());
300+
301+ for (MElementTypeInfo <T , C > elementTypeInfo : elementTypeInfos ) {
302+ final JsonSchemaBuilder elementTypeInfoSchema = createElementRefSchema (elementTypeInfo );
303+ schemas .add (elementTypeInfoSchema );
280304 }
281- return schema ;
305+ return schemas ;
282306
283307 }
284308
@@ -297,6 +321,19 @@ private JsonSchemaBuilder createElementRefSchema(
297321 return schema ;
298322 }
299323
324+ private JsonSchemaBuilder createPossiblyAnyOfTypeSchema (
325+ final List <JsonSchemaBuilder > schemas ) {
326+ if (schemas .size () == 0 ) {
327+ return new JsonSchemaBuilder ();
328+ } else if (schemas .size () == 1 ) {
329+ return schemas .get (0 );
330+ } else {
331+ final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
332+ schema .addAnyOf (schemas );
333+ return schema ;
334+ }
335+ }
336+
300337 private JsonSchemaBuilder createPossiblyCollectionTypeSchema (
301338 boolean collection , final JsonSchemaBuilder itemTypeSchema ) {
302339 final JsonSchemaBuilder typeSchemaBuilder ;
0 commit comments