Skip to content

Commit 1df4952

Browse files
committed
Change SchemaWalker
Issue #41 continued. While not as generic as it could be (it will probably get more generic than that later), the need here is to process the current schema before going any further. Make SchemaWalker abstract. Require implementations to implement one method, processCurrent(ProcessingReport, SchemaTree), so that whatever is needed to be "done to the schema" is done _before_ walking further. Signed-off-by: Francis Galiegue <[email protected]>
1 parent 0993c91 commit 1df4952

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed

src/main/java/com/github/fge/jsonschema/processors/walk/KeywordWalker.java renamed to src/main/java/com/github/fge/jsonschema/processors/walk/PointerCollector.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717

1818
package com.github.fge.jsonschema.processors.walk;
1919

20-
import com.github.fge.jsonschema.exceptions.ProcessingException;
2120
import com.github.fge.jsonschema.jsonpointer.JsonPointer;
22-
import com.github.fge.jsonschema.report.ProcessingReport;
2321
import com.github.fge.jsonschema.tree.SchemaTree;
2422

2523
import java.util.Collection;
2624

27-
public interface KeywordWalker
25+
public interface PointerCollector
2826
{
29-
void walk(final Collection<JsonPointer> pointers,
30-
final ProcessingReport report, final SchemaTree tree)
31-
throws ProcessingException;
27+
void collect(final Collection<JsonPointer> pointers, final SchemaTree tree);
3228
}

src/main/java/com/github/fge/jsonschema/processors/walk/SchemaWalker.java

+16-33
Original file line numberDiff line numberDiff line change
@@ -23,70 +23,58 @@
2323
import com.github.fge.jsonschema.library.Dictionary;
2424
import com.github.fge.jsonschema.processing.Processor;
2525
import com.github.fge.jsonschema.processors.data.SchemaHolder;
26-
import com.github.fge.jsonschema.report.ProcessingMessage;
2726
import com.github.fge.jsonschema.report.ProcessingReport;
2827
import com.github.fge.jsonschema.tree.SchemaTree;
2928
import com.google.common.collect.Lists;
3029
import com.google.common.collect.Maps;
31-
import com.google.common.collect.Ordering;
3230
import com.google.common.collect.Sets;
3331

3432
import java.util.List;
3533
import java.util.Map;
36-
import java.util.Set;
37-
38-
import static com.github.fge.jsonschema.messages.SyntaxMessages.*;
3934

4035
/*
4136
* NOTE NOTE NOTE: the schema MUST be valid at this point
4237
*/
43-
public final class SchemaWalker
38+
public abstract class SchemaWalker
4439
implements Processor<SchemaHolder, SchemaHolder>
4540
{
46-
private final Map<String, KeywordWalker> walkers;
41+
private final Map<String, PointerCollector> collectors;
4742

48-
public SchemaWalker(final Dictionary<KeywordWalker> dict)
43+
protected SchemaWalker(final Dictionary<PointerCollector> dict)
4944
{
50-
walkers = dict.entries();
45+
collectors = dict.entries();
5146
}
5247

5348
@Override
54-
public SchemaHolder process(final ProcessingReport report,
49+
public final SchemaHolder process(final ProcessingReport report,
5550
final SchemaHolder input)
5651
throws ProcessingException
5752
{
5853
walk(report, input.getValue());
5954
return input;
6055
}
6156

57+
public abstract void processCurrent(final ProcessingReport report,
58+
final SchemaTree tree)
59+
throws ProcessingException;
60+
6261
private void walk(final ProcessingReport report, final SchemaTree tree)
6362
throws ProcessingException
6463
{
64+
processCurrent(report, tree);
6565
final JsonNode node = tree.getNode();
6666

67-
/*
68-
* Grab all walkers and object member names. Retain walkers only for
69-
* registered keywords, and remove from the member names set what is in
70-
* the walkers' key set: if non empty, some walkers are missing (on
71-
* purpose or not), report them.
72-
*/
73-
final Map<String, KeywordWalker> map = Maps.newTreeMap();
74-
map.putAll(walkers);
75-
76-
final Set<String> fieldNames = Sets.newHashSet(node.fieldNames());
77-
map.keySet().retainAll(fieldNames);
78-
fieldNames.removeAll(map.keySet());
67+
final Map<String, PointerCollector> map = Maps.newTreeMap();
68+
map.putAll(collectors);
7969

80-
if (!fieldNames.isEmpty())
81-
report.warn(newMsg(tree).message(UNKNOWN_KEYWORDS)
82-
.put("ignored", Ordering.natural().sortedCopy(fieldNames)));
70+
map.keySet().retainAll(Sets.newHashSet(node.fieldNames()));
8371

8472
/*
85-
* Now, walk each keyword, and collect pointers for further processing.
73+
* Collect pointers for further processing.
8674
*/
8775
final List<JsonPointer> pointers = Lists.newArrayList();
88-
for (final KeywordWalker walker: map.values())
89-
walker.walk(pointers, report, tree);
76+
for (final PointerCollector collector: map.values())
77+
collector.collect(pointers, tree);
9078

9179
/*
9280
* Operate on these pointers.
@@ -95,11 +83,6 @@ private void walk(final ProcessingReport report, final SchemaTree tree)
9583
walk(report, tree.append(pointer));
9684
}
9785

98-
private static ProcessingMessage newMsg(final SchemaTree tree)
99-
{
100-
return new ProcessingMessage().put("schema", tree);
101-
}
102-
10386
@Override
10487
public String toString()
10588
{

0 commit comments

Comments
 (0)