Skip to content

Commit

Permalink
working around empty sequence groups. #55
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Jun 12, 2011
1 parent 014cca2 commit 7a5226c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
14 changes: 8 additions & 6 deletions cli/src/main/scala/scalaxb/compiler/xsd/GenSource.scala
Expand Up @@ -348,7 +348,7 @@ abstract class GenSource(val schema: SchemaDecl,
{childString}</source>
}

val groups = filterGroup(decl)
val groups = filterGroup(decl) filter { g => primaryCompositor(g).particles.size > 0 }
val companionSuperNames: List[String] = "scalaxb.ElemNameParser[" + fqn + "]" :: groups.map(g =>
buildFormatterName(g.namespace, groupTypeName(g)))

Expand Down Expand Up @@ -495,7 +495,8 @@ abstract class GenSource(val schema: SchemaDecl,
if (groups.isEmpty) List("scalaxb.AnyElemNameParser")
else groups.map { g => buildFormatterName(g.namespace, groupTypeName(g)) }

val companionCode = <source>{ buildComment(group) } trait {formatterName} extends {superNames.mkString(" with ")} {{
val companionCode = if (compositor.particles.size == 0) <source></source>
else <source>{ buildComment(group) } trait {formatterName} extends {superNames.mkString(" with ")} {{
private val targetNamespace: Option[String] = { quote(schema.targetNamespace) }

def parse{localName}(node: scala.xml.Node, stack: List[scalaxb.ElemName]): Parser[{param.baseTypeName}] =
Expand Down Expand Up @@ -753,11 +754,11 @@ object {localName} {{
def flattenElements(namespace: Option[String], family: String,
compositor: HasParticle, index: Int, wrapTopSequence: Boolean): List[ElemDecl] = {
compositor match {
case ref:GroupRef =>
List(buildCompositorRef(ref, index))
case ref:GroupRef => flattenElements(namespace, family, buildGroup(ref), index, wrapTopSequence)

case group:GroupDecl =>
List(buildCompositorRef(group, index))
if (primaryCompositor(group).particles.isEmpty) Nil
else List(buildCompositorRef(group, index))

case seq: SequenceDecl =>
if (wrapTopSequence &&
Expand All @@ -770,12 +771,13 @@ object {localName} {{
val occurence = mergeOccurrence(buildOccurrence(choice).copy(nillable = false),
buildOccurrence(seq))
List(buildCompositorRef(choice, occurence, 0))
case group: GroupDecl => flattenElements(namespace, family, group, index, wrapTopSequence)
case _ => List(buildCompositorRef(seq, index))
}
else List(buildCompositorRef(seq, index))
else splitLongSequence(
namespace, family, compositor.particles).zipWithIndex flatMap {
case (ref: GroupRef, i: Int) => List(buildCompositorRef(ref, i))
case (ref: GroupRef, i: Int) => flattenElements(namespace, family, buildGroup(ref), i, wrapTopSequence)
case (compositor2: HasParticle, i: Int) => List(buildCompositorRef(compositor2, i))
case (elem: ElemDecl, i: Int) => List(elem)
case (ref: ElemRef, i: Int) => List(buildElement(ref))
Expand Down
9 changes: 6 additions & 3 deletions cli/src/main/scala/scalaxb/compiler/xsd/Parsers.scala
Expand Up @@ -91,7 +91,8 @@ trait Parsers extends Args with Params {
else splitLong[SequenceDecl](seq.particles) { SequenceDecl(_, 1, 1, 0) }

val parserList = if (mixed) (0 to seq.particles.size * 2 - 1).toList map { i =>
if (i % 2 == 0) buildParser(seq.particles(i / 2), mixed, mixed)
if (seq.particles.size == 0) buildTextParser
else if (i % 2 == 0) buildParser(seq.particles(i / 2), mixed, mixed)
else buildTextParser
}
else splits map { buildParser(_, mixed, mixed) }
Expand All @@ -118,7 +119,8 @@ trait Parsers extends Args with Params {
else argList.mkString("," + newline + indent(4))

"{ case " +
parserVariableList.mkString(" ~ ") +
(if (seq.particles.size == 0) "_"
else parserVariableList.mkString(" ~ ")) +
(if (mixed) " => Seq.concat(" + argsString + ")"
else if (wrapInDataRecord) " => scalaxb.DataRecord(" + fqn + "(" + argsString + "))"
else " => " + fqn + "(" + argsString + ")") +
Expand Down Expand Up @@ -151,7 +153,8 @@ trait Parsers extends Args with Params {
// choice repeatable in case any one particle is repeatable.
// this may violate the schema, but it is a compromise as long as plurals are
// treated as Seq[DataRecord].
def buildChoiceParser(choice: ChoiceDecl, occurrence: Occurrence, mixed: Boolean): String = {
def buildChoiceParser(choice: ChoiceDecl, occurrence: Occurrence, mixed: Boolean): String = {
assert(choice.particles.size > 0, "choice has no particles: " + choice)
val containsStructure = if (mixed) true
else choice.particles exists(_ match {
case elem: ElemDecl => false
Expand Down
13 changes: 13 additions & 0 deletions integration/src/test/resources/general.xsd
Expand Up @@ -194,6 +194,15 @@
<xs:element name="string30" type="xs:string"/>
</xs:all>
</xs:complexType>

<xs:complexType name="EmptyComplexTypeTest">
</xs:complexType>

<xs:complexType name="EmptySequenceGroupTest">
<xs:sequence>
<xs:group ref="gen:emptySeqGroup"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="TopLevelMultipleSeqTest">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
Expand Down Expand Up @@ -373,4 +382,8 @@
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="class" type="xs:NMTOKENS"/>
</xs:attributeGroup>

<xs:group name="emptySeqGroup">
<xs:sequence/>
</xs:group>
</xs:schema>

0 comments on commit 7a5226c

Please sign in to comment.