Commit 38c07285 by Amelin Konstantin

Add exception handler to Main.

parent c617955d
......@@ -37,34 +37,50 @@ object Main {
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
val sourceXmlValid = if (template.isStraight) nsdXmlValid else rtsXmlValid
val outputXmlValid = if (template.isStraight) rtsXmlValid else nsdXmlValid
Try {
val sourceXml = XML.loadFile(f.toString)
val msgCheckSource = Try {
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}"
}
val template = Template(sourceXml)
val outputXml = XML.loadString(p.format(template.render))
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}"
}
val sourceXmlValid = if (template.isStraight) nsdXmlValid else rtsXmlValid
val outputXmlValid = if (template.isStraight) rtsXmlValid else nsdXmlValid
val msgCheckSource = Try {
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 {
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")
.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"
}
......
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