@@ -54,18 +54,12 @@ import io.gatehill.imposter.plugin.soap.model.TypeOperationMessage
5454import io.gatehill.imposter.plugin.soap.model.WsdlOperation
5555import io.gatehill.imposter.plugin.soap.model.WsdlService
5656import io.gatehill.imposter.plugin.soap.parser.SchemaContext
57- import io.gatehill.imposter.plugin.soap.util.SchemaGenerator
57+ import io.gatehill.imposter.plugin.soap.util.SchemaUtil
5858import io.gatehill.imposter.plugin.soap.util.SoapUtil
5959import io.gatehill.imposter.util.LogUtil
6060import org.apache.logging.log4j.LogManager
6161import org.apache.logging.log4j.Logger
6262import org.apache.xmlbeans.SchemaType
63- import org.apache.xmlbeans.SchemaTypeSystem
64- import org.apache.xmlbeans.XmlBeans
65- import org.apache.xmlbeans.XmlError
66- import org.apache.xmlbeans.XmlException
67- import org.apache.xmlbeans.XmlOptions
68- import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument
6963import org.apache.xmlbeans.impl.xsd2inst.SampleXmlUtil
7064import javax.xml.namespace.QName
7165
@@ -114,8 +108,6 @@ class SoapExampleService {
114108 service : WsdlService ,
115109 operation : WsdlOperation ,
116110 ): String {
117- val parts = operation.outputRef?.let { listOf (it) } ? : emptyList()
118-
119111 // by convention, the suffix 'Response' is added to the operation name
120112 val rootElementName = operation.name + " Response"
121113 val rootElement = if (service.targetNamespace?.isNotBlank() == true ) {
@@ -124,8 +116,9 @@ class SoapExampleService {
124116 QName (rootElementName)
125117 }
126118
127- val elementSchema = SchemaGenerator .createCompositePartSchema(service.targetNamespace ? : " " , rootElement, parts)
128- val sts: SchemaTypeSystem = buildSchemaTypeSystem(schemaContext, elementSchema)
119+ val parts = operation.outputRef?.let { listOf (it) } ? : emptyList()
120+ val elementSchema = SchemaUtil .createCompositePartSchema(service.targetNamespace ? : " " , rootElement, parts)
121+ val sts = SchemaUtil .compileSchemas(schemaContext.sts, schemaContext.entityResolver, arrayOf(elementSchema))
129122
130123 val elem = sts.findDocumentType(rootElement)
131124 ? : throw RuntimeException (" Could not find a generated element with name \" $rootElement \" " )
@@ -164,9 +157,9 @@ class SoapExampleService {
164157
165158 private fun generateElementExample (
166159 schemaContext : SchemaContext ,
167- outputRef : ElementOperationMessage ,
160+ message : ElementOperationMessage ,
168161 ): String {
169- val rootElementName = outputRef .elementName
162+ val rootElementName = message .elementName
170163 val elem: SchemaType = schemaContext.sts.findDocumentType(rootElementName)
171164 ? : throw RuntimeException (" Could not find a global element with name \" $rootElementName \" " )
172165
@@ -178,46 +171,16 @@ class SoapExampleService {
178171 service : WsdlService ,
179172 message : TypeOperationMessage ,
180173 ): String {
181- val elementName = QName (service.targetNamespace, message.partName)
182- val elementSchema = SchemaGenerator .createSinglePartSchema(service.targetNamespace ? : " " , message)
183- val sts: SchemaTypeSystem = buildSchemaTypeSystem(schemaContext, elementSchema)
174+ val elementSchema = SchemaUtil .createSinglePartSchema(service.targetNamespace ? : " " , message)
175+ val sts = SchemaUtil .compileSchemas(schemaContext.sts, schemaContext.entityResolver, arrayOf(elementSchema))
184176
177+ val elementName = QName (service.targetNamespace, message.partName)
185178 val elem = sts.findDocumentType(elementName)
186179 ? : throw RuntimeException (" Could not find a generated element with name \" $elementName \" " )
187180
188181 return SampleXmlUtil .createSampleForType(elem)
189182 }
190183
191- private fun buildSchemaTypeSystem (
192- schemaContext : SchemaContext ,
193- vararg extraSchemas : SchemaDocument ,
194- ): SchemaTypeSystem {
195- var sts: SchemaTypeSystem ? = null
196-
197- val schemas = schemaContext.schemas + extraSchemas
198- if (schemas.isNotEmpty()) {
199- val errors = mutableListOf<XmlError >()
200-
201- val compileOptions = XmlOptions ()
202- .setLoadLineNumbers()
203- .setLoadMessageDigest()
204- .setEntityResolver(schemaContext.entityResolver)
205- .setErrorListener(errors)
206-
207- try {
208- // TODO consider reusing the SchemaTypeSystem from SchemaContext
209- sts = XmlBeans .compileXsd(schemas, XmlBeans .getBuiltinTypeSystem(), compileOptions)
210- } catch (e: Exception ) {
211- if (errors.isEmpty() || e !is XmlException ) {
212- throw RuntimeException (" Error compiling XSD" , e)
213- }
214- throw RuntimeException (" Schema compilation errors: " + errors.joinToString(" \n " ))
215- }
216- }
217- sts ? : throw IllegalStateException (" No schemas to process" )
218- return sts
219- }
220-
221184 private fun transmitExample (httpExchange : HttpExchange , example : String? , bodyHolder : MessageBodyHolder ) {
222185 example ? : run {
223186 logger.warn(" No example found - returning empty response" )
0 commit comments