Commit 1354bc97 by Amelin Konstantin

Add log to file, defined in application.conf

parent 10bfeca9
...@@ -10,17 +10,16 @@ ...@@ -10,17 +10,16 @@
Импортировать проект. <https://www.jetbrains.com/help/idea/sbt-support.html#import_sbt> Импортировать проект. <https://www.jetbrains.com/help/idea/sbt-support.html#import_sbt>
## Запуск проекта ## Запуск проекта dev mode
1) Создать в корне проекта папку **output**. 1) Создать в корне проекта папку **output**.
2) Запуск из терминала 2) Запуск из терминала
``` ```
sbt run sbt "run input output"
или или
sbt ~run sbt "~run input output"
``` ```
3) Запуск из IDE. <https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html#RunningDebugging> 3) Запуск из IDE. <https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html#RunningDebugging>
4) Запуск тестов из терминала 4) Запуск тестов из терминала
...@@ -29,4 +28,7 @@ sbt test ...@@ -29,4 +28,7 @@ sbt test
или или
sbt ~test sbt ~test
``` ```
## Запуск проекта production mode
1) Для отключения детального логгирования установить **debug = false** в src/main/resources/application.conf
# Application settings # Application settings
# Set debug mode # Set debug mode
debug = true debug = true
\ No newline at end of file
# Log file name
log = "log.txt"
\ No newline at end of file
import com.typesafe.config.{Config, ConfigFactory} import com.typesafe.config.{Config, ConfigFactory}
import scala.xml._ import scala.xml._
import java.nio.file.{Files, Path, Paths} import java.nio.file.{Files, Path, Paths, StandardOpenOption}
import java.time.LocalDateTime
import scala.util.{Failure, Success, Try} import scala.util.{Failure, Success, Try}
import xmlvalid._ import xmlvalid._
import templates._ import templates._
...@@ -8,14 +11,15 @@ import templates._ ...@@ -8,14 +11,15 @@ import templates._
object Main { object Main {
val conf: Config = ConfigFactory.load val conf: Config = ConfigFactory.load
val debugMode: Boolean = conf.getBoolean("debug") val debugMode: Boolean = conf.getBoolean("debug")
val logFileName: String = conf.getString("log")
implicit def templateFactory(source: Node): Template with Logger = { implicit def templateFactory(source: Node): Template = {
(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 Logger case "repo" => if (debugMode) new RepoTemplate(source) with Logger else new RepoTemplate(source)
case v => throw new Exception("Undefined trade type") case v => throw new Exception("Undefined trade type")
} }
} }
...@@ -26,35 +30,41 @@ object Main { ...@@ -26,35 +30,41 @@ object Main {
else if (!Files.exists(Paths.get(args(1)))) println("Error: OUTPUT directory doesn`t exist") else if (!Files.exists(Paths.get(args(1)))) println("Error: OUTPUT directory doesn`t exist")
else { else {
val p = new PrettyPrinter(140, 2) val p = new PrettyPrinter(140, 2)
val nsdXmlValid = XMLValid("src/main/resources/nsd/nsd-ext-merged-schema.xsd")
val rtsXmlValid = XMLValid("src/main/resources/rts/fpml-recordkeeping-merged-schema.xsd")
var logString = ""
for (v <- Files.walk(Paths.get(args(0))).toArray; f = v.asInstanceOf[Path]; if !Files.isDirectory(f)) {
val fSourceName= f.getFileName.toString
val fOutputName = s"Converted_$fSourceName"
val sourceXml = XML.loadFile(f.toString)
val template = Template(sourceXml)
val outputXml = XML.loadString(p.format(template.render))
val now = LocalDateTime.now().toString
for (v <- Files.walk(Paths.get(args(0))).toArray) { val sourceXmlValid = if (template.isStraight) nsdXmlValid else rtsXmlValid
val f = v.asInstanceOf[Path] val outputXmlValid = if (template.isStraight) rtsXmlValid else nsdXmlValid
if (!Files.isDirectory(f)) {
val fName= f.getFileName.toString.split('.') val msgCheckSource = Try {
val sourceXml = XML.loadFile(f.toString) sourceXmlValid.loadString(sourceXml.mkString)
val template = Template(sourceXml) } match {
val outputXml = XML.loadString(p.format(template.render)) case Success(_) => s"'$fSourceName' successfully checked against schema"
//val outputXml = template.template case Failure(ex) => s"'$fSourceName' isn`t wellformed or invalid: ${ex.getMessage}"
//template.log()
Try {
XMLValid("src/main/resources/nsd/nsd-ext-merged-schema.xsd").loadString(sourceXml.mkString)
} match {
case Success(_) => println("Source xml success check against NSD schema")
case Failure(ex) => println(s"Source xml isn`t wellformed or invalid: ${ex.getMessage}")
}
Try {
XMLValid("src/main/resources/rts/fpml-recordkeeping-merged-schema.xsd").loadString(outputXml.mkString)
} match {
case Success(_) => println("Output xml success check against RTS schema")
case Failure(ex) => println(s"Output xml isn`t wellformed or invalid: ${ex.getMessage}")
}
XML.save(Paths.get(args(1), fName(0) + "_RTS." + fName(1)).toString, outputXml, xmlDecl = true)
} }
val msgCheckOutput = Try {
outputXmlValid.loadString(outputXml.mkString)
} match {
case Success(_) => s"'$fOutputName' successfully checked against schema"
case Failure(ex) => s"'$fOutputName' isn`t wellformed or invalid: ${ex.getMessage}"
}
XML.save(Paths.get(args(1), fOutputName).toString, outputXml, xmlDecl = true)
logString += template.info(s"$now\n$msgCheckSource\n$msgCheckOutput") + "\n\n"
} }
Files.write(Paths.get(args(1), logFileName), logString.getBytes("utf-8"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)
} }
} }
} }
...@@ -21,9 +21,9 @@ trait Logger { ...@@ -21,9 +21,9 @@ trait Logger {
m.get(idx) match { m.get(idx) match {
case None => case None =>
m(idx) = mutable.Queue((s"tag: ${i.label}", text, attr)) m(idx) = mutable.Queue((i.label, text, attr))
case _ => case _ =>
m(idx).enqueue((s"tag: ${i.label}", text, attr)) m(idx).enqueue((i.label, text, attr))
} }
if (i.attributes.nonEmpty) { if (i.attributes.nonEmpty) {
...@@ -61,5 +61,25 @@ trait Logger { ...@@ -61,5 +61,25 @@ trait Logger {
case _ => Text(msgNotFound) case _ => Text(msgNotFound)
} }
def log(): Unit = data.foreach(e => println(e._2)) override def info(msg: String): String = {
val additionalInfo = data.filter {
case (k, v) if v.nonEmpty => true
case _ => false
}
.toVector
.flatMap { case (k, v ) => v }
.filter {
case (t, v, m) if v != null || (m != null && m.nonEmpty) => true
case _ => false
}
.map {
case (t, v, m) => s"tag: $t, value: ${if (v != null) v else ""}" +
(if (m != null && m.nonEmpty) ", " + m.toVector.map { case (k, v1) => v1}.map {
case (k1, v2) => s"attr: $k1, value: $v2"
}.mkString(", ") else "")
}
.mkString("\n")
s"$msg\nNot used:\n$additionalInfo"
}
} }
...@@ -83,4 +83,6 @@ class RepoTemplate(source: Node) extends TradeTemplate(source) { ...@@ -83,4 +83,6 @@ class RepoTemplate(source: Node) extends TradeTemplate(source) {
</fpml:bond> </fpml:bond>
</fpmlext:repo> </fpmlext:repo>
} }
def isStraight = true
} }
\ No newline at end of file
...@@ -19,6 +19,8 @@ abstract class Template(protected[this] val source: Node) { ...@@ -19,6 +19,8 @@ abstract class Template(protected[this] val source: Node) {
def template: Node def template: Node
def isStraight: Boolean
def render: Node = { def render: Node = {
def attrIsEmpty(n: Node): Boolean = n match { def attrIsEmpty(n: Node): Boolean = n match {
case v: Elem if v.child.isEmpty => v.attributes.isEmpty case v: Elem if v.child.isEmpty => v.attributes.isEmpty
...@@ -48,10 +50,12 @@ abstract class Template(protected[this] val source: Node) { ...@@ -48,10 +50,12 @@ abstract class Template(protected[this] val source: Node) {
rt2.transform(rt1.transform(template)).head rt2.transform(rt1.transform(template)).head
} }
def info(msg: String): String = msg
} }
object Template { object Template {
def apply[T <: Template](source: Node)(implicit templateFactory: Node => T): T = templateFactory(source) def apply(source: Node)(implicit templateFactory: Node => Template): Template = templateFactory(source)
} }
...@@ -79,6 +79,8 @@ class TemplateSpec extends WordSpec { ...@@ -79,6 +79,8 @@ class TemplateSpec extends WordSpec {
</nonpublicExecutionReport> </nonpublicExecutionReport>
</document> </document>
} }
def isStraight: Boolean = true
} }
"Template" should { "Template" should {
...@@ -127,17 +129,6 @@ class TemplateSpec extends WordSpec { ...@@ -127,17 +129,6 @@ class TemplateSpec extends WordSpec {
assertThrows[Exception] { assertThrows[Exception] {
val template = Template(sourceXml) val template = Template(sourceXml)
} }
}
}
it should {
"generate valid RTS output xml using valid NSD source xml" ignore {
val sourceXml: Node = XML.loadFile("src/test/resources/templates/nsd/Example repo NSD.xml")
val outputXml = new RepoTemplate(sourceXml).template
val checkedXml = XMLValid("src/test/resources/templates/rts/fpml-recordkeeping-merged-schema.xsd").loadString(outputXml.mkString)
assert(checkedXml.isInstanceOf[Node])
} }
} }
} }
......
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