Skip to content

Commit

Permalink
Issue #60.
Browse files Browse the repository at this point in the history
  • Loading branch information
highsource committed Dec 27, 2015
1 parent b1b9aa9 commit 7f64d12
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 87 deletions.
Expand Up @@ -5,9 +5,9 @@
import javax.xml.namespace.QName;

import org.apache.commons.lang3.Validate;
import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPackageInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;

public class ElementInfoVertex<T, C extends T> extends InfoVertex<T,C> {

Expand Down Expand Up @@ -59,7 +59,7 @@ public boolean equals(Object obj) {
@Override
public String toString() {
final QName elementName = this.elementInfo.getElementName();
final MTypeInfo<T, C> scope = elementInfo.getScope();
final MClassInfo<T, C> scope = elementInfo.getScope();
return MessageFormat.format(
"Element [{0}], scope [{1}]",
elementName.toString(),
Expand Down
Expand Up @@ -7,11 +7,13 @@
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyElementPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MAttributePropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElement;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefsPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeRef;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementsPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MModelInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfoVisitor;
Expand Down Expand Up @@ -93,8 +95,7 @@ public PropertyInfoVertex<T, C> visitElementsPropertyInfo(
MElementsPropertyInfo<T, C> info) {
// Elements property has a "soft" dependency types of its elements
// That is, some of these types may be excluded
for (MElementTypeInfo<T, C> elementTypeInfo : info
.getElementTypeInfos()) {
for (MElementTypeRef<T, C> elementTypeInfo : info.getElementTypeInfos()) {
addSoftDependencyOnType(vertex, elementTypeInfo);
}
return vertex;
Expand All @@ -112,8 +113,7 @@ public PropertyInfoVertex<T, C> visitElementRefPropertyInfo(
@Override
public PropertyInfoVertex<T, C> visitElementRefsPropertyInfo(
MElementRefsPropertyInfo<T, C> info) {
for (MElementTypeInfo<T, C> elementTypeInfo : info
.getElementTypeInfos()) {
for (MElement<T, C> elementTypeInfo : info.getElementTypeInfos()) {
// Element references property has "soft" dependencies on the
// types
// of its elements
Expand All @@ -123,9 +123,8 @@ public PropertyInfoVertex<T, C> visitElementRefsPropertyInfo(
return vertex;
}

private void addSubstitutionHeadDependencies(
MElementTypeInfo<T, C> elementTypeInfo,
PropertyInfoVertex<T, C> propertyInfoVertex) {
private <M extends MElementTypeInfo<T, C, O>, O> void addSubstitutionHeadDependencies(
M elementTypeInfo, PropertyInfoVertex<T, C> propertyInfoVertex) {
final MClassInfo<T, C> classInfo = propertyInfoVertex.getClassInfo();
final QName elementName = elementTypeInfo.getElementName();
for (MElementInfo<T, C> elementInfo : this.modelInfo.getElementInfos()) {
Expand All @@ -136,7 +135,8 @@ private void addSubstitutionHeadDependencies(
.elementInfo(elementInfo);
modelInfoGraphBuilder.addSoftDependency(elementInfoVertex,
propertyInfoVertex);
modelInfoGraphBuilder.addSoftDependency(propertyInfoVertex, elementInfoVertex);
modelInfoGraphBuilder.addSoftDependency(propertyInfoVertex,
elementInfoVertex);
}
}
}
Expand Down
Expand Up @@ -48,11 +48,11 @@ public JsonSchemaBuilder produce(MClassInfo<T, C> classInfo) {
final String localName = classInfo
.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER);
classInfoSchema.addTitle(localName);
final MClassTypeInfo<T, C> baseTypeInfo = classInfo.getBaseTypeInfo();
final MClassTypeInfo<T, C, ?> baseTypeInfo = classInfo.getBaseTypeInfo();
final JsonSchemaBuilder typeInfoSchema;
if (baseTypeInfo != null) {
final JsonSchemaBuilder baseTypeInfoSchema = mappingCompiler
.createTypeInfoSchemaRef(baseTypeInfo);
.createTypeInfoSchemaRef(baseTypeInfo, baseTypeInfo);
typeInfoSchema = new JsonSchemaBuilder();
typeInfoSchema.addAllOf(baseTypeInfoSchema);
typeInfoSchema.addAllOf(classInfoSchema);
Expand Down
Expand Up @@ -34,7 +34,7 @@ public JsonSchemaBuilder produce(MEnumLeafInfo<T, C> enumLeafInfo) {
final MTypeInfo<T, C> baseTypeInfo = enumLeafInfo.getBaseTypeInfo();
final JsonSchemaBuilder typeInfoSchema;
final JsonSchemaBuilder baseTypeInfoSchema = mappingCompiler
.createTypeInfoSchemaRef(baseTypeInfo);
.createTypeInfoSchemaRef(enumLeafInfo, baseTypeInfo);
typeInfoSchema = new JsonSchemaBuilder();
typeInfoSchema.addAllOf(baseTypeInfoSchema);

Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.jvnet.jaxb2_commons.xml.bind.model.MElementInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.origin.MOriginated;

public class JsonSchemaMappingCompiler<T, C extends T> {

Expand Down Expand Up @@ -61,7 +62,7 @@ private void addElementInfos(final JsonSchemaBuilder schema) {
for (MElementInfo<T, C> elementInfo : mapping.getElementInfos()) {
final QName elementName = elementInfo.getElementName();
final MTypeInfo<T, C> typeInfo = elementInfo.getTypeInfo();
final MTypeInfo<T, C> scope = elementInfo.getScope();
final MClassInfo<T, C> scope = elementInfo.getScope();

final JsonSchemaBuilder elementInfoSchema = new JsonSchemaBuilder();
elementInfoSchema.addType(JsonSchemaConstants.OBJECT_TYPE);
Expand All @@ -85,7 +86,7 @@ private void addElementInfos(final JsonSchemaBuilder schema) {
nameConstant));

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

elementInfoSchema
.add(JsonixJsonSchemaConstants.ELEMENT_NAME_PROPERTY_NAME,
Expand All @@ -97,7 +98,7 @@ private void addElementInfos(final JsonSchemaBuilder schema) {
if (scope != null) {
elementInfoSchema.add(
JsonixJsonSchemaConstants.SCOPE_PROPERTY_NAME,
createTypeInfoSchemaRef(scope));
createTypeInfoSchemaRef(scope, scope));
}
schema.addAnyOf(elementInfoSchema);
}
Expand Down Expand Up @@ -129,9 +130,10 @@ private void addClassInfoSchemas(final JsonSchemaBuilder schema) {
}
}

public JsonSchemaBuilder createTypeInfoSchemaRef(MTypeInfo<T, C> typeInfo) {
public <M extends MOriginated<O>, O> JsonSchemaBuilder createTypeInfoSchemaRef(
M originated, MTypeInfo<T, C> typeInfo) {
return typeInfo
.acceptTypeInfoVisitor(new JsonSchemaRefTypeInfoProducerVisitor<T, C>(
this));
.acceptTypeInfoVisitor(new JsonSchemaRefTypeInfoProducerVisitor<T, C, O>(
this, originated));
}
}
Expand Up @@ -15,17 +15,22 @@
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyAttributePropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MAnyElementPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MAttributePropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElement;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefsPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeInfos;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeRef;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementsPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfoVisitor;
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MValuePropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MWrappable;
import org.jvnet.jaxb2_commons.xml.bind.model.origin.MElementOrigin;
import org.jvnet.jaxb2_commons.xml.bind.model.origin.MElementTypeRefOrigin;
import org.jvnet.jaxb2_commons.xml.bind.model.origin.MOriginated;

import com.sun.tools.xjc.model.Multiplicity;

Expand Down Expand Up @@ -56,7 +61,7 @@ public JsonSchemaBuilder visitElementPropertyInfo(
addElementNameSchema(info.getElementName(), schema);
addWrappableSchema(info, schema);

final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info
final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info, info
.getTypeInfo());
final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(
info, itemTypeSchema);
Expand Down Expand Up @@ -147,7 +152,7 @@ public JsonSchemaBuilder visitValuePropertyInfo(
addPropertyInfoSchema(info, schema);
addPropertyInfoTypeSchema(StandardNaming.VALUE, schema);

final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info
final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info, info
.getTypeInfo());
final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(
info, itemTypeSchema);
Expand Down Expand Up @@ -200,7 +205,7 @@ public JsonSchemaBuilder visitAttributePropertyInfo(
addPropertyInfoSchema(info, schema);
addPropertyInfoTypeSchema(StandardNaming.ATTRIBUTE, schema);
addAttributeNameSchema(info.getAttributeName(), schema);
final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info
final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info, info
.getTypeInfo());
final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(
info, itemTypeSchema);
Expand Down Expand Up @@ -271,12 +276,12 @@ private void addAttributeNameSchema(QName attributeName,
}

private JsonSchemaBuilder createElementTypeInfosSchema(
MElementTypeInfos<T, C> info) {
MElementTypeInfos<T, C, MElementTypeRef<T, C>, MElementTypeRefOrigin> info) {

final JsonSchemaBuilder schema = new JsonSchemaBuilder();

if (!info.getElementTypeInfos().isEmpty()) {
for (MElementTypeInfo<T, C> elementTypeInfo : info
for (MElementTypeRef<T, C> elementTypeInfo : info
.getElementTypeInfos()) {
final JsonSchemaBuilder elementTypeInfoSchema = createElementTypeInfoSchema(elementTypeInfo);
schema.addAnyOf(elementTypeInfoSchema);
Expand All @@ -286,33 +291,33 @@ private JsonSchemaBuilder createElementTypeInfosSchema(
}

private JsonSchemaBuilder createElementTypeInfoSchema(
MElementTypeInfo<T, C> elementTypeInfo) {
MElementTypeRef<T, C> elementTypeInfo) {
final JsonSchemaBuilder elementTypeInfoSchema = new JsonSchemaBuilder();
addElementNameSchema(elementTypeInfo.getElementName(),
elementTypeInfoSchema);
elementTypeInfoSchema.addAnyOf(createTypeSchema(elementTypeInfo
elementTypeInfoSchema.addAnyOf(createTypeSchema(elementTypeInfo, elementTypeInfo
.getTypeInfo()));
return elementTypeInfoSchema;
}

private List<JsonSchemaBuilder> createElementRefsSchema(
MElementTypeInfos<T, C> info) {
MElementTypeInfos<T, C, MElement<T, C>, MElementOrigin> info) {

final List<MElementTypeInfo<T, C>> elementTypeInfos = info
final List<MElement<T, C>> elementTypeInfos = info
.getElementTypeInfos();
final List<JsonSchemaBuilder> schemas = new ArrayList<JsonSchemaBuilder>(
elementTypeInfos.size());

for (MElementTypeInfo<T, C> elementTypeInfo : elementTypeInfos) {
for (MElement<T, C> elementTypeInfo : elementTypeInfos) {
final JsonSchemaBuilder elementTypeInfoSchema = createElementRefSchema(elementTypeInfo);
schemas.add(elementTypeInfoSchema);
}
return schemas;

}

private JsonSchemaBuilder createElementRefSchema(
MElementTypeInfo<T, C> elementTypeInfo) {
private <M extends MElementTypeInfo<T, C, O>, O> JsonSchemaBuilder createElementRefSchema(
M elementTypeInfo) {
final JsonSchemaBuilder schema = new JsonSchemaBuilder();
addElementNameSchema(elementTypeInfo.getElementName(), schema);
schema.addType(JsonSchemaConstants.OBJECT_TYPE);
Expand All @@ -321,7 +326,7 @@ private JsonSchemaBuilder createElementRefSchema(
new JsonSchemaBuilder()
.addRef(XmlSchemaJsonSchemaConstants.QNAME_TYPE_INFO_SCHEMA_REF));
schema.addProperty(JsonixConstants.VALUE_PROPERTY_NAME,
createTypeSchema(elementTypeInfo.getTypeInfo()));
createTypeSchema(elementTypeInfo, elementTypeInfo.getTypeInfo()));
return schema;
}

Expand Down Expand Up @@ -364,8 +369,9 @@ private JsonSchemaBuilder createPossiblyCollectionTypeSchema(
return typeSchema;
}

private JsonSchemaBuilder createTypeSchema(MTypeInfo<T, C> typeInfo) {
private <M extends MOriginated<O>, O> JsonSchemaBuilder createTypeSchema(
M originated, MTypeInfo<T, C> typeInfo) {
return getClassInfoCompiler().getMappingCompiler()
.createTypeInfoSchemaRef(typeInfo);
.createTypeInfoSchemaRef(originated, typeInfo);
}
}
@@ -1,6 +1,8 @@
package org.hisrc.jsonix.compilation.jsonschema;

import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
Expand All @@ -13,6 +15,8 @@
import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder;
import org.hisrc.jsonix.jsonschema.JsonSchemaConstants;
import org.hisrc.jsonix.jsonschema.JsonSchemaKeywords;
import org.hisrc.jsonix.xml.xsom.CollectSimpleTypeNamesVisitor;
import org.hisrc.xml.xsom.SchemaComponentAware;
import org.jvnet.jaxb2_commons.xml.bind.model.MBuiltinLeafInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MClassRef;
Expand All @@ -24,20 +28,27 @@
import org.jvnet.jaxb2_commons.xml.bind.model.MPackagedTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfoVisitor;
import org.jvnet.jaxb2_commons.xml.bind.model.MWildcardTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.origin.MOriginated;

public class JsonSchemaRefTypeInfoProducerVisitor<T, C extends T> implements
import com.sun.xml.xsom.XSComponent;

public class JsonSchemaRefTypeInfoProducerVisitor<T, C extends T, O> implements
MTypeInfoVisitor<T, C, JsonSchemaBuilder> {

private final JsonSchemaMappingCompiler<T, C> mappingCompiler;
private final MOriginated<O> originated;
private final Modules<T, C> modules;
private final Module<T, C> module;
private final Mapping<T, C> mapping;
private final Map<QName, String> typeNameSchemaRefs;

public JsonSchemaRefTypeInfoProducerVisitor(
JsonSchemaMappingCompiler<T, C> mappingCompiler) {
JsonSchemaMappingCompiler<T, C> mappingCompiler,
MOriginated<O> originated) {
Validate.notNull(mappingCompiler);
Validate.notNull(originated);
this.mappingCompiler = mappingCompiler;
this.originated = originated;
this.modules = mappingCompiler.getModules();
this.module = mappingCompiler.getModule();
this.mapping = mappingCompiler.getMapping();
Expand Down Expand Up @@ -96,17 +107,33 @@ public JsonSchemaBuilder visitIDREFS(MIDREFS<T, C> info) {

@Override
public JsonSchemaBuilder visitBuiltinLeafInfo(MBuiltinLeafInfo<T, C> info) {
final QName typeName = info.getTypeName();
final String $ref = this.typeNameSchemaRefs.get(typeName);

final O origin = this.originated.getOrigin();

final List<QName> simpleTypeNames = new LinkedList<QName>();
if (origin instanceof SchemaComponentAware) {
final XSComponent component = ((SchemaComponentAware) origin)
.getSchemaComponent();
if (component != null) {
final CollectSimpleTypeNamesVisitor visitor = new CollectSimpleTypeNamesVisitor();
component.visit(visitor);
simpleTypeNames.addAll(visitor.getTypeNames());
}
}

simpleTypeNames.add(info.getTypeName());

final JsonSchemaBuilder schema = new JsonSchemaBuilder();
if ($ref != null) {
return schema.addRef($ref);
} else {
return schema
.addDescription(MessageFormat
.format("WARNING, the type [{0}] is not supported, using the lax schema {}.",
typeName));
for (QName candidateName : simpleTypeNames) {
final String $ref = this.typeNameSchemaRefs.get(candidateName);
if ($ref != null) {
return schema.addRef($ref);
}
}
return schema
.addDescription(MessageFormat
.format("WARNING, the type [{0}] is not supported, using the lax schema {}.",
info.getTypeName()));
}

@Override
Expand Down

0 comments on commit 7f64d12

Please sign in to comment.