diff --git a/cli/src/main/scala/scalaxb/compiler/xsd/Args.scala b/cli/src/main/scala/scalaxb/compiler/xsd/Args.scala
index aadd671a1..1fe2fb16e 100644
--- a/cli/src/main/scala/scalaxb/compiler/xsd/Args.scala
+++ b/cli/src/main/scala/scalaxb/compiler/xsd/Args.scala
@@ -58,11 +58,22 @@ trait Args extends Params {
wrapForLongAll: Boolean = false, formatter: Option[String] = None): String = {
val stack = "scalaxb.ElemName(node) :: stack"
def fromU = buildFromXML(typeName, "_", stackItem, formatter)
-
+ def fromValue(x: String) = buildFromXML(typeName, "scala.xml.Text(" + quote(x) + ")", stackItem, formatter)
+
val retval = if (wrapForLongAll) {
// PrefixedAttribute only contains pre, so you need to pass in node to get the namespace.
- if (selector.contains("@")) selector + ".headOption map { x => scalaxb.DataRecord(x, node, " +
- buildFromXML(typeName, "x", stackItem, formatter) + ") }"
+ if (selector.contains("@"))
+ (defaultValue, fixedValue) match {
+ case (_, Some(x)) =>
+ "Some(scalaxb.DataRecord(None, None, " + fromValue(x) + "))"
+ case (Some(x), _) =>
+ selector + ".headOption map { x => scalaxb.DataRecord(x, node, " +
+ buildFromXML(typeName, "x", stackItem, formatter) + ") } orElse " +
+ "Some(scalaxb.DataRecord(None, None, " + fromValue(x) + "))"
+ case _ =>
+ selector + ".headOption map { x => scalaxb.DataRecord(x, node, " +
+ buildFromXML(typeName, "x", stackItem, formatter) + ") }"
+ }
else selector + ".headOption map { x => scalaxb.DataRecord(x, " +
buildFromXML(typeName, "x", stackItem, formatter) + ") }"
} else (cardinality, nillable) match {
@@ -102,12 +113,14 @@ trait Args extends Params {
def buildArgForAttribute(decl: AttributeLike, stackItem: Option[String], longAttribute: Boolean): String =
if (longAttribute) {
decl match {
- case attr: AttributeDecl =>
+ case attr: AttributeDecl =>
val o = attr.copy(use = OptionalUse)
val arg = buildArg(o, buildSelector(o), stackItem, longAttribute)
- if (longAttribute) arg + " map { " + quote(buildNodeName(o, false)) + " -> _ }"
+ if (longAttribute) {
+ arg + " map { " + quote(buildNodeName(o, false)) + " -> _ }"
+ }
else arg
- case ref: AttributeRef => buildArgForAttribute(buildAttribute(ref), stackItem, longAttribute)
+ case ref: AttributeRef => buildArgForAttribute(buildAttribute(ref), stackItem, longAttribute)
case group: AttributeGroupDecl => buildAttributeGroupArg(group, longAttribute)
}
} else buildArg(decl)
diff --git a/cli/src/main/scala/scalaxb/compiler/xsd/GenSource.scala b/cli/src/main/scala/scalaxb/compiler/xsd/GenSource.scala
index afc0881a6..dfe323861 100644
--- a/cli/src/main/scala/scalaxb/compiler/xsd/GenSource.scala
+++ b/cli/src/main/scala/scalaxb/compiler/xsd/GenSource.scala
@@ -900,16 +900,8 @@ object {localName} {{
"lazy val " + makeParamName(buildParam(attr).name, true) + " = " +
wrapperName + ".get(" + quote(buildNodeName(attr, false)) + ") map { _.as[" + buildTypeName(attr.typeSymbol, true) + "] }"
case (attr: AttributeDecl, Single) =>
- attr.defaultValue match {
- case Some(dv) =>
- val typeName = buildTypeName(attr.typeSymbol, true)
- "lazy val " + makeParamName(buildParam(attr).name, true) + " = " +
- wrapperName + ".get(" + quote(buildNodeName(attr, false)) + ").map( _.as[" + typeName +
- s"""] ).getOrElse(scalaxb.fromXML[$typeName]($dv))"""
- case None =>
- "lazy val " + makeParamName(buildParam(attr).name, true) + " = " +
- wrapperName + "(" + quote(buildNodeName(attr, false)) + ").as[" + buildTypeName(attr.typeSymbol, true) + "]"
- }
+ "lazy val " + makeParamName(buildParam(attr).name, true) + " = " +
+ wrapperName + "(" + quote(buildNodeName(attr, false)) + ").as[" + buildTypeName(attr.typeSymbol, true) + "]"
}
}
diff --git a/integration/src/test/resources/GeneralUsage.scala b/integration/src/test/resources/GeneralUsage.scala
index da52e0f64..e8eed7210 100644
--- a/integration/src/test/resources/GeneralUsage.scala
+++ b/integration/src/test/resources/GeneralUsage.scala
@@ -42,6 +42,7 @@ object GeneralUsage {
testAll
testLongAll
testLongAttribute
+ testAnyAttribute
testTopLevelMultipleSeq
testTopLevelOptionalSeq
testTopLevelMustipleSeqAny
@@ -156,9 +157,9 @@ JDREVGRw==
val obj = fromXML[SingularSimpleTypeTest](subject)
def check(obj: Any) = obj match {
- case SingularSimpleTypeTest(1, None, None, Some(Some(1)), Seq(2, 1), Seq(), None,
+ case x@SingularSimpleTypeTest(1, None, None, Some(Some(1)), Seq(2, 1), Seq(), None,
WHOLE, None, None, None, Seq(WHOLE, SKIM), Seq(),
- attrs) =>
+ attrs) if x.attr3 == WHOLE =>
case _ => sys.error("match failed: " + obj.toString)
}
check(obj)
@@ -422,6 +423,29 @@ JDREVGRw==
val document = toXML[LongAttributeTest](obj, "foo", defaultScope)
println(document)
}
+
+ def testAnyAttribute {
+ println("testAnyAttribute")
+ val subject =
+ val obj = fromXML[AnyAttributeTest](subject)
+ def check(obj: AnyAttributeTest): Unit = {
+ obj.attributes("@milk1") match {
+ case DataRecord(_, _, "SKIM") =>
+ case _ => sys.error("match failed: " + obj.attributes("@milk1").toString + " " + obj.toString)
+ }
+ obj.attributes("@{http://www.w3.org/2005/05/xmlmime}contentType") match {
+ case DataRecord(_, _, "foo") =>
+ case _ => sys.error("match failed: " + obj.attributes("@{http://www.w3.org/2005/05/xmlmime}contentType").toString + " " + obj.toString)
+ }
+ }
+ check(obj)
+ val document = toXML[AnyAttributeTest](obj, "foo", defaultScope)
+ println(document)
+ check(fromXML[AnyAttributeTest](document))
+ }
def testTopLevelMultipleSeq {
println("testTopLevelMultipleSeq")
diff --git a/integration/src/test/resources/general.xsd b/integration/src/test/resources/general.xsd
index e79f93856..d2a691c2a 100644
--- a/integration/src/test/resources/general.xsd
+++ b/integration/src/test/resources/general.xsd
@@ -147,7 +147,8 @@
-
+
+
@@ -573,6 +574,12 @@
+
+
+
+
+
+
diff --git a/integration/src/test/resources/xmlmime.xsd b/integration/src/test/resources/xmlmime.xsd
index 71417f92e..2248433f1 100644
--- a/integration/src/test/resources/xmlmime.xsd
+++ b/integration/src/test/resources/xmlmime.xsd
@@ -50,5 +50,6 @@
+
diff --git a/notes/1.3.0.markdown b/notes/1.3.0.markdown
index d22970fb7..c6859201e 100644
--- a/notes/1.3.0.markdown
+++ b/notes/1.3.0.markdown
@@ -24,10 +24,12 @@
### case class >22 and attributes change
-Starting scalaxb 1.3.0, the generated code will be >22 by default. In addition, all attributes will be handled via `attributes` field.
+Starting scalaxb 1.3.0, the generated code will be >22 by default. In addition, all attributes will be handled via `attributes` field.
-To bring back the older behavior
+To bring back the older behavior:
contentsSizeLimit in (Compile, scalaxb) := 20
namedAttributes in (Compile, scalaxb) := true
+
+Related, 1.3.0 fixes attribute's default value handling [#288][288].