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
37e468dc
Commit
37e468dc
authored
Nov 27, 2018
by
王品堯
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
使用slf4j, 簡化程式因翻譯檔不會有array的值故移除
parent
4e077b96
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
24 deletions
+19
-24
pom.xml
pom.xml
+7
-0
src/main/java/JsonFileCompare.java
src/main/java/JsonFileCompare.java
+12
-24
No files found.
pom.xml
View file @
37e468dc
...
...
@@ -19,6 +19,13 @@
<artifactId>
jackson-databind
</artifactId>
<version>
2.9.7
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-simple
</artifactId>
<version>
1.7.25
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/JsonFileCompare.java
View file @
37e468dc
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
...
...
@@ -11,10 +12,9 @@ import java.util.stream.Collectors;
import
java.util.stream.IntStream
;
public
class
JsonFileCompare
{
private
static
Logger
log
=
LoggerFactory
.
getLogger
(
JsonFileCompare
.
class
);
public
static
void
main
(
String
[]
args
)
{
try
{
ObjectMapper
objectMapper
=
new
ObjectMapper
();
Path
originPath
=
Paths
.
get
(
args
[
0
]);
...
...
@@ -25,12 +25,12 @@ public class JsonFileCompare {
IntStream
.
range
(
1
,
args
.
length
).
forEach
(
i
->
findMissingKeys
(
Paths
.
get
(
args
[
i
]),
originSet
));
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
args
[
0
]
+
" is not exists or not json files."
);
log
.
error
(
"{} is not exists or not json files."
,
args
[
0
],
e
);
System
.
exit
(
1
);
}
}
p
ublic
static
void
addKeys
(
String
currentPath
,
JsonNode
jsonNode
,
Set
set
,
List
<
Integer
>
suffix
)
{
p
rivate
static
void
addKeys
(
String
currentPath
,
JsonNode
jsonNode
,
Set
<
String
>
set
,
List
<
Integer
>
suffix
)
{
if
(
jsonNode
.
isObject
())
{
ObjectNode
objectNode
=
(
ObjectNode
)
jsonNode
;
Iterator
<
Map
.
Entry
<
String
,
JsonNode
>>
iter
=
objectNode
.
fields
();
...
...
@@ -40,29 +40,19 @@ public class JsonFileCompare {
Map
.
Entry
<
String
,
JsonNode
>
entry
=
iter
.
next
();
addKeys
(
pathPrefix
+
entry
.
getKey
(),
entry
.
getValue
(),
set
,
suffix
);
}
}
else
if
(
jsonNode
.
isArray
())
{
ArrayNode
arrayNode
=
(
ArrayNode
)
jsonNode
;
for
(
int
i
=
0
;
i
<
arrayNode
.
size
();
i
++)
{
suffix
.
add
(
i
+
1
);
addKeys
(
currentPath
,
arrayNode
.
get
(
i
),
set
,
suffix
);
if
(
i
+
1
<
arrayNode
.
size
())
{
suffix
.
remove
(
arrayNode
.
size
()
-
1
);
}
}
}
else
if
(
jsonNode
.
isValueNode
())
{
StringBuilder
sb
=
new
StringBuilder
(
currentPath
);
if
(
currentPath
.
contains
(
"-"
))
{
for
(
int
i
=
0
;
i
<
suffix
.
size
();
i
++
)
{
currentPath
+=
"-"
+
suffix
.
get
(
i
);
for
(
Integer
suffix1
:
suffix
)
{
sb
.
append
(
"-"
).
append
(
suffix1
);
}
}
set
.
add
(
currentPath
);
set
.
add
(
sb
.
toString
()
);
}
}
p
ublic
static
void
findMissingKeys
(
Path
targetPath
,
Set
<
String
>
originSet
)
{
p
rivate
static
void
findMissingKeys
(
Path
targetPath
,
Set
<
String
>
originSet
)
{
if
(!
targetPath
.
toFile
().
exists
())
return
;
try
{
Set
<
String
>
targetSet
=
new
HashSet
<>();
...
...
@@ -76,13 +66,11 @@ public class JsonFileCompare {
.
filter
(
key
->
!
targetSet
.
contains
(
key
))
.
collect
(
Collectors
.
toSet
());
if
(!
missingKey
.
isEmpty
())
{
System
.
err
.
println
(
"Missing Key on "
+
targetPath
.
toString
());
System
.
err
.
println
(
"Missing Keys: "
);
missingKey
.
stream
().
sorted
().
forEach
(
System
.
err
::
println
);
log
.
error
(
"Missing Key on {} \n Missing Keys: {}"
,
targetPath
,
missingKey
.
stream
().
sorted
().
collect
(
Collectors
.
toList
()));
System
.
exit
(
1
);
}
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
targetPath
.
toString
()
+
" is not json files."
);
log
.
error
(
"{} is not json files."
,
targetPath
,
e
);
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