11package org .hisrc .jsonix .compilation .jsc ;
22
3+ import java .util .List ;
4+
35import javax .xml .namespace .QName ;
46
57import org .apache .commons .lang3 .Validate ;
8+ import org .hisrc .jsonix .JsonixConstants ;
69import org .hisrc .jsonix .jsonschema .JsonSchemaBuilder ;
10+ import org .hisrc .jsonix .jsonschema .JsonSchemaConstants ;
11+ import org .hisrc .jsonix .naming .StandardNaming ;
712import org .jvnet .jaxb2_commons .xml .bind .model .MAnyAttributePropertyInfo ;
813import org .jvnet .jaxb2_commons .xml .bind .model .MAnyElementPropertyInfo ;
914import org .jvnet .jaxb2_commons .xml .bind .model .MAttributePropertyInfo ;
@@ -38,13 +43,11 @@ public JsonSchemaClassInfoCompiler<T, C> getClassInfoCompiler() {
3843 public JsonSchemaBuilder visitElementPropertyInfo (
3944 MElementPropertyInfo <T , C > info ) {
4045 final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
41- // TODO append type
42- // options.append(naming.type(),
43- // this.codeModel.string(naming.element()));
4446 addPropertyInfoSchema (info , schema );
47+ addPropertyInfoTypeSchema (StandardNaming .ELEMENT , schema );
4548 addWrappableSchema (info , schema );
4649 addElementNameSchema (info .getElementName (), schema );
47- final JsonSchemaBuilder itemTypeSchema = createItemTypeSchema (info
50+ final JsonSchemaBuilder itemTypeSchema = createTypeSchema (info
4851 .getTypeInfo ());
4952 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
5053 info .isCollection (), itemTypeSchema );
@@ -56,10 +59,8 @@ public JsonSchemaBuilder visitElementPropertyInfo(
5659 public JsonSchemaBuilder visitElementsPropertyInfo (
5760 MElementsPropertyInfo <T , C > info ) {
5861 final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
59- // TODO
60- // options.append(naming.type(),
61- // this.codeModel.string(naming.elements()));
6262 addPropertyInfoSchema (info , schema );
63+ addPropertyInfoTypeSchema (StandardNaming .ELEMENTS , schema );
6364 addWrappableSchema (info , schema );
6465 final JsonSchemaBuilder itemTypeSchema = createElementTypeInfosSchema (info );
6566 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
@@ -73,31 +74,142 @@ public JsonSchemaBuilder visitElementRefPropertyInfo(
7374 MElementRefPropertyInfo <T , C > info ) {
7475 final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
7576 addPropertyInfoSchema (info , schema );
77+ addPropertyInfoTypeSchema (StandardNaming .ELEMENT_REF , schema );
7678 addWrappableSchema (info , schema );
7779 addElementNameSchema (info .getElementName (), schema );
7880
79- final JsonSchemaBuilder typeSchemaBuilder ;
80- if (info .isCollection ()) {
81- typeSchemaBuilder = new JsonSchemaBuilder ();
82- schema .addType ("array" ).addItem (typeSchemaBuilder );
83- } else {
84- typeSchemaBuilder = schema ;
81+ final JsonSchemaBuilder itemTypeSchema = new JsonSchemaBuilder ();
82+ if (info .isMixed ()) {
83+ itemTypeSchema
84+ .addAnyOf (new JsonSchemaBuilder ()
85+ .addType (XmlSchemaJsonSchemaConstants .STRING_TYPE_INFO_SCHEMA_REF ));
86+ }
87+ if (info .isDomAllowed ()) {
88+ itemTypeSchema
89+ .addAnyOf (new JsonSchemaBuilder ()
90+ .addType (JsonixJsonSchemaConstants .DOM_TYPE_INFO_SCHEMA_REF ));
91+ }
92+ if (info .isTypedObjectAllowed ()) {
93+ itemTypeSchema .addAnyOf (createElementRefSchema (info ));
8594 }
95+ final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
96+ info .isCollection (), itemTypeSchema );
97+ schema .addAnyOf (typeSchema );
98+ return schema ;
99+ }
100+
101+ @ Override
102+ public JsonSchemaBuilder visitElementRefsPropertyInfo (
103+ MElementRefsPropertyInfo <T , C > info ) {
104+ final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
105+ addPropertyInfoSchema (info , schema );
106+ addPropertyInfoTypeSchema (StandardNaming .ELEMENT_REFS , schema );
107+ addWrappableSchema (info , schema );
108+
86109 final JsonSchemaBuilder itemTypeSchema = new JsonSchemaBuilder ();
87- // TODO
88- // createWildcardSchema(info, schema);
89110 if (info .isMixed ()) {
90- itemTypeSchema .addAnyOf (new JsonSchemaBuilder ().addType ("string" ));
111+ itemTypeSchema
112+ .addAnyOf (new JsonSchemaBuilder ()
113+ .addType (XmlSchemaJsonSchemaConstants .STRING_TYPE_INFO_SCHEMA_REF ));
91114 }
92- // TODO
93- // itemTypeSchema.addAnyOf(createElementTypeInfoSchema(info));
115+ if (info .isDomAllowed ()) {
116+ itemTypeSchema
117+ .addAnyOf (new JsonSchemaBuilder ()
118+ .addType (JsonixJsonSchemaConstants .DOM_TYPE_INFO_SCHEMA_REF ));
119+ }
120+ if (info .isTypedObjectAllowed ()) {
121+ itemTypeSchema .addAnyOf (createElementRefsSchema (info ));
122+ }
123+ final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
124+ info .isCollection (), itemTypeSchema );
125+ schema .addAnyOf (typeSchema );
126+ return schema ;
127+ }
128+
129+ @ Override
130+ public JsonSchemaBuilder visitValuePropertyInfo (
131+ MValuePropertyInfo <T , C > info ) {
132+ final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
133+ addPropertyInfoSchema (info , schema );
134+ addPropertyInfoTypeSchema (StandardNaming .VALUE , schema );
135+ final JsonSchemaBuilder itemTypeSchema = createTypeSchema (info
136+ .getTypeInfo ());
94137 final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
95138 info .isCollection (), itemTypeSchema );
96- // options.append(naming.type(),
97- // this.codeModel.string(naming.elementRef()));
139+ schema .addAnyOf (typeSchema );
98140 return schema ;
99141 }
100142
143+ @ Override
144+ public JsonSchemaBuilder visitAnyElementPropertyInfo (
145+ MAnyElementPropertyInfo <T , C > info ) {
146+ final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
147+ addPropertyInfoSchema (info , schema );
148+ addPropertyInfoTypeSchema (StandardNaming .ANY_ELEMENT , schema );
149+
150+ final JsonSchemaBuilder itemTypeSchema = new JsonSchemaBuilder ();
151+ if (info .isMixed ()) {
152+ itemTypeSchema
153+ .addAnyOf (new JsonSchemaBuilder ()
154+ .addType (XmlSchemaJsonSchemaConstants .STRING_TYPE_INFO_SCHEMA_REF ));
155+ }
156+ if (info .isDomAllowed ()) {
157+ itemTypeSchema
158+ .addAnyOf (new JsonSchemaBuilder ()
159+ .addType (JsonixJsonSchemaConstants .DOM_TYPE_INFO_SCHEMA_REF ));
160+ }
161+ if (info .isTypedObjectAllowed ()) {
162+ final JsonSchemaBuilder anyElementSchema = new JsonSchemaBuilder ()
163+ .addType (JsonSchemaConstants .OBJECT_TYPE )
164+ .addProperty (
165+ JsonixConstants .NAME_PROPERTY_NAME ,
166+ new JsonSchemaBuilder ()
167+ .addRef (XmlSchemaJsonSchemaConstants .QNAME_TYPE_INFO_SCHEMA_REF ))
168+ .addProperty (JsonixConstants .VALUE_PROPERTY_NAME ,
169+ new JsonSchemaBuilder ());
170+ itemTypeSchema .addAnyOf (anyElementSchema );
171+ }
172+ final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
173+ info .isCollection (), itemTypeSchema );
174+ schema .addAnyOf (typeSchema );
175+ return schema ;
176+ }
177+
178+ @ Override
179+ public JsonSchemaBuilder visitAttributePropertyInfo (
180+ MAttributePropertyInfo <T , C > info ) {
181+ final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
182+ addPropertyInfoSchema (info , schema );
183+ addPropertyInfoTypeSchema (StandardNaming .ATTRIBUTE , schema );
184+ addAttributeNameSchema (info .getAttributeName (), schema );
185+ final JsonSchemaBuilder itemTypeSchema = createTypeSchema (info
186+ .getTypeInfo ());
187+ final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema (
188+ info .isCollection (), itemTypeSchema );
189+ schema .addAnyOf (typeSchema );
190+ return schema ;
191+ }
192+
193+ @ Override
194+ public JsonSchemaBuilder visitAnyAttributePropertyInfo (
195+ MAnyAttributePropertyInfo <T , C > info ) {
196+ final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
197+ addPropertyInfoSchema (info , schema );
198+ addPropertyInfoTypeSchema (StandardNaming .ANY_ATTRIBUTE , schema );
199+ final JsonSchemaBuilder typeSchema = new JsonSchemaBuilder ().addType (
200+ JsonSchemaConstants .OBJECT_TYPE ).addAdditionalProperties (
201+ new JsonSchemaBuilder ()
202+ .addType (JsonSchemaConstants .STRING_TYPE ));
203+ schema .addAnyOf (typeSchema );
204+ return schema ;
205+ }
206+
207+ private void addPropertyInfoTypeSchema (String string ,
208+ JsonSchemaBuilder schema ) {
209+ // TODO
210+
211+ }
212+
101213 private void addPropertyInfoSchema (MPropertyInfo <T , C > propertyInfo ,
102214 JsonSchemaBuilder schemaBuilder ) {
103215 schemaBuilder .addTitle (propertyInfo .getPrivateName ());
@@ -117,6 +229,17 @@ private void addWrappableSchema(MWrappable info,
117229 }
118230 }
119231
232+ private void addElementNameSchema (QName elementName ,
233+ JsonSchemaBuilder schemaBuilder ) {
234+ // TODO add element name
235+ }
236+
237+ private void addAttributeNameSchema (QName attributeName ,
238+ JsonSchemaBuilder schema ) {
239+ // TODO Auto-generated method stub
240+
241+ }
242+
120243 private JsonSchemaBuilder createElementTypeInfosSchema (
121244 MElementTypeInfos <T , C > info ) {
122245
@@ -137,60 +260,58 @@ private JsonSchemaBuilder createElementTypeInfoSchema(
137260 final JsonSchemaBuilder elementTypeInfoSchema = new JsonSchemaBuilder ();
138261 addElementNameSchema (elementTypeInfo .getElementName (),
139262 elementTypeInfoSchema );
140- elementTypeInfoSchema .addAnyOf (createItemTypeSchema (elementTypeInfo
263+ elementTypeInfoSchema .addAnyOf (createTypeSchema (elementTypeInfo
141264 .getTypeInfo ()));
142265 return elementTypeInfoSchema ;
143266 }
144267
145- private void addElementNameSchema (QName elementName ,
146- JsonSchemaBuilder schemaBuilder ) {
147- // TODO add element name
268+ private JsonSchemaBuilder createElementRefsSchema (
269+ MElementTypeInfos <T , C > info ) {
270+
271+ final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
272+
273+ final List <MElementTypeInfo <T , C >> elementTypeInfos = info
274+ .getElementTypeInfos ();
275+ if (!elementTypeInfos .isEmpty ()) {
276+ for (MElementTypeInfo <T , C > elementTypeInfo : elementTypeInfos ) {
277+ final JsonSchemaBuilder elementTypeInfoSchema = createElementRefSchema (elementTypeInfo );
278+ schema .addAnyOf (elementTypeInfoSchema );
279+ }
280+ }
281+ return schema ;
282+
148283 }
149284
150- private JsonSchemaBuilder createItemTypeSchema (MTypeInfo <T , C > typeInfo ) {
151- return getClassInfoCompiler ().getMappingCompiler ()
152- .createTypeInfoSchemaRef (typeInfo );
285+ private JsonSchemaBuilder createElementRefSchema (
286+ MElementTypeInfo <T , C > elementTypeInfo ) {
287+ final JsonSchemaBuilder schema = new JsonSchemaBuilder ();
288+ addElementNameSchema (elementTypeInfo .getElementName (), schema );
289+ // TODO constant
290+ schema .addType (JsonSchemaConstants .OBJECT_TYPE );
291+ schema .addProperty (
292+ JsonixConstants .NAME_PROPERTY_NAME ,
293+ new JsonSchemaBuilder ()
294+ .addRef (XmlSchemaJsonSchemaConstants .QNAME_TYPE_INFO_SCHEMA_REF ));
295+ schema .addProperty (JsonixConstants .VALUE_PROPERTY_NAME ,
296+ createTypeSchema (elementTypeInfo .getTypeInfo ()));
297+ return schema ;
153298 }
154299
155300 private JsonSchemaBuilder createPossiblyCollectionTypeSchema (
156301 boolean collection , final JsonSchemaBuilder itemTypeSchema ) {
157302 final JsonSchemaBuilder typeSchemaBuilder ;
158303 if (collection ) {
159304 typeSchemaBuilder = new JsonSchemaBuilder ();
160- typeSchemaBuilder .addType ("array" ).addItem (itemTypeSchema );
305+ typeSchemaBuilder .addType (JsonSchemaConstants .ARRAY_TYPE ).addItem (
306+ itemTypeSchema );
161307 } else {
162308 typeSchemaBuilder = itemTypeSchema ;
163309 }
164310 return typeSchemaBuilder ;
165311 }
166312
167- @ Override
168- public JsonSchemaBuilder visitAnyElementPropertyInfo (
169- MAnyElementPropertyInfo <T , C > info ) {
170- throw new UnsupportedOperationException ();
171- }
172-
173- @ Override
174- public JsonSchemaBuilder visitAttributePropertyInfo (
175- MAttributePropertyInfo <T , C > info ) {
176- throw new UnsupportedOperationException ();
177- }
178-
179- @ Override
180- public JsonSchemaBuilder visitAnyAttributePropertyInfo (
181- MAnyAttributePropertyInfo <T , C > info ) {
182- throw new UnsupportedOperationException ();
183- }
184-
185- @ Override
186- public JsonSchemaBuilder visitValuePropertyInfo (
187- MValuePropertyInfo <T , C > info ) {
188- throw new UnsupportedOperationException ();
189- }
190-
191- @ Override
192- public JsonSchemaBuilder visitElementRefsPropertyInfo (
193- MElementRefsPropertyInfo <T , C > info ) {
194- throw new UnsupportedOperationException ();
313+ private JsonSchemaBuilder createTypeSchema (MTypeInfo <T , C > typeInfo ) {
314+ return getClassInfoCompiler ().getMappingCompiler ()
315+ .createTypeInfoSchemaRef (typeInfo );
195316 }
196317}
0 commit comments