Skip to content

Commit 0a6fd79

Browse files
author
Rakesh Kumar
committed
Update cli tool to allow schema directory
- This updates allow cli tool to accept directory which contains multiple jsonschema file. It recursively look into files and validate their syntax Example command: java -jar json-schema-validator-2.2.6-lib.jar --syntax --directory <DIRECTORY-PATH> issue: java-json-tools#216
1 parent 5aa7c81 commit 0a6fd79

File tree

1 file changed

+52
-4
lines changed
  • src/main/java/com/github/fge/jsonschema/main/cli

1 file changed

+52
-4
lines changed

src/main/java/com/github/fge/jsonschema/main/cli/Main.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@
3838

3939
import java.io.File;
4040
import java.io.IOException;
41+
import java.util.ArrayList;
4142
import java.util.Arrays;
43+
import java.util.LinkedList;
4244
import java.util.List;
45+
import java.util.Queue;
4346

4447
import static com.github.fge.jsonschema.main.cli.RetCode.*;
4548

4649
public final class Main
4750
{
51+
private static final String DIRECTORY = "directory";
52+
4853
private static final HelpFormatter HELP = new CustomHelpFormatter();
4954

5055
private static final ObjectMapper MAPPER = JacksonUtils.newMapper();
@@ -63,17 +68,21 @@ public static void main(final String... args)
6368
"no output; exit with the relevant return code (see below)");
6469
parser.accepts("syntax",
6570
"check the syntax of schema(s) given as argument(s)");
71+
parser.accepts(DIRECTORY,
72+
"directory which contains the schema files. If this parameter is provided then you don't need to provide files in argument list")
73+
.withRequiredArg();
6674
parser.accepts("fakeroot",
6775
"pretend that the current directory is absolute URI \"uri\"")
6876
.withRequiredArg();
6977
parser.formatHelpWith(HELP);
7078

7179
final OptionSet optionSet;
7280
final boolean isSyntax;
73-
final int requiredArgs;
81+
int requiredArgs;
7482

7583
Reporter reporter = Reporters.DEFAULT;
7684
String fakeRoot = null;
85+
String schemaDirectoryPath = null;
7786

7887
try {
7988
optionSet = parser.parse(args);
@@ -100,22 +109,43 @@ public static void main(final String... args)
100109
if (optionSet.has("fakeroot"))
101110
fakeRoot = (String) optionSet.valueOf("fakeroot");
102111

112+
if (optionSet.has(DIRECTORY))
113+
schemaDirectoryPath = (String) optionSet.valueOf(DIRECTORY);
114+
103115
isSyntax = optionSet.has("syntax");
104116
requiredArgs = isSyntax ? 1 : 2;
105117

118+
// If directory argument is present then there is no other argument expected
119+
requiredArgs = optionSet.has(DIRECTORY) ? 0 : requiredArgs;
120+
106121
@SuppressWarnings("unchecked")
107122
final List<String> arguments
108123
= (List<String>) optionSet.nonOptionArguments();
109-
124+
110125
if (arguments.size() < requiredArgs) {
111126
System.err.println("missing arguments");
112127
parser.printHelpOn(System.err);
113128
System.exit(CMD_ERROR.get());
114129
}
115130

116131
final List<File> files = Lists.newArrayList();
117-
for (final String target: arguments)
118-
files.add(new File(target).getCanonicalFile());
132+
133+
if (schemaDirectoryPath != null) {
134+
135+
File directory = new File(schemaDirectoryPath);
136+
if(!directory.isDirectory()) {
137+
System.err.println(String.format("Given directory path(%s) is not a directory",
138+
schemaDirectoryPath));
139+
parser.printHelpOn(System.err);
140+
System.exit(CMD_ERROR.get());
141+
} else {
142+
files.addAll(getFilesFromGivenDirectory(directory));
143+
}
144+
145+
} else {
146+
for (final String target: arguments)
147+
files.add(new File(target).getCanonicalFile());
148+
}
119149

120150
if (optionSet.has("brief"))
121151
reporter = Reporters.BRIEF;
@@ -143,6 +173,24 @@ else if (optionSet.has("quiet")) {
143173
syntaxValidator = factory.getSyntaxValidator();
144174
}
145175

176+
private static List<File> getFilesFromGivenDirectory(File directory)
177+
throws IOException {
178+
List<File> files = new ArrayList<File>();
179+
Queue<File> queue = new LinkedList<File>();
180+
queue.offer(directory);
181+
while(!queue.isEmpty()) {
182+
File tempDirectory = queue.poll();
183+
for(File entry : tempDirectory.listFiles()) {
184+
if(entry.isFile()) {
185+
files.add(entry.getCanonicalFile());
186+
} else {
187+
queue.offer(entry);
188+
}
189+
}
190+
}
191+
return files;
192+
}
193+
146194
private void proceed(final Reporter reporter, final List<File> files,
147195
final boolean isSyntax)
148196
throws IOException, ProcessingException

0 commit comments

Comments
 (0)