Commit 38c07285 by Amelin Konstantin

Add exception handler to Main.

parent c617955d
...@@ -37,34 +37,50 @@ object Main { ...@@ -37,34 +37,50 @@ object Main {
for (v <- Files.walk(Paths.get(args(0))).toArray; f = v.asInstanceOf[Path]; if !Files.isDirectory(f)) { for (v <- Files.walk(Paths.get(args(0))).toArray; f = v.asInstanceOf[Path]; if !Files.isDirectory(f)) {
val fSourceName= f.getFileName.toString val fSourceName= f.getFileName.toString
val fOutputName = s"Converted_$fSourceName" 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 val now = LocalDateTime.now().toString
val sourceXmlValid = if (template.isStraight) nsdXmlValid else rtsXmlValid Try {
val outputXmlValid = if (template.isStraight) rtsXmlValid else nsdXmlValid val sourceXml = XML.loadFile(f.toString)
val msgCheckSource = Try { val template = Template(sourceXml)
sourceXmlValid.loadString(sourceXml.mkString) val outputXml = XML.loadString(p.format(template.render))
} match {
case Success(_) => s"'$fSourceName' successfully checked against schema"
case Failure(ex) => s"'$fSourceName' isn`t wellformed or invalid: ${ex.getMessage}"
}
val msgCheckOutput = Try { val sourceXmlValid = if (template.isStraight) nsdXmlValid else rtsXmlValid
outputXmlValid.loadString(outputXml.mkString) val outputXmlValid = if (template.isStraight) rtsXmlValid else nsdXmlValid
} match {
case Success(_) => s"'$fOutputName' successfully checked against schema" val msgCheckSource = Try {
case Failure(ex) => s"'$fOutputName' isn`t wellformed or invalid: ${ex.getMessage}" sourceXmlValid.loadString(sourceXml.mkString)
} } match {
case Success(_) => s"'$fSourceName' successfully checked against schema"
case Failure(ex) => s"'$fSourceName' isn`t wellformed or invalid: ${ex.getMessage}"
}
XML.save(Paths.get(args(1), fOutputName).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}"
}
logString += template.info(s"$now\n$msgCheckSource\n$msgCheckOutput") + "\n\n" XML.save(Paths.get(args(1), fOutputName).toString, outputXml, xmlDecl = true)
template.info(s"$now\n$msgCheckSource\n$msgCheckOutput") + "\n\n"
} match {
case Success(ti) => logString += ti
case Failure(ex) => logString += s"'$now\nImpossible to convert'$fSourceName': ${ex.getMessage}\n\n"
}
} }
Files.write(Paths.get(args(1), logFileName), logString.getBytes("utf-8"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING) Try {
Files.write(
Paths.get(args(1), logFileName),
logString.getBytes("utf-8"),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING)
} match {
case Success(_) => println(s"Work has finished. $logFileName located in ${args(1)}")
case Failure(ex) => println(s"Error. Work hasn`t finished: ${ex.getMessage}")
}
} }
} }
} }
...@@ -72,13 +72,13 @@ trait Logger { ...@@ -72,13 +72,13 @@ trait Logger {
case (t, v, m) if v != null || (m != null && m.nonEmpty) => true case (t, v, m) if v != null || (m != null && m.nonEmpty) => true
case _ => false case _ => false
} }
.map { .map {
case (t, v, m) => s"tag: $t, value: ${if (v != null) v else ""}" + 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 { (if (m != null && m.nonEmpty) ", " + m.toVector.map { case (k, v1) => v1}.map {
case (k1, v2) => s"attr: $k1, value: $v2" case (k1, v2) => s"attr: $k1, value: $v2"
}.mkString(", ") else "") }.mkString(", ") else "")
} }
.mkString("\n") .mkString("\n")
s"$msg\nNot used:\n$additionalInfo" s"$msg\nNot used:\n$additionalInfo"
} }
......
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