Skip to content

Commit 8100eea

Browse files
committed
Issue #22.
1 parent ed402e6 commit 8100eea

13 files changed

+395
-75
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.hisrc.jsonix;
2+
3+
public class JsonixConstants {
4+
5+
private JsonixConstants() {
6+
}
7+
8+
public static String NAME_PROPERTY_NAME = "name";
9+
public static String VALUE_PROPERTY_NAME = "value";
10+
}

compiler/src/main/java/org/hisrc/jsonix/compilation/jsc/JsonSchemaMappingCompiler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.hisrc.jsonix.definition.Module;
66
import org.hisrc.jsonix.definition.Modules;
77
import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder;
8+
import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo;
89
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;
910

1011
public class JsonSchemaMappingCompiler<T, C extends T> {
@@ -41,7 +42,17 @@ public Mapping<T, C> getMapping() {
4142
}
4243

4344
public JsonSchemaBuilder compile() {
44-
throw new UnsupportedOperationException();
45+
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
46+
final JsonSchemaClassInfoCompiler<T, C> classInfoCompiler = new JsonSchemaClassInfoCompiler<T, C>(
47+
this);
48+
for (MClassInfo<T, C> classInfo : mapping.getClassInfos()) {
49+
final JsonSchemaBuilder classInfoSchema = classInfoCompiler
50+
.compile(classInfo);
51+
// TODO constant
52+
schema.addDefinition(classInfo.getContainerLocalName("."),
53+
classInfoSchema);
54+
}
55+
return schema;
4556
}
4657

4758
public JsonSchemaBuilder createTypeInfoSchemaRef(MTypeInfo<T, C> typeInfo) {

compiler/src/main/java/org/hisrc/jsonix/compilation/jsc/JsonSchemaModulesCompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public Modules<T, C> getModules() {
2323
return modules;
2424
}
2525

26-
public void compile(JsonSchemaWriter<T, C> writer) {
26+
public void compile(JsonStructureWriter<T, C> writer) {
2727
final JsonProvider provider = JsonProvider.provider();
2828
final JsonBuilderFactory builderFactory = provider
2929
.createBuilderFactory(null);
@@ -37,7 +37,7 @@ public void compile(JsonSchemaWriter<T, C> writer) {
3737
.compile();
3838
final JsonObject schema = schemaBuilder
3939
.build(builderFactory);
40-
writer.writeJsonSchema(module, schema, output);
40+
writer.writeJsonStructure(module, schema, output);
4141
}
4242
}
4343
}

compiler/src/main/java/org/hisrc/jsonix/compilation/jsc/JsonSchemaPropertyInfoCompilerVisitor.java

Lines changed: 177 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package org.hisrc.jsonix.compilation.jsc;
22

3+
import java.util.List;
4+
35
import javax.xml.namespace.QName;
46

57
import org.apache.commons.lang3.Validate;
8+
import org.hisrc.jsonix.JsonixConstants;
69
import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder;
10+
import org.hisrc.jsonix.jsonschema.JsonSchemaConstants;
11+
import org.hisrc.jsonix.naming.StandardNaming;
712
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyAttributePropertyInfo;
813
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyElementPropertyInfo;
914
import 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
}

compiler/src/main/java/org/hisrc/jsonix/compilation/jsc/JsonSchemaWriter.java renamed to compiler/src/main/java/org/hisrc/jsonix/compilation/jsc/JsonStructureWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import org.hisrc.jsonix.definition.Module;
66
import org.hisrc.jsonix.definition.Output;
77

8-
public interface JsonSchemaWriter<T, C extends T> {
8+
public interface JsonStructureWriter<T, C extends T> {
99

10-
public void writeJsonSchema(Module<T, C> module, JsonStructure schema,
10+
public void writeJsonStructure(Module<T, C> module, JsonStructure schema,
1111
Output output);
1212

1313
}

compiler/src/main/java/org/hisrc/jsonix/compilation/jsc/JsonixJsonSchemaConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ private JsonixJsonSchemaConstants() {
1010

1111
public static final String WILDCARD_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/"
1212
+ JsonSchemaKeywords.definitions + "/" + "wildcard";
13-
}
13+
public static final String DOM_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/"
14+
+ JsonSchemaKeywords.definitions + "/" + "dome";}

0 commit comments

Comments
 (0)