Skip to content

Commit

Permalink
fixes xs:include without targetNamespace. fixes #102
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Oct 31, 2011
1 parent df4504d commit b25db4f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 35 deletions.
50 changes: 36 additions & 14 deletions cli/src/main/scala/scalaxb/compiler/Module.scala
Expand Up @@ -109,7 +109,7 @@ object Module {
trait Module {
type RawSchema
type Schema
type Context
type Context <: Adder[Schema]

private lazy val logger = Logger("module")
def verbose: Boolean = false
Expand Down Expand Up @@ -231,7 +231,9 @@ trait Module {
val importables = Seq[(Importable, From)](files map { f => importables0(f) -> f }: _*)

val schemas = ListMap[Importable, Schema](importables map { case (importable, file) =>
(importable, parse(importable, context)) }: _*)
val s = parse(importable, context)
context.add(importable.location, s)
(importable, s) }: _*)

val additionalImportables = ListMap.empty[Importable, File]

Expand All @@ -255,14 +257,13 @@ trait Module {
added = true
val importable = toImportable(implicitly[CanBeRawSchema[File, RawSchema]].toURI(x),
implicitly[CanBeRawSchema[File, RawSchema]].toRawSchema(x))
schemas(importable) = parse(importable, context)
val s = parse(importable, context)
context.add(importable.location, s)
schemas(importable) = s
(importable, x) })
if (added) addMissingFiles()
}

addMissingFiles()
processContext(context, config)

def headerSnippet(pkg: Option[String]) =
Snippet(<source>// Generated by &lt;a href="http://scalaxb.org/"&gt;scalaxb&lt;/a&gt;.
{ pkg map { x => "package " + x } getOrElse {""} }</source>)
Expand Down Expand Up @@ -307,6 +308,21 @@ trait Module {
output
}

def processUnnamedIncludes() {
val list = importables.toList ++ additionalImportables.toList

for {
(i, f) <- list
loc <- i.includeLocations
(i2, f2) <- list if f != f2 && shortenUri(new URI(loc)) == shortenUri(i2.location)
} {
context.setOuterNamespace(i2.location, i.targetNamespace)
}
}

addMissingFiles()
processUnnamedIncludes()
processContext(context, config)
processImportables(importables.toList) :::
processImportables(additionalImportables.toList) :::
List(processProtocol) :::
Expand Down Expand Up @@ -335,19 +351,19 @@ trait Module {
context: Context, config: Config): Seq[Node]

def toImportable(location: URI, rawschema: RawSchema): Importable


def shortenUri(uri: URI): String = {
val path = Option[String](uri.getPath) getOrElse {""}
(new File(path)).getName
}

def missingDependencies(importable: Importable, files: Seq[Importable]): List[String] = {
def shorten(uri: URI): String = {
val path = Option[String](uri.getPath) getOrElse {""}
(new File(path)).getName
}

val nsBased = importable.importNamespaces.toList flatMap { ns =>
files filter { _.targetNamespace == ns }
}
val XML_LOCATION = "http://www.w3.org/2001/xml.xsd"
val locationBased = importable.importLocations.toList flatMap { loc =>
val deps = files filter { f => shorten(f.location) == shorten(new URI(loc)) }
val deps = files filter { f => shortenUri(f.location) == shortenUri(new URI(loc)) }
if (deps.isEmpty && loc != XML_LOCATION) {
logger.warn((new File(importable.location.getPath).getName) + " imports " + loc +
" but no schema with that name was compiled together.")
Expand All @@ -356,7 +372,7 @@ trait Module {
else Nil
}
val includes = importable.includeLocations.toList flatMap { loc =>
val deps = files filter { f => shorten(f.location) == shorten(new URI(loc)) }
val deps = files filter { f => shortenUri(f.location) == shortenUri(new URI(loc)) }
if (deps.isEmpty && loc != XML_LOCATION) {
logger.warn("Warning: " + (new File(importable.location.getPath).getName) + " includes " + loc +
" but no schema with that name was compiled together.")
Expand Down Expand Up @@ -476,3 +492,9 @@ class CaseClassTooLong(fqn: String, xmlname: String) extends RuntimeException(
fqn, xmlname
)
)

// this is a mechanism to implement http://www.w3.org/TR/xmlschema-1/#compound-schema
trait Adder[A] {
def add(uri: URI, value: A)
def setOuterNamespace(uri: URI, outer: Option[String])
}
12 changes: 9 additions & 3 deletions cli/src/main/scala/scalaxb/compiler/wsdl11/Driver.scala
Expand Up @@ -23,7 +23,7 @@
package scalaxb.compiler.wsdl11

import scala.collection.mutable
import scalaxb.compiler.{Module, Config, Snippet, CustomXML, CanBeWriter}
import scalaxb.compiler.{Module, Config, Snippet, CustomXML, CanBeWriter, Adder}
import scalaxb.{DataRecord}
import wsdl11._
import java.io.{Reader}
Expand Down Expand Up @@ -153,7 +153,6 @@ class Driver extends Module { driver =>
val xsd = xsdRawSchema map { x =>
val schema = SchemaDecl.fromXML(x, context.xsdcontext)
logger.debug(schema.toString)
context.xsdcontext.schemas += schema
schema
}

Expand Down Expand Up @@ -185,4 +184,11 @@ case class WsdlContext(xsdcontext: XsdContext = XsdContext(),
services: mutable.ListMap[(Option[String], String), XServiceType] = mutable.ListMap(),
faults: mutable.ListMap[(Option[String], String), XFaultType] = mutable.ListMap(),
messages: mutable.ListMap[(Option[String], String), XMessageType] = mutable.ListMap(),
var soap11: Boolean = false )
var soap11: Boolean = false ) extends Adder[WsdlPair] {
def add(uri: URI, value: WsdlPair) {
value.schemas map {xsdcontext.add(uri, _)}
}
def setOuterNamespace(uri: URI, outer: Option[String]) {
xsdcontext.setOuterNamespace(uri, outer)
}
}
18 changes: 16 additions & 2 deletions cli/src/main/scala/scalaxb/compiler/xsd/Decl.scala
Expand Up @@ -25,6 +25,8 @@ package scalaxb.compiler.xsd
import scala.collection.{Map, Set}
import scala.collection.mutable
import scala.collection.immutable
import scalaxb.compiler.Adder
import java.net.URI

abstract class Decl

Expand All @@ -37,7 +39,7 @@ object Incrementor {
}

case class XsdContext(
schemas: mutable.ListBuffer[SchemaDecl] = mutable.ListBuffer(),
uriToSchema: mutable.ListMap[URI, SchemaDecl] = mutable.ListMap(),
typeNames: mutable.ListMap[NameKey, String] = mutable.ListMap(),
enumValueNames: mutable.ListMap[Option[String],
mutable.ListMap[(String, EnumerationDecl), String]] = mutable.ListMap(),
Expand All @@ -50,7 +52,19 @@ case class XsdContext(
substituteGroups: mutable.ListBuffer[(Option[String], String)] = mutable.ListBuffer(),
prefixes: mutable.ListMap[String, String] = mutable.ListMap(),
duplicatedTypes: mutable.ListBuffer[(SchemaDecl, Decl)] = mutable.ListBuffer()
) {
) extends Adder[SchemaDecl] {
def schemas = uriToSchema.valuesIterator.toSeq

def add(uri: URI, value: SchemaDecl) {
uriToSchema(uri) = value
}
def setOuterNamespace(uri: URI, outer: Option[String]) {
val x = uriToSchema(uri)
uriToSchema(uri) = x.copy(targetNamespace = x.targetNamespace match {
case Some(ns) => Some(ns)
case _ => outer
})
}
}

class ParserConfig {
Expand Down
1 change: 0 additions & 1 deletion cli/src/main/scala/scalaxb/compiler/xsd/Driver.scala
Expand Up @@ -72,7 +72,6 @@ class Driver extends Module { driver =>

def toSchema(context: Context): Schema = {
val schema = SchemaDecl.fromXML(raw, context)
context.schemas += schema
logger.debug("toSchema: " + schema.toString())
schema
}
Expand Down
17 changes: 17 additions & 0 deletions integration/src/test/resources/include.xsd
@@ -0,0 +1,17 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xipo="http://www.example.com/IPO"
elementFormDefault="qualified">
<xs:complexType name="PartsType">
<xs:sequence>
<xs:element name="part" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="number" type="xipo:SKU"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
15 changes: 1 addition & 14 deletions integration/src/test/resources/report.xsd
Expand Up @@ -8,6 +8,7 @@
<!-- for SKU -->
<import namespace="http://www.example.com/IPO"/>
<import namespace="http://www.example.com/circular"/>
<include schemaLocation="include.xsd" />

<annotation>
<documentation xml:lang="en">
Expand Down Expand Up @@ -66,20 +67,6 @@
</sequence>
</complexType>

<complexType name="PartsType">
<sequence>
<element name="part" maxOccurs="unbounded">
<complexType>
<simpleContent>
<extension base="string">
<attribute name="number" type="xipo:SKU"/>
</extension>
</simpleContent>
</complexType>
</element>
</sequence>
</complexType>

<element name="otherCircular">
<complexType>
<sequence>
Expand Down
3 changes: 2 additions & 1 deletion integration/src/test/scala/ImportTest.scala
Expand Up @@ -6,9 +6,10 @@ object ImportTest extends TestBase {
val reportxsd = new File("integration/src/test/resources/report.xsd")
val circularxsd = new File("integration/src/test/resources/circular.xsd")
val conflictxsd = new File("integration/src/test/resources/conflict.xsd")
val includexsd = new File("integration/src/test/resources/include.xsd")

lazy val generated = module.processFiles(
List(ipoxsd, reportxsd, circularxsd, conflictxsd),
List(ipoxsd, reportxsd, circularxsd, conflictxsd, includexsd),
Config(packageNames = Map(None -> Some("ipo"),
Some("http://www.example.com/Report") -> Some("org.report") ),
packageDir = true, outdir = tmp) )
Expand Down

0 comments on commit b25db4f

Please sign in to comment.