38
38
39
39
import java .io .File ;
40
40
import java .io .IOException ;
41
+ import java .util .ArrayList ;
41
42
import java .util .Arrays ;
43
+ import java .util .LinkedList ;
42
44
import java .util .List ;
45
+ import java .util .Queue ;
43
46
44
47
import static com .github .fge .jsonschema .main .cli .RetCode .*;
45
48
46
49
public final class Main
47
50
{
51
+ private static final String DIRECTORY = "directory" ;
52
+
48
53
private static final HelpFormatter HELP = new CustomHelpFormatter ();
49
54
50
55
private static final ObjectMapper MAPPER = JacksonUtils .newMapper ();
@@ -63,17 +68,21 @@ public static void main(final String... args)
63
68
"no output; exit with the relevant return code (see below)" );
64
69
parser .accepts ("syntax" ,
65
70
"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 ();
66
74
parser .accepts ("fakeroot" ,
67
75
"pretend that the current directory is absolute URI \" uri\" " )
68
76
.withRequiredArg ();
69
77
parser .formatHelpWith (HELP );
70
78
71
79
final OptionSet optionSet ;
72
80
final boolean isSyntax ;
73
- final int requiredArgs ;
81
+ int requiredArgs ;
74
82
75
83
Reporter reporter = Reporters .DEFAULT ;
76
84
String fakeRoot = null ;
85
+ String schemaDirectoryPath = null ;
77
86
78
87
try {
79
88
optionSet = parser .parse (args );
@@ -100,22 +109,43 @@ public static void main(final String... args)
100
109
if (optionSet .has ("fakeroot" ))
101
110
fakeRoot = (String ) optionSet .valueOf ("fakeroot" );
102
111
112
+ if (optionSet .has (DIRECTORY ))
113
+ schemaDirectoryPath = (String ) optionSet .valueOf (DIRECTORY );
114
+
103
115
isSyntax = optionSet .has ("syntax" );
104
116
requiredArgs = isSyntax ? 1 : 2 ;
105
117
118
+ // If directory argument is present then there is no other argument expected
119
+ requiredArgs = optionSet .has (DIRECTORY ) ? 0 : requiredArgs ;
120
+
106
121
@ SuppressWarnings ("unchecked" )
107
122
final List <String > arguments
108
123
= (List <String >) optionSet .nonOptionArguments ();
109
-
124
+
110
125
if (arguments .size () < requiredArgs ) {
111
126
System .err .println ("missing arguments" );
112
127
parser .printHelpOn (System .err );
113
128
System .exit (CMD_ERROR .get ());
114
129
}
115
130
116
131
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
+ }
119
149
120
150
if (optionSet .has ("brief" ))
121
151
reporter = Reporters .BRIEF ;
@@ -143,6 +173,24 @@ else if (optionSet.has("quiet")) {
143
173
syntaxValidator = factory .getSyntaxValidator ();
144
174
}
145
175
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
+
146
194
private void proceed (final Reporter reporter , final List <File > files ,
147
195
final boolean isSyntax )
148
196
throws IOException , ProcessingException
0 commit comments