Skip to content

Commit ed402e6

Browse files
committed
Issue #22. Work in progress.
1 parent 325b862 commit ed402e6

File tree

2 files changed

+129
-8
lines changed

2 files changed

+129
-8
lines changed

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

Lines changed: 127 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.hisrc.jsonix.compilation.jsc;
22

3+
import javax.xml.namespace.QName;
4+
35
import org.apache.commons.lang3.Validate;
46
import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder;
57
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyAttributePropertyInfo;
@@ -8,9 +10,14 @@
810
import org.jvnet.jaxb2_commons.xml.bind.model.MElementPropertyInfo;
911
import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefPropertyInfo;
1012
import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefsPropertyInfo;
13+
import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeInfo;
14+
import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeInfos;
1115
import org.jvnet.jaxb2_commons.xml.bind.model.MElementsPropertyInfo;
16+
import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfo;
1217
import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfoVisitor;
18+
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;
1319
import org.jvnet.jaxb2_commons.xml.bind.model.MValuePropertyInfo;
20+
import org.jvnet.jaxb2_commons.xml.bind.model.MWrappable;
1421

1522
public class JsonSchemaPropertyInfoCompilerVisitor<T, C extends T> implements
1623
MPropertyInfoVisitor<T, C, JsonSchemaBuilder> {
@@ -30,13 +37,131 @@ public JsonSchemaClassInfoCompiler<T, C> getClassInfoCompiler() {
3037
@Override
3138
public JsonSchemaBuilder visitElementPropertyInfo(
3239
MElementPropertyInfo<T, C> info) {
33-
throw new UnsupportedOperationException();
40+
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
41+
// TODO append type
42+
// options.append(naming.type(),
43+
// this.codeModel.string(naming.element()));
44+
addPropertyInfoSchema(info, schema);
45+
addWrappableSchema(info, schema);
46+
addElementNameSchema(info.getElementName(), schema);
47+
final JsonSchemaBuilder itemTypeSchema = createItemTypeSchema(info
48+
.getTypeInfo());
49+
final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(
50+
info.isCollection(), itemTypeSchema);
51+
schema.addAnyOf(typeSchema);
52+
return schema;
3453
}
3554

3655
@Override
3756
public JsonSchemaBuilder visitElementsPropertyInfo(
3857
MElementsPropertyInfo<T, C> info) {
39-
throw new UnsupportedOperationException();
58+
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
59+
// TODO
60+
// options.append(naming.type(),
61+
// this.codeModel.string(naming.elements()));
62+
addPropertyInfoSchema(info, schema);
63+
addWrappableSchema(info, schema);
64+
final JsonSchemaBuilder itemTypeSchema = createElementTypeInfosSchema(info);
65+
final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(
66+
info.isCollection(), itemTypeSchema);
67+
schema.addAnyOf(typeSchema);
68+
return schema;
69+
}
70+
71+
@Override
72+
public JsonSchemaBuilder visitElementRefPropertyInfo(
73+
MElementRefPropertyInfo<T, C> info) {
74+
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
75+
addPropertyInfoSchema(info, schema);
76+
addWrappableSchema(info, schema);
77+
addElementNameSchema(info.getElementName(), schema);
78+
79+
final JsonSchemaBuilder typeSchemaBuilder;
80+
if (info.isCollection()) {
81+
typeSchemaBuilder = new JsonSchemaBuilder();
82+
schema.addType("array").addItem(typeSchemaBuilder);
83+
} else {
84+
typeSchemaBuilder = schema;
85+
}
86+
final JsonSchemaBuilder itemTypeSchema = new JsonSchemaBuilder();
87+
// TODO
88+
// createWildcardSchema(info, schema);
89+
if (info.isMixed()) {
90+
itemTypeSchema.addAnyOf(new JsonSchemaBuilder().addType("string"));
91+
}
92+
// TODO
93+
// itemTypeSchema.addAnyOf(createElementTypeInfoSchema(info));
94+
final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(
95+
info.isCollection(), itemTypeSchema);
96+
// options.append(naming.type(),
97+
// this.codeModel.string(naming.elementRef()));
98+
return schema;
99+
}
100+
101+
private void addPropertyInfoSchema(MPropertyInfo<T, C> propertyInfo,
102+
JsonSchemaBuilder schemaBuilder) {
103+
schemaBuilder.addTitle(propertyInfo.getPrivateName());
104+
// TODO
105+
// if (propertyInfo.isCollection()) {
106+
// options.append(naming.collection(), this.codeModel._boolean(true));
107+
// }
108+
}
109+
110+
private void addWrappableSchema(MWrappable info,
111+
JsonSchemaBuilder schemaBuilder) {
112+
final QName wrapperElementName = info.getWrapperElementName();
113+
if (wrapperElementName != null) {
114+
// TODO add wrapper element name
115+
// options.append(naming.wrapperElementName(), mappingCompiler
116+
// .createElementNameExpression(wrapperElementName));
117+
}
118+
}
119+
120+
private JsonSchemaBuilder createElementTypeInfosSchema(
121+
MElementTypeInfos<T, C> info) {
122+
123+
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
124+
125+
if (!info.getElementTypeInfos().isEmpty()) {
126+
for (MElementTypeInfo<T, C> elementTypeInfo : info
127+
.getElementTypeInfos()) {
128+
final JsonSchemaBuilder elementTypeInfoSchema = createElementTypeInfoSchema(elementTypeInfo);
129+
schema.addAnyOf(elementTypeInfoSchema);
130+
}
131+
}
132+
return schema;
133+
}
134+
135+
private JsonSchemaBuilder createElementTypeInfoSchema(
136+
MElementTypeInfo<T, C> elementTypeInfo) {
137+
final JsonSchemaBuilder elementTypeInfoSchema = new JsonSchemaBuilder();
138+
addElementNameSchema(elementTypeInfo.getElementName(),
139+
elementTypeInfoSchema);
140+
elementTypeInfoSchema.addAnyOf(createItemTypeSchema(elementTypeInfo
141+
.getTypeInfo()));
142+
return elementTypeInfoSchema;
143+
}
144+
145+
private void addElementNameSchema(QName elementName,
146+
JsonSchemaBuilder schemaBuilder) {
147+
// TODO add element name
148+
}
149+
150+
private JsonSchemaBuilder createItemTypeSchema(MTypeInfo<T, C> typeInfo) {
151+
return getClassInfoCompiler().getMappingCompiler()
152+
.createTypeInfoSchemaRef(typeInfo);
153+
}
154+
155+
private JsonSchemaBuilder createPossiblyCollectionTypeSchema(
156+
boolean collection, final JsonSchemaBuilder itemTypeSchema) {
157+
final JsonSchemaBuilder typeSchemaBuilder;
158+
if (collection) {
159+
typeSchemaBuilder = new JsonSchemaBuilder();
160+
typeSchemaBuilder.addType("array").addItem(itemTypeSchema);
161+
} else {
162+
typeSchemaBuilder = itemTypeSchema;
163+
}
164+
return typeSchemaBuilder;
40165
}
41166

42167
@Override
@@ -63,12 +188,6 @@ public JsonSchemaBuilder visitValuePropertyInfo(
63188
throw new UnsupportedOperationException();
64189
}
65190

66-
@Override
67-
public JsonSchemaBuilder visitElementRefPropertyInfo(
68-
MElementRefPropertyInfo<T, C> info) {
69-
throw new UnsupportedOperationException();
70-
}
71-
72191
@Override
73192
public JsonSchemaBuilder visitElementRefsPropertyInfo(
74193
MElementRefsPropertyInfo<T, C> info) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ private XmlSchemaJsonSchemaConstants() {
3131
+ JsonSchemaKeywords.definitions + "/" + "IDREFS";
3232
public static final String IDREF_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/"
3333
+ JsonSchemaKeywords.definitions + "/" + "IDREF";
34+
public static final String STRING_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/"
35+
+ JsonSchemaKeywords.definitions + "/" + "string";
3436

3537
}

0 commit comments

Comments
 (0)