From 23482722bec2da43a91bf34a5974862bed7e7c4c Mon Sep 17 00:00:00 2001 From: highsource Date: Sun, 17 Jul 2016 13:00:34 +0200 Subject: [PATCH] Issue #67 work in progress. --- .../JsonSchemaEnumLeafInfoProducer.java | 71 ----- .../jsonschema/JsonSchemaMappingCompiler.java | 97 +++---- .../jsonschema/JsonSchemaModuleCompiler.java | 30 +-- .../JsonSchemaModulesGenerator.java | 14 +- ...JsonSchemaPropertyInfoProducerVisitor.java | 203 +++++--------- .../JsonSchemaRefTypeInfoProducerVisitor.java | 167 ------------ .../JsonSchemaTypeInfoProducer.java | 9 - .../XmlSchemaJsonSchemaConstants.java | 33 +-- .../ClassInfoProducer.java} | 69 ++--- .../typeinfo/EnumLeafInfoProducer.java | 60 +++++ .../jsonschema/typeinfo/TypeInfoProducer.java | 20 ++ ...CreateTypeInfoCompilerTypeInfoVisitor.java | 228 ---------------- ...ssionVisitor.java => IsLiteralEquals.java} | 52 ++-- .../compilation/mapping/MappingCompiler.java | 81 +----- .../mapping/PropertyInfoVisitor.java | 62 +++-- .../typeinfo/BuiltinLeafInfoCompiler.java | 25 +- .../mapping/typeinfo/ClassInfoCompiler.java | 84 ++++++ .../mapping/typeinfo/ClassRefCompiler.java | 22 ++ .../typeinfo/CreateTypeInfoCompiler.java | 247 ++++++++++++++++++ .../typeinfo/EnumLeafInfoCompiler.java | 76 ++++++ .../{ => mapping}/typeinfo/ListCompiler.java | 37 ++- .../typeinfo/PackagedTypeInfoCompiler.java | 16 +- .../mapping/typeinfo/TypeInfoCompiler.java | 18 ++ .../builtin/Base64BinaryTypeInfoCompiler.java | 6 +- .../builtin/BinaryTypeInfoCompiler.java | 14 +- .../builtin/BooleanTypeInfoCompiler.java | 20 ++ .../builtin/DecimalTypeInfoCompiler.java | 28 ++ .../builtin/DurationTypeInfoCompiler.java | 11 +- .../builtin/HexBinaryTypeInfoCompiler.java | 6 +- .../builtin/IntegerTypeInfoCompiler.java | 21 ++ .../NormalizedStringTypeInfoCompiler.java | 21 ++ .../builtin/QNameTypeInfoCompiler.java | 12 +- .../builtin/StringTypeInfoCompiler.java | 21 ++ .../XMLGregorianCalendarTypeInfoCompiler.java | 14 +- .../typeinfo/EnumLeafInfoCompiler.java | 30 --- .../typeinfo/TypeInfoCompiler.java | 16 -- .../builtin/BooleanTypeInfoCompiler.java | 17 -- .../builtin/DecimalTypeInfoCompiler.java | 24 -- .../builtin/IntegerTypeInfoCompiler.java | 17 -- .../builtin/StringTypeInfoCompiler.java | 17 -- .../tests/basic/JsonixPluginZeroTest.java | 2 +- .../src/test/resources/basic/zero/schema.xsd | 2 +- 42 files changed, 953 insertions(+), 1067 deletions(-) delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaEnumLeafInfoProducer.java delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaRefTypeInfoProducerVisitor.java delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaTypeInfoProducer.java rename compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/{JsonSchemaClassInfoProducer.java => typeinfo/ClassInfoProducer.java} (53%) create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/EnumLeafInfoProducer.java create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/TypeInfoProducer.java delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/CreateTypeInfoCompilerTypeInfoVisitor.java rename compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/{CheckValueStringLiteralExpressionVisitor.java => IsLiteralEquals.java} (50%) rename compiler/src/main/java/org/hisrc/jsonix/compilation/{ => mapping}/typeinfo/BuiltinLeafInfoCompiler.java (53%) create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ClassInfoCompiler.java create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ClassRefCompiler.java create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/CreateTypeInfoCompiler.java create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/EnumLeafInfoCompiler.java rename compiler/src/main/java/org/hisrc/jsonix/compilation/{ => mapping}/typeinfo/ListCompiler.java (61%) rename compiler/src/main/java/org/hisrc/jsonix/compilation/{ => mapping}/typeinfo/PackagedTypeInfoCompiler.java (76%) create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/TypeInfoCompiler.java rename compiler/src/main/java/org/hisrc/jsonix/compilation/{ => mapping}/typeinfo/builtin/Base64BinaryTypeInfoCompiler.java (61%) rename compiler/src/main/java/org/hisrc/jsonix/compilation/{ => mapping}/typeinfo/builtin/BinaryTypeInfoCompiler.java (53%) create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/BooleanTypeInfoCompiler.java create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/DecimalTypeInfoCompiler.java rename compiler/src/main/java/org/hisrc/jsonix/compilation/{ => mapping}/typeinfo/builtin/DurationTypeInfoCompiler.java (72%) rename compiler/src/main/java/org/hisrc/jsonix/compilation/{ => mapping}/typeinfo/builtin/HexBinaryTypeInfoCompiler.java (61%) create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/IntegerTypeInfoCompiler.java create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/NormalizedStringTypeInfoCompiler.java rename compiler/src/main/java/org/hisrc/jsonix/compilation/{ => mapping}/typeinfo/builtin/QNameTypeInfoCompiler.java (74%) create mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/StringTypeInfoCompiler.java rename compiler/src/main/java/org/hisrc/jsonix/compilation/{ => mapping}/typeinfo/builtin/XMLGregorianCalendarTypeInfoCompiler.java (76%) delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/EnumLeafInfoCompiler.java delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/TypeInfoCompiler.java delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/BooleanTypeInfoCompiler.java delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/DecimalTypeInfoCompiler.java delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/IntegerTypeInfoCompiler.java delete mode 100644 compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/StringTypeInfoCompiler.java diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaEnumLeafInfoProducer.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaEnumLeafInfoProducer.java deleted file mode 100644 index 2eb9670..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaEnumLeafInfoProducer.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.hisrc.jsonix.compilation.jsonschema; - -import javax.xml.namespace.QName; - -import org.apache.commons.lang3.Validate; -import org.hisrc.jsonix.JsonixConstants; -import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder; -import org.hisrc.jsonix.naming.StandardNaming; -import org.jvnet.jaxb2_commons.xml.bind.model.MEnumConstantInfo; -import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo; -import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo; - -public class JsonSchemaEnumLeafInfoProducer implements - JsonSchemaTypeInfoProducer, T, C> { - - private final JsonSchemaMappingCompiler mappingCompiler; - - public JsonSchemaEnumLeafInfoProducer( - JsonSchemaMappingCompiler mappingCompiler) { - Validate.notNull(mappingCompiler); - this.mappingCompiler = mappingCompiler; - } - - public JsonSchemaMappingCompiler getMappingCompiler() { - return mappingCompiler; - } - - @Override - public JsonSchemaBuilder produce(MEnumLeafInfo enumLeafInfo) { - final JsonSchemaBuilder enumLeafInfoSchema = new JsonSchemaBuilder(); - final String localName = enumLeafInfo - .getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER); - enumLeafInfoSchema.addTitle(localName); - final MTypeInfo baseTypeInfo = enumLeafInfo.getBaseTypeInfo(); - final JsonSchemaBuilder typeInfoSchema; - final JsonSchemaBuilder baseTypeInfoSchema = mappingCompiler - .createTypeInfoSchemaRef(enumLeafInfo, baseTypeInfo); - typeInfoSchema = new JsonSchemaBuilder(); - typeInfoSchema.addAllOf(baseTypeInfoSchema); - - for (MEnumConstantInfo enumConstant : enumLeafInfo.getConstants()) { - final JsonSchemaBuilder enumConstantSchema = createEnumConstant( - enumLeafInfo, enumConstant); - // TODO generate enums - if (enumConstantSchema != null) { - typeInfoSchema.addEnum(enumConstantSchema); - } - } - - typeInfoSchema.add(JsonixJsonSchemaConstants.TYPE_TYPE_PROPERTY_NAME, - StandardNaming.ENUM_INFO); - final QName typeName = enumLeafInfo.getTypeName(); - if (typeName != null) { - typeInfoSchema - .add(JsonixJsonSchemaConstants.TYPE_NAME_PROPERTY_NAME, - new JsonSchemaBuilder() - .add(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME, - typeName.getLocalPart()) - .add(JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME, - typeName.getNamespaceURI())); - } - - return typeInfoSchema; - } - - private JsonSchemaBuilder createEnumConstant( - MEnumLeafInfo enumLeafInfo, - MEnumConstantInfo enumConstant) { - return null; - } -} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaMappingCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaMappingCompiler.java index c76afb7..122365c 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaMappingCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaMappingCompiler.java @@ -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; @@ -17,25 +21,23 @@ public class JsonSchemaMappingCompiler { - private JsonSchemaModuleCompiler moduleCompiler; private final Modules modules; private final Module module; private final Mapping mapping; + private final JsonBuilderFactory jsonBuilderFactory; - public JsonSchemaMappingCompiler( - JsonSchemaModuleCompiler moduleCompiler, Mapping mapping) { - Validate.notNull(moduleCompiler); + public JsonSchemaMappingCompiler(JsonBuilderFactory jsonBuilderFactory, Modules modules, Module module, + Mapping 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 getModuleCompiler() { - return moduleCompiler; - } - public Modules getModules() { return modules; } @@ -48,13 +50,17 @@ public Mapping 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; } @@ -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 enumLeafInfoCompiler = new JsonSchemaEnumLeafInfoProducer( - this); + private void addEnumLeafInfoSchemas(final JsonSchemaBuilder schema) { for (MEnumLeafInfo enumLeafInfo : mapping.getEnumLeafInfos()) { - final JsonSchemaBuilder enumLeafInfoSchema = enumLeafInfoCompiler - .produce(enumLeafInfo); - schema.addDefinition( - enumLeafInfo - .getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER), + final EnumLeafInfoProducer enumLeafInfoCompiler = new EnumLeafInfoProducer(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 classInfoCompiler = new JsonSchemaClassInfoProducer( - this); for (MClassInfo classInfo : mapping.getClassInfos()) { - final JsonSchemaBuilder classInfoSchema = classInfoCompiler - .produce(classInfo); - schema.addDefinition( - classInfo - .getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER), + final ClassInfoProducer classInfoCompiler = new ClassInfoProducer(classInfo); + final JsonSchemaBuilder classInfoSchema = classInfoCompiler.compile(this); + schema.addDefinition(classInfo.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER), classInfoSchema); } } - public , O> JsonSchemaBuilder createTypeInfoSchemaRef( - M originated, MTypeInfo typeInfo) { - return typeInfo - .acceptTypeInfoVisitor(new JsonSchemaRefTypeInfoProducerVisitor( - this, originated)); + public , O> JsonSchemaBuilder createTypeInfoSchemaRef(M originated, + MTypeInfo typeInfo) { + return typeInfo.acceptTypeInfoVisitor(new CreateTypeInfoProducer(originated)) + .createTypeInfoSchemaRef(this); } } diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModuleCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModuleCompiler.java index 4347af2..ebe052f 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModuleCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModuleCompiler.java @@ -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; @@ -13,26 +15,22 @@ public class JsonSchemaModuleCompiler { - private final JsonSchemaModulesGenerator modulesCompiler; private final Modules modules; private final Module module; + private final JsonBuilderFactory jsonBuilderFactory; - // private final JsonSchema jsonSchema; - - public JsonSchemaModuleCompiler( - JsonSchemaModulesGenerator modulesCompiler, - Module module, JsonSchema jsonSchema) { - Validate.notNull(modulesCompiler); + public JsonSchemaModuleCompiler(JsonBuilderFactory jsonBuilderFactory, Modules modules, Module 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 getModulesCompiler() { - return modulesCompiler; + public JsonBuilderFactory getJsonBuilderFactory() { + return jsonBuilderFactory; } public Modules getModules() { @@ -49,7 +47,7 @@ public JsonSchemaBuilder compile() { for (Mapping mapping : this.module.getMappings()) { if (!mapping.isEmpty()) { final JsonSchemaMappingCompiler mappingCompiler = new JsonSchemaMappingCompiler( - this, mapping); + jsonBuilderFactory, modules, module, mapping); mappingSchemas.put(mapping, mappingCompiler.compile()); } @@ -61,13 +59,11 @@ public JsonSchemaBuilder compile() { } else { schema = new JsonSchemaBuilder(); schema.addId(getModule().getSchemaId()); - for (Entry, JsonSchemaBuilder> entry : mappingSchemas - .entrySet()) { + for (Entry, JsonSchemaBuilder> entry : mappingSchemas.entrySet()) { final Mapping 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())); } } diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModulesGenerator.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModulesGenerator.java index 63da7c8..80cbefd 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModulesGenerator.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaModulesGenerator.java @@ -25,19 +25,15 @@ public Modules getModules() { public void generate(JsonStructureWriter writer) { final JsonProvider provider = JsonProvider.provider(); - final JsonBuilderFactory builderFactory = provider - .createBuilderFactory(null); + final JsonBuilderFactory builderFactory = provider.createBuilderFactory(null); for (final Module module : this.modules.getModules()) { if (!module.isEmpty()) { for (JsonSchema jsonSchema : module.getJsonSchemas()) { final JsonSchemaModuleCompiler moduleCompiler = new JsonSchemaModuleCompiler( - 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()); } } } diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaPropertyInfoProducerVisitor.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaPropertyInfoProducerVisitor.java index bdce6c6..07f415c 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaPropertyInfoProducerVisitor.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaPropertyInfoProducerVisitor.java @@ -34,60 +34,49 @@ import com.sun.tools.xjc.model.Multiplicity; -public class JsonSchemaPropertyInfoProducerVisitor implements - MPropertyInfoVisitor { - - private final JsonSchemaClassInfoProducer classInfoCompiler; +public class JsonSchemaPropertyInfoProducerVisitor + implements MPropertyInfoVisitor { private final XSFunctionApplier multiplicityCounter = new XSFunctionApplier( ParticleMultiplicityCounter.INSTANCE); - public JsonSchemaPropertyInfoProducerVisitor( - JsonSchemaClassInfoProducer classInfoCompiler) { - Validate.notNull(classInfoCompiler); - this.classInfoCompiler = classInfoCompiler; - } + private JsonSchemaMappingCompiler mappingCompiler; - public JsonSchemaClassInfoProducer getClassInfoCompiler() { - return classInfoCompiler; + public JsonSchemaPropertyInfoProducerVisitor(JsonSchemaMappingCompiler mappingCompiler) { + Validate.notNull(mappingCompiler); + this.mappingCompiler = mappingCompiler; } @Override - public JsonSchemaBuilder visitElementPropertyInfo( - MElementPropertyInfo info) { + public JsonSchemaBuilder visitElementPropertyInfo(MElementPropertyInfo info) { final JsonSchemaBuilder schema = new JsonSchemaBuilder(); addPropertyInfoSchema(info, schema); addPropertyInfoTypeSchema(StandardNaming.ELEMENT, schema); addElementNameSchema(info.getElementName(), schema); addWrappableSchema(info, schema); - final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info, info - .getTypeInfo()); - final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema( - info, itemTypeSchema); + final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info, info.getTypeInfo()); + final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(info, itemTypeSchema); schema.addAllOf(typeSchema); return schema; } @Override - public JsonSchemaBuilder visitElementsPropertyInfo( - MElementsPropertyInfo info) { + public JsonSchemaBuilder visitElementsPropertyInfo(MElementsPropertyInfo info) { final JsonSchemaBuilder schema = new JsonSchemaBuilder(); addPropertyInfoSchema(info, schema); addPropertyInfoTypeSchema(StandardNaming.ELEMENTS, schema); addWrappableSchema(info, schema); final JsonSchemaBuilder itemTypeSchema = createElementTypeInfosSchema(info); - final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema( - info, itemTypeSchema); + final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(info, itemTypeSchema); schema.addAllOf(typeSchema); return schema; } @Override - public JsonSchemaBuilder visitElementRefPropertyInfo( - MElementRefPropertyInfo info) { + public JsonSchemaBuilder visitElementRefPropertyInfo(MElementRefPropertyInfo info) { final JsonSchemaBuilder schema = new JsonSchemaBuilder(); addPropertyInfoSchema(info, schema); @@ -95,30 +84,25 @@ public JsonSchemaBuilder visitElementRefPropertyInfo( addElementNameSchema(info.getElementName(), schema); addWrappableSchema(info, schema); - final List itemTypeSchemas = new ArrayList( - 3); + final List itemTypeSchemas = new ArrayList(3); if (info.isMixed()) { itemTypeSchemas - .add(new JsonSchemaBuilder() - .addRef(XmlSchemaJsonSchemaConstants.STRING_TYPE_INFO_SCHEMA_REF)); + .add(new JsonSchemaBuilder().addRef(XmlSchemaJsonSchemaConstants.STRING_TYPE_INFO_SCHEMA_REF)); } if (info.isDomAllowed()) { - itemTypeSchemas - .add(new JsonSchemaBuilder() - .addRef(JsonixJsonSchemaConstants.DOM_TYPE_INFO_SCHEMA_REF)); + itemTypeSchemas.add(new JsonSchemaBuilder().addRef(JsonixJsonSchemaConstants.DOM_TYPE_INFO_SCHEMA_REF)); } if (info.isTypedObjectAllowed()) { itemTypeSchemas.add(createElementRefSchema(info)); } - final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema( - info, createPossiblyAnyOfTypeSchema(itemTypeSchemas)); + final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(info, + createPossiblyAnyOfTypeSchema(itemTypeSchemas)); schema.addAllOf(typeSchema); return schema; } @Override - public JsonSchemaBuilder visitElementRefsPropertyInfo( - MElementRefsPropertyInfo info) { + public JsonSchemaBuilder visitElementRefsPropertyInfo(MElementRefsPropertyInfo info) { final JsonSchemaBuilder schema = new JsonSchemaBuilder(); addPropertyInfoSchema(info, schema); addPropertyInfoTypeSchema(StandardNaming.ELEMENT_REFS, schema); @@ -128,151 +112,115 @@ public JsonSchemaBuilder visitElementRefsPropertyInfo( 2 + info.getElementTypeInfos().size()); if (info.isMixed()) { itemTypeSchemas - .add(new JsonSchemaBuilder() - .addRef(XmlSchemaJsonSchemaConstants.STRING_TYPE_INFO_SCHEMA_REF)); + .add(new JsonSchemaBuilder().addRef(XmlSchemaJsonSchemaConstants.STRING_TYPE_INFO_SCHEMA_REF)); } if (info.isDomAllowed()) { - itemTypeSchemas - .add(new JsonSchemaBuilder() - .addRef(JsonixJsonSchemaConstants.DOM_TYPE_INFO_SCHEMA_REF)); + itemTypeSchemas.add(new JsonSchemaBuilder().addRef(JsonixJsonSchemaConstants.DOM_TYPE_INFO_SCHEMA_REF)); } if (info.isTypedObjectAllowed()) { itemTypeSchemas.addAll(createElementRefsSchema(info)); } - final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema( - info, createPossiblyAnyOfTypeSchema(itemTypeSchemas)); + final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(info, + createPossiblyAnyOfTypeSchema(itemTypeSchemas)); schema.addAllOf(typeSchema); return schema; } @Override - public JsonSchemaBuilder visitValuePropertyInfo( - MValuePropertyInfo info) { + public JsonSchemaBuilder visitValuePropertyInfo(MValuePropertyInfo info) { final JsonSchemaBuilder schema = new JsonSchemaBuilder(); addPropertyInfoSchema(info, schema); addPropertyInfoTypeSchema(StandardNaming.VALUE, schema); - final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info, info - .getTypeInfo()); - final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema( - info, itemTypeSchema); + final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info, info.getTypeInfo()); + final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(info, itemTypeSchema); schema.addAllOf(typeSchema); return schema; } @Override - public JsonSchemaBuilder visitAnyElementPropertyInfo( - MAnyElementPropertyInfo info) { + public JsonSchemaBuilder visitAnyElementPropertyInfo(MAnyElementPropertyInfo info) { final JsonSchemaBuilder schema = new JsonSchemaBuilder(); addPropertyInfoSchema(info, schema); addPropertyInfoTypeSchema(StandardNaming.ANY_ELEMENT, schema); - final List itemTypeSchemas = new ArrayList( - 3); + final List itemTypeSchemas = new ArrayList(3); if (info.isMixed()) { itemTypeSchemas - .add(new JsonSchemaBuilder() - .addRef(XmlSchemaJsonSchemaConstants.STRING_TYPE_INFO_SCHEMA_REF)); + .add(new JsonSchemaBuilder().addRef(XmlSchemaJsonSchemaConstants.STRING_TYPE_INFO_SCHEMA_REF)); } if (info.isDomAllowed()) { - itemTypeSchemas - .add(new JsonSchemaBuilder() - .addRef(JsonixJsonSchemaConstants.DOM_TYPE_INFO_SCHEMA_REF)); + itemTypeSchemas.add(new JsonSchemaBuilder().addRef(JsonixJsonSchemaConstants.DOM_TYPE_INFO_SCHEMA_REF)); } if (info.isTypedObjectAllowed()) { - final JsonSchemaBuilder anyElementSchema = new JsonSchemaBuilder() - .addType(JsonSchemaConstants.OBJECT_TYPE) - .addProperty( - JsonixConstants.NAME_PROPERTY_NAME, - new JsonSchemaBuilder() - .addRef(XmlSchemaJsonSchemaConstants.QNAME_TYPE_INFO_SCHEMA_REF)) - .addProperty(JsonixConstants.VALUE_PROPERTY_NAME, - new JsonSchemaBuilder()); + final JsonSchemaBuilder anyElementSchema = new JsonSchemaBuilder().addType(JsonSchemaConstants.OBJECT_TYPE) + .addProperty(JsonixConstants.NAME_PROPERTY_NAME, + new JsonSchemaBuilder().addRef(XmlSchemaJsonSchemaConstants.QNAME_TYPE_INFO_SCHEMA_REF)) + .addProperty(JsonixConstants.VALUE_PROPERTY_NAME, new JsonSchemaBuilder()); itemTypeSchemas.add(anyElementSchema); } - final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema( - info, createPossiblyAnyOfTypeSchema(itemTypeSchemas)); + final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(info, + createPossiblyAnyOfTypeSchema(itemTypeSchemas)); schema.addAllOf(typeSchema); return schema; } @Override - public JsonSchemaBuilder visitAttributePropertyInfo( - MAttributePropertyInfo info) { + public JsonSchemaBuilder visitAttributePropertyInfo(MAttributePropertyInfo info) { final JsonSchemaBuilder schema = new JsonSchemaBuilder(); addPropertyInfoSchema(info, schema); addPropertyInfoTypeSchema(StandardNaming.ATTRIBUTE, schema); addAttributeNameSchema(info.getAttributeName(), schema); - final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info, info - .getTypeInfo()); - final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema( - info, itemTypeSchema); + final JsonSchemaBuilder itemTypeSchema = createTypeSchema(info, info.getTypeInfo()); + final JsonSchemaBuilder typeSchema = createPossiblyCollectionTypeSchema(info, itemTypeSchema); schema.addAllOf(typeSchema); return schema; } @Override - public JsonSchemaBuilder visitAnyAttributePropertyInfo( - MAnyAttributePropertyInfo info) { + public JsonSchemaBuilder visitAnyAttributePropertyInfo(MAnyAttributePropertyInfo info) { final JsonSchemaBuilder schema = new JsonSchemaBuilder(); addPropertyInfoSchema(info, schema); addPropertyInfoTypeSchema(StandardNaming.ANY_ATTRIBUTE, schema); - final JsonSchemaBuilder typeSchema = new JsonSchemaBuilder().addType( - JsonSchemaConstants.OBJECT_TYPE).addAdditionalProperties( - new JsonSchemaBuilder() - .addType(JsonSchemaConstants.STRING_TYPE)); + final JsonSchemaBuilder typeSchema = new JsonSchemaBuilder().addType(JsonSchemaConstants.OBJECT_TYPE) + .addAdditionalProperties(new JsonSchemaBuilder().addType(JsonSchemaConstants.STRING_TYPE)); schema.addAllOf(typeSchema); return schema; } - private void addPropertyInfoTypeSchema(String string, - JsonSchemaBuilder schema) { - schema.add(JsonixJsonSchemaConstants.PROPERTY_TYPE_PROPERTY_NAME, - string); + private void addPropertyInfoTypeSchema(String string, JsonSchemaBuilder schema) { + schema.add(JsonixJsonSchemaConstants.PROPERTY_TYPE_PROPERTY_NAME, string); } - private void addPropertyInfoSchema(MPropertyInfo propertyInfo, - JsonSchemaBuilder schema) { + private void addPropertyInfoSchema(MPropertyInfo propertyInfo, JsonSchemaBuilder schema) { schema.addTitle(propertyInfo.getPrivateName()); } private void addWrappableSchema(MWrappable info, JsonSchemaBuilder schema) { final QName wrapperElementName = info.getWrapperElementName(); if (wrapperElementName != null) { - addNameSchema( - schema, - JsonixJsonSchemaConstants.WRAPPER_ELEMENT_NAME_PROPERTY_NAME, - wrapperElementName); + addNameSchema(schema, JsonixJsonSchemaConstants.WRAPPER_ELEMENT_NAME_PROPERTY_NAME, wrapperElementName); } } - private void addNameSchema(JsonSchemaBuilder schema, final String key, - final QName name) { + private void addNameSchema(JsonSchemaBuilder schema, final String key, final QName name) { schema.add(key, createNameSchema(name)); } private JsonSchemaBuilder createNameSchema(final QName elementName) { - return new JsonSchemaBuilder().add( - JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME, - elementName.getLocalPart()).add( - JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME, - elementName.getNamespaceURI()); + return new JsonSchemaBuilder() + .add(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME, elementName.getLocalPart()) + .add(JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME, elementName.getNamespaceURI()); } - private void addElementNameSchema(QName elementName, - JsonSchemaBuilder schema) { - addNameSchema(schema, - JsonixJsonSchemaConstants.ELEMENT_NAME_PROPERTY_NAME, - elementName); + private void addElementNameSchema(QName elementName, JsonSchemaBuilder schema) { + addNameSchema(schema, JsonixJsonSchemaConstants.ELEMENT_NAME_PROPERTY_NAME, elementName); } - private void addAttributeNameSchema(QName attributeName, - JsonSchemaBuilder schema) { - addNameSchema(schema, - JsonixJsonSchemaConstants.ATTRIBUTE_NAME_PROPERTY_NAME, - attributeName); + private void addAttributeNameSchema(QName attributeName, JsonSchemaBuilder schema) { + addNameSchema(schema, JsonixJsonSchemaConstants.ATTRIBUTE_NAME_PROPERTY_NAME, attributeName); } private JsonSchemaBuilder createElementTypeInfosSchema( @@ -281,8 +229,7 @@ private JsonSchemaBuilder createElementTypeInfosSchema( final JsonSchemaBuilder schema = new JsonSchemaBuilder(); if (!info.getElementTypeInfos().isEmpty()) { - for (MElementTypeRef elementTypeInfo : info - .getElementTypeInfos()) { + for (MElementTypeRef elementTypeInfo : info.getElementTypeInfos()) { final JsonSchemaBuilder elementTypeInfoSchema = createElementTypeInfoSchema(elementTypeInfo); schema.addAnyOf(elementTypeInfoSchema); } @@ -290,23 +237,18 @@ private JsonSchemaBuilder createElementTypeInfosSchema( return schema; } - private JsonSchemaBuilder createElementTypeInfoSchema( - MElementTypeRef elementTypeInfo) { + private JsonSchemaBuilder createElementTypeInfoSchema(MElementTypeRef elementTypeInfo) { final JsonSchemaBuilder elementTypeInfoSchema = new JsonSchemaBuilder(); - addElementNameSchema(elementTypeInfo.getElementName(), - elementTypeInfoSchema); - elementTypeInfoSchema.addAnyOf(createTypeSchema(elementTypeInfo, elementTypeInfo - .getTypeInfo())); + addElementNameSchema(elementTypeInfo.getElementName(), elementTypeInfoSchema); + elementTypeInfoSchema.addAnyOf(createTypeSchema(elementTypeInfo, elementTypeInfo.getTypeInfo())); return elementTypeInfoSchema; } private List createElementRefsSchema( MElementTypeInfos, MElementOrigin> info) { - final List> elementTypeInfos = info - .getElementTypeInfos(); - final List schemas = new ArrayList( - elementTypeInfos.size()); + final List> elementTypeInfos = info.getElementTypeInfos(); + final List schemas = new ArrayList(elementTypeInfos.size()); for (MElement elementTypeInfo : elementTypeInfos) { final JsonSchemaBuilder elementTypeInfoSchema = createElementRefSchema(elementTypeInfo); @@ -316,22 +258,18 @@ private List createElementRefsSchema( } - private , O> JsonSchemaBuilder createElementRefSchema( - M elementTypeInfo) { + private , O> JsonSchemaBuilder createElementRefSchema(M elementTypeInfo) { final JsonSchemaBuilder schema = new JsonSchemaBuilder(); addElementNameSchema(elementTypeInfo.getElementName(), schema); schema.addType(JsonSchemaConstants.OBJECT_TYPE); - schema.addProperty( - JsonixConstants.NAME_PROPERTY_NAME, - new JsonSchemaBuilder() - .addRef(XmlSchemaJsonSchemaConstants.QNAME_TYPE_INFO_SCHEMA_REF)); + schema.addProperty(JsonixConstants.NAME_PROPERTY_NAME, + new JsonSchemaBuilder().addRef(XmlSchemaJsonSchemaConstants.QNAME_TYPE_INFO_SCHEMA_REF)); schema.addProperty(JsonixConstants.VALUE_PROPERTY_NAME, createTypeSchema(elementTypeInfo, elementTypeInfo.getTypeInfo())); return schema; } - private JsonSchemaBuilder createPossiblyAnyOfTypeSchema( - final List schemas) { + private JsonSchemaBuilder createPossiblyAnyOfTypeSchema(final List schemas) { if (schemas.size() == 0) { return new JsonSchemaBuilder(); } else if (schemas.size() == 1) { @@ -343,17 +281,14 @@ private JsonSchemaBuilder createPossiblyAnyOfTypeSchema( } } - private JsonSchemaBuilder createPossiblyCollectionTypeSchema( - MPropertyInfo propertyInfo, + private JsonSchemaBuilder createPossiblyCollectionTypeSchema(MPropertyInfo propertyInfo, final JsonSchemaBuilder itemTypeSchema) { final JsonSchemaBuilder typeSchema; if (propertyInfo.isCollection()) { typeSchema = new JsonSchemaBuilder(); - typeSchema.addType(JsonSchemaConstants.ARRAY_TYPE).addItem( - itemTypeSchema); + typeSchema.addType(JsonSchemaConstants.ARRAY_TYPE).addItem(itemTypeSchema); - final Multiplicity multiplicity = multiplicityCounter - .apply(propertyInfo.getOrigin()); + final Multiplicity multiplicity = multiplicityCounter.apply(propertyInfo.getOrigin()); if (multiplicity != null) { if (multiplicity.min != null) { typeSchema.addMinItems(multiplicity.min); @@ -369,9 +304,7 @@ private JsonSchemaBuilder createPossiblyCollectionTypeSchema( return typeSchema; } - private , O> JsonSchemaBuilder createTypeSchema( - M originated, MTypeInfo typeInfo) { - return getClassInfoCompiler().getMappingCompiler() - .createTypeInfoSchemaRef(originated, typeInfo); + private , O> JsonSchemaBuilder createTypeSchema(M originated, MTypeInfo typeInfo) { + return mappingCompiler.createTypeInfoSchemaRef(originated, typeInfo); } } diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaRefTypeInfoProducerVisitor.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaRefTypeInfoProducerVisitor.java deleted file mode 100644 index d075145..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaRefTypeInfoProducerVisitor.java +++ /dev/null @@ -1,167 +0,0 @@ -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; - -import org.apache.commons.lang3.Validate; -import org.hisrc.jsonix.compilation.mapping.MappingCompiler; -import org.hisrc.jsonix.definition.Mapping; -import org.hisrc.jsonix.definition.Module; -import org.hisrc.jsonix.definition.Modules; -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; -import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo; -import org.jvnet.jaxb2_commons.xml.bind.model.MID; -import org.jvnet.jaxb2_commons.xml.bind.model.MIDREF; -import org.jvnet.jaxb2_commons.xml.bind.model.MIDREFS; -import org.jvnet.jaxb2_commons.xml.bind.model.MList; -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; - -import com.sun.xml.xsom.XSComponent; - -public class JsonSchemaRefTypeInfoProducerVisitor implements - MTypeInfoVisitor { - - private final JsonSchemaMappingCompiler mappingCompiler; - private final MOriginated originated; - private final Modules modules; - private final Module module; - private final Mapping mapping; - private final Map typeNameSchemaRefs; - - public JsonSchemaRefTypeInfoProducerVisitor( - JsonSchemaMappingCompiler mappingCompiler, - MOriginated 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(); - this.typeNameSchemaRefs = XmlSchemaJsonSchemaConstants.TYPE_NAME_SCHEMA_REFS; - } - - public JsonSchemaMappingCompiler getMappingCompiler() { - return mappingCompiler; - } - - public Modules getModules() { - return modules; - } - - public Module getModule() { - return module; - } - - public Mapping getMapping() { - return mapping; - } - - @Override - public JsonSchemaBuilder visitClassInfo(MClassInfo info) { - return createTypeInfoSchemaRef(info); - } - - @Override - public JsonSchemaBuilder visitClassRef(MClassRef info) { - return createTypeInfoSchemaRef(info); - } - - @Override - public JsonSchemaBuilder visitList(MList info) { - return new JsonSchemaBuilder().addType(JsonSchemaConstants.ARRAY_TYPE) - .addItem(info.getItemTypeInfo().acceptTypeInfoVisitor(this)); - } - - @Override - public JsonSchemaBuilder visitID(MID info) { - return new JsonSchemaBuilder() - .addRef(XmlSchemaJsonSchemaConstants.ID_TYPE_INFO_SCHEMA_REF); - } - - @Override - public JsonSchemaBuilder visitIDREF(MIDREF info) { - return new JsonSchemaBuilder() - .addRef(XmlSchemaJsonSchemaConstants.IDREF_TYPE_INFO_SCHEMA_REF); - } - - @Override - public JsonSchemaBuilder visitIDREFS(MIDREFS info) { - return new JsonSchemaBuilder() - .addRef(XmlSchemaJsonSchemaConstants.IDREFS_TYPE_INFO_SCHEMA_REF); - } - - @Override - public JsonSchemaBuilder visitBuiltinLeafInfo(MBuiltinLeafInfo info) { - - final O origin = this.originated.getOrigin(); - - final List simpleTypeNames = new LinkedList(); - 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(); - 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 - public JsonSchemaBuilder visitEnumLeafInfo(MEnumLeafInfo info) { - return createTypeInfoSchemaRef(info); - } - - @Override - public JsonSchemaBuilder visitWildcardTypeInfo(MWildcardTypeInfo info) { - return new JsonSchemaBuilder() - .addRef(JsonixJsonSchemaConstants.WILDCARD_TYPE_INFO_SCHEMA_REF); - } - - private JsonSchemaBuilder createTypeInfoSchemaRef( - MPackagedTypeInfo info) { - - final String typeInfoSchemaId = getModules().getSchemaId( - info.getPackageInfo().getPackageName()); - - final String schemaId = getMapping().getSchemaId(); - - final String mappingSchemaId = typeInfoSchemaId.equals(schemaId) ? "#" - : typeInfoSchemaId; - final String typeInfoRef = mappingSchemaId - + "/" - + JsonSchemaKeywords.definitions - + "/" - + info.getContainerLocalName(MappingCompiler.DEFAULT_SCOPED_NAME_DELIMITER); - return new JsonSchemaBuilder().addRef(typeInfoRef); - } -} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaTypeInfoProducer.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaTypeInfoProducer.java deleted file mode 100644 index 864c5df..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaTypeInfoProducer.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.hisrc.jsonix.compilation.jsonschema; - -import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder; -import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo; - -public interface JsonSchemaTypeInfoProducer, T, C extends T> { - - public JsonSchemaBuilder produce(MTI typeInfo); -} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/XmlSchemaJsonSchemaConstants.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/XmlSchemaJsonSchemaConstants.java index b075e2b..f83f141 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/XmlSchemaJsonSchemaConstants.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/XmlSchemaJsonSchemaConstants.java @@ -1,11 +1,5 @@ package org.hisrc.jsonix.compilation.jsonschema; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; - -import javax.xml.namespace.QName; - import org.hisrc.jsonix.jsonschema.JsonSchemaKeywords; import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants; @@ -17,32 +11,9 @@ private XmlSchemaJsonSchemaConstants() { public static final String SCHEMA_ID = JsonixJsonSchemaConstants.JSONIX_JSONSCHEMAS_BASE_URI + "/w3c/2001/XMLSchema.jsonschema#"; - public static final Map TYPE_NAME_SCHEMA_REFS; - static { - final Map tnsr = new LinkedHashMap(); - for (QName typeName : XmlSchemaConstants.TYPE_NAMES) { - tnsr.put(typeName, SCHEMA_ID + "/" + JsonSchemaKeywords.definitions - + "/" + typeName.getLocalPart()); - } - tnsr.put(XmlSchemaConstants.CALENDAR, - JsonixJsonSchemaConstants.CALENDAR_TYPE_INFO_SCHEMA_REF); - TYPE_NAME_SCHEMA_REFS = Collections.unmodifiableMap(tnsr); - } - - public static final String ID_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/" - + JsonSchemaKeywords.definitions + "/" - + XmlSchemaConstants.ID.getLocalPart(); - public static final String IDREFS_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/" - + JsonSchemaKeywords.definitions + "/" - + XmlSchemaConstants.IDREFS.getLocalPart(); - public static final String IDREF_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/" - + JsonSchemaKeywords.definitions + "/" - + XmlSchemaConstants.IDREF.getLocalPart(); - public static final String STRING_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/" - + JsonSchemaKeywords.definitions + "/" + public static final String STRING_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/" + JsonSchemaKeywords.definitions + "/" + XmlSchemaConstants.STRING.getLocalPart(); - public static final String QNAME_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/" - + JsonSchemaKeywords.definitions + "/" + public static final String QNAME_TYPE_INFO_SCHEMA_REF = SCHEMA_ID + "/" + JsonSchemaKeywords.definitions + "/" + XmlSchemaConstants.QNAME.getLocalPart(); } diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaClassInfoProducer.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/ClassInfoProducer.java similarity index 53% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaClassInfoProducer.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/ClassInfoProducer.java index 83a603e..b0da3de 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/JsonSchemaClassInfoProducer.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/ClassInfoProducer.java @@ -1,4 +1,4 @@ -package org.hisrc.jsonix.compilation.jsonschema; +package org.hisrc.jsonix.compilation.jsonschema.typeinfo; import java.math.BigInteger; import java.util.ArrayList; @@ -10,7 +10,9 @@ import org.apache.commons.lang3.Validate; import org.hisrc.jsonix.JsonixConstants; -import org.hisrc.jsonix.definition.Mapping; +import org.hisrc.jsonix.compilation.jsonschema.JsonSchemaMappingCompiler; +import org.hisrc.jsonix.compilation.jsonschema.JsonSchemaPropertyInfoProducerVisitor; +import org.hisrc.jsonix.compilation.jsonschema.JsonixJsonSchemaConstants; import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder; import org.hisrc.jsonix.jsonschema.JsonSchemaConstants; import org.hisrc.jsonix.naming.StandardNaming; @@ -22,37 +24,29 @@ import com.sun.tools.xjc.model.Multiplicity; -public class JsonSchemaClassInfoProducer implements - JsonSchemaTypeInfoProducer, T, C> { +public class ClassInfoProducer extends PackagedTypeInfoProducer { - private final JsonSchemaMappingCompiler mappingCompiler; - private final Mapping mapping; private final XSFunctionApplier multiplicityCounter = new XSFunctionApplier( ParticleMultiplicityCounter.INSTANCE); - public JsonSchemaClassInfoProducer( - JsonSchemaMappingCompiler mappingCompiler) { - Validate.notNull(mappingCompiler); - this.mappingCompiler = mappingCompiler; - this.mapping = mappingCompiler.getMapping(); - } + private MClassInfo classInfo; - public JsonSchemaMappingCompiler getMappingCompiler() { - return mappingCompiler; + public ClassInfoProducer(MClassInfo classInfo) { + super(Validate.notNull(classInfo)); + this.classInfo = classInfo; } @Override - public JsonSchemaBuilder produce(MClassInfo classInfo) { + public JsonSchemaBuilder compile(JsonSchemaMappingCompiler mappingCompiler) { final JsonSchemaBuilder classInfoSchema = new JsonSchemaBuilder(); classInfoSchema.addType(JsonSchemaConstants.OBJECT_TYPE); - final String localName = classInfo - .getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER); + final String localName = classInfo.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER); classInfoSchema.addTitle(localName); final MClassTypeInfo baseTypeInfo = classInfo.getBaseTypeInfo(); final JsonSchemaBuilder typeInfoSchema; if (baseTypeInfo != null) { - final JsonSchemaBuilder baseTypeInfoSchema = mappingCompiler - .createTypeInfoSchemaRef(baseTypeInfo, baseTypeInfo); + final JsonSchemaBuilder baseTypeInfoSchema = mappingCompiler.createTypeInfoSchemaRef(baseTypeInfo, + baseTypeInfo); typeInfoSchema = new JsonSchemaBuilder(); typeInfoSchema.addAllOf(baseTypeInfoSchema); typeInfoSchema.addAllOf(classInfoSchema); @@ -60,30 +54,23 @@ public JsonSchemaBuilder produce(MClassInfo classInfo) { typeInfoSchema = classInfoSchema; } - final Map propertyInfoSchemas = compilePropertyInfos(classInfo); - final List propertiesOrder = new ArrayList( - propertyInfoSchemas.size()); + final Map propertyInfoSchemas = compilePropertyInfos(mappingCompiler); + final List propertiesOrder = new ArrayList(propertyInfoSchemas.size()); propertiesOrder.addAll(propertyInfoSchemas.keySet()); classInfoSchema.addProperties(propertyInfoSchemas); for (MPropertyInfo propertyInfo : classInfo.getProperties()) { - final Multiplicity multiplicity = multiplicityCounter - .apply(propertyInfo.getOrigin()); - if (multiplicity != null && multiplicity.min != null - && multiplicity.min.compareTo(BigInteger.ZERO) > 0) { + final Multiplicity multiplicity = multiplicityCounter.apply(propertyInfo.getOrigin()); + if (multiplicity != null && multiplicity.min != null && multiplicity.min.compareTo(BigInteger.ZERO) > 0) { typeInfoSchema.addRequired(propertyInfo.getPrivateName()); } } - typeInfoSchema.add(JsonixJsonSchemaConstants.TYPE_TYPE_PROPERTY_NAME, - StandardNaming.CLASS_INFO); + typeInfoSchema.add(JsonixJsonSchemaConstants.TYPE_TYPE_PROPERTY_NAME, StandardNaming.CLASS_INFO); final QName typeName = classInfo.getTypeName(); if (typeName != null) { - typeInfoSchema - .add(JsonixJsonSchemaConstants.TYPE_NAME_PROPERTY_NAME, - new JsonSchemaBuilder() - .add(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME, - typeName.getLocalPart()) - .add(JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME, - typeName.getNamespaceURI())); + typeInfoSchema.add(JsonixJsonSchemaConstants.TYPE_NAME_PROPERTY_NAME, + new JsonSchemaBuilder() + .add(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME, typeName.getLocalPart()).add( + JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME, typeName.getNamespaceURI())); } if (!propertiesOrder.isEmpty()) { typeInfoSchema.add("propertiesOrder", propertiesOrder); @@ -91,17 +78,13 @@ public JsonSchemaBuilder produce(MClassInfo classInfo) { return typeInfoSchema; } - private Map compilePropertyInfos( - MClassInfo classInfo) { + private Map compilePropertyInfos(JsonSchemaMappingCompiler mappingCompiler) { final Map propertyInfoSchemas = new LinkedHashMap( classInfo.getProperties().size()); for (MPropertyInfo propertyInfo : classInfo.getProperties()) { - if (mapping.getPropertyInfos().contains(propertyInfo)) { - propertyInfoSchemas - .put(propertyInfo.getPrivateName(), - propertyInfo - .acceptPropertyInfoVisitor(new JsonSchemaPropertyInfoProducerVisitor( - this))); + if (mappingCompiler.getMapping().getPropertyInfos().contains(propertyInfo)) { + propertyInfoSchemas.put(propertyInfo.getPrivateName(), propertyInfo + .acceptPropertyInfoVisitor(new JsonSchemaPropertyInfoProducerVisitor(mappingCompiler))); } } return propertyInfoSchemas; diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/EnumLeafInfoProducer.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/EnumLeafInfoProducer.java new file mode 100644 index 0000000..6be48c0 --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/EnumLeafInfoProducer.java @@ -0,0 +1,60 @@ +package org.hisrc.jsonix.compilation.jsonschema.typeinfo; + +import javax.xml.namespace.QName; + +import org.apache.commons.lang3.Validate; +import org.hisrc.jsonix.JsonixConstants; +import org.hisrc.jsonix.compilation.jsonschema.JsonSchemaMappingCompiler; +import org.hisrc.jsonix.compilation.jsonschema.JsonixJsonSchemaConstants; +import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder; +import org.hisrc.jsonix.naming.StandardNaming; +import org.jvnet.jaxb2_commons.xml.bind.model.MEnumConstantInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo; + +public class EnumLeafInfoProducer extends PackagedTypeInfoProducer { + + private MEnumLeafInfo enumLeafInfo; + + public EnumLeafInfoProducer(MEnumLeafInfo enumLeafInfo) { + super(Validate.notNull(enumLeafInfo)); + this.enumLeafInfo = enumLeafInfo; + } + + @Override + public JsonSchemaBuilder compile(JsonSchemaMappingCompiler mappingCompiler) { + final JsonSchemaBuilder enumLeafInfoSchema = new JsonSchemaBuilder(); + final String localName = enumLeafInfo.getContainerLocalName(JsonixConstants.DEFAULT_SCOPED_NAME_DELIMITER); + enumLeafInfoSchema.addTitle(localName); + final MTypeInfo baseTypeInfo = enumLeafInfo.getBaseTypeInfo(); + final JsonSchemaBuilder typeInfoSchema; + final JsonSchemaBuilder baseTypeInfoSchema = mappingCompiler.createTypeInfoSchemaRef(enumLeafInfo, + baseTypeInfo); + typeInfoSchema = new JsonSchemaBuilder(); + typeInfoSchema.addAllOf(baseTypeInfoSchema); + + for (MEnumConstantInfo enumConstant : enumLeafInfo.getConstants()) { + final JsonSchemaBuilder enumConstantSchema = createEnumConstant(enumLeafInfo, enumConstant); + // TODO generate enums + if (enumConstantSchema != null) { + typeInfoSchema.addEnum(enumConstantSchema); + } + } + + typeInfoSchema.add(JsonixJsonSchemaConstants.TYPE_TYPE_PROPERTY_NAME, StandardNaming.ENUM_INFO); + final QName typeName = enumLeafInfo.getTypeName(); + if (typeName != null) { + typeInfoSchema.add(JsonixJsonSchemaConstants.TYPE_NAME_PROPERTY_NAME, + new JsonSchemaBuilder() + .add(JsonixJsonSchemaConstants.LOCAL_PART_PROPERTY_NAME, typeName.getLocalPart()).add( + JsonixJsonSchemaConstants.NAMESPACE_URI_PROPERTY_NAME, typeName.getNamespaceURI())); + } + + return typeInfoSchema; + } + + private JsonSchemaBuilder createEnumConstant(MEnumLeafInfo enumLeafInfo, + MEnumConstantInfo enumConstant) { + return null; + } +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/TypeInfoProducer.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/TypeInfoProducer.java new file mode 100644 index 0000000..7c153d1 --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/jsonschema/typeinfo/TypeInfoProducer.java @@ -0,0 +1,20 @@ +package org.hisrc.jsonix.compilation.jsonschema.typeinfo; + +import javax.json.JsonValue; + +import org.hisrc.jsonix.compilation.jsonschema.JsonSchemaMappingCompiler; +import org.hisrc.jsonix.jsonschema.JsonSchemaBuilder; + +import com.sun.xml.xsom.XmlString; + +public interface TypeInfoProducer { + + public JsonSchemaBuilder compile(JsonSchemaMappingCompiler mappingCompiler); + + public JsonSchemaBuilder createTypeInfoSchemaRef(JsonSchemaMappingCompiler mappingCompiler); + + public JsonValue createValue(JsonSchemaMappingCompiler mappingCompiler, XmlString item); + + public JsonValue createValue(JsonSchemaMappingCompiler mappingCompiler, String item); + +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/CreateTypeInfoCompilerTypeInfoVisitor.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/CreateTypeInfoCompilerTypeInfoVisitor.java deleted file mode 100644 index 0b2c891..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/CreateTypeInfoCompilerTypeInfoVisitor.java +++ /dev/null @@ -1,228 +0,0 @@ -/** - * Jsonix is a JavaScript library which allows you to convert between XML - * and JavaScript object structures. - * - * Copyright (c) 2010 - 2014, Alexey Valikov, Highsource.org - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * - * * Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.hisrc.jsonix.compilation.mapping; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.QName; - -import org.apache.commons.lang3.Validate; -import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.EnumLeafInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.ListCompiler; -import org.hisrc.jsonix.compilation.typeinfo.PackagedTypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.TypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.builtin.Base64BinaryTypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.builtin.BooleanTypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.builtin.DecimalTypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.builtin.DurationTypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.builtin.HexBinaryTypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.builtin.IntegerTypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.builtin.QNameTypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.builtin.StringTypeInfoCompiler; -import org.hisrc.jsonix.compilation.typeinfo.builtin.XMLGregorianCalendarTypeInfoCompiler; -import org.hisrc.jsonix.xml.xsom.CollectEnumerationValuesVisitor; -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; -import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo; -import org.jvnet.jaxb2_commons.xml.bind.model.MID; -import org.jvnet.jaxb2_commons.xml.bind.model.MIDREF; -import org.jvnet.jaxb2_commons.xml.bind.model.MIDREFS; -import org.jvnet.jaxb2_commons.xml.bind.model.MList; -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; -import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants; - -import com.sun.xml.xsom.XSComponent; -import com.sun.xml.xsom.XmlString; - -public class CreateTypeInfoCompilerTypeInfoVisitor - implements MTypeInfoVisitor> { - - private static final String IDREFS_TYPE_INFO_NAME = "IDREFS"; - private static final String IDREF_TYPE_INFO_NAME = "IDREF"; - private static final String ID_TYPE_INFO_NAME = "ID"; - - private Map> XSD_TYPE_MAPPING = new HashMap>(); - { - XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYTYPE, new BuiltinLeafInfoCompiler("AnyType")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYSIMPLETYPE, new BuiltinLeafInfoCompiler("AnySimpleType")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.STRING, new StringTypeInfoCompiler("String")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.NORMALIZEDSTRING, - new StringTypeInfoCompiler("NormalizedString")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.TOKEN, new StringTypeInfoCompiler("Token")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.LANGUAGE, new StringTypeInfoCompiler("Language")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.NAME, new StringTypeInfoCompiler("Name")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.NCNAME, new StringTypeInfoCompiler("NCName")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, new StringTypeInfoCompiler("ID")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, new StringTypeInfoCompiler("String")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, new StringTypeInfoCompiler("IDREF")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, new StringTypeInfoCompiler("String")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, new StringTypeInfoCompiler("IDREFS")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, new StringTypeInfoCompiler("Strings")); - // XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITY, new - // BuiltinLeafInfoCompiler("Entity")); - // XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITIES, new - // BuiltinLeafInfoCompiler("Entities")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKEN, new StringTypeInfoCompiler("NMToken")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKENS, new StringTypeInfoCompiler("NMTokens")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.BOOLEAN, new BooleanTypeInfoCompiler()); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.BASE64BINARY, new Base64BinaryTypeInfoCompiler()); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.HEXBINARY, new HexBinaryTypeInfoCompiler()); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.FLOAT, new DecimalTypeInfoCompiler("Float")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.DECIMAL, new DecimalTypeInfoCompiler("Decimal")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.INTEGER, new IntegerTypeInfoCompiler("Integer")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONPOSITIVEINTEGER, - new IntegerTypeInfoCompiler("NonPositiveInteger")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.NEGATIVEINTEGER, - new IntegerTypeInfoCompiler("NegativeInteger")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.LONG, new IntegerTypeInfoCompiler("Long")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.INT, new IntegerTypeInfoCompiler("Int")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.SHORT, new IntegerTypeInfoCompiler("Short")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.BYTE, new IntegerTypeInfoCompiler("Byte")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONNEGATIVEINTEGER, - new IntegerTypeInfoCompiler("NonNegativeInteger")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDLONG, new IntegerTypeInfoCompiler("UnsignedLong")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDINT, new IntegerTypeInfoCompiler("UnsignedInt")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDSHORT, new IntegerTypeInfoCompiler("UnsignedShort")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDBYTE, new IntegerTypeInfoCompiler("UnsignedByte")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.POSITIVEINTEGER, - new IntegerTypeInfoCompiler("PositiveInteger")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.DOUBLE, new DecimalTypeInfoCompiler("Double")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, new StringTypeInfoCompiler("AnyURI")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, new StringTypeInfoCompiler("String")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.QNAME, new QNameTypeInfoCompiler()); - // XSD_TYPE_MAPPING.put(XmlSchemaConstants.NOTATION, new - // BuiltinLeafInfoCompiler("Notation")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.DURATION, new DurationTypeInfoCompiler()); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATETIME, - new XMLGregorianCalendarTypeInfoCompiler("DateTime")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.TIME, new XMLGregorianCalendarTypeInfoCompiler("Time")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATE, new XMLGregorianCalendarTypeInfoCompiler("Date")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEARMONTH, - new XMLGregorianCalendarTypeInfoCompiler("GYearMonth")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEAR, new XMLGregorianCalendarTypeInfoCompiler("GYear")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTHDAY, - new XMLGregorianCalendarTypeInfoCompiler("GMonthDay")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.GDAY, new XMLGregorianCalendarTypeInfoCompiler("GDay")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTH, new XMLGregorianCalendarTypeInfoCompiler("GMonth")); - XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, new BuiltinLeafInfoCompiler("Calendar")); - // XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, new - // BuiltinLeafInfoCompiler("String")); - } - - private final MOriginated originated; - - public CreateTypeInfoCompilerTypeInfoVisitor(MOriginated originated) { - Validate.notNull(originated); - this.originated = originated; - } - - public TypeInfoCompiler visitEnumLeafInfo(MEnumLeafInfo info) { - return new EnumLeafInfoCompiler(info, info.getBaseTypeInfo().acceptTypeInfoVisitor(this)); - } - - public TypeInfoCompiler visitClassInfo(MClassInfo info) { - return new PackagedTypeInfoCompiler(info); - } - - @Override - public TypeInfoCompiler visitClassRef(MClassRef info) { - return new PackagedTypeInfoCompiler(info); - } - - public TypeInfoCompiler visitList(MList info) { - return new ListCompiler(info, info.getItemTypeInfo().acceptTypeInfoVisitor(this)); - } - - public TypeInfoCompiler visitBuiltinLeafInfo(MBuiltinLeafInfo info) { - - final O origin = this.originated.getOrigin(); - - final List simpleTypeNames = new LinkedList(); - final List enumerationValues = new LinkedList(); - 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()); - - final CollectEnumerationValuesVisitor collectEnumerationValuesVisitor = new CollectEnumerationValuesVisitor(); - component.visit(collectEnumerationValuesVisitor); - enumerationValues.addAll(collectEnumerationValuesVisitor.getValues()); - if (!enumerationValues.isEmpty()) { - System.out.println(enumerationValues); - } - } - } - - simpleTypeNames.add(info.getTypeName()); - - for (QName candidateName : simpleTypeNames) { - final TypeInfoCompiler typeInfoCompiler = XSD_TYPE_MAPPING.get(candidateName); - if (typeInfoCompiler != null) { - return typeInfoCompiler; - } - } - return null; - } - - public TypeInfoCompiler visitWildcardTypeInfo(MWildcardTypeInfo info) { - // TODO ???? - return null; - } - - @Override - public TypeInfoCompiler visitID(MID info) { - return new BuiltinLeafInfoCompiler(ID_TYPE_INFO_NAME); - } - - @Override - public TypeInfoCompiler visitIDREF(MIDREF info) { - return new BuiltinLeafInfoCompiler(IDREF_TYPE_INFO_NAME); - } - - @Override - public TypeInfoCompiler visitIDREFS(MIDREFS info) { - return new BuiltinLeafInfoCompiler(IDREFS_TYPE_INFO_NAME); - } -} \ No newline at end of file diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/CheckValueStringLiteralExpressionVisitor.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/IsLiteralEquals.java similarity index 50% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/CheckValueStringLiteralExpressionVisitor.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/IsLiteralEquals.java index 15dcc25..779b9a5 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/CheckValueStringLiteralExpressionVisitor.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/IsLiteralEquals.java @@ -11,19 +11,19 @@ import org.hisrc.jscm.codemodel.literal.JSNullLiteral; import org.hisrc.jscm.codemodel.literal.JSStringLiteral; -public class CheckValueStringLiteralExpressionVisitor extends - DefaultExpressionVisitor { +public class IsLiteralEquals extends DefaultExpressionVisitor + implements JSLiteralVisitor { private final String value; - public CheckValueStringLiteralExpressionVisitor(String value) { + public IsLiteralEquals(String value) { Validate.notNull(value); this.value = value; } @Override public Boolean visitLiteral(JSLiteral value) throws RuntimeException { - return value.acceptLiteralVisitor(new CheckValueStringLiteralVisitor()); + return value.acceptLiteralVisitor(this); } @Override @@ -31,34 +31,28 @@ public Boolean visitExpression(JSExpression value) throws RuntimeException { return Boolean.FALSE; } - private final class CheckValueStringLiteralVisitor implements - JSLiteralVisitor { - - @Override - public Boolean visit(JSStringLiteral value) { - return CheckValueStringLiteralExpressionVisitor.this.value.equals(value.asString()); - } - - @Override - public Boolean visit(JSNullLiteral value) { - return Boolean.FALSE; - } - - @Override - public Boolean visit(JSBooleanLiteral value) { - return Boolean.FALSE; - } + @Override + public Boolean visit(JSStringLiteral value) { + return IsLiteralEquals.this.value.equals(value.asString()); + } - @Override - public Boolean visit(JSDecimalIntegerLiteral value) { - return Boolean.FALSE; - } + @Override + public Boolean visit(JSNullLiteral value) { + return Boolean.FALSE; + } - @Override - public Boolean visit(JSDecimalNonIntegerLiteral value) { - return Boolean.FALSE; - } + @Override + public Boolean visit(JSBooleanLiteral value) { + return Boolean.FALSE; + } + @Override + public Boolean visit(JSDecimalIntegerLiteral value) { + return Boolean.FALSE; } + @Override + public Boolean visit(JSDecimalNonIntegerLiteral value) { + return Boolean.FALSE; + } } diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/MappingCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/MappingCompiler.java index 84970ad..dc6d4e3 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/MappingCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/MappingCompiler.java @@ -37,7 +37,6 @@ import java.util.LinkedHashSet; import java.util.Set; -import javax.xml.XMLConstants; import javax.xml.namespace.QName; import org.apache.commons.lang3.ObjectUtils; @@ -48,7 +47,10 @@ import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; import org.hisrc.jscm.codemodel.expression.JSMemberExpression; import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; -import org.hisrc.jsonix.compilation.typeinfo.TypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.ClassInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.CreateTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.EnumLeafInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.TypeInfoCompiler; import org.hisrc.jsonix.definition.Mapping; import org.hisrc.jsonix.definition.MappingDependency; import org.hisrc.jsonix.definition.Module; @@ -56,10 +58,8 @@ import org.hisrc.jsonix.definition.Output; import org.hisrc.jsonix.naming.Naming; import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo; -import org.jvnet.jaxb2_commons.xml.bind.model.MClassTypeInfo; import org.jvnet.jaxb2_commons.xml.bind.model.MElementInfo; import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeInfo; -import org.jvnet.jaxb2_commons.xml.bind.model.MEnumConstantInfo; import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo; import org.jvnet.jaxb2_commons.xml.bind.model.MPackageInfo; import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfo; @@ -188,76 +188,11 @@ private void compileEnumLeafInfos(JSArrayLiteral typeInfos) { } private JSObjectLiteral compileClassInfo(MClassInfo classInfo) { - final JSObjectLiteral classInfoMapping = this.codeModel.object(); - final String localName = classInfo.getContainerLocalName(DEFAULT_SCOPED_NAME_DELIMITER); - classInfoMapping.append(naming.localName(), this.codeModel.string(localName)); - final String targetNamespace = mapping.getTargetNamespaceURI(); - final QName defaultTypeName = new QName(targetNamespace, localName); - final QName typeName = classInfo.getTypeName(); - - if (!defaultTypeName.equals(typeName)) { - final JSAssignmentExpression typeNameExpression; - if (typeName == null) { - typeNameExpression = getCodeModel()._null(); - } else if (defaultTypeName.getNamespaceURI().equals(typeName.getNamespaceURI())) { - typeNameExpression = getCodeModel().string(typeName.getLocalPart()); - } else { - final JSObjectLiteral typeNameObject = getCodeModel().object(); - typeNameObject.append(naming.namespaceURI(), getCodeModel().string(typeName.getNamespaceURI())); - typeNameObject.append(naming.localPart(), getCodeModel().string(typeName.getLocalPart())); - if (!XMLConstants.DEFAULT_NS_PREFIX.equals(typeName.getPrefix())) { - typeNameObject.append(naming.prefix(), getCodeModel().string(typeName.getPrefix())); - } - typeNameExpression = typeNameObject; - } - classInfoMapping.append(naming.typeName(), typeNameExpression); - - } - - final MClassTypeInfo baseTypeInfo = classInfo.getBaseTypeInfo(); - if (baseTypeInfo != null) { - classInfoMapping.append(naming.baseTypeInfo(), - getTypeInfoCompiler(classInfo, baseTypeInfo).createTypeInfoDeclaration(this)); - } - final JSArrayLiteral ps = compilePropertyInfos(classInfo); - if (!ps.getElements().isEmpty()) { - classInfoMapping.append(naming.propertyInfos(), ps); - } - return classInfoMapping; + return new ClassInfoCompiler(classInfo).compile(this); } private JSObjectLiteral compileEnumLeafInfo(MEnumLeafInfo enumLeafInfo) { - final JSObjectLiteral mapping = this.codeModel.object(); - mapping.append(naming.type(), this.codeModel.string(naming.enumInfo())); - mapping.append(naming.localName(), - this.codeModel.string(enumLeafInfo.getContainerLocalName(DEFAULT_SCOPED_NAME_DELIMITER))); - - final MTypeInfo baseTypeInfo = enumLeafInfo.getBaseTypeInfo(); - if (baseTypeInfo != null) { - final TypeInfoCompiler baseTypeInfoCompiler = getTypeInfoCompiler(enumLeafInfo, baseTypeInfo); - final JSAssignmentExpression baseTypeInfoDeclaration = baseTypeInfoCompiler.createTypeInfoDeclaration(this); - if (!baseTypeInfoDeclaration - .acceptExpressionVisitor(new CheckValueStringLiteralExpressionVisitor("String"))) { - mapping.append(naming.baseTypeInfo(), baseTypeInfoDeclaration); - } - final JSArrayLiteral values = this.codeModel.array(); - for (MEnumConstantInfo enumConstantInfo : enumLeafInfo.getConstants()) { - values.append(baseTypeInfoCompiler.createValue(this.codeModel, enumConstantInfo.getLexicalValue())); - } - mapping.append(naming.values(), values); - } - return mapping; - } - - private JSArrayLiteral compilePropertyInfos(MClassInfo classInfo) { - final JSArrayLiteral propertyInfoMappings = this.codeModel.array(); - for (MPropertyInfo propertyInfo : classInfo.getProperties()) { - if (mapping.getPropertyInfos().contains(propertyInfo)) { - propertyInfoMappings - .append(propertyInfo.acceptPropertyInfoVisitor(new PropertyInfoVisitor(this))); - } - } - return propertyInfoMappings; + return new EnumLeafInfoCompiler(enumLeafInfo).compile(this); } private void compileElementInfos(JSArrayLiteral eis) { @@ -277,7 +212,7 @@ private JSObjectLiteral compileElementInfo(MElementInfo elementInfo) { QName elementName = elementInfo.getElementName(); value.append(naming.elementName(), createElementNameExpression(elementName)); if (typeInfoDeclaration != null) { - if (!typeInfoDeclaration.acceptExpressionVisitor(new CheckValueStringLiteralExpressionVisitor("String"))) { + if (!typeInfoDeclaration.acceptExpressionVisitor(new IsLiteralEquals("String"))) { value.append(naming.typeInfo(), typeInfoDeclaration); } } @@ -341,6 +276,6 @@ public , O> TypeInfoCompiler getTypeIn public TypeInfoCompiler getTypeInfoCompiler(MOriginated originated, MTypeInfo typeInfo) { - return typeInfo.acceptTypeInfoVisitor(new CreateTypeInfoCompilerTypeInfoVisitor(originated)); + return typeInfo.acceptTypeInfoVisitor(new CreateTypeInfoCompiler(originated)); } } \ No newline at end of file diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/PropertyInfoVisitor.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/PropertyInfoVisitor.java index a651c78..c8b9b4f 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/PropertyInfoVisitor.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/PropertyInfoVisitor.java @@ -11,10 +11,9 @@ import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; import org.hisrc.jscm.codemodel.expression.JSMemberExpression; import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; -import org.hisrc.jsonix.compilation.typeinfo.TypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.TypeInfoCompiler; import org.hisrc.jsonix.naming.Naming; import org.hisrc.jsonix.xml.xsom.CollectEnumerationValuesVisitor; -import org.hisrc.jsonix.xml.xsom.CollectSimpleTypeNamesVisitor; import org.hisrc.jsonix.xml.xsom.ParticleMultiplicityCounter; import org.hisrc.xml.xsom.SchemaComponentAware; import org.hisrc.xml.xsom.XSFunctionApplier; @@ -43,7 +42,7 @@ import com.sun.xml.xsom.XSComponent; import com.sun.xml.xsom.XmlString; -final class PropertyInfoVisitor implements MPropertyInfoVisitor { +public final class PropertyInfoVisitor implements MPropertyInfoVisitor { private final JSCodeModel codeModel; private final MappingCompiler mappingCompiler; @@ -87,47 +86,56 @@ private , O> void createTypedOptions(final M final JSObjectLiteral options) { final MTypeInfo typeInfo = info.getTypeInfo(); - typeInfo.acceptTypeInfoVisitor(new DefaultTypeInfoVisitor() { + typeInfo.acceptTypeInfoVisitor(new DefaultTypeInfoVisitor>() { + @Override - public Void visitTypeInfo(MTypeInfo typeInfo) { + public TypeInfoCompiler visitTypeInfo(MTypeInfo typeInfo) { final TypeInfoCompiler typeInfoCompiler = PropertyInfoVisitor.this.mappingCompiler .getTypeInfoCompiler(info, typeInfo); - final JSAssignmentExpression typeInfoDeclaration = typeInfoCompiler.createTypeInfoDeclaration(PropertyInfoVisitor.this.mappingCompiler); + final JSAssignmentExpression typeInfoDeclaration = typeInfoCompiler + .createTypeInfoDeclaration(PropertyInfoVisitor.this.mappingCompiler); if (!typeInfoDeclaration - .acceptExpressionVisitor(new CheckValueStringLiteralExpressionVisitor("String"))) { + .acceptExpressionVisitor(new IsLiteralEquals("String"))) { options.append(naming.typeInfo(), typeInfoDeclaration); } - - if (info instanceof MOriginated) - { + return typeInfoCompiler; + } + + @Override + public TypeInfoCompiler visitBuiltinLeafInfo(MBuiltinLeafInfo typeInfo) { + final TypeInfoCompiler typeInfoCompiler = visitTypeInfo(typeInfo); + + if (info instanceof MOriginated) { MOriginated originated = (MOriginated) info; Object origin = originated.getOrigin(); if (origin instanceof SchemaComponentAware) { final XSComponent component = ((SchemaComponentAware) origin).getSchemaComponent(); if (component != null) { - + final CollectEnumerationValuesVisitor collectEnumerationValuesVisitor = new CollectEnumerationValuesVisitor(); component.visit(collectEnumerationValuesVisitor); final List enumerationValues = collectEnumerationValuesVisitor.getValues(); - if (!enumerationValues.isEmpty()) - { + if (enumerationValues != null && !enumerationValues.isEmpty()) { final JSArrayLiteral values = PropertyInfoVisitor.this.codeModel.array(); - for (XmlString enumerationValue : enumerationValues) - { - values.append(typeInfoCompiler.createValue(codeModel, enumerationValue)); + boolean valueSupported = true; + for (XmlString enumerationValue : enumerationValues) { + final JSAssignmentExpression value = typeInfoCompiler.createValue(PropertyInfoVisitor.this.mappingCompiler, + enumerationValue); + if (value == null) { + valueSupported = false; + break; + } else { + values.append(value); + } + } + if (valueSupported) { + options.append(naming.values(), values); } - options.append(naming.values(), values); } } } } - - return null; - } - - @Override - public Void visitBuiltinLeafInfo(MBuiltinLeafInfo info) { - return super.visitBuiltinLeafInfo(info); + return typeInfoCompiler; } }); } @@ -142,7 +150,7 @@ public Void visitTypeInfo(MTypeInfo typeInfo) { .getTypeInfoCompiler(propertyInfo, typeInfo) .createTypeInfoDeclaration(PropertyInfoVisitor.this.mappingCompiler); if (!typeInfoDeclaration - .acceptExpressionVisitor(new CheckValueStringLiteralExpressionVisitor("String"))) { + .acceptExpressionVisitor(new IsLiteralEquals("String"))) { options.append(naming.typeInfo(), typeInfoDeclaration); } return null; @@ -173,7 +181,7 @@ private , O> void createElementTypeInfoOptio private , O> void createElementTypeInfoOptions(M info, String privateName, QName elementName, JSObjectLiteral options) { JSMemberExpression elementNameExpression = mappingCompiler.createElementNameExpression(elementName); - if (!elementNameExpression.acceptExpressionVisitor(new CheckValueStringLiteralExpressionVisitor(privateName))) { + if (!elementNameExpression.acceptExpressionVisitor(new IsLiteralEquals(privateName))) { options.append(naming.elementName(), elementNameExpression); } createTypedOptions(info, options); @@ -243,7 +251,7 @@ public JSObjectLiteral visitAttributePropertyInfo(MAttributePropertyInfo i final JSMemberExpression attributeNameExpression = mappingCompiler .createAttributeNameExpression(info.getAttributeName()); if (!attributeNameExpression - .acceptExpressionVisitor(new CheckValueStringLiteralExpressionVisitor(info.getPrivateName()))) { + .acceptExpressionVisitor(new IsLiteralEquals(info.getPrivateName()))) { options.append(naming.attributeName(), attributeNameExpression); } options.append(naming.type(), this.codeModel.string(naming.attribute())); diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/BuiltinLeafInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/BuiltinLeafInfoCompiler.java similarity index 53% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/BuiltinLeafInfoCompiler.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/BuiltinLeafInfoCompiler.java index ca6a3b6..f7c437c 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/BuiltinLeafInfoCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/BuiltinLeafInfoCompiler.java @@ -1,11 +1,13 @@ -package org.hisrc.jsonix.compilation.typeinfo; +package org.hisrc.jsonix.compilation.mapping.typeinfo; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; +import javax.xml.namespace.QName; import org.apache.commons.lang3.Validate; -import org.hisrc.jscm.codemodel.JSCodeModel; import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; +import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; +import org.hisrc.jsonix.compilation.jsonschema.typeinfo.TypeInfoProducer; import org.hisrc.jsonix.compilation.mapping.MappingCompiler; import com.sun.xml.xsom.XmlString; @@ -16,8 +18,12 @@ public class BuiltinLeafInfoCompiler implements TypeInfoCompi protected final DatatypeFactory datatypeFactory; - public BuiltinLeafInfoCompiler(String name) { + private QName qualifiedName; + + public BuiltinLeafInfoCompiler(String name, QName qualifiedName) { Validate.notNull(name); + Validate.notNull(qualifiedName); + this.qualifiedName = qualifiedName; this.name = name; try { this.datatypeFactory = DatatypeFactory.newInstance(); @@ -33,12 +39,17 @@ public JSAssignmentExpression createTypeInfoDeclaration(MappingCompiler ma } @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, XmlString item) { - return createValue(codeModel, item.value); + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, XmlString item) { + return createValue(mappingCompiler, item.value); + } + + @Override + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + return null; } @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { - return codeModel.string(item); + public JSObjectLiteral compile(MappingCompiler mappingCompiler) { + throw new UnsupportedOperationException(); } } diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ClassInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ClassInfoCompiler.java new file mode 100644 index 0000000..3142e2a --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ClassInfoCompiler.java @@ -0,0 +1,84 @@ +package org.hisrc.jsonix.compilation.mapping.typeinfo; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; + +import org.apache.commons.lang3.Validate; +import org.hisrc.jscm.codemodel.JSCodeModel; +import org.hisrc.jscm.codemodel.expression.JSArrayLiteral; +import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; +import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.compilation.mapping.PropertyInfoVisitor; +import org.hisrc.jsonix.definition.Mapping; +import org.hisrc.jsonix.naming.Naming; +import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MClassTypeInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfo; + +public class ClassInfoCompiler extends PackagedTypeInfoCompiler { + + private MClassInfo classInfo; + + public ClassInfoCompiler(MClassInfo classInfo) { + super(Validate.notNull(classInfo)); + this.classInfo = classInfo; + } + + @Override + public JSObjectLiteral compile(MappingCompiler mappingCompiler) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); + final Mapping mapping = mappingCompiler.getMapping(); + final Naming naming = mappingCompiler.getNaming(); + + final JSObjectLiteral classInfoMapping = codeModel.object(); + final String localName = classInfo.getContainerLocalName(MappingCompiler.DEFAULT_SCOPED_NAME_DELIMITER); + classInfoMapping.append(naming.localName(), codeModel.string(localName)); + final String targetNamespace = mapping.getTargetNamespaceURI(); + final QName defaultTypeName = new QName(targetNamespace, localName); + final QName typeName = classInfo.getTypeName(); + + if (!defaultTypeName.equals(typeName)) { + final JSAssignmentExpression typeNameExpression; + if (typeName == null) { + typeNameExpression = codeModel._null(); + } else if (defaultTypeName.getNamespaceURI().equals(typeName.getNamespaceURI())) { + typeNameExpression = codeModel.string(typeName.getLocalPart()); + } else { + final JSObjectLiteral typeNameObject = codeModel.object(); + typeNameObject.append(naming.namespaceURI(), codeModel.string(typeName.getNamespaceURI())); + typeNameObject.append(naming.localPart(), codeModel.string(typeName.getLocalPart())); + if (!XMLConstants.DEFAULT_NS_PREFIX.equals(typeName.getPrefix())) { + typeNameObject.append(naming.prefix(), codeModel.string(typeName.getPrefix())); + } + typeNameExpression = typeNameObject; + } + classInfoMapping.append(naming.typeName(), typeNameExpression); + + } + + final MClassTypeInfo baseTypeInfo = classInfo.getBaseTypeInfo(); + if (baseTypeInfo != null) { + classInfoMapping.append(naming.baseTypeInfo(), mappingCompiler.getTypeInfoCompiler(classInfo, baseTypeInfo) + .createTypeInfoDeclaration(mappingCompiler)); + } + final JSArrayLiteral ps = compilePropertyInfos(mappingCompiler); + if (!ps.getElements().isEmpty()) { + classInfoMapping.append(naming.propertyInfos(), ps); + } + return classInfoMapping; + } + + private JSArrayLiteral compilePropertyInfos(MappingCompiler mappingCompiler) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); + final Mapping mapping = mappingCompiler.getMapping(); + final JSArrayLiteral propertyInfoMappings = codeModel.array(); + for (MPropertyInfo propertyInfo : classInfo.getProperties()) { + if (mapping.getPropertyInfos().contains(propertyInfo)) { + propertyInfoMappings + .append(propertyInfo.acceptPropertyInfoVisitor(new PropertyInfoVisitor(mappingCompiler))); + } + } + return propertyInfoMappings; + } +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ClassRefCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ClassRefCompiler.java new file mode 100644 index 0000000..97b5bd8 --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ClassRefCompiler.java @@ -0,0 +1,22 @@ +package org.hisrc.jsonix.compilation.mapping.typeinfo; + +import org.apache.commons.lang3.Validate; +import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.jvnet.jaxb2_commons.xml.bind.model.MClassRef; + +public class ClassRefCompiler extends PackagedTypeInfoCompiler { + + private MClassRef classRef; + + public ClassRefCompiler(MClassRef classRef) { + super(Validate.notNull(classRef)); + this.classRef = classRef; + } + + @Override + public JSObjectLiteral compile(MappingCompiler mappingCompiler) { + throw new UnsupportedOperationException(); + } + +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/CreateTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/CreateTypeInfoCompiler.java new file mode 100644 index 0000000..2643452 --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/CreateTypeInfoCompiler.java @@ -0,0 +1,247 @@ +/** + * Jsonix is a JavaScript library which allows you to convert between XML + * and JavaScript object structures. + * + * Copyright (c) 2010 - 2014, Alexey Valikov, Highsource.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * * Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.hisrc.jsonix.compilation.mapping.typeinfo; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import org.apache.commons.lang3.Validate; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.Base64BinaryTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.BooleanTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.DecimalTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.DurationTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.HexBinaryTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.IntegerTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.NormalizedStringTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.QNameTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.StringTypeInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.builtin.XMLGregorianCalendarTypeInfoCompiler; +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; +import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MID; +import org.jvnet.jaxb2_commons.xml.bind.model.MIDREF; +import org.jvnet.jaxb2_commons.xml.bind.model.MIDREFS; +import org.jvnet.jaxb2_commons.xml.bind.model.MList; +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; +import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants; + +import com.sun.xml.xsom.XSComponent; + +public class CreateTypeInfoCompiler implements MTypeInfoVisitor> { + + private static final String IDREFS_TYPE_INFO_NAME = "IDREFS"; + private static final String IDREF_TYPE_INFO_NAME = "IDREF"; + private static final String ID_TYPE_INFO_NAME = "ID"; + + private Map> XSD_TYPE_MAPPING = new HashMap>(); + { + XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYTYPE, + new BuiltinLeafInfoCompiler("AnyType", XmlSchemaConstants.ANYTYPE)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYSIMPLETYPE, + new BuiltinLeafInfoCompiler("AnySimpleType", XmlSchemaConstants.ANYSIMPLETYPE)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.STRING, + new StringTypeInfoCompiler("String", XmlSchemaConstants.STRING)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.NORMALIZEDSTRING, + new NormalizedStringTypeInfoCompiler("NormalizedString", XmlSchemaConstants.NORMALIZEDSTRING)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.TOKEN, + new NormalizedStringTypeInfoCompiler("Token", XmlSchemaConstants.TOKEN)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.LANGUAGE, + new NormalizedStringTypeInfoCompiler("Language", XmlSchemaConstants.LANGUAGE)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.NAME, + new NormalizedStringTypeInfoCompiler("Name", XmlSchemaConstants.NAME)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.NCNAME, + new NormalizedStringTypeInfoCompiler("NCName", XmlSchemaConstants.NCNAME)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, + new BuiltinLeafInfoCompiler("ID", XmlSchemaConstants.ID)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.ID, + new BuiltinLeafInfoCompiler("String", XmlSchemaConstants.ID)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, + new BuiltinLeafInfoCompiler("IDREF", XmlSchemaConstants.IDREF)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREF, + new BuiltinLeafInfoCompiler("String", XmlSchemaConstants.IDREF)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, + new BuiltinLeafInfoCompiler("IDREFS", XmlSchemaConstants.IDREFS)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.IDREFS, + new BuiltinLeafInfoCompiler("Strings", XmlSchemaConstants.IDREFS)); + // XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITY, new + // BuiltinLeafInfoCompiler("Entity")); + // XSD_TYPE_MAPPING.put(XmlSchemaConstants.ENTITIES, new + // BuiltinLeafInfoCompiler("Entities")); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKEN, + new NormalizedStringTypeInfoCompiler("NMToken", XmlSchemaConstants.NMTOKEN)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.NMTOKENS, + new NormalizedStringTypeInfoCompiler("NMTokens", XmlSchemaConstants.NMTOKENS)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.BOOLEAN, new BooleanTypeInfoCompiler()); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.BASE64BINARY, new Base64BinaryTypeInfoCompiler()); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.HEXBINARY, new HexBinaryTypeInfoCompiler()); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.FLOAT, + new DecimalTypeInfoCompiler("Float", XmlSchemaConstants.FLOAT)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.DECIMAL, + new DecimalTypeInfoCompiler("Decimal", XmlSchemaConstants.DECIMAL)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.INTEGER, + new IntegerTypeInfoCompiler("Integer", XmlSchemaConstants.INTEGER)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONPOSITIVEINTEGER, + new IntegerTypeInfoCompiler("NonPositiveInteger", XmlSchemaConstants.NONPOSITIVEINTEGER)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.NEGATIVEINTEGER, + new IntegerTypeInfoCompiler("NegativeInteger", XmlSchemaConstants.NEGATIVEINTEGER)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.LONG, + new IntegerTypeInfoCompiler("Long", XmlSchemaConstants.LONG)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.INT, + new IntegerTypeInfoCompiler("Int", XmlSchemaConstants.INT)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.SHORT, + new IntegerTypeInfoCompiler("Short", XmlSchemaConstants.SHORT)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.BYTE, + new IntegerTypeInfoCompiler("Byte", XmlSchemaConstants.BYTE)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.NONNEGATIVEINTEGER, + new IntegerTypeInfoCompiler("NonNegativeInteger", XmlSchemaConstants.NONNEGATIVEINTEGER)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDLONG, + new IntegerTypeInfoCompiler("UnsignedLong", XmlSchemaConstants.UNSIGNEDLONG)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDINT, + new IntegerTypeInfoCompiler("UnsignedInt", XmlSchemaConstants.UNSIGNEDINT)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDSHORT, + new IntegerTypeInfoCompiler("UnsignedShort", XmlSchemaConstants.UNSIGNEDSHORT)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.UNSIGNEDBYTE, + new IntegerTypeInfoCompiler("UnsignedByte", XmlSchemaConstants.UNSIGNEDBYTE)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.POSITIVEINTEGER, + new IntegerTypeInfoCompiler("PositiveInteger", XmlSchemaConstants.POSITIVEINTEGER)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.DOUBLE, + new DecimalTypeInfoCompiler("Double", XmlSchemaConstants.DOUBLE)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, + new NormalizedStringTypeInfoCompiler("AnyURI", XmlSchemaConstants.ANYURI)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.ANYURI, + new NormalizedStringTypeInfoCompiler("String", XmlSchemaConstants.ANYURI)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.QNAME, new QNameTypeInfoCompiler()); + // XSD_TYPE_MAPPING.put(XmlSchemaConstants.NOTATION, new + // BuiltinLeafInfoCompiler("Notation")); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.DURATION, new DurationTypeInfoCompiler()); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATETIME, + new XMLGregorianCalendarTypeInfoCompiler("DateTime", XmlSchemaConstants.DATETIME)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.TIME, + new XMLGregorianCalendarTypeInfoCompiler("Time", XmlSchemaConstants.TIME)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.DATE, + new XMLGregorianCalendarTypeInfoCompiler("Date", XmlSchemaConstants.DATE)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEARMONTH, + new XMLGregorianCalendarTypeInfoCompiler("GYearMonth", XmlSchemaConstants.GYEARMONTH)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.GYEAR, + new XMLGregorianCalendarTypeInfoCompiler("GYear", XmlSchemaConstants.GYEAR)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTHDAY, + new XMLGregorianCalendarTypeInfoCompiler("GMonthDay", XmlSchemaConstants.GMONTHDAY)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.GDAY, + new XMLGregorianCalendarTypeInfoCompiler("GDay", XmlSchemaConstants.GDAY)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.GMONTH, + new XMLGregorianCalendarTypeInfoCompiler("GMonth", XmlSchemaConstants.GMONTH)); + XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, + new BuiltinLeafInfoCompiler("Calendar", XmlSchemaConstants.CALENDAR)); + // XSD_TYPE_MAPPING.put(XmlSchemaConstants.CALENDAR, new + // BuiltinLeafInfoCompiler("String")); + } + + private final MOriginated originated; + + public CreateTypeInfoCompiler(MOriginated originated) { + Validate.notNull(originated); + this.originated = originated; + } + + public TypeInfoCompiler visitEnumLeafInfo(MEnumLeafInfo info) { + return new EnumLeafInfoCompiler(info); + } + + public TypeInfoCompiler visitClassInfo(MClassInfo info) { + return new ClassInfoCompiler(info); + } + + @Override + public TypeInfoCompiler visitClassRef(MClassRef info) { + return new ClassRefCompiler(info); + } + + public TypeInfoCompiler visitList(MList info) { + return new ListCompiler(info, info.getItemTypeInfo().acceptTypeInfoVisitor(this)); + } + + public TypeInfoCompiler visitBuiltinLeafInfo(MBuiltinLeafInfo info) { + + final O origin = this.originated.getOrigin(); + + final List simpleTypeNames = new LinkedList(); + 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()); + + for (QName candidateName : simpleTypeNames) { + final TypeInfoCompiler typeInfoCompiler = XSD_TYPE_MAPPING.get(candidateName); + if (typeInfoCompiler != null) { + return typeInfoCompiler; + } + } + return null; + } + + public TypeInfoCompiler visitWildcardTypeInfo(MWildcardTypeInfo info) { + throw new UnsupportedOperationException(); + } + + @Override + public TypeInfoCompiler visitID(MID info) { + return new BuiltinLeafInfoCompiler(ID_TYPE_INFO_NAME, XmlSchemaConstants.ID); + } + + @Override + public TypeInfoCompiler visitIDREF(MIDREF info) { + return new BuiltinLeafInfoCompiler(IDREF_TYPE_INFO_NAME, XmlSchemaConstants.IDREF); + } + + @Override + public TypeInfoCompiler visitIDREFS(MIDREFS info) { + return new BuiltinLeafInfoCompiler(IDREFS_TYPE_INFO_NAME, XmlSchemaConstants.IDREFS); + } +} \ No newline at end of file diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/EnumLeafInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/EnumLeafInfoCompiler.java new file mode 100644 index 0000000..68ae2ca --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/EnumLeafInfoCompiler.java @@ -0,0 +1,76 @@ +package org.hisrc.jsonix.compilation.mapping.typeinfo; + +import org.apache.commons.lang3.Validate; +import org.hisrc.jscm.codemodel.JSCodeModel; +import org.hisrc.jscm.codemodel.expression.JSArrayLiteral; +import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; +import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; +import org.hisrc.jsonix.compilation.mapping.IsLiteralEquals; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.naming.Naming; +import org.jvnet.jaxb2_commons.xml.bind.model.MEnumConstantInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo; + +import com.sun.xml.xsom.XmlString; + +public class EnumLeafInfoCompiler extends PackagedTypeInfoCompiler { + + private final MEnumLeafInfo enumLeafInfo; + + public EnumLeafInfoCompiler(MEnumLeafInfo enumLeafInfo) { + super(Validate.notNull(enumLeafInfo)); + this.enumLeafInfo = enumLeafInfo; + } + + @Override + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, XmlString item) { + final MTypeInfo baseTypeInfo = enumLeafInfo.getBaseTypeInfo(); + final TypeInfoCompiler baseTypeInfoCompiler = mappingCompiler.getTypeInfoCompiler(enumLeafInfo, + baseTypeInfo); + return baseTypeInfoCompiler.createValue(mappingCompiler, item); + } + + @Override + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + final MTypeInfo baseTypeInfo = enumLeafInfo.getBaseTypeInfo(); + final TypeInfoCompiler baseTypeInfoCompiler = mappingCompiler.getTypeInfoCompiler(enumLeafInfo, + baseTypeInfo); + return baseTypeInfoCompiler.createValue(mappingCompiler, item); + } + + @Override + public JSObjectLiteral compile(MappingCompiler mappingCompiler) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); + final JSObjectLiteral mapping = codeModel.object(); + final Naming naming = mappingCompiler.getNaming(); + mapping.append(naming.type(), codeModel.string(naming.enumInfo())); + mapping.append(naming.localName(), + codeModel.string(enumLeafInfo.getContainerLocalName(MappingCompiler.DEFAULT_SCOPED_NAME_DELIMITER))); + + final MTypeInfo baseTypeInfo = enumLeafInfo.getBaseTypeInfo(); + final TypeInfoCompiler baseTypeInfoCompiler = mappingCompiler.getTypeInfoCompiler(enumLeafInfo, + baseTypeInfo); + final JSAssignmentExpression baseTypeInfoDeclaration = baseTypeInfoCompiler + .createTypeInfoDeclaration(mappingCompiler); + if (!baseTypeInfoDeclaration.acceptExpressionVisitor(new IsLiteralEquals("String"))) { + mapping.append(naming.baseTypeInfo(), baseTypeInfoDeclaration); + } + final JSArrayLiteral values = codeModel.array(); + boolean valuesSupported = true; + for (MEnumConstantInfo enumConstantInfo : enumLeafInfo.getConstants()) { + final JSAssignmentExpression value = baseTypeInfoCompiler.createValue(mappingCompiler, enumConstantInfo.getLexicalValue()); + if (value == null) { + valuesSupported = false; + break; + } else { + values.append(value); + } + } + if (valuesSupported) { + mapping.append(naming.values(), values); + } + return mapping; + } + +} \ No newline at end of file diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/ListCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ListCompiler.java similarity index 61% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/ListCompiler.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ListCompiler.java index e4afcc0..5bb4e6a 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/ListCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/ListCompiler.java @@ -1,11 +1,11 @@ -package org.hisrc.jsonix.compilation.typeinfo; +package org.hisrc.jsonix.compilation.mapping.typeinfo; import org.apache.commons.lang3.Validate; import org.hisrc.jscm.codemodel.JSCodeModel; import org.hisrc.jscm.codemodel.expression.JSArrayLiteral; import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; -import org.hisrc.jsonix.compilation.mapping.CheckValueStringLiteralExpressionVisitor; +import org.hisrc.jsonix.compilation.mapping.IsLiteralEquals; import org.hisrc.jsonix.compilation.mapping.MappingCompiler; import org.hisrc.jsonix.naming.Naming; import org.jvnet.jaxb2_commons.xml.bind.model.MList; @@ -34,34 +34,47 @@ public JSAssignmentExpression createTypeInfoDeclaration(MappingCompiler ma list.append(naming.type(), codeModel.string(naming.list())); final JSAssignmentExpression typeInfoDeclaration = this.itemTypeInfoCompiler .createTypeInfoDeclaration(mappingCompiler); - if (!typeInfoDeclaration.acceptExpressionVisitor(new CheckValueStringLiteralExpressionVisitor("String"))) { + if (!typeInfoDeclaration.acceptExpressionVisitor(new IsLiteralEquals("String"))) { list.append(naming.baseTypeInfo(), typeInfoDeclaration); } return list; } @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, XmlString item) { - final String value = item.value; - final String[] values = value.split(" "); + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, XmlString item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); + final String[] values = item.value.split(" "); final JSArrayLiteral result = codeModel.array(); - for (String v : values) { - if (!v.isEmpty()) { - result.append(itemTypeInfoCompiler.createValue(codeModel, new XmlString(v, item.context))); + for (String value : values) { + final JSAssignmentExpression v = itemTypeInfoCompiler.createValue(mappingCompiler, + new XmlString(value, item.context)); + if (v == null) { + return null; + } else { + result.append(v); } } return result; } @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); final String[] values = item.split(" "); final JSArrayLiteral result = codeModel.array(); for (String value : values) { - if (!value.isEmpty()) { - result.append(itemTypeInfoCompiler.createValue(codeModel, value)); + final JSAssignmentExpression v = itemTypeInfoCompiler.createValue(mappingCompiler, value); + if (v == null) { + return null; + } else { + result.append(v); } } return result; } + + @Override + public JSObjectLiteral compile(MappingCompiler mappingCompiler) { + throw new UnsupportedOperationException(); + } } diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/PackagedTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/PackagedTypeInfoCompiler.java similarity index 76% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/PackagedTypeInfoCompiler.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/PackagedTypeInfoCompiler.java index 532ae2d..580949f 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/PackagedTypeInfoCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/PackagedTypeInfoCompiler.java @@ -1,4 +1,4 @@ -package org.hisrc.jsonix.compilation.typeinfo; +package org.hisrc.jsonix.compilation.mapping.typeinfo; import org.apache.commons.lang3.Validate; import org.hisrc.jscm.codemodel.JSCodeModel; @@ -9,7 +9,7 @@ import com.sun.xml.xsom.XmlString; -public class PackagedTypeInfoCompiler implements TypeInfoCompiler { +public abstract class PackagedTypeInfoCompiler implements TypeInfoCompiler { private final MPackagedTypeInfo typeInfo; @@ -31,14 +31,14 @@ public JSAssignmentExpression createTypeInfoDeclaration(MappingCompiler ma + this.typeInfo.getContainerLocalName(MappingCompiler.DEFAULT_SCOPED_NAME_DELIMITER); return codeModel.string(typeInfoName); } - + @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, XmlString item) { - throw new UnsupportedOperationException(); + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, XmlString item) { + return createValue(mappingCompiler, item.value); } - + @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { - throw new UnsupportedOperationException(); + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + return null; } } diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/TypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/TypeInfoCompiler.java new file mode 100644 index 0000000..42a413c --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/TypeInfoCompiler.java @@ -0,0 +1,18 @@ +package org.hisrc.jsonix.compilation.mapping.typeinfo; + +import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; +import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; + +import com.sun.xml.xsom.XmlString; + +public interface TypeInfoCompiler { + + public JSAssignmentExpression createTypeInfoDeclaration(MappingCompiler mappingCompiler); + + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, XmlString item); + + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item); + + public JSObjectLiteral compile(MappingCompiler mappingCompiler); +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/Base64BinaryTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/Base64BinaryTypeInfoCompiler.java similarity index 61% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/Base64BinaryTypeInfoCompiler.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/Base64BinaryTypeInfoCompiler.java index 22d0c97..c0b5cc1 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/Base64BinaryTypeInfoCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/Base64BinaryTypeInfoCompiler.java @@ -1,11 +1,13 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; import javax.xml.bind.DatatypeConverter; +import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants; + public class Base64BinaryTypeInfoCompiler extends BinaryTypeInfoCompiler { public Base64BinaryTypeInfoCompiler() { - super("Base64Binary"); + super("Base64Binary", XmlSchemaConstants.BASE64BINARY); } @Override diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/BinaryTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/BinaryTypeInfoCompiler.java similarity index 53% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/BinaryTypeInfoCompiler.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/BinaryTypeInfoCompiler.java index ac80dd3..cce72cd 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/BinaryTypeInfoCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/BinaryTypeInfoCompiler.java @@ -1,18 +1,22 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; + +import javax.xml.namespace.QName; import org.hisrc.jscm.codemodel.JSCodeModel; import org.hisrc.jscm.codemodel.expression.JSArrayLiteral; import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; -import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.BuiltinLeafInfoCompiler; public abstract class BinaryTypeInfoCompiler extends BuiltinLeafInfoCompiler { - public BinaryTypeInfoCompiler(String name) { - super(name); + public BinaryTypeInfoCompiler(String name, QName qualifiedName) { + super(name, qualifiedName); } @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); final byte[] value = parse(item); final JSArrayLiteral result = codeModel.array(); for (final byte b : value) { diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/BooleanTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/BooleanTypeInfoCompiler.java new file mode 100644 index 0000000..5debefe --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/BooleanTypeInfoCompiler.java @@ -0,0 +1,20 @@ +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; + +import org.hisrc.jscm.codemodel.JSCodeModel; +import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.BuiltinLeafInfoCompiler; +import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants; + +public class BooleanTypeInfoCompiler extends BuiltinLeafInfoCompiler { + + public BooleanTypeInfoCompiler() { + super("Boolean", XmlSchemaConstants.BOOLEAN); + } + + @Override + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); + return codeModel._boolean(Boolean.valueOf(item)); + } +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/DecimalTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/DecimalTypeInfoCompiler.java new file mode 100644 index 0000000..4a8dcb8 --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/DecimalTypeInfoCompiler.java @@ -0,0 +1,28 @@ +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; + +import javax.xml.namespace.QName; + +import org.hisrc.jscm.codemodel.JSCodeModel; +import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.BuiltinLeafInfoCompiler; + +public class DecimalTypeInfoCompiler extends BuiltinLeafInfoCompiler { + + public DecimalTypeInfoCompiler(String name, QName qualifiedName) { + super(name, qualifiedName); + } + + @Override + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); + // Hack to make -INF an INF work + if ("-INF".equals(item)) { + return codeModel.globalVariable("Infinity").negative(); + } else if ("INF".equals(item)) { + return codeModel.globalVariable("Infinity"); + } else { + return codeModel.decimal(item); + } + } +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/DurationTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/DurationTypeInfoCompiler.java similarity index 72% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/DurationTypeInfoCompiler.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/DurationTypeInfoCompiler.java index 755f730..c4dc494 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/DurationTypeInfoCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/DurationTypeInfoCompiler.java @@ -1,20 +1,23 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; import javax.xml.datatype.Duration; import org.hisrc.jscm.codemodel.JSCodeModel; import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; -import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.BuiltinLeafInfoCompiler; +import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants; public class DurationTypeInfoCompiler extends BuiltinLeafInfoCompiler { public DurationTypeInfoCompiler() { - super("Duration"); + super("Duration", XmlSchemaConstants.DURATION); } @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); final JSObjectLiteral result = codeModel.object(); final Duration duration = datatypeFactory.newDuration(item); if (duration.getSign() <= 0) { diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/HexBinaryTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/HexBinaryTypeInfoCompiler.java similarity index 61% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/HexBinaryTypeInfoCompiler.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/HexBinaryTypeInfoCompiler.java index c0b83b4..6e2349c 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/HexBinaryTypeInfoCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/HexBinaryTypeInfoCompiler.java @@ -1,11 +1,13 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; import javax.xml.bind.DatatypeConverter; +import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants; + public class HexBinaryTypeInfoCompiler extends BinaryTypeInfoCompiler { public HexBinaryTypeInfoCompiler() { - super("HexBinary"); + super("HexBinary", XmlSchemaConstants.HEXBINARY); } @Override diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/IntegerTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/IntegerTypeInfoCompiler.java new file mode 100644 index 0000000..1493578 --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/IntegerTypeInfoCompiler.java @@ -0,0 +1,21 @@ +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; + +import javax.xml.namespace.QName; + +import org.hisrc.jscm.codemodel.JSCodeModel; +import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.BuiltinLeafInfoCompiler; + +public class IntegerTypeInfoCompiler extends BuiltinLeafInfoCompiler { + + public IntegerTypeInfoCompiler(String name, QName qualifiedName) { + super(name, qualifiedName); + } + + @Override + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); + return codeModel.decimal(item); + } +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/NormalizedStringTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/NormalizedStringTypeInfoCompiler.java new file mode 100644 index 0000000..16180e4 --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/NormalizedStringTypeInfoCompiler.java @@ -0,0 +1,21 @@ +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; + +import javax.xml.bind.annotation.adapters.NormalizedStringAdapter; +import javax.xml.namespace.QName; + +import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; + +public class NormalizedStringTypeInfoCompiler extends StringTypeInfoCompiler { + + private final NormalizedStringAdapter normalizedStringAdapter = new NormalizedStringAdapter(); + + public NormalizedStringTypeInfoCompiler(String name, QName qualifiedName) { + super(name, qualifiedName); + } + + @Override + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + return mappingCompiler.getCodeModel().string(normalizedStringAdapter.unmarshal(item)); + } +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/QNameTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/QNameTypeInfoCompiler.java similarity index 74% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/QNameTypeInfoCompiler.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/QNameTypeInfoCompiler.java index 7ddb937..e6f21da 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/QNameTypeInfoCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/QNameTypeInfoCompiler.java @@ -1,4 +1,4 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; import java.util.Iterator; @@ -10,7 +10,9 @@ import org.hisrc.jscm.codemodel.JSCodeModel; import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; -import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.BuiltinLeafInfoCompiler; +import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants; import org.relaxng.datatype.ValidationContext; import com.sun.xml.xsom.XmlString; @@ -18,14 +20,16 @@ public class QNameTypeInfoCompiler extends BuiltinLeafInfoCompiler { public QNameTypeInfoCompiler() { - super("QName"); + super("QName", XmlSchemaConstants.QNAME); } @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, XmlString item) { + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, XmlString item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); final ValidationContext context = item.context; final QName value = DatatypeConverter.parseQName(item.value, new NamespaceContext() { + @SuppressWarnings("rawtypes") @Override public Iterator getPrefixes(String namespaceURI) { throw new UnsupportedOperationException(); diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/StringTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/StringTypeInfoCompiler.java new file mode 100644 index 0000000..050adec --- /dev/null +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/StringTypeInfoCompiler.java @@ -0,0 +1,21 @@ +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; + +import javax.xml.namespace.QName; + +import org.hisrc.jscm.codemodel.JSCodeModel; +import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.BuiltinLeafInfoCompiler; + +public class StringTypeInfoCompiler extends BuiltinLeafInfoCompiler { + + public StringTypeInfoCompiler(String name,QName qualifiedName) { + super(name, qualifiedName); + } + + @Override + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); + return codeModel.string(item); + } +} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/XMLGregorianCalendarTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/XMLGregorianCalendarTypeInfoCompiler.java similarity index 76% rename from compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/XMLGregorianCalendarTypeInfoCompiler.java rename to compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/XMLGregorianCalendarTypeInfoCompiler.java index caa3cdb..00d9c84 100644 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/XMLGregorianCalendarTypeInfoCompiler.java +++ b/compiler/src/main/java/org/hisrc/jsonix/compilation/mapping/typeinfo/builtin/XMLGregorianCalendarTypeInfoCompiler.java @@ -1,22 +1,24 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; +package org.hisrc.jsonix.compilation.mapping.typeinfo.builtin; import javax.xml.datatype.DatatypeConstants; -import javax.xml.datatype.Duration; import javax.xml.datatype.XMLGregorianCalendar; +import javax.xml.namespace.QName; import org.hisrc.jscm.codemodel.JSCodeModel; import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; import org.hisrc.jscm.codemodel.expression.JSObjectLiteral; -import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler; +import org.hisrc.jsonix.compilation.mapping.MappingCompiler; +import org.hisrc.jsonix.compilation.mapping.typeinfo.BuiltinLeafInfoCompiler; public class XMLGregorianCalendarTypeInfoCompiler extends BuiltinLeafInfoCompiler { - public XMLGregorianCalendarTypeInfoCompiler(String name) { - super(name); + public XMLGregorianCalendarTypeInfoCompiler(String name, QName qualifiedName) { + super(name, qualifiedName); } @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { + public JSAssignmentExpression createValue(MappingCompiler mappingCompiler, String item) { + final JSCodeModel codeModel = mappingCompiler.getCodeModel(); final JSObjectLiteral result = codeModel.object(); final XMLGregorianCalendar calendar = datatypeFactory.newXMLGregorianCalendar(item); if (calendar.getYear() != DatatypeConstants.FIELD_UNDEFINED) { diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/EnumLeafInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/EnumLeafInfoCompiler.java deleted file mode 100644 index 7488f8a..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/EnumLeafInfoCompiler.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.hisrc.jsonix.compilation.typeinfo; - -import org.apache.commons.lang3.Validate; -import org.hisrc.jscm.codemodel.JSCodeModel; -import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; -import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo; - -import com.sun.xml.xsom.XmlString; - -public class EnumLeafInfoCompiler extends PackagedTypeInfoCompiler { - - private final MEnumLeafInfo typeInfo; - private final TypeInfoCompiler baseTypeInfoCompiler; - - public EnumLeafInfoCompiler(MEnumLeafInfo typeInfo, TypeInfoCompiler baseTypeInfoCompiler) { - super(Validate.notNull(typeInfo)); - Validate.notNull(baseTypeInfoCompiler); - this.typeInfo = typeInfo; - this.baseTypeInfoCompiler = baseTypeInfoCompiler; - } - - @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, XmlString item) { - return baseTypeInfoCompiler.createValue(codeModel, item); - } - @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { - return baseTypeInfoCompiler.createValue(codeModel, item); - } -} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/TypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/TypeInfoCompiler.java deleted file mode 100644 index 13e4ce0..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/TypeInfoCompiler.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.hisrc.jsonix.compilation.typeinfo; - -import org.hisrc.jscm.codemodel.JSCodeModel; -import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; -import org.hisrc.jsonix.compilation.mapping.MappingCompiler; - -import com.sun.xml.xsom.XmlString; - -public interface TypeInfoCompiler { - - public JSAssignmentExpression createTypeInfoDeclaration(MappingCompiler mappingCompiler); - - public JSAssignmentExpression createValue(JSCodeModel codeModel, XmlString item); - - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item); -} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/BooleanTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/BooleanTypeInfoCompiler.java deleted file mode 100644 index eb68d1e..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/BooleanTypeInfoCompiler.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; - -import org.hisrc.jscm.codemodel.JSCodeModel; -import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; -import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler; - -public class BooleanTypeInfoCompiler extends BuiltinLeafInfoCompiler { - - public BooleanTypeInfoCompiler() { - super("Boolean"); - } - - @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { - return codeModel._boolean(Boolean.valueOf(item)); - } -} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/DecimalTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/DecimalTypeInfoCompiler.java deleted file mode 100644 index dc7141a..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/DecimalTypeInfoCompiler.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; - -import org.hisrc.jscm.codemodel.JSCodeModel; -import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; -import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler; - -public class DecimalTypeInfoCompiler extends BuiltinLeafInfoCompiler { - - public DecimalTypeInfoCompiler(String name) { - super(name); - } - - @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { - // Hack to make -INF an INF work - if ("-INF".equals(item)) { - return codeModel.globalVariable("Infinity").negative(); - } else if ("INF".equals(item)) { - return codeModel.globalVariable("Infinity"); - } else { - return codeModel.decimal(item); - } - } -} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/IntegerTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/IntegerTypeInfoCompiler.java deleted file mode 100644 index d112cc9..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/IntegerTypeInfoCompiler.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; - -import org.hisrc.jscm.codemodel.JSCodeModel; -import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; -import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler; - -public class IntegerTypeInfoCompiler extends BuiltinLeafInfoCompiler { - - public IntegerTypeInfoCompiler(String name) { - super(name); - } - - @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { - return codeModel.decimal(item); - } -} diff --git a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/StringTypeInfoCompiler.java b/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/StringTypeInfoCompiler.java deleted file mode 100644 index d526b4b..0000000 --- a/compiler/src/main/java/org/hisrc/jsonix/compilation/typeinfo/builtin/StringTypeInfoCompiler.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.hisrc.jsonix.compilation.typeinfo.builtin; - -import org.hisrc.jscm.codemodel.JSCodeModel; -import org.hisrc.jscm.codemodel.expression.JSAssignmentExpression; -import org.hisrc.jsonix.compilation.typeinfo.BuiltinLeafInfoCompiler; - -public class StringTypeInfoCompiler extends BuiltinLeafInfoCompiler { - - public StringTypeInfoCompiler(String name) { - super(name); - } - - @Override - public JSAssignmentExpression createValue(JSCodeModel codeModel, String item) { - return codeModel.string(item); - } -} diff --git a/compiler/src/test/java/org/hisrc/jsonix/xjc/plugin/tests/basic/JsonixPluginZeroTest.java b/compiler/src/test/java/org/hisrc/jsonix/xjc/plugin/tests/basic/JsonixPluginZeroTest.java index b2ae899..3152587 100644 --- a/compiler/src/test/java/org/hisrc/jsonix/xjc/plugin/tests/basic/JsonixPluginZeroTest.java +++ b/compiler/src/test/java/org/hisrc/jsonix/xjc/plugin/tests/basic/JsonixPluginZeroTest.java @@ -60,7 +60,7 @@ public void compilesBasicZero() throws Exception { "-xmlschema", getClass().getResource("/basic/zero/schema.xsd") .toExternalForm(), "-d", - "target/generated-sources/basic/zero", "-extension", "-Xjsonix" + "target/generated-sources/basic/zero", "-extension", "-Xjsonix", "-Xjsonix-generateJsonSchema" }; diff --git a/compiler/src/test/resources/basic/zero/schema.xsd b/compiler/src/test/resources/basic/zero/schema.xsd index a619c2d..dd0200b 100644 --- a/compiler/src/test/resources/basic/zero/schema.xsd +++ b/compiler/src/test/resources/basic/zero/schema.xsd @@ -7,7 +7,7 @@ - +