Commit ee6e333a by Amelin Konstantin

Define correct type to template factory. Add attrIsEmty to render, fix RepoTemplate

parent 0abb089c
......@@ -5,13 +5,13 @@ import templates._
object Main extends App {
implicit def templateFactory(source: Node): Template with Logger = {
implicit def templateFactory(source: Node): Template with Logger= {
(source \ "trade").head.child
.filter {
case v: Elem => true
case _ => false
}(1).label match {
case "repo" => new RepoTemplate(source) with DebugLogger
case "repo" => new RepoTemplate(source) with Logger
case v => throw new Exception("Undefined trade type")
}
}
......@@ -24,7 +24,6 @@ object Main extends App {
template.log()
Try {
XMLValid("src/main/resources/nsd/nsd-ext-merged-schema.xsd").loadString(sourceXml.mkString)
} match {
......
......@@ -6,11 +6,6 @@ import scala.xml.{Node, Text}
trait Logger {
this: Template =>
def log(): Unit = ()
}
trait DebugLogger extends Logger {
this: Template =>
private[this] val data = {
val m:mutable.LinkedHashMap[Int, mutable.Queue[(String, String, mutable.LinkedHashMap[Int, (String, String)])]] =
......@@ -65,5 +60,5 @@ trait DebugLogger extends Logger {
case _ => Text(msgNotFound)
}
override def log(): Unit = data.foreach(e => println(e._2))
def log(): Unit = data.foreach(e => println(e._2))
}
......@@ -181,10 +181,10 @@ class RepoTemplate(source: Node) extends TradeTemplate(source) {
*/}
</fpmlext:forwardLeg>
<fpml:equity id={ get(Try { (source \ "trade" \ "repo" \ "equity").head }, "id") }>
<fpml:instrumentId instrumentIdScheme="">{ get(Try { (source \ "trade" \ "repo" \ "equity" \ "instrumentId").head }) }</fpml:instrumentId>
<fpml:instrumentId instrumentIdScheme={ get(Try { (source \ "trade" \ "repo" \ "equity" \ "instrumentId").head }, "instrumentIdScheme") }>{ get(Try { (source \ "trade" \ "repo" \ "equity" \ "instrumentId").head }) }</fpml:instrumentId>
</fpml:equity>
<fpml:bond id={ get(Try { (source \ "trade" \ "repo" \ "bond").head }, "id") }>
<fpml:instrumentId instrumentIdScheme="">{ get(Try { (source \ "trade" \ "repo" \ "bond" \ "instrumentId").head }) }</fpml:instrumentId>
<fpml:instrumentId instrumentIdScheme={ get(Try { (source \ "trade" \ "repo" \ "bond" \ "instrumentId").head }, "instrumentIdScheme") }>{ get(Try { (source \ "trade" \ "repo" \ "bond" \ "instrumentId").head }) }</fpml:instrumentId>
</fpml:bond>
</fpmlext:repo>
<fpml:documentation>
......
......@@ -19,6 +19,11 @@ abstract class Template(protected[this] val source: Node) {
def template: Node
def render: Node = {
def attrIsEmpty(n: Node): Boolean = n match {
case v: Elem if v.child.isEmpty => v.attributes.isEmpty
case v => v.attributes.isEmpty && v.child.forall(attrIsEmpty)
}
val rule1: RewriteRule = new RewriteRule {
override def transform(n: Node): NodeSeq = n match {
case v: Elem if v.attributes.nonEmpty && v.attributes.value.text.contains(msgNotFound) =>
......@@ -31,7 +36,7 @@ abstract class Template(protected[this] val source: Node) {
override def transform(n: Node): NodeSeq = n match {
case v if v.text == msgNotFound =>
NodeSeq.Empty
case v: Elem if v.text.filterNot(_.isWhitespace).isEmpty =>
case v: Elem if attrIsEmpty(v) && v.text.filterNot(_.isWhitespace).isEmpty =>
NodeSeq.Empty
case _ => n
}
......@@ -45,7 +50,7 @@ abstract class Template(protected[this] val source: Node) {
}
object Template {
def apply(source: Node)(implicit templateFactory: Node => Template with Logger): Template with Logger = templateFactory(source)
def apply[T <: Template](source: Node)(implicit templateFactory: Node => T): T = templateFactory(source)
}
......@@ -56,7 +56,7 @@ class TemplateSpec extends WordSpec {
{ get(Try { (source \\ "repo" \ "from").head }) }
</from>
<to href={ get(Try { (source \\ "repo" \ "to").head }, "href") }>
{ get(Try { (source \\ "repo" \ "to").head }) }
{ get(Try { (source \\ "repo" \ "notExist").head }) }
</to>
</repo>
<notFoundpart>{ get(Try { (source \\ "notFoundpart").head }) }</notFoundpart>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment