Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
SPB_Exchange_Repo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Amelin Konstantin
SPB_Exchange_Repo
Commits
ff5e7399
Commit
ff5e7399
authored
Jul 09, 2019
by
Amelin Konstantin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change sorting logic and refactor code.
parent
a573894d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
54 deletions
+94
-54
src/main/scala/Main.scala
+94
-54
No files found.
src/main/scala/Main.scala
View file @
ff5e7399
...
...
@@ -3,7 +3,7 @@ import com.typesafe.config.{Config, ConfigFactory}
import
scala.xml._
import
java.nio.file.
{
Files
,
Paths
,
StandardOpenOption
}
import
java.io.
{
File
,
FileInputStream
,
InputStream
}
import
java.util.zip.
ZipFile
import
java.util.zip.
{
ZipEntry
,
ZipFile
}
import
java.time.LocalDateTime
import
scala.util.
{
Failure
,
Success
,
Try
}
...
...
@@ -35,74 +35,102 @@ object Main {
.
filter
{
!
_
.
isDirectory
}
.
sortBy
{
_
.
lastModified
}
val
start
=
LocalDateTime
.
now
().
toString
val
successSourcesName
=
s
"${args(0)}/${start}_success"
val
successSources
=
new
File
(
successSourcesName
)
val
errorSourcesName
=
s
"${args(0)}/${start}_error"
val
errorSources
=
new
File
(
errorSourcesName
)
val
start
=
LocalDateTime
.
now
.
toString
if
(
successSources
.
mkdir
()
&&
errorSources
.
mkdir
())
{
val
sourceNameValidated
=
s
"${args(0)}/${start}_validated"
val
sourceValidated
=
new
File
(
sourceNameValidated
)
val
sourceNameNotValidated
=
s
"${args(0)}/${start}_not_validated"
val
sourceNotValidated
=
new
File
(
sourceNameNotValidated
)
val
sourceNameError
=
s
"${args(0)}/${start}_error"
val
sourceError
=
new
File
(
sourceNameError
)
for
(
f
<-
files
)
{
val
fSourceName
=
f
.
getName
val
fSourceNameArray
=
fSourceName
.
split
(
'.'
)
val
fSourceNameExtension
=
fSourceNameArray
.
last
.
toLowerCase
val
now
=
LocalDateTime
.
now
().
toString
val
outputNameValidated
=
s
"${args(1)}/${start}_validated"
val
outputValidated
=
new
File
(
outputNameValidated
)
val
outputNameNotValidated
=
s
"${args(1)}/${start}_not_validated"
val
outputNotValidated
=
new
File
(
outputNameNotValidated
)
if
(
fSourceNameExtension
==
"zip"
)
Try
{
val
fZip
=
new
ZipFile
(
f
)
val
fZipEntries
=
fZip
.
entries
if
(
sourceValidated
.
mkdir
&&
sourceNotValidated
.
mkdir
&&
sourceError
.
mkdir
&&
outputValidated
.
mkdir
&&
outputNotValidated
.
mkdir
)
{
while
(
fZipEntries
.
hasMoreElements
)
{
val
e
=
fZipEntries
.
nextElement
if
(!
e
.
isDirectory
)
{
val
fZipEntryName
=
e
.
getName
val
nowForZip
=
LocalDateTime
.
now
().
toString
val
sourceArray
=
{
for
(
f
<-
files
)
yield
{
val
name
=
f
.
getName
val
ext
=
name
.
split
(
'.'
).
last
.
toLowerCase
if
(
ext
==
"zip"
)
{
Try
{
convert
(
fZip
.
getInputStream
(
e
),
s
"${fSourceName}_$fZipEntryName"
,
s
"Converted_${fSourceName}_$fZipEntryName"
,
nsdXmlValid
,
rtsXmlValid
,
p
)
val
zip
=
new
ZipFile
(
f
)
zip
.
stream
.
toArray
.
map
{
z
=>
z
.
asInstanceOf
[
ZipEntry
]
}
.
map
{
e
=>
FileEntry
(
s
"${name}_${e.getName}"
,
e
.
getLastModifiedTime
.
toMillis
,
Some
(
zip
.
getInputStream
(
e
)),
zip
.
getInputStream
(
e
),
None
)
}
}
match
{
case
Success
((
oXml
,
ti
))
=>
XML
.
save
(
Paths
.
get
(
args
(
1
),
s
"Converted_${fSourceName}_$fZipEntryName"
).
toString
,
oXml
,
xmlDecl
=
true
)
logString
+=
s
"$nowForZip\n$ti"
Files
.
copy
(
fZip
.
getInputStream
(
e
),
Paths
.
get
(
successSourcesName
,
s
"${fSourceName}_$fZipEntryName"
))
case
Success
(
v
)
=>
v
case
Failure
(
ex
)
=>
logString
+=
s
"'$nowForZip\nImpossible to convert '${fSourceName}_$fZipEntryName': ${ex.getMessage}\n\n"
Files
.
copy
(
fZip
.
getInputStream
(
e
),
Paths
.
get
(
errorSourcesName
,
s
"${fSourceName}_$fZipEntryName"
))
}
Array
(
FileEntry
(
name
,
f
.
lastModified
,
None
,
new
FileInputStream
(
f
),
Some
(
ex
.
getMessage
)))
}
}
else
Array
(
FileEntry
(
name
,
f
.
lastModified
,
Some
(
new
FileInputStream
(
f
)),
new
FileInputStream
(
f
),
None
))
}
}
match
{
case
Success
(
_
)
=>
case
Failure
(
ex
)
=>
logString
+=
s
"'$now\nImpossible to convert '$fSourceName': ${ex.getMessage}\n\n"
}
else
Try
{
.
flatten
.
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
{
case
Some
(
msg
)
=>
logString
+=
s
"$now\nImpossible to convert '${f.name}': $msg\n\n"
Files
.
copy
(
f
.
streamMove
,
Paths
.
get
(
sourceNameError
,
f
.
name
))
case
None
=>
Try
{
convert
(
new
FileInputStream
(
f
)
,
s
"$fSourceName"
,
s
"Converted_$fSourceName
"
,
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
(
args
(
1
),
s
"Converted_$fSourceName"
).
toString
,
oXml
,
xmlDecl
=
true
)
XML
.
save
(
Paths
.
get
(
outputNameValidated
,
s
"converted_${f.name}"
).
toString
,
oXml
,
xmlDecl
=
true
)
logString
+=
s
"$now\n$ti"
Files
.
copy
(
f
.
toPath
,
Paths
.
get
(
successSourcesName
,
f
.
getName
))
Files
.
copy
(
f
.
streamMove
,
Paths
.
get
(
sourceNameValidated
,
f
.
name
))
case
Failure
(
ex
)
=>
logString
+=
s
"'$now\nImpossible to convert '$fSourceName': ${ex.getMessage}\n\n"
Files
.
copy
(
f
.
toPath
,
Paths
.
get
(
errorSourcesName
,
f
.
getName
))
logString
+=
s
"$now\nImpossible to convert '${f.name}': ${ex.getMessage}\n\n"
Files
.
copy
(
f
.
streamMove
,
Paths
.
get
(
sourceNameError
,
f
.
name
))
}
}
}
...
...
@@ -115,18 +143,22 @@ object Main {
StandardOpenOption
.
CREATE
,
StandardOpenOption
.
TRUNCATE_EXISTING
)
if
(
s
uccessSources
.
listFiles
.
nonEmpty
)
Files
.
copy
(
Paths
.
get
(
args
(
0
),
logFileName
),
Paths
.
get
(
s
uccessSourcesName
,
logFileName
))
if
(
s
ourceValidated
.
listFiles
.
nonEmpty
)
Files
.
copy
(
Paths
.
get
(
args
(
0
),
logFileName
),
Paths
.
get
(
s
ourceNameValidated
,
logFileName
))
else
successSources
.
delete
sourceValidated
.
delete
if
(
sourceNotValidated
.
listFiles
.
isEmpty
)
sourceNotValidated
.
delete
if
(
sourceError
.
listFiles
.
isEmpty
)
sourceError
.
delete
if
(
outputValidated
.
listFiles
.
isEmpty
)
outputValidated
.
delete
if
(
outputNotValidated
.
listFiles
.
isEmpty
)
outputNotValidated
.
delete
if
(
errorSources
.
listFiles
.
isEmpty
)
errorSources
.
delete
}
match
{
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}"
)
}
}
else
println
(
s
"Error. Converting hasn`t been finished: unable to create
source moving directory
."
)
println
(
s
"Error. Converting hasn`t been finished: unable to create
moving directories
."
)
}
}
...
...
@@ -211,4 +243,12 @@ object Main {
(
outputXml
,
template
.
info
(
s
"$msgCheckSource\n$msgCheckOutput"
)
+
"\n\n"
)
}
case
class
FileEntry
(
name
:
String
,
modified
:
Long
,
streamConvert
:
Option
[
InputStream
],
streamMove
:
InputStream
,
msg
:
Option
[
String
]
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment