Commit 023693eb by Amelin Konstantin

Fix exception handling

parent ff5e7399
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
Запуск осуществляется из командной строки с двумя параметрами: путь к директории входящих сообщений, путь к директории сконвертированных сообщений. Запуск осуществляется из командной строки с двумя параметрами: путь к директории входящих сообщений, путь к директории сконвертированных сообщений.
Конвертер работает по принципу пакетного обработчика. Действие выполняется над всеми файлами, находящимися в директории входящих сообщений. Конвертер работает по принципу пакетного обработчика. Действие выполняется над всеми файлами, находящимися в директории входящих сообщений.
Поддерживается формат xml и zip (архив, cодержащий xml файлы). Поддерживается формат xml и zip (архив, cодержащий xml файлы).
Лог файл с отчетом о конвертации сохраняется в директории сконвертированных сообщений. Лог файл с отчетом о конвертации сохраняется в директории входящих сообщений.
Запуск. Запуск.
......
...@@ -61,7 +61,7 @@ object Main { ...@@ -61,7 +61,7 @@ object Main {
val name = f.getName val name = f.getName
val ext = name.split('.').last.toLowerCase val ext = name.split('.').last.toLowerCase
if (ext == "zip") { if (ext == "zip")
Try { Try {
val zip = new ZipFile(f) val zip = new ZipFile(f)
...@@ -73,6 +73,7 @@ object Main { ...@@ -73,6 +73,7 @@ object Main {
e => e =>
FileEntry( FileEntry(
s"${name}_${e.getName}", s"${name}_${e.getName}",
//e.getName,
e.getLastModifiedTime.toMillis, e.getLastModifiedTime.toMillis,
Some(zip.getInputStream(e)), Some(zip.getInputStream(e)),
zip.getInputStream(e), zip.getInputStream(e),
...@@ -82,59 +83,60 @@ object Main { ...@@ -82,59 +83,60 @@ object Main {
case Success(v) => case Success(v) =>
v v
case Failure(ex) => case Failure(ex) =>
Array(FileEntry(name, f.lastModified, None, new FileInputStream(f), Some(ex.getMessage))) Array(FileEntry(name, f.lastModified, None, new FileInputStream(f), Some(ex.toString)))
} }
} else else
Array(FileEntry(name, f.lastModified, Some(new FileInputStream(f)), new FileInputStream(f), None)) Array(FileEntry(name, f.lastModified, Some(new FileInputStream(f)), new FileInputStream(f), None))
} }
} }
.flatten .flatten
.sortWith { (a, b) => (a.modified == b.modified && a.name <= b.name) || (a.modified < b.modified) } .sortWith { (a, b) => (a.modified == b.modified && a.name <= b.name) || (a.modified < b.modified) }
for (f <- sourceArray) {
val now = LocalDateTime.now.toString
f.msg match { Try {
case Some(msg) => for (f <- sourceArray) {
logString += s"$now\nImpossible to convert '${f.name}': $msg\n\n" val now = LocalDateTime.now.toString
Files.copy(f.streamMove, Paths.get(sourceNameError, f.name))
case None => f.msg match {
Try { case Some(msg) =>
convert( logString += s"'$now'\nImpossible to convert '${f.name}': $msg\n\n"
f.streamConvert.get, Files.copy(f.streamMove, Paths.get(sourceNameError, f.name))
f.name,
s"converted_${f.name}",
nsdXmlValid,
rtsXmlValid,
p)
} match {
case Success((oXml, ti)) if ti.contains("isn`t wellformed or invalid") =>
XML.save(
Paths.get(outputNameNotValidated, s"converted_${f.name}").toString,
oXml,
xmlDecl = true
)
logString += s"$now\n$ti"
Files.copy(f.streamMove, Paths.get(sourceNameNotValidated, f.name))
case Success((oXml, ti)) =>
XML.save(
Paths.get(outputNameValidated, s"converted_${f.name}").toString,
oXml,
xmlDecl = true
)
logString += s"$now\n$ti"
Files.copy(f.streamMove, Paths.get(sourceNameValidated, f.name))
case Failure(ex) => case None =>
logString += s"$now\nImpossible to convert '${f.name}': ${ex.getMessage}\n\n" Try {
Files.copy(f.streamMove, Paths.get(sourceNameError, f.name)) convert(
} f.streamConvert.get,
f.name,
s"converted_${f.name}",
nsdXmlValid,
rtsXmlValid,
p)
} match {
case Success((oXml, ti)) if ti.contains("isn`t wellformed or invalid") =>
XML.save(
Paths.get(outputNameNotValidated, s"converted_${f.name}").toString,
oXml,
xmlDecl = true
)
logString += s"'$now'\n$ti"
Files.copy(f.streamMove, Paths.get(sourceNameNotValidated, f.name))
case Success((oXml, ti)) =>
XML.save(
Paths.get(outputNameValidated, s"converted_${f.name}").toString,
oXml,
xmlDecl = true
)
logString += s"'$now'\n$ti"
Files.copy(f.streamMove, Paths.get(sourceNameValidated, f.name))
case Failure(ex) =>
logString += s"'$now'\nImpossible to convert '${f.name}': ${ex.toString}\n\n"
Files.copy(f.streamMove, Paths.get(sourceNameError, f.name))
}
}
} }
}
Try {
//files.foreach { _.delete } //files.foreach { _.delete }
Files.write( Files.write(
...@@ -155,10 +157,11 @@ object Main { ...@@ -155,10 +157,11 @@ object Main {
} match { } match {
case Success(_) => println(s"Converting has been finished. $logFileName located in ${args(0)}") case Success(_) => println(s"Converting has been finished. $logFileName located in ${args(0)}")
case Failure(ex) => println(s"Error. Converting hasn`t been finished: ${ex.getMessage}") case Failure(ex) => println(s"Error. Converting hasn`t been finished: ${ex.toString}")
} }
} else } else
println(s"Error. Converting hasn`t been finished: unable to create moving directories.") println(s"Error. Converting hasn`t been finished: unable to create directories to move files.")
} }
} }
...@@ -209,12 +212,13 @@ object Main { ...@@ -209,12 +212,13 @@ object Main {
} }
def convert( def convert(
inputStream: InputStream, inputStream: InputStream,
sName: String, sName: String,
oName: String, oName: String,
nsdScheme: XMLValid, nsdScheme: XMLValid,
rtsScheme: XMLValid, rtsScheme: XMLValid,
pp: PrettyPrinter): (Node, String) = { pp: PrettyPrinter): (Node, String) = {
val sourceXml = XML.load(inputStream) val sourceXml = XML.load(inputStream)
val template = Template(sourceXml) val template = Template(sourceXml)
...@@ -231,24 +235,24 @@ object Main { ...@@ -231,24 +235,24 @@ object Main {
sourceXmlValid.loadString(sourceXml.mkString) sourceXmlValid.loadString(sourceXml.mkString)
} match { } match {
case Success(_) => s"'$sName' successfully checked against schema" case Success(_) => s"'$sName' successfully checked against schema"
case Failure(ex) => s"'$sName' isn`t wellformed or invalid: ${ex.getMessage}" case Failure(ex) => s"'$sName' isn`t wellformed or invalid: ${ex.toString}"
} }
val msgCheckOutput = Try { val msgCheckOutput = Try {
outputXmlValid.loadString(outputXml.mkString) outputXmlValid.loadString(outputXml.mkString)
} match { } match {
case Success(_) => s"'$oName' successfully checked against schema" case Success(_) => s"'$oName' successfully checked against schema"
case Failure(ex) => s"'$oName' isn`t wellformed or invalid: ${ex.getMessage}" case Failure(ex) => s"'$oName' isn`t wellformed or invalid: ${ex.toString}"
} }
(outputXml, template.info(s"$msgCheckSource\n$msgCheckOutput") + "\n\n") (outputXml, template.info(s"$msgCheckSource\n$msgCheckOutput") + "\n\n")
} }
case class FileEntry( case class FileEntry(
name: String, name: String,
modified: Long, modified: Long,
streamConvert: Option[InputStream], streamConvert: Option[InputStream],
streamMove: InputStream, streamMove: InputStream,
msg: Option[String] msg: Option[String]
) )
} }
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