Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
LanguageFileCompare
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
測試專案
LanguageFileCompare
Commits
0b869c30
Commit
0b869c30
authored
Nov 28, 2018
by
王品堯
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加處理js檔案類型
parent
6f073c97
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
27 deletions
+47
-27
src/main/java/LanguageFileCompare.java
src/main/java/LanguageFileCompare.java
+47
-27
No files found.
src/main/java/LanguageFileCompare.java
View file @
0b869c30
...
...
@@ -4,7 +4,12 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
javax.script.Bindings
;
import
javax.script.ScriptContext
;
import
javax.script.ScriptEngine
;
import
javax.script.ScriptEngineManager
;
import
java.io.FileInputStream
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
...
...
@@ -17,22 +22,40 @@ public class LanguageFileCompare {
private
static
Logger
log
=
LoggerFactory
.
getLogger
(
LanguageFileCompare
.
class
);
public
static
void
main
(
String
[]
args
)
{
try
{
ObjectMapper
objectMapper
=
new
ObjectMapper
();
if
(
args
.
length
>
0
)
{
Path
originPath
=
Paths
.
get
(
args
[
0
]);
JsonNode
twJson
=
objectMapper
.
readTree
(
new
String
(
Files
.
readAllBytes
(
originPath
)));
if
(
args
.
length
>
0
)
{
Path
originPath
=
Paths
.
get
(
args
[
0
]);
JsonNode
twJson
=
getJsonNode
(
originPath
);
if
(
twJson
!=
null
)
{
Set
<
String
>
originSet
=
new
HashSet
<>();
addKeys
(
""
,
twJson
,
originSet
,
new
ArrayList
<>());
IntStream
.
range
(
1
,
args
.
length
)
.
forEach
(
i
->
findMissingKeys
(
Paths
.
get
(
args
[
i
]),
originSet
));
}
}
checkProperties
();
}
private
static
JsonNode
getJsonNode
(
Path
path
)
{
String
ext
=
path
.
getFileName
().
toString
();
try
{
ObjectMapper
objectMapper
=
new
ObjectMapper
();
if
(
ext
.
endsWith
(
".json"
))
{
/* CHM、HRB */
return
objectMapper
.
readTree
(
new
String
(
Files
.
readAllBytes
(
path
)));
}
else
if
(
ext
.
endsWith
(
".js"
))
{
/* CHMB use js file to translate */
ScriptEngineManager
scriptEngineManager
=
new
ScriptEngineManager
();
ScriptEngine
engine
=
scriptEngineManager
.
getEngineByName
(
"Nashorn"
);
engine
.
eval
(
new
FileReader
(
path
.
toFile
()));
Bindings
bindings
=
engine
.
getBindings
(
ScriptContext
.
ENGINE_SCOPE
);
return
objectMapper
.
valueToTree
(
bindings
.
get
(
"local"
));
}
}
catch
(
Exception
e
)
{
log
.
error
(
"{} is not exists or
not json files."
,
args
[
0
]
,
e
);
log
.
error
(
"{} is not exists or
file not correct."
,
path
.
getFileName
()
,
e
);
System
.
exit
(
1
);
}
checkProperties
()
;
return
null
;
}
private
static
void
addKeys
(
...
...
@@ -60,26 +83,23 @@ public class LanguageFileCompare {
private
static
void
findMissingKeys
(
Path
targetPath
,
Set
<
String
>
originSet
)
{
if
(!
targetPath
.
toFile
().
exists
())
return
;
try
{
Set
<
String
>
targetSet
=
new
HashSet
<>();
ObjectMapper
objectMapper
=
new
ObjectMapper
();
JsonNode
json
=
objectMapper
.
readTree
(
new
String
(
Files
.
readAllBytes
(
targetPath
)));
addKeys
(
""
,
json
,
targetSet
,
new
ArrayList
<>());
Set
<
String
>
missingKey
=
originSet
.
parallelStream
()
.
filter
(
key
->
!
targetSet
.
contains
(
key
))
.
collect
(
Collectors
.
toSet
());
if
(!
missingKey
.
isEmpty
())
{
log
.
error
(
"Missing Key on {} \nMissing Keys: {}"
,
targetPath
.
getFileName
(),
missingKey
.
stream
().
sorted
().
collect
(
Collectors
.
toList
()));
System
.
exit
(
1
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"{} is not json files."
,
targetPath
,
e
);
Set
<
String
>
targetSet
=
new
HashSet
<>();
JsonNode
json
=
getJsonNode
(
targetPath
);
if
(
json
==
null
)
return
;
addKeys
(
""
,
json
,
targetSet
,
new
ArrayList
<>());
Set
<
String
>
missingKey
=
originSet
.
parallelStream
()
.
filter
(
key
->
!
targetSet
.
contains
(
key
))
.
collect
(
Collectors
.
toSet
());
if
(!
missingKey
.
isEmpty
())
{
log
.
error
(
"Missing Key on {} \nMissing Keys: {}"
,
targetPath
.
getFileName
(),
missingKey
.
stream
().
sorted
().
collect
(
Collectors
.
toList
()));
System
.
exit
(
1
);
}
}
...
...
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