Skip to content

Instantly share code, notes, and snippets.

@eed3si9n
Created May 27, 2011 02:38
Show Gist options
  • Save eed3si9n/994538 to your computer and use it in GitHub Desktop.
Save eed3si9n/994538 to your computer and use it in GitHub Desktop.
wsdl 2.0
/*
* Copyright (c) 2011 e.e d3si9n
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package scalaxb.compiler.wsdl20
import scala.collection.mutable
import scalaxb.compiler.{Module, Config, Snippet, CustomXML, CanBeWriter}
import scalaxb.compiler.xsd.{SchemaDecl, XsdContext}
import scalaxb.{DataRecord}
import wsdl20._
import java.io.{Reader}
import java.net.{URI}
import scala.xml.{Node}
class Driver extends Module { driver =>
type Schema = WsdlPair
type Context = WsdlContext
type RawSchema = scala.xml.Node
val xsddriver = new scalaxb.compiler.xsd.Driver {
override val verbose = driver.verbose
}
def buildContext = WsdlContext()
def readerToRawSchema(reader: Reader): RawSchema = CustomXML.load(reader)
def nodeToRawSchema(node: Node) = node
override def packageName(namespace: Option[String], context: Context): Option[String] =
xsddriver.packageName(namespace, context.xsdcontext)
override def processContext(context: Context, cnfg: Config) {
xsddriver.processContext(context.xsdcontext, cnfg)
context.descriptions foreach {processDescription(_, context)}
}
def processDescription(description: XDescriptionType, context: Context) {
val ns = Some(description.targetNamespace.toString)
def processInterface(intf: XInterfaceType, context: Context) {
intf.xinterfacetypeoption collect {
case DataRecord(_, _, x: XInterfaceFaultType) => context.faults((ns, x.name)) = x
}
intf.xinterfacetypeoption collect {
case DataRecord(_, _, x: XInterfaceOperationType) => context.operations((ns, x.name)) = x
}
}
description.xdescriptiontypeoption collect {
case DataRecord(_, _, x: XInterfaceType) =>
context.interfaces((ns, x.name)) = x
processInterface(x, context: Context)
}
description.xdescriptiontypeoption collect {
case DataRecord(_, _, x: XBindingType) => context.bindings((ns, x.name)) = x
}
description.xdescriptiontypeoption collect {
case DataRecord(_, _, x: XServiceType) => context.services((ns, x.name)) = x
}
}
override def generateProtocol(snippet: Snippet,
context: Context, cnfg: Config): Seq[Node] =
xsddriver.generateProtocol(snippet, context.xsdcontext, cnfg)
override def generate(pair: WsdlPair, cntxt: Context, cnfg: Config): Snippet = {
val xsdgenerated = pair.schema map {
xsddriver.generate(_, cntxt.xsdcontext, cnfg)
} getOrElse { Snippet(<source></source>) }
val generator = new GenSource {
val logger = driver
val context = cntxt
val scope = pair.scope
val xsdgenerator = new scalaxb.compiler.xsd.GenSource(
SchemaDecl(targetNamespace = Some(pair.description.targetNamespace.toString), scope = pair.scope),
cntxt.xsdcontext) {
val logger = driver
val config = cnfg
}
}
mergeSnippets(xsdgenerated :: generator.generate(pair.description) :: Nil)
}
override def toImportable(alocation: URI, rawschema: RawSchema): Importable = new Importable {
val location = alocation
val raw = rawschema
lazy val wsdl = scalaxb.fromXML[XDescriptionType](rawschema)
val targetNamespace = Some(wsdl.targetNamespace.toString)
val importNamespaces: Seq[String] = Nil
val importLocations: Seq[String] = Nil
val includeLocations: Seq[String] = Nil
def toSchema(context: Context): WsdlPair = {
log("wsdl20.Driver: " + wsdl.toString)
context.descriptions += wsdl
val xsd = (wsdl.xdescriptiontypeoption flatMap {
case DataRecord(_, _, XTypesType(_, Seq(DataRecord(_, _, node: Node)), _)) =>
val schema = SchemaDecl.fromXML(node , context.xsdcontext)
context.xsdcontext.schemas += schema
log("wsdl20.Driver: " + schema.toString)
Some(schema)
case _ => None
}).headOption
WsdlPair(wsdl, xsd, rawschema.scope)
}
}
def generateRuntimeFiles[To](implicit evTo: CanBeWriter[To]): List[To] =
List(generateFromResource[To](Some("scalaxb"), "scalaxb.scala", "/scalaxb.scala.template"),
generateFromResource[To](Some("scalaxb"), "soap.scala", "/soap.scala.template"),
generateFromResource[To](Some("soapenvelope12"), "soapenvelope12.scala", "/soapenvelope12.scala.template"),
generateFromResource[To](Some("soapenvelope12"), "soapenvelope12_xmlprotocol.scala",
"/soapenvelope12_xmlprotocol.scala.template"))
}
case class WsdlPair(description: XDescriptionType, schema: Option[SchemaDecl], scope: scala.xml.NamespaceBinding)
case class WsdlContext(xsdcontext: XsdContext = XsdContext(),
descriptions: mutable.ListBuffer[XDescriptionType] = mutable.ListBuffer(),
interfaces: mutable.ListMap[(Option[String], String), XInterfaceType] = mutable.ListMap(),
bindings: mutable.ListMap[(Option[String], String), XBindingType] = mutable.ListMap(),
services: mutable.ListMap[(Option[String], String), XServiceType] = mutable.ListMap(),
faults: mutable.ListMap[(Option[String], String), XInterfaceFaultType] = mutable.ListMap(),
operations: mutable.ListMap[(Option[String], String), XInterfaceOperationType] = mutable.ListMap() )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment