Commit 0abb089c by Amelin Konstantin

split debug and main behavior. Add trait Logger

parent 3474fac9
......@@ -5,13 +5,13 @@ import templates._
object Main extends App {
implicit def templateFactory(source: Node): Template = {
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)
case "repo" => new RepoTemplate(source) with DebugLogger
case v => throw new Exception("Undefined trade type")
}
}
......@@ -22,7 +22,7 @@ object Main extends App {
val outputXml = XML.loadString(p.format(template.render))
//val outputXml = template.template
//template.log()
template.log()
Try {
......
......@@ -3,7 +3,7 @@ package templates
import scala.util.Try
import scala.xml.Node
final class RepoTemplate(source: Node) extends TradeTemplate(source) {
class RepoTemplate(source: Node) extends TradeTemplate(source) {
protected[this] def templateTrade: Node = {
<fpml:trade>
<fpml:tradeHeader>
......
package templates
import scala.collection.mutable
import scala.util.{Success, Try}
import scala.xml.transform.{RewriteRule, RuleTransformer}
import scala.xml._
abstract class Template(protected[this] val source: Node) {
protected[this] val msgNotFound = "Not found"
private[this] val data = {
val m:mutable.LinkedHashMap[Int, mutable.Queue[(String, String, mutable.LinkedHashMap[Int, (String, String)])]] =
mutable.LinkedHashMap()
val items = source \\ "_"
for (i <- items) {
if (i.attributes.nonEmpty || (i.child.length == 1)) {
val idx = i.hashCode
val attr: mutable.LinkedHashMap[Int, (String, String)] =
if (i.attributes.nonEmpty) mutable.LinkedHashMap() else null
val text = if (i.child.nonEmpty && !i.child.head.text.contains("\n")) i.child.head.text else null
m.get(idx) match {
case None =>
m(idx) = mutable.Queue((s"tag: ${i.label}", text, attr))
case _ =>
m(idx).enqueue((s"tag: ${i.label}", text, attr))
}
if (i.attributes.nonEmpty) {
for (a <- i.attributes) {
m(idx).last._3(a.value.hashCode) = (a.key, a.value.text)
}
}
}
protected[this] def get(content: Try[Node], attr: String = null): Text = content match {
case Success(v) if attr != null => v.attribute(attr) match {
case None => Text(msgNotFound)
case o => Text(o.get.text)
}
m
}
protected[this] def get(content: Try[Node], attr: String = null): Text =
content match {
case Success(v) =>
var res = Text(v.text)
if (attr != null) v.attribute(attr) match {
case None =>
res = Text(msgNotFound)
case o =>
data(v.hashCode).head._3 -= o.get.hashCode
res = Text(o.get.text)
} else if (data(v.hashCode).nonEmpty) {
if (data(v.hashCode).head._3 != null) {
val e = data(v.hashCode).head.copy(_2 = null)
data(v.hashCode).enqueue(e)
}
data(v.hashCode).dequeue()
}
res
case _ => Text(msgNotFound)
case Success(v) => Text(v.text)
case _ => Text(msgNotFound)
}
def template: Node
......@@ -87,12 +42,10 @@ abstract class Template(protected[this] val source: Node) {
rt2.transform(rt1.transform(template)).head
}
def log(): Unit = data.foreach(e => println(e._2))
}
object Template {
def apply(source: Node)(implicit templateFactory: Node => Template): Template = templateFactory(source)
def apply(source: Node)(implicit templateFactory: Node => Template with Logger): Template with Logger = templateFactory(source)
}
package templates
import scala.collection.mutable
import scala.util.{Success, Try}
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)])]] =
mutable.LinkedHashMap()
val items = source \\ "_"
for (i <- items) {
if (i.attributes.nonEmpty || (i.child.length == 1)) {
val idx = i.hashCode
val attr: mutable.LinkedHashMap[Int, (String, String)] =
if (i.attributes.nonEmpty) mutable.LinkedHashMap() else null
val text = if (i.child.nonEmpty && !i.child.head.text.contains("\n")) i.child.head.text else null
m.get(idx) match {
case None =>
m(idx) = mutable.Queue((s"tag: ${i.label}", text, attr))
case _ =>
m(idx).enqueue((s"tag: ${i.label}", text, attr))
}
if (i.attributes.nonEmpty) {
for (a <- i.attributes) {
m(idx).last._3(a.value.hashCode) = (a.key, a.value.text)
}
}
}
}
m
}
override protected[this] def get(content: Try[Node], attr: String = null): Text = content match {
case Success(v) =>
var res = Text(v.text)
if (attr != null) v.attribute(attr) match {
case None =>
res = Text(msgNotFound)
case o =>
data(v.hashCode).head._3 -= o.get.hashCode
res = Text(o.get.text)
} else if (data(v.hashCode).nonEmpty) {
if (data(v.hashCode).head._3 != null) {
val e = data(v.hashCode).head.copy(_2 = null)
data(v.hashCode).enqueue(e)
}
data(v.hashCode).dequeue()
}
res
case _ => Text(msgNotFound)
}
override def log(): Unit = data.foreach(e => println(e._2))
}
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