Skip to content

Commit

Permalink
Issue #67 work in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
highsource committed Jul 17, 2016
1 parent e8c0526 commit 2348272
Show file tree
Hide file tree
Showing 42 changed files with 953 additions and 1,067 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.hisrc.jsonix.compilation.jsonschema;

import javax.json.JsonBuilderFactory;
import javax.xml.namespace.QName;

import org.apache.commons.lang3.Validate;
import org.hisrc.jsonix.JsonixConstants;
import org.hisrc.jsonix.compilation.jsonschema.typeinfo.ClassInfoProducer;
import org.hisrc.jsonix.compilation.jsonschema.typeinfo.CreateTypeInfoProducer;
import org.hisrc.jsonix.compilation.jsonschema.typeinfo.EnumLeafInfoProducer;
import org.hisrc.jsonix.definition.Mapping;
import org.hisrc.jsonix.definition.Module;
import org.hisrc.jsonix.definition.Modules;
Expand All @@ -17,25 +21,23 @@

public class JsonSchemaMappingCompiler<T, C extends T> {

private JsonSchemaModuleCompiler<T, C> moduleCompiler;
private final Modules<T, C> modules;
private final Module<T, C> module;
private final Mapping<T, C> mapping;
private final JsonBuilderFactory jsonBuilderFactory;

public JsonSchemaMappingCompiler(
JsonSchemaModuleCompiler<T, C> moduleCompiler, Mapping<T, C> mapping) {
Validate.notNull(moduleCompiler);
public JsonSchemaMappingCompiler(JsonBuilderFactory jsonBuilderFactory, Modules<T, C> modules, Module<T, C> module,
Mapping<T, C> mapping) {
Validate.notNull(jsonBuilderFactory);
Validate.notNull(modules);
Validate.notNull(module);
Validate.notNull(mapping);
this.moduleCompiler = moduleCompiler;
this.modules = moduleCompiler.getModules();
this.module = moduleCompiler.getModule();
this.jsonBuilderFactory = jsonBuilderFactory;
this.modules = modules;
this.module = module;
this.mapping = mapping;
}

public JsonSchemaModuleCompiler<T, C> getModuleCompiler() {
return moduleCompiler;
}

public Modules<T, C> getModules() {
return modules;
}
Expand All @@ -48,13 +50,17 @@ public Mapping<T, C> getMapping() {
return mapping;
}

public JsonBuilderFactory getJsonBuilderFactory() {
return jsonBuilderFactory;
}

public JsonSchemaBuilder compile() {
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
final String schemaId = mapping.getSchemaId();
schema.addId(schemaId);
addElementInfos(schema);
addClassInfoSchemas(schema);
addElementLeafInfoSchemas(schema);
addEnumLeafInfoSchemas(schema);
return schema;
}

Expand All @@ -70,70 +76,51 @@ private void addElementInfos(final JsonSchemaBuilder schema) {
.addRef(XmlSchemaJsonSchemaConstants.QNAME_TYPE_INFO_SCHEMA_REF);
final JsonSchemaBuilder nameConstant = new JsonSchemaBuilder();
nameConstant.addType(JsonSchemaConstants.OBJECT_TYPE);
nameConstant
.addProperty(
JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME,
new JsonSchemaBuilder().addEnum(elementName
.getLocalPart()));
nameConstant.addProperty(
JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME,
new JsonSchemaBuilder().addEnum(elementName
.getNamespaceURI()));

elementInfoSchema.addProperty(
JsonixConstants.NAME_PROPERTY_NAME,
new JsonSchemaBuilder().addAllOf(qNameRef).addAllOf(
nameConstant));
nameConstant.addProperty(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME,
new JsonSchemaBuilder().addEnum(elementName.getLocalPart()));
nameConstant.addProperty(JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME,
new JsonSchemaBuilder().addEnum(elementName.getNamespaceURI()));

elementInfoSchema.addProperty(JsonixConstants.NAME_PROPERTY_NAME,
new JsonSchemaBuilder().addAllOf(qNameRef).addAllOf(nameConstant));

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

elementInfoSchema
.add(JsonixJsonSchemaConstants.ELEMENT_NAME_PROPERTY_NAME,
new JsonSchemaBuilder()
.add(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME,
elementName.getLocalPart())
.add(JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME,
elementName.getNamespaceURI()));
elementInfoSchema.add(JsonixJsonSchemaConstants.ELEMENT_NAME_PROPERTY_NAME,
new JsonSchemaBuilder()
.add(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME, elementName.getLocalPart()).add(
JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME,
elementName.getNamespaceURI()));
if (scope != null) {
elementInfoSchema.add(
JsonixJsonSchemaConstants.SCOPE_PROPERTY_NAME,
elementInfoSchema.add(JsonixJsonSchemaConstants.SCOPE_PROPERTY_NAME,
createTypeInfoSchemaRef(scope, scope));
}
schema.addAnyOf(elementInfoSchema);
}
}

private void addElementLeafInfoSchemas(final JsonSchemaBuilder schema) {
final JsonSchemaEnumLeafInfoProducer<T, C> enumLeafInfoCompiler = new JsonSchemaEnumLeafInfoProducer<T, C>(
this);
private void addEnumLeafInfoSchemas(final JsonSchemaBuilder schema) {
for (MEnumLeafInfo<T, C> enumLeafInfo : mapping.getEnumLeafInfos()) {
final JsonSchemaBuilder enumLeafInfoSchema = enumLeafInfoCompiler
.produce(enumLeafInfo);
schema.addDefinition(
enumLeafInfo
.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER),
final EnumLeafInfoProducer<T, C> enumLeafInfoCompiler = new EnumLeafInfoProducer<T, C>(enumLeafInfo);
final JsonSchemaBuilder enumLeafInfoSchema = enumLeafInfoCompiler.compile(this);
schema.addDefinition(enumLeafInfo.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER),
enumLeafInfoSchema);
}
}

private void addClassInfoSchemas(final JsonSchemaBuilder schema) {
final JsonSchemaClassInfoProducer<T, C> classInfoCompiler = new JsonSchemaClassInfoProducer<T, C>(
this);
for (MClassInfo<T, C> classInfo : mapping.getClassInfos()) {
final JsonSchemaBuilder classInfoSchema = classInfoCompiler
.produce(classInfo);
schema.addDefinition(
classInfo
.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER),
final ClassInfoProducer<T, C> classInfoCompiler = new ClassInfoProducer<T, C>(classInfo);
final JsonSchemaBuilder classInfoSchema = classInfoCompiler.compile(this);
schema.addDefinition(classInfo.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER),
classInfoSchema);
}
}

public <M extends MOriginated<O>, O> JsonSchemaBuilder createTypeInfoSchemaRef(
M originated, MTypeInfo<T, C> typeInfo) {
return typeInfo
.acceptTypeInfoVisitor(new JsonSchemaRefTypeInfoProducerVisitor<T, C, O>(
this, originated));
public <M extends MOriginated<O>, O> JsonSchemaBuilder createTypeInfoSchemaRef(M originated,
MTypeInfo<T, C> typeInfo) {
return typeInfo.acceptTypeInfoVisitor(new CreateTypeInfoProducer<T, C, O>(originated))
.createTypeInfoSchemaRef(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Map;
import java.util.Map.Entry;

import javax.json.JsonBuilderFactory;

import org.apache.commons.lang3.Validate;
import org.hisrc.jsonix.definition.JsonSchema;
import org.hisrc.jsonix.definition.Mapping;
Expand All @@ -13,26 +15,22 @@

public class JsonSchemaModuleCompiler<T, C extends T> {

private final JsonSchemaModulesGenerator<T, C> modulesCompiler;
private final Modules<T, C> modules;
private final Module<T, C> module;
private final JsonBuilderFactory jsonBuilderFactory;

// private final JsonSchema jsonSchema;

public JsonSchemaModuleCompiler(
JsonSchemaModulesGenerator<T, C> modulesCompiler,
Module<T, C> module, JsonSchema jsonSchema) {
Validate.notNull(modulesCompiler);
public JsonSchemaModuleCompiler(JsonBuilderFactory jsonBuilderFactory, Modules<T, C> modules, Module<T, C> module,
JsonSchema jsonSchema) {
Validate.notNull(jsonBuilderFactory);
Validate.notNull(module);
Validate.notNull(jsonSchema);
this.modulesCompiler = modulesCompiler;
this.modules = modulesCompiler.getModules();
this.jsonBuilderFactory = jsonBuilderFactory;
this.modules = modules;
this.module = module;
// this.jsonSchema = jsonSchema;
}

public JsonSchemaModulesGenerator<T, C> getModulesCompiler() {
return modulesCompiler;
public JsonBuilderFactory getJsonBuilderFactory() {
return jsonBuilderFactory;
}

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

}
Expand All @@ -61,13 +59,11 @@ public JsonSchemaBuilder compile() {
} else {
schema = new JsonSchemaBuilder();
schema.addId(getModule().getSchemaId());
for (Entry<Mapping<T, C>, JsonSchemaBuilder> entry : mappingSchemas
.entrySet()) {
for (Entry<Mapping<T, C>, JsonSchemaBuilder> entry : mappingSchemas.entrySet()) {
final Mapping<T, C> mapping = entry.getKey();
final JsonSchemaBuilder mappingSchema = entry.getValue();
schema.addDefinition(mapping.getMappingName(), mappingSchema);
schema.addAnyOf(new JsonSchemaBuilder().addRef(mapping
.getSchemaId()));
schema.addAnyOf(new JsonSchemaBuilder().addRef(mapping.getSchemaId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,15 @@ public Modules<T, C> getModules() {

public void generate(JsonStructureWriter<T, C> writer) {
final JsonProvider provider = JsonProvider.provider();
final JsonBuilderFactory builderFactory = provider
.createBuilderFactory(null);
final JsonBuilderFactory builderFactory = provider.createBuilderFactory(null);
for (final Module<T, C> module : this.modules.getModules()) {
if (!module.isEmpty()) {
for (JsonSchema jsonSchema : module.getJsonSchemas()) {
final JsonSchemaModuleCompiler<T, C> moduleCompiler = new JsonSchemaModuleCompiler<T, C>(
this, module, jsonSchema);
final JsonSchemaBuilder moduleSchema = moduleCompiler
.compile();
final JsonObject moduleSchemaJsonObject = moduleSchema
.build(builderFactory);
writer.writeJsonStructure(module, moduleSchemaJsonObject,
jsonSchema.getFileName());
builderFactory, modules, module, jsonSchema);
final JsonSchemaBuilder moduleSchema = moduleCompiler.compile();
final JsonObject moduleSchemaJsonObject = moduleSchema.build(builderFactory);
writer.writeJsonStructure(module, moduleSchemaJsonObject, jsonSchema.getFileName());
}
}
}
Expand Down
Loading

0 comments on commit 2348272

Please sign in to comment.