47
47
public class JsonSchemaFactory {
48
48
private static final Logger logger = LoggerFactory
49
49
.getLogger (JsonSchemaFactory .class );
50
+ // This is just a constant for listening to all Keywords.
51
+ public static final String ALL_KEYWORD_WALK_LISTENER_KEY = "com.networknt.AllKeywordWalkListener" ;
50
52
51
53
52
54
public static class Builder {
@@ -57,7 +59,7 @@ public static class Builder {
57
59
private URNFactory urnFactory ;
58
60
private final Map <String , JsonMetaSchema > jsonMetaSchemas = new HashMap <String , JsonMetaSchema >();
59
61
private final Map <String , String > uriMap = new HashMap <String , String >();
60
- private final List <KeywordWalkListener > jsonKeywordWalkListeners = new ArrayList < KeywordWalkListener >();
62
+ private final Map < String , List <KeywordWalkListener >> keywordWalkListenersMap = new HashMap < String , List < KeywordWalkListener > >();
61
63
62
64
public Builder () {
63
65
// Adds support for creating {@link URL}s.
@@ -141,15 +143,42 @@ public Builder addUrnFactory(URNFactory urnFactory) {
141
143
return this ;
142
144
}
143
145
144
- public Builder addKeywordWalkListener (KeywordWalkListener jsonKeywordWalkListener ) {
145
- this .jsonKeywordWalkListeners .add (jsonKeywordWalkListener );
146
- return this ;
147
- }
146
+ public Builder addKeywordWalkListener (KeywordWalkListener keywordWalkListener ) {
147
+ if (keywordWalkListenersMap .get (ALL_KEYWORD_WALK_LISTENER_KEY ) == null ) {
148
+ List <KeywordWalkListener > keywordWalkListeners = new ArrayList <KeywordWalkListener >();
149
+ keywordWalkListenersMap .put (ALL_KEYWORD_WALK_LISTENER_KEY , keywordWalkListeners );
150
+ }
151
+ keywordWalkListenersMap .get (ALL_KEYWORD_WALK_LISTENER_KEY ).add (keywordWalkListener );
152
+ return this ;
153
+ }
154
+
155
+ public Builder addKeywordWalkListener (String keyword , KeywordWalkListener keywordWalkListener ) {
156
+ if (keywordWalkListenersMap .get (keyword ) == null ) {
157
+ List <KeywordWalkListener > keywordWalkListeners = new ArrayList <KeywordWalkListener >();
158
+ keywordWalkListenersMap .put (keyword , keywordWalkListeners );
159
+ }
160
+ keywordWalkListenersMap .get (keyword ).add (keywordWalkListener );
161
+ return this ;
162
+ }
148
163
149
- public Builder addKeywordWalkListeners (Collection <KeywordWalkListener > jsonKeywordWalkListener ) {
150
- this .jsonKeywordWalkListeners .addAll (jsonKeywordWalkListener );
151
- return this ;
152
- }
164
+
165
+ public Builder addKeywordWalkListeners (List <KeywordWalkListener > keywordWalkListeners ) {
166
+ if (keywordWalkListenersMap .get (ALL_KEYWORD_WALK_LISTENER_KEY ) == null ) {
167
+ List <KeywordWalkListener > ikeywordWalkListeners = new ArrayList <KeywordWalkListener >();
168
+ keywordWalkListenersMap .put (ALL_KEYWORD_WALK_LISTENER_KEY , ikeywordWalkListeners );
169
+ }
170
+ keywordWalkListenersMap .get (ALL_KEYWORD_WALK_LISTENER_KEY ).addAll (keywordWalkListeners );
171
+ return this ;
172
+ }
173
+
174
+ public Builder addKeywordWalkListeners (String keyword , List <KeywordWalkListener > keywordWalkListeners ) {
175
+ if (keywordWalkListenersMap .get (keyword ) == null ) {
176
+ List <KeywordWalkListener > ikeywordWalkListeners = new ArrayList <KeywordWalkListener >();
177
+ keywordWalkListenersMap .put (keyword , ikeywordWalkListeners );
178
+ }
179
+ keywordWalkListenersMap .get (keyword ).addAll (keywordWalkListeners );
180
+ return this ;
181
+ }
153
182
154
183
public JsonSchemaFactory build () {
155
184
// create builtin keywords with (custom) formats.
@@ -161,7 +190,7 @@ public JsonSchemaFactory build() {
161
190
urnFactory ,
162
191
jsonMetaSchemas ,
163
192
uriMap ,
164
- jsonKeywordWalkListeners
193
+ keywordWalkListenersMap
165
194
);
166
195
}
167
196
}
@@ -174,7 +203,7 @@ public JsonSchemaFactory build() {
174
203
private final Map <String , JsonMetaSchema > jsonMetaSchemas ;
175
204
private final Map <String , String > uriMap ;
176
205
private final ConcurrentMap <URI , JsonSchema > uriSchemaCache = new ConcurrentHashMap <URI , JsonSchema >();
177
- private List <KeywordWalkListener > jsonKeywordWalkListeners = new ArrayList < KeywordWalkListener >() ;
206
+ private final Map < String , List <KeywordWalkListener >> keywordWalkListenersMap ;
178
207
179
208
180
209
private JsonSchemaFactory (
@@ -185,7 +214,7 @@ private JsonSchemaFactory(
185
214
final URNFactory urnFactory ,
186
215
final Map <String , JsonMetaSchema > jsonMetaSchemas ,
187
216
final Map <String , String > uriMap ,
188
- final List <KeywordWalkListener > jsonKeywordWalkListeners ) {
217
+ final Map < String , List <KeywordWalkListener >> keywordWalkListenersMap ) {
189
218
if (mapper == null ) {
190
219
throw new IllegalArgumentException ("ObjectMapper must not be null" );
191
220
} else if (defaultMetaSchemaURI == null || defaultMetaSchemaURI .trim ().isEmpty ()) {
@@ -208,7 +237,7 @@ private JsonSchemaFactory(
208
237
this .urnFactory = urnFactory ;
209
238
this .jsonMetaSchemas = jsonMetaSchemas ;
210
239
this .uriMap = uriMap ;
211
- this .jsonKeywordWalkListeners = jsonKeywordWalkListeners ;
240
+ this .keywordWalkListenersMap = keywordWalkListenersMap ;
212
241
}
213
242
214
243
/**
@@ -282,7 +311,7 @@ protected JsonSchema newJsonSchema(final URI schemaUri, final JsonNode schemaNod
282
311
283
312
protected ValidationContext createValidationContext (final JsonNode schemaNode ) {
284
313
final JsonMetaSchema jsonMetaSchema = findMetaSchemaForSchema (schemaNode );
285
- return new ValidationContext (this .uriFactory , this .urnFactory , jsonMetaSchema , this , null , this .jsonKeywordWalkListeners );
314
+ return new ValidationContext (this .uriFactory , this .urnFactory , jsonMetaSchema , this , null , this .keywordWalkListenersMap );
286
315
}
287
316
288
317
private JsonMetaSchema findMetaSchemaForSchema (final JsonNode schemaNode ) {
@@ -354,7 +383,7 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co
354
383
355
384
JsonSchema jsonSchema ;
356
385
if (idMatchesSourceUri (jsonMetaSchema , schemaNode , schemaUri )) {
357
- jsonSchema = new JsonSchema (new ValidationContext (this .uriFactory , this .urnFactory , jsonMetaSchema , this , config , this .jsonKeywordWalkListeners ), mappedUri , schemaNode , true /*retrieved via id, resolving will not change anything*/ );
386
+ jsonSchema = new JsonSchema (new ValidationContext (this .uriFactory , this .urnFactory , jsonMetaSchema , this , config , this .keywordWalkListenersMap ), mappedUri , schemaNode , true /*retrieved via id, resolving will not change anything*/ );
358
387
} else {
359
388
final ValidationContext validationContext = createValidationContext (schemaNode );
360
389
validationContext .setConfig (config );
0 commit comments