From 2596b24804a838631d70731fe9a04ccc0786a3ea Mon Sep 17 00:00:00 2001 From: threefish <306955302@qq.com> Date: Sun, 14 Aug 2022 14:27:21 +0800 Subject: [PATCH] Update RequestTemplateCreator.groovy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持复杂对象,原问题参考 https://github.com/membrane/soa-model/issues/268 --- .../creator/RequestTemplateCreator.groovy | 462 +++++++++--------- 1 file changed, 234 insertions(+), 228 deletions(-) diff --git a/core/src/main/groovy/com/predic8/wstool/creator/RequestTemplateCreator.groovy b/core/src/main/groovy/com/predic8/wstool/creator/RequestTemplateCreator.groovy index 4a2ac6b2..e26f9a37 100755 --- a/core/src/main/groovy/com/predic8/wstool/creator/RequestTemplateCreator.groovy +++ b/core/src/main/groovy/com/predic8/wstool/creator/RequestTemplateCreator.groovy @@ -13,242 +13,248 @@ limitations under the License. */ -package com.predic8.wstool.creator; - -import groovy.xml.* - -import org.slf4j.Logger -import org.slf4j.LoggerFactory +package com.predic8.wstool.creator import com.predic8.schema.* -import com.predic8.schema.creator.* +import com.predic8.schema.creator.AbstractSchemaCreator import com.predic8.schema.restriction.BaseRestriction import com.predic8.schema.restriction.facet.* import com.predic8.soamodel.Consts import com.predic8.soamodel.ModelAccessException +import groovy.xml.MarkupBuilderHelper +import org.slf4j.Logger +import org.slf4j.LoggerFactory class RequestTemplateCreator extends AbstractSchemaCreator { - - private static final Logger log = LoggerFactory.getLogger(RequestTemplateCreator.class) - - - void createElement(Element element, RequestTemplateCreatorContext ctx){ - - ctx.elements.add(element) - if(ctx.elements.findAll{it == ctx.element}.size() >= ctx.maxRecursionDepth) { - yield("""\n""") - return - } - - if(element.minOccurs == element.maxOccurs && element.maxOccurs != '1') yield("\n") - else if(element.minOccurs == '0' && element.maxOccurs == '1') yield("\n") - else if (element.minOccurs != element.maxOccurs) yield("\n") - if(element.nillable) yield("\n") - log.debug "Element ${element?.name}" - if ( element.ref ) { - element.schema.getElement(element.ref).create(this,ctx) - return - } - ctx.element = element - - if(element.fixedValue != null){ // != null for empty strings - yield("\n") - builder."${getElementTagName(element, ctx)}"(element.fixedValue, ) - return - } - if(element.defaultValue != null){ // != null for empty strings - yield("\n") - builder."${getElementTagName(element, ctx)}"(element.defaultValue, ) - return - } - - if(element.embeddedType){ - element.embeddedType.create(this, ctx) - return - } - if(element.type) { - def refType = element.schema.getType(element.type) - if(refType && !(refType instanceof BuiltInSchemaType)){ - refType.create(this, ctx) - return - } - if(refType && (refType instanceof BuiltInSchemaType)){ - def attrs = [:] - declNSifNeeded(getNSPrefix(element, ctx),element.namespaceUri,attrs,ctx) - if(element.type.localPart=='dateTime') yield('') - builder."${getElementTagName(element, ctx)}"(TemplateUtil.getTemplateValue(element.type),attrs) - } - } - if(!element.type && !element.embeddedType) { - builder."${getElementTagName(element, ctx)}"() - } - } - - void createComplexType(ComplexType complexType, RequestTemplateCreatorContext ctx){ - log.debug "ComplexType ${complexType?.name}" - def schema = complexType.schema - ctx.path = "${ctx.path}${ctx.element.name}/" - - if(complexType.model instanceof ComplexContent && complexType.model.hasRestriction()){ - complexType.model?.create(this, ctx) - complexType.anyAttribute?.create(this, ctx) - return - } - - def attrs = [:] - declNSifNeeded(getNSPrefix(ctx.element, ctx),ctx.element.namespaceUri,attrs,ctx) - attrs.putAll(createAttributes(complexType, ctx)) - builder."${getElementTagName(ctx.element, ctx)}"(attrs){ - complexType.model?.create(this, ctx) - complexType.anyAttribute?.create(this, ctx) - } - } - - Map createAttributes(Object obj, RequestTemplateCreatorContext ctx){ - def res = [:] - def attrs = obj.allAttributes - attrs.each{ - def attr = it.ref ? obj.schema.getAttribute(it.ref) : it - if(attr.fixedValue) { - res[attr.name] = attr.fixedValue - return - } - if(attr.simpleType){ - if(attr.simpleType.restriction){ - res[attr.name] = TemplateUtil.getTemplateValue(attr.simpleType.restriction.base) + return } - } else { - declNSifNeeded(getNSPrefix(attr, ctx),attr.namespaceUri,res,ctx) - res["${getNSPrefix(attr, ctx)}:${attr.name}"] = TemplateUtil.getTemplateValue(attr.type) - } - } - res - } - - void createSimpleRestriction(BaseRestriction restriction, RequestTemplateCreatorContext ctx){ - if(restriction.facets){ - yield("\n") - } - buildElement(ctx,text:'???') - } - - void createEnumerationFacet(EnumerationFacet facet, RequestTemplateCreatorContext ctx){ - yield("possible value: ${facet.value}") - } - - void createLengthFacet(LengthFacet facet, RequestTemplateCreatorContext ctx){ - yield("maximum length: ${facet.value}") - } - - void createTotalDigitsFacet(TotalDigitsFacet facet, RequestTemplateCreatorContext ctx){ - yield("maximum number of digits: ${facet.value}") - } - - void createFractionDigits(FractionDigits fdigits, RequestTemplateCreatorContext ctx) { - yield("fraction digits: ${fdigits.value}") - } - - void createPatternFacet(PatternFacet facet, RequestTemplateCreatorContext ctx){ - yield("Pattern: ${facet.value}") - } - - void createMaxLengthFacet(MaxLengthFacet facet, RequestTemplateCreatorContext ctx){ - yield("Max Length: ${facet.value}") - } - - void createMinLengthFacet(MinLengthFacet facet, RequestTemplateCreatorContext ctx){ - yield("Min Lenght: ${facet.value}") - } - - void createMaxInclusiveFacet(MaxInclusiveFacet facet, RequestTemplateCreatorContext ctx){ - yield("Max Inclusive: ${facet.value}") - } - - void createMinInclusiveFacet(MinInclusiveFacet facet, RequestTemplateCreatorContext ctx){ - yield("Min Inclusive: ${facet.value}") - } - - void createMaxExclusiveFacet(MaxExclusiveFacet facet, RequestTemplateCreatorContext ctx){ - yield("Max Exclusive: ${facet.value}") - } - - void createMinExclusiveFacet(MinExclusiveFacet facet, RequestTemplateCreatorContext ctx){ - yield("Min Exclusive: ${facet.value}") - } - - void createWhiteSpaceFacet(WhiteSpaceFacet facet, RequestTemplateCreatorContext ctx){ - yield("White Space: ${facet.value}") - } - - void createExtension(Extension extension, RequestTemplateCreatorContext ctx){ - if(extension.base.namespaceURI.equals(Consts.SCHEMA_NS)){ - yield("${TemplateUtil.getTemplateValue(extension.base)}") - return - } - def baseType = extension.schema.getType(extension.base) - if(!baseType) throw new ModelAccessException("Could not find the referenced type '${extension.basePN}' in schema '${extension.schema.targetNamespace}'.") - if(baseType instanceof SimpleType) return - baseType.model?.create(this, ctx) - extension.anyAttribute?.create(this,ctx) - extension.model?.create(this,ctx) - } - - void createAnnotation(Annotation annotation, RequestTemplateCreatorContext ctx) { - annotation.documentations.each{ yield("") } - } - - void createPart(part, RequestTemplateCreatorContext ctx){ - if(!part.type) throw new ModelAccessException("There is no type information for the part '${part.name}' although the referencing operation was declared as RPC style.") - if(part.type instanceof ComplexType || part.type instanceof SimpleType){ - part.type.create(this, ctx) - return - } - if(part.type instanceof BuiltInSchemaType){ - if(part.type.qname.localPart=='dateTime') yield('') - builder."${part.name}"(TemplateUtil.getTemplateValue(part.type.qname)) - } - } - - void createSimpleContent(SimpleContent simpleContent, RequestTemplateCreatorContext ctx){ - simpleContent.extension?.create(this, ctx) - simpleContent.restriction?.create(this, ctx) - } - - void createAny(Any any, RequestTemplateCreatorContext ctx){ - /*TODO - * Change the yield calls to comment, like the followin: - * yield("") - */ - builder.mkp.comment "This element can be extended by any element from ${any.namespace ?: 'any'} namespace" - } - - void createAnyAttribute(AnyAttribute anyAttribute, RequestTemplateCreatorContext ctx){ - yield("") - } - - void createComplexContentRestriction(Restriction restriction, RequestTemplateCreatorContext ctx){ - if(restriction.base) restriction.schema.getType(restriction.base).create(this, ctx) - } - - @Override - protected getElementTagName(Element element, ctx){ - /*Only if the element is from the same namespace as the - * top-level-element of the request, it doesn't need a prefix. - */ - if(!element.toplevel && element.schema.elementFormDefault=="unqualified" && ctx.elements[0].namespaceUri == element.namespaceUri) - return element.name - else - return "${getNSPrefix(element, ctx)}:${element.name}" - } - - private yield(s) { - new MarkupBuilderHelper(builder).yieldUnescaped(s) - } + + if(element.minOccurs == element.maxOccurs && element.maxOccurs != '1') yield("\n") + else if(element.minOccurs == '0' && element.maxOccurs == '1') yield("\n") + else if (element.minOccurs != element.maxOccurs) yield("\n") + if(element.nillable) yield("\n") + log.debug "Element ${element?.name}" + if ( element.ref ) { + element.schema.getElement(element.ref).create(this,ctx) + return + } + ctx.element = element + + if(element.fixedValue != null){ // != null for empty strings + yield("\n") + builder."${getElementTagName(element, ctx)}"(element.fixedValue, ) + return + } + if(element.defaultValue != null){ // != null for empty strings + yield("\n") + builder."${getElementTagName(element, ctx)}"(element.defaultValue, ) + return + } + + if(element.embeddedType){ + element.embeddedType.create(this, ctx) + return + } + if(element.type) { + def refType = element.schema.getType(element.type) + if(refType && !(refType instanceof BuiltInSchemaType)){ + refType.create(this, ctx) + return + } + if(refType && (refType instanceof BuiltInSchemaType)){ + def attrs = [:] + declNSifNeeded(getNSPrefix(element, ctx),element.namespaceUri,attrs,ctx) + if(element.type.localPart=='dateTime') yield('') + builder."${getElementTagName(element, ctx)}"(TemplateUtil.getTemplateValue(element.type),attrs) + } + } + if(!element.type && !element.embeddedType) { + builder."${getElementTagName(element, ctx)}"() + } + } + + void createComplexType(ComplexType complexType, RequestTemplateCreatorContext ctx){ + log.debug "ComplexType ${complexType?.name}" + def schema = complexType.schema + ctx.path = "${ctx.path}${ctx.element.name}/" + + if(complexType.model instanceof ComplexContent && complexType.model.hasRestriction()){ + complexType.model?.create(this, ctx) + complexType.anyAttribute?.create(this, ctx) + return + } + + def attrs = [:] + declNSifNeeded(getNSPrefix(ctx.element, ctx),ctx.element.namespaceUri,attrs,ctx) + attrs.putAll(createAttributes(complexType, ctx)) + builder."${getElementTagName(ctx.element, ctx)}"(attrs){ + complexType.model?.create(this, ctx) + complexType.anyAttribute?.create(this, ctx) + } + } + + Map createAttributes(Object obj, RequestTemplateCreatorContext ctx){ + def res = [:] + def attrs = obj.allAttributes + attrs.each{ + def attr = it.ref ? obj.schema.getAttribute(it.ref) : it + if(attr.fixedValue) { + res[attr.name] = attr.fixedValue + return + } + if(attr.simpleType){ + if(attr.simpleType.restriction){ + res[attr.name] = TemplateUtil.getTemplateValue(attr.simpleType.restriction.base) + } + } else { + declNSifNeeded(getNSPrefix(attr, ctx),attr.namespaceUri,res,ctx) + res["${getNSPrefix(attr, ctx)}:${attr.name}"] = TemplateUtil.getTemplateValue(attr.type) + } + } + res + } + + void createSimpleRestriction(BaseRestriction restriction, RequestTemplateCreatorContext ctx){ + if(restriction.facets){ + yield("\n") + } + buildElement(ctx,text:'???') + } + + void createEnumerationFacet(EnumerationFacet facet, RequestTemplateCreatorContext ctx){ + yield("possible value: ${facet.value}") + } + + void createLengthFacet(LengthFacet facet, RequestTemplateCreatorContext ctx){ + yield("maximum length: ${facet.value}") + } + + void createTotalDigitsFacet(TotalDigitsFacet facet, RequestTemplateCreatorContext ctx){ + yield("maximum number of digits: ${facet.value}") + } + + void createFractionDigits(FractionDigits fdigits, RequestTemplateCreatorContext ctx) { + yield("fraction digits: ${fdigits.value}") + } + + void createPatternFacet(PatternFacet facet, RequestTemplateCreatorContext ctx){ + yield("Pattern: ${facet.value}") + } + + void createMaxLengthFacet(MaxLengthFacet facet, RequestTemplateCreatorContext ctx){ + yield("Max Length: ${facet.value}") + } + + void createMinLengthFacet(MinLengthFacet facet, RequestTemplateCreatorContext ctx){ + yield("Min Lenght: ${facet.value}") + } + + void createMaxInclusiveFacet(MaxInclusiveFacet facet, RequestTemplateCreatorContext ctx){ + yield("Max Inclusive: ${facet.value}") + } + + void createMinInclusiveFacet(MinInclusiveFacet facet, RequestTemplateCreatorContext ctx){ + yield("Min Inclusive: ${facet.value}") + } + + void createMaxExclusiveFacet(MaxExclusiveFacet facet, RequestTemplateCreatorContext ctx){ + yield("Max Exclusive: ${facet.value}") + } + + void createMinExclusiveFacet(MinExclusiveFacet facet, RequestTemplateCreatorContext ctx){ + yield("Min Exclusive: ${facet.value}") + } + + void createWhiteSpaceFacet(WhiteSpaceFacet facet, RequestTemplateCreatorContext ctx){ + yield("White Space: ${facet.value}") + } + + void createExtension(Extension extension, RequestTemplateCreatorContext ctx){ + if(extension.base.namespaceURI.equals(Consts.SCHEMA_NS)){ + yield("${TemplateUtil.getTemplateValue(extension.base)}") + return + } + def baseType = extension.schema.getType(extension.base) + if(!baseType) throw new ModelAccessException("Could not find the referenced type '${extension.basePN}' in schema '${extension.schema.targetNamespace}'.") + if(baseType instanceof SimpleType) return + baseType.model?.create(this, ctx) + extension.anyAttribute?.create(this,ctx) + extension.model?.create(this,ctx) + } + + void createAnnotation(Annotation annotation, RequestTemplateCreatorContext ctx) { + annotation.documentations.each{ yield("") } + } + + void createPart(part, RequestTemplateCreatorContext ctx){ + if(!part.type) throw new ModelAccessException("There is no type information for the part '${part.name}' although the referencing operation was declared as RPC style.") + if (part.type instanceof ComplexType || part.type instanceof SimpleType) { + // 注释掉,以支持复杂对象 原问题参考 https://github.com/membrane/soa-model/issues/268 // part.type.create(this, ctx) + ctx.element = part.type + def attrs = [:] + declNSifNeeded(getNSPrefix(ctx.element, ctx), part.type.namespaceUri, attrs, ctx) + attrs.putAll(createAttributes(part.type, ctx)) + builder."${part.name}"(attrs) { + part.type.model?.create(this, ctx) + part.type.anyAttribute?.create(this, ctx) + } + return + } + if(part.type instanceof BuiltInSchemaType){ + if(part.type.qname.localPart=='dateTime') yield('') + builder."${part.name}"(TemplateUtil.getTemplateValue(part.type.qname)) + } + } + + void createSimpleContent(SimpleContent simpleContent, RequestTemplateCreatorContext ctx){ + simpleContent.extension?.create(this, ctx) + simpleContent.restriction?.create(this, ctx) + } + + void createAny(Any any, RequestTemplateCreatorContext ctx){ + /*TODO + * Change the yield calls to comment, like the followin: + * yield("") + */ + builder.mkp.comment "This element can be extended by any element from ${any.namespace ?: 'any'} namespace" + } + + void createAnyAttribute(AnyAttribute anyAttribute, RequestTemplateCreatorContext ctx){ + yield("") + } + + void createComplexContentRestriction(Restriction restriction, RequestTemplateCreatorContext ctx){ + if(restriction.base) restriction.schema.getType(restriction.base).create(this, ctx) + } + + @Override + protected getElementTagName(Element element, ctx){ + /*Only if the element is from the same namespace as the + * top-level-element of the request, it doesn't need a prefix. + */ + if(!element.toplevel && element.schema.elementFormDefault=="unqualified" && ctx.elements[0].namespaceUri == element.namespaceUri) + return element.name + else + return "${getNSPrefix(element, ctx)}:${element.name}" + } + + private yield(s) { + new MarkupBuilderHelper(builder).yieldUnescaped(s) + } }