Skip to content

Commit 2348272

Browse files
committed
Issue #67 work in progress.
1 parent e8c0526 commit 2348272

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+953
-1067
lines changed

compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaEnumLeafInfoProducer.java

-71
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.hisrc.jsonix.compilation.jsonschema;
22

3+
import javax.json.JsonBuilderFactory;
34
import javax.xml.namespace.QName;
45

56
import org.apache.commons.lang3.Validate;
67
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;
711
import org.hisrc.jsonix.definition.Mapping;
812
import org.hisrc.jsonix.definition.Module;
913
import org.hisrc.jsonix.definition.Modules;
@@ -17,25 +21,23 @@
1721

1822
public class JsonSchemaMappingCompiler<T, C extends T> {
1923

20-
private JsonSchemaModuleCompiler<T, C> moduleCompiler;
2124
private final Modules<T, C> modules;
2225
private final Module<T, C> module;
2326
private final Mapping<T, C> mapping;
27+
private final JsonBuilderFactory jsonBuilderFactory;
2428

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);
2834
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;
3238
this.mapping = mapping;
3339
}
3440

35-
public JsonSchemaModuleCompiler<T, C> getModuleCompiler() {
36-
return moduleCompiler;
37-
}
38-
3941
public Modules<T, C> getModules() {
4042
return modules;
4143
}
@@ -48,13 +50,17 @@ public Mapping<T, C> getMapping() {
4850
return mapping;
4951
}
5052

53+
public JsonBuilderFactory getJsonBuilderFactory() {
54+
return jsonBuilderFactory;
55+
}
56+
5157
public JsonSchemaBuilder compile() {
5258
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
5359
final String schemaId = mapping.getSchemaId();
5460
schema.addId(schemaId);
5561
addElementInfos(schema);
5662
addClassInfoSchemas(schema);
57-
addElementLeafInfoSchemas(schema);
63+
addEnumLeafInfoSchemas(schema);
5864
return schema;
5965
}
6066

@@ -70,70 +76,51 @@ private void addElementInfos(final JsonSchemaBuilder schema) {
7076
.addRef(XmlSchemaJsonSchemaConstants.QNAME_TYPE_INFO_SCHEMA_REF);
7177
final JsonSchemaBuilder nameConstant = new JsonSchemaBuilder();
7278
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));
8786

8887
elementInfoSchema.addProperty(JsonixConstants.VALUE_PROPERTY_NAME,
8988
createTypeInfoSchemaRef(elementInfo, typeInfo));
9089

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()));
9895
if (scope != null) {
99-
elementInfoSchema.add(
100-
JsonixJsonSchemaConstants.SCOPE_PROPERTY_NAME,
96+
elementInfoSchema.add(JsonixJsonSchemaConstants.SCOPE_PROPERTY_NAME,
10197
createTypeInfoSchemaRef(scope, scope));
10298
}
10399
schema.addAnyOf(elementInfoSchema);
104100
}
105101
}
106102

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) {
110104
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),
116108
enumLeafInfoSchema);
117109
}
118110
}
119111

120112
private void addClassInfoSchemas(final JsonSchemaBuilder schema) {
121-
final JsonSchemaClassInfoProducer<T, C> classInfoCompiler = new JsonSchemaClassInfoProducer<T, C>(
122-
this);
123113
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),
129117
classInfoSchema);
130118
}
131119
}
132120

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);
138125
}
139126
}

compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModuleCompiler.java

+13-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Map;
55
import java.util.Map.Entry;
66

7+
import javax.json.JsonBuilderFactory;
8+
79
import org.apache.commons.lang3.Validate;
810
import org.hisrc.jsonix.definition.JsonSchema;
911
import org.hisrc.jsonix.definition.Mapping;
@@ -13,26 +15,22 @@
1315

1416
public class JsonSchemaModuleCompiler<T, C extends T> {
1517

16-
private final JsonSchemaModulesGenerator<T, C> modulesCompiler;
1718
private final Modules<T, C> modules;
1819
private final Module<T, C> module;
20+
private final JsonBuilderFactory jsonBuilderFactory;
1921

20-
// private final JsonSchema jsonSchema;
21-
22-
public JsonSchemaModuleCompiler(
23-
JsonSchemaModulesGenerator<T, C> modulesCompiler,
24-
Module<T, C> module, JsonSchema jsonSchema) {
25-
Validate.notNull(modulesCompiler);
22+
public JsonSchemaModuleCompiler(JsonBuilderFactory jsonBuilderFactory, Modules<T, C> modules, Module<T, C> module,
23+
JsonSchema jsonSchema) {
24+
Validate.notNull(jsonBuilderFactory);
2625
Validate.notNull(module);
2726
Validate.notNull(jsonSchema);
28-
this.modulesCompiler = modulesCompiler;
29-
this.modules = modulesCompiler.getModules();
27+
this.jsonBuilderFactory = jsonBuilderFactory;
28+
this.modules = modules;
3029
this.module = module;
31-
// this.jsonSchema = jsonSchema;
3230
}
3331

34-
public JsonSchemaModulesGenerator<T, C> getModulesCompiler() {
35-
return modulesCompiler;
32+
public JsonBuilderFactory getJsonBuilderFactory() {
33+
return jsonBuilderFactory;
3634
}
3735

3836
public Modules<T, C> getModules() {
@@ -49,7 +47,7 @@ public JsonSchemaBuilder compile() {
4947
for (Mapping<T, C> mapping : this.module.getMappings()) {
5048
if (!mapping.isEmpty()) {
5149
final JsonSchemaMappingCompiler<T, C> mappingCompiler = new JsonSchemaMappingCompiler<T, C>(
52-
this, mapping);
50+
jsonBuilderFactory, modules, module, mapping);
5351
mappingSchemas.put(mapping, mappingCompiler.compile());
5452

5553
}
@@ -61,13 +59,11 @@ public JsonSchemaBuilder compile() {
6159
} else {
6260
schema = new JsonSchemaBuilder();
6361
schema.addId(getModule().getSchemaId());
64-
for (Entry<Mapping<T, C>, JsonSchemaBuilder> entry : mappingSchemas
65-
.entrySet()) {
62+
for (Entry<Mapping<T, C>, JsonSchemaBuilder> entry : mappingSchemas.entrySet()) {
6663
final Mapping<T, C> mapping = entry.getKey();
6764
final JsonSchemaBuilder mappingSchema = entry.getValue();
6865
schema.addDefinition(mapping.getMappingName(), mappingSchema);
69-
schema.addAnyOf(new JsonSchemaBuilder().addRef(mapping
70-
.getSchemaId()));
66+
schema.addAnyOf(new JsonSchemaBuilder().addRef(mapping.getSchemaId()));
7167
}
7268
}
7369

compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModulesGenerator.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,15 @@ public Modules<T, C> getModules() {
2525

2626
public void generate(JsonStructureWriter<T, C> writer) {
2727
final JsonProvider provider = JsonProvider.provider();
28-
final JsonBuilderFactory builderFactory = provider
29-
.createBuilderFactory(null);
28+
final JsonBuilderFactory builderFactory = provider.createBuilderFactory(null);
3029
for (final Module<T, C> module : this.modules.getModules()) {
3130
if (!module.isEmpty()) {
3231
for (JsonSchema jsonSchema : module.getJsonSchemas()) {
3332
final JsonSchemaModuleCompiler<T, C> moduleCompiler = new JsonSchemaModuleCompiler<T, C>(
34-
this, module, jsonSchema);
35-
final JsonSchemaBuilder moduleSchema = moduleCompiler
36-
.compile();
37-
final JsonObject moduleSchemaJsonObject = moduleSchema
38-
.build(builderFactory);
39-
writer.writeJsonStructure(module, moduleSchemaJsonObject,
40-
jsonSchema.getFileName());
33+
builderFactory, modules, module, jsonSchema);
34+
final JsonSchemaBuilder moduleSchema = moduleCompiler.compile();
35+
final JsonObject moduleSchemaJsonObject = moduleSchema.build(builderFactory);
36+
writer.writeJsonStructure(module, moduleSchemaJsonObject, jsonSchema.getFileName());
4137
}
4238
}
4339
}

0 commit comments

Comments
 (0)