Skip to content

Commit 5ddccf4

Browse files
committed
Issue #22.
1 parent 6899cb8 commit 5ddccf4

File tree

14 files changed

+195
-97
lines changed

14 files changed

+195
-97
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ public Mapping<T, C> getMapping() {
5050
public JsonSchemaBuilder compile() {
5151
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
5252
final String schemaId = mapping.getSchemaId();
53-
if (!schemaId.isEmpty()) {
54-
schema.addId(schemaId);
55-
}
53+
schema.addId(schemaId);
5654
addElementInfos(schema);
5755
addClassInfoSchemas(schema);
5856
addElementLeafInfoSchemas(schema);
Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.hisrc.jsonix.compilation.jsonschema;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
import java.util.Map.Entry;
6+
7+
import javax.json.JsonBuilderFactory;
8+
import javax.json.spi.JsonProvider;
59

610
import org.apache.commons.lang3.Validate;
711
import org.hisrc.jsonix.definition.JsonSchema;
@@ -15,17 +19,19 @@ public class JsonSchemaModuleCompiler<T, C extends T> {
1519
private final JsonSchemaModulesCompiler<T, C> modulesCompiler;
1620
private final Modules<T, C> modules;
1721
private final Module<T, C> module;
18-
// private final JsonSchema jsonSchema;
22+
23+
// private final JsonSchema jsonSchema;
1924

2025
public JsonSchemaModuleCompiler(
21-
JsonSchemaModulesCompiler<T, C> modulesCompiler, Module<T, C> module, JsonSchema jsonSchema) {
26+
JsonSchemaModulesCompiler<T, C> modulesCompiler,
27+
Module<T, C> module, JsonSchema jsonSchema) {
2228
Validate.notNull(modulesCompiler);
2329
Validate.notNull(module);
2430
Validate.notNull(jsonSchema);
2531
this.modulesCompiler = modulesCompiler;
2632
this.modules = modulesCompiler.getModules();
2733
this.module = module;
28-
// this.jsonSchema = jsonSchema;
34+
// this.jsonSchema = jsonSchema;
2935
}
3036

3137
public JsonSchemaModulesCompiler<T, C> getModulesCompiler() {
@@ -40,30 +46,42 @@ public Module<T, C> getModule() {
4046
return module;
4147
}
4248

43-
public JsonSchemaBuilder compile() {
44-
final List<JsonSchemaBuilder> mappingSchemas = new ArrayList<JsonSchemaBuilder>(
49+
public JsonSchemaBuilder compile(JsonStructureWriter<T, C> writer) {
50+
final JsonProvider provider = JsonProvider.provider();
51+
final JsonBuilderFactory builderFactory = provider
52+
.createBuilderFactory(null);
53+
54+
final Map<Mapping<T, C>, JsonSchemaBuilder> mappingSchemas = new LinkedHashMap<Mapping<T, C>, JsonSchemaBuilder>(
4555
this.module.getMappings().size());
4656
for (Mapping<T, C> mapping : this.module.getMappings()) {
4757
if (!mapping.isEmpty()) {
4858
final JsonSchemaMappingCompiler<T, C> mappingCompiler = new JsonSchemaMappingCompiler<T, C>(
4959
this, mapping);
60+
mappingSchemas.put(mapping, mappingCompiler.compile());
5061

51-
final JsonSchemaBuilder mappingSchema = mappingCompiler
52-
.compile();
53-
54-
mappingSchemas.add(mappingSchema);
5562
}
5663
}
5764

65+
final JsonSchemaBuilder schema;
5866
if (mappingSchemas.size() == 1) {
59-
return mappingSchemas.get(0);
67+
schema = mappingSchemas.values().iterator().next();
6068
} else {
61-
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
62-
for (JsonSchemaBuilder mappingSchema : mappingSchemas) {
63-
schema.addAnyOf(mappingSchema);
69+
schema = new JsonSchemaBuilder();
70+
schema.addId(getModule().getSchemaId());
71+
for (Entry<Mapping<T, C>, JsonSchemaBuilder> entry : mappingSchemas
72+
.entrySet()) {
73+
final Mapping<T, C> mapping = entry.getKey();
74+
final JsonSchemaBuilder mappingSchema = entry.getValue();
75+
schema.addDefinition(mapping.getMappingName(), mappingSchema);
76+
schema.addAnyOf(new JsonSchemaBuilder().addRef(mapping
77+
.getSchemaId()));
6478
}
65-
return schema;
6679
}
67-
}
6880

81+
for (JsonSchema jsonSchema : module.getJsonSchemas()) {
82+
writer.writeJsonStructure(module, schema.build(builderFactory),
83+
jsonSchema.getFileName());
84+
}
85+
return schema;
86+
}
6987
}
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package org.hisrc.jsonix.compilation.jsonschema;
22

3-
import javax.json.JsonBuilderFactory;
4-
import javax.json.spi.JsonProvider;
5-
63
import org.apache.commons.lang3.Validate;
74
import org.hisrc.jsonix.definition.JsonSchema;
85
import org.hisrc.jsonix.definition.Module;
96
import org.hisrc.jsonix.definition.Modules;
10-
import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder;
117

128
public class JsonSchemaModulesCompiler<T, C extends T> {
139

@@ -23,22 +19,14 @@ public Modules<T, C> getModules() {
2319
}
2420

2521
public void compile(JsonStructureWriter<T, C> writer) {
26-
final JsonProvider provider = JsonProvider.provider();
27-
final JsonBuilderFactory builderFactory = provider
28-
.createBuilderFactory(null);
29-
3022
for (final Module<T, C> module : this.modules.getModules()) {
3123
if (!module.isEmpty()) {
3224
for (JsonSchema jsonSchema : module.getJsonSchemas()) {
3325
final JsonSchemaModuleCompiler<T, C> moduleCompiler = new JsonSchemaModuleCompiler<T, C>(
3426
this, module, jsonSchema);
35-
final JsonSchemaBuilder schema = moduleCompiler.compile();
36-
writer.writeJsonStructure(module,
37-
schema.build(builderFactory),
38-
jsonSchema.getFileName());
27+
moduleCompiler.compile(writer);
3928
}
4029
}
4130
}
4231
}
43-
4432
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public JsonSchemaBuilder visitClassRef(MClassRef<T, C> info) {
7272

7373
@Override
7474
public JsonSchemaBuilder visitList(MList<T, C> info) {
75-
return new JsonSchemaBuilder().addType(JsonSchemaConstants.ARRAY_TYPE).addItem(
76-
info.getItemTypeInfo().acceptTypeInfoVisitor(this));
75+
return new JsonSchemaBuilder().addType(JsonSchemaConstants.ARRAY_TYPE)
76+
.addItem(info.getItemTypeInfo().acceptTypeInfoVisitor(this));
7777
}
7878

7979
@Override
@@ -125,8 +125,11 @@ private JsonSchemaBuilder createTypeInfoSchemaRef(
125125

126126
final String typeInfoSchemaId = getModules().getSchemaId(
127127
info.getPackageInfo().getPackageName());
128-
final String mappingSchemaId = typeInfoSchemaId.equals(getMapping()
129-
.getSchemaId()) ? "#" : typeInfoSchemaId;
128+
129+
final String schemaId = getMapping().getSchemaId();
130+
131+
final String mappingSchemaId = typeInfoSchemaId.equals(schemaId) ? "#"
132+
: typeInfoSchemaId;
130133
final String typeInfoRef = mappingSchemaId
131134
+ "/"
132135
+ JsonSchemaKeywords.definitions

compiler/src/main/java/org/hisrc/jsonix/configuration/JsonSchemaConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
public class JsonSchemaConfiguration {
1414

1515
public static final String LOCAL_ELEMENT_NAME = "jsonSchema";
16-
public static final String STANDARD_FILE_NAME_PATTERN = ModuleConfiguration.MODULE_NAME_PROPERTY
17-
+ ".jsonschema";
16+
public static final String STANDARD_FILE_NAME_PATTERN = ModuleConfiguration.MODULE_NAME_PROPERTY +
17+
// + "." + MappingConfiguration.MAPPING_NAME_PROPERTY +
18+
".jsonschema";
1819

1920
private String fileName;
2021
public static final QName JSON_SCHEMA_NAME = new QName(

compiler/src/main/java/org/hisrc/jsonix/configuration/MappingConfiguration.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hisrc.jsonix.analysis.ModelInfoGraphAnalyzer;
1414
import org.hisrc.jsonix.context.JsonixContext;
1515
import org.hisrc.jsonix.definition.Mapping;
16+
import org.hisrc.jsonix.jsonschema.JsonSchemaKeywords;
1617
import org.jvnet.jaxb2_commons.xml.bind.model.MElementInfo;
1718
import org.jvnet.jaxb2_commons.xml.bind.model.MModelInfo;
1819
import org.jvnet.jaxb2_commons.xml.bind.model.MPackageInfo;
@@ -25,12 +26,15 @@
2526
@XmlType(propOrder = {})
2627
public class MappingConfiguration {
2728

29+
public static final String MAPPING_NAME_PROPERTY = "${mapping.name}";
30+
public static final String MAPPING_TARGET_NAMESPACE_URI_PROPERTY = "${mapping.targetNamespace}";
2831
public static final String LOCAL_ELEMENT_NAME = "mapping";
2932

33+
private ModuleConfiguration moduleConfiguration;
3034
private String id;
3135
private String name;
3236
private String _package;
33-
private String schemaId;
37+
private String schemaId = MappingConfiguration.MAPPING_TARGET_NAMESPACE_URI_PROPERTY + "#";
3438
private String targetNamespaceURI;
3539
private String defaultElementNamespaceURI;
3640
private String defaultAttributeNamespaceURI;
@@ -186,20 +190,16 @@ public <T, C extends T> Mapping<T, C> build(JsonixContext context,
186190
defaultAttributeNamespaceURI = mostUsedAttributeNamespaceURI;
187191
}
188192

189-
final String schemaId;
190-
if (this.schemaId != null) {
191-
schemaId = this.schemaId;
192-
} else {
193-
schemaId = targetNamespaceURI
194-
+ (targetNamespaceURI.endsWith("#") ? "" : "#");
195-
logger.debug(MessageFormat
196-
.format("Mapping [{0}] will use schema id \"{1}\" based on the target namespace URI for the package [{2}].",
197-
mappingName, schemaId, packageName));
198-
199-
}
193+
final String mappingSchemaId = schemaId
194+
.replace(ModuleConfiguration.MODULE_SCHEMA_ID_PROPERTY,
195+
getModuleConfiguration().getSchemaId())
196+
.replace(ModuleConfiguration.MODULE_NAME_PROPERTY,
197+
getModuleConfiguration().getName())
198+
.replace(MappingConfiguration.MAPPING_NAME_PROPERTY, getName())
199+
.replace(MappingConfiguration.MAPPING_TARGET_NAMESPACE_URI_PROPERTY, targetNamespaceURI);
200200

201201
final Mapping<T, C> mapping = new Mapping<T, C>(context, analyzer,
202-
packageInfo, mappingName, schemaId, targetNamespaceURI,
202+
packageInfo, mappingName, mappingSchemaId, targetNamespaceURI,
203203
defaultElementNamespaceURI, defaultAttributeNamespaceURI);
204204

205205
if (getExcludesConfiguration() != null) {
@@ -280,4 +280,16 @@ public <T, C extends T> Mapping<T, C> build(JsonixContext context,
280280
public String toString() {
281281
return MessageFormat.format("[{0}:{1}]", getId(), getName());
282282
}
283+
284+
public ModuleConfiguration getModuleConfiguration() {
285+
return moduleConfiguration;
286+
}
287+
288+
public void setModuleConfiguration(ModuleConfiguration moduleConfiguration) {
289+
if (this.moduleConfiguration != null) {
290+
throw new IllegalStateException(
291+
"Module configuration was already assigned.");
292+
}
293+
this.moduleConfiguration = moduleConfiguration;
294+
}
283295
}

compiler/src/main/java/org/hisrc/jsonix/configuration/ModuleConfiguration.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ public class ModuleConfiguration {
2727
// private Log log = new SystemLog();
2828

2929
public static final String MODULE_NAME_PROPERTY = "${module.name}";
30+
public static final String MODULE_SCHEMA_ID_PROPERTY = "${module.schemaId}";
3031
public static final String MODULE_NAME_SEPARATOR = "_";
3132

3233
private String name;
34+
private String schemaId = MODULE_NAME_PROPERTY + ".jsonschema#";
3335
private List<MappingConfiguration> mappingConfigurations = new LinkedList<MappingConfiguration>();
3436
private List<OutputConfiguration> outputConfigurations = new LinkedList<OutputConfiguration>();
3537
private List<JsonSchemaConfiguration> jsonSchemaConfigurations = new LinkedList<JsonSchemaConfiguration>();
@@ -47,6 +49,15 @@ public void setName(String name) {
4749
this.name = name;
4850
}
4951

52+
@XmlAttribute(name = "schemaId")
53+
public String getSchemaId() {
54+
return schemaId;
55+
}
56+
57+
public void setSchemaId(String schemaId) {
58+
this.schemaId = schemaId;
59+
}
60+
5061
@XmlElement(name = "mapping")
5162
public List<MappingConfiguration> getMappingConfigurations() {
5263
return mappingConfigurations;
@@ -101,13 +112,14 @@ public <T, C extends T> Module<T, C> build(
101112
return null;
102113
}
103114

104-
final String moduleName;
105-
if (this.name != null) {
106-
moduleName = this.name;
107-
} else {
108-
moduleName = createModuleName(moduleMappings);
115+
final String moduleName = this.name;
116+
if (moduleName == null) {
117+
throw new IllegalStateException("Module name was not assigned yet.");
109118
}
110119

120+
final String moduleSchemaId = schemaId.replace(
121+
ModuleConfiguration.MODULE_NAME_PROPERTY, moduleName);
122+
111123
final List<Output> outputs = new ArrayList<Output>(
112124
this.outputConfigurations.size());
113125
for (OutputConfiguration outputConfiguration : this.outputConfigurations) {
@@ -125,22 +137,22 @@ public <T, C extends T> Module<T, C> build(
125137
jsonSchemas.add(jsonSchema);
126138
}
127139
}
128-
return new Module<T, C>(moduleName, moduleMappings, outputs,
129-
jsonSchemas);
140+
return new Module<T, C>(moduleName, moduleSchemaId, moduleMappings,
141+
outputs, jsonSchemas);
130142
}
131143

132-
private <T, C extends T> String createModuleName(
133-
Iterable<Mapping<T, C>> mappings) {
134-
boolean first = true;
135-
final StringBuilder sb = new StringBuilder();
136-
for (Mapping<T, C> mapping : mappings) {
137-
if (!first) {
138-
sb.append(MODULE_NAME_SEPARATOR);
139-
}
140-
sb.append(mapping.getMappingName());
141-
first = false;
142-
}
143-
return sb.toString();
144-
}
144+
// private <T, C extends T> String createModuleName(
145+
// Iterable<Mapping<T, C>> mappings) {
146+
// boolean first = true;
147+
// final StringBuilder sb = new StringBuilder();
148+
// for (Mapping<T, C> mapping : mappings) {
149+
// if (!first) {
150+
// sb.append(MODULE_NAME_SEPARATOR);
151+
// }
152+
// sb.append(mapping.getMappingName());
153+
// first = false;
154+
// }
155+
// return sb.toString();
156+
// }
145157

146158
}

0 commit comments

Comments
 (0)