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._ ...@@ -5,13 +5,13 @@ import templates._
object Main extends App { object Main extends App {
implicit def templateFactory(source: Node): Template with Logger = { implicit def templateFactory(source: Node): Template with Logger= {
(source \ "trade").head.child (source \ "trade").head.child
.filter { .filter {
case v: Elem => true case v: Elem => true
case _ => false case _ => false
}(1).label match { }(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") case v => throw new Exception("Undefined trade type")
} }
} }
...@@ -24,7 +24,6 @@ object Main extends App { ...@@ -24,7 +24,6 @@ object Main extends App {
template.log() template.log()
Try { Try {
XMLValid("src/main/resources/nsd/nsd-ext-merged-schema.xsd").loadString(sourceXml.mkString) XMLValid("src/main/resources/nsd/nsd-ext-merged-schema.xsd").loadString(sourceXml.mkString)
} match { } match {
......
...@@ -6,11 +6,6 @@ import scala.xml.{Node, Text} ...@@ -6,11 +6,6 @@ import scala.xml.{Node, Text}
trait Logger { trait Logger {
this: Template => this: Template =>
def log(): Unit = ()
}
trait DebugLogger extends Logger {
this: Template =>
private[this] val data = { private[this] val data = {
val m:mutable.LinkedHashMap[Int, mutable.Queue[(String, String, mutable.LinkedHashMap[Int, (String, String)])]] = val m:mutable.LinkedHashMap[Int, mutable.Queue[(String, String, mutable.LinkedHashMap[Int, (String, String)])]] =
...@@ -65,5 +60,5 @@ trait DebugLogger extends Logger { ...@@ -65,5 +60,5 @@ trait DebugLogger extends Logger {
case _ => Text(msgNotFound) 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) { ...@@ -181,10 +181,10 @@ class RepoTemplate(source: Node) extends TradeTemplate(source) {
*/} */}
</fpmlext:forwardLeg> </fpmlext:forwardLeg>
<fpml:equity id={ get(Try { (source \ "trade" \ "repo" \ "equity").head }, "id") }> <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:equity>
<fpml:bond id={ get(Try { (source \ "trade" \ "repo" \ "bond").head }, "id") }> <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> </fpml:bond>
</fpmlext:repo> </fpmlext:repo>
<fpml:documentation> <fpml:documentation>
......
...@@ -19,6 +19,11 @@ abstract class Template(protected[this] val source: Node) { ...@@ -19,6 +19,11 @@ abstract class Template(protected[this] val source: Node) {
def template: Node def template: Node
def render: 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 { val rule1: RewriteRule = new RewriteRule {
override def transform(n: Node): NodeSeq = n match { override def transform(n: Node): NodeSeq = n match {
case v: Elem if v.attributes.nonEmpty && v.attributes.value.text.contains(msgNotFound) => 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) { ...@@ -31,7 +36,7 @@ abstract class Template(protected[this] val source: Node) {
override def transform(n: Node): NodeSeq = n match { override def transform(n: Node): NodeSeq = n match {
case v if v.text == msgNotFound => case v if v.text == msgNotFound =>
NodeSeq.Empty NodeSeq.Empty
case v: Elem if v.text.filterNot(_.isWhitespace).isEmpty => case v: Elem if attrIsEmpty(v) && v.text.filterNot(_.isWhitespace).isEmpty =>
NodeSeq.Empty NodeSeq.Empty
case _ => n case _ => n
} }
...@@ -45,7 +50,7 @@ abstract class Template(protected[this] val source: Node) { ...@@ -45,7 +50,7 @@ abstract class Template(protected[this] val source: Node) {
} }
object Template { 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 { ...@@ -56,7 +56,7 @@ class TemplateSpec extends WordSpec {
{ get(Try { (source \\ "repo" \ "from").head }) } { get(Try { (source \\ "repo" \ "from").head }) }
</from> </from>
<to href={ get(Try { (source \\ "repo" \ "to").head }, "href") }> <to href={ get(Try { (source \\ "repo" \ "to").head }, "href") }>
{ get(Try { (source \\ "repo" \ "to").head }) } { get(Try { (source \\ "repo" \ "notExist").head }) }
</to> </to>
</repo> </repo>
<notFoundpart>{ get(Try { (source \\ "notFoundpart").head }) }</notFoundpart> <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