|
| 1 | +/* |
| 2 | + * Copyright (c) 2013, Francis Galiegue <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Lesser GNU General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * Lesser GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.github.fge.jsonschema.processors.walk; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import com.github.fge.jsonschema.exceptions.ProcessingException; |
| 22 | +import com.github.fge.jsonschema.jsonpointer.JsonPointer; |
| 23 | +import com.github.fge.jsonschema.library.Dictionary; |
| 24 | +import com.github.fge.jsonschema.processing.Processor; |
| 25 | +import com.github.fge.jsonschema.processors.data.SchemaHolder; |
| 26 | +import com.github.fge.jsonschema.report.ProcessingMessage; |
| 27 | +import com.github.fge.jsonschema.report.ProcessingReport; |
| 28 | +import com.github.fge.jsonschema.tree.SchemaTree; |
| 29 | +import com.google.common.collect.Lists; |
| 30 | +import com.google.common.collect.Maps; |
| 31 | +import com.google.common.collect.Ordering; |
| 32 | +import com.google.common.collect.Sets; |
| 33 | + |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.Set; |
| 37 | + |
| 38 | +import static com.github.fge.jsonschema.messages.SyntaxMessages.*; |
| 39 | + |
| 40 | +/* |
| 41 | + * NOTE NOTE NOTE: the schema MUST be valid at this point |
| 42 | + */ |
| 43 | +public final class SchemaWalker |
| 44 | + implements Processor<SchemaHolder, SchemaHolder> |
| 45 | +{ |
| 46 | + private final Map<String, KeywordWalker> walkers; |
| 47 | + |
| 48 | + public SchemaWalker(final Dictionary<KeywordWalker> dict) |
| 49 | + { |
| 50 | + walkers = dict.entries(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public SchemaHolder process(final ProcessingReport report, |
| 55 | + final SchemaHolder input) |
| 56 | + throws ProcessingException |
| 57 | + { |
| 58 | + walk(report, input.getValue()); |
| 59 | + return input; |
| 60 | + } |
| 61 | + |
| 62 | + private void walk(final ProcessingReport report, final SchemaTree tree) |
| 63 | + throws ProcessingException |
| 64 | + { |
| 65 | + final JsonNode node = tree.getNode(); |
| 66 | + |
| 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()); |
| 79 | + |
| 80 | + if (!fieldNames.isEmpty()) |
| 81 | + report.warn(newMsg(tree).message(UNKNOWN_KEYWORDS) |
| 82 | + .put("ignored", Ordering.natural().sortedCopy(fieldNames))); |
| 83 | + |
| 84 | + /* |
| 85 | + * Now, walk each keyword, and collect pointers for further processing. |
| 86 | + */ |
| 87 | + final List<JsonPointer> pointers = Lists.newArrayList(); |
| 88 | + for (final KeywordWalker walker: map.values()) |
| 89 | + walker.walk(pointers, report, tree); |
| 90 | + |
| 91 | + /* |
| 92 | + * Operate on these pointers. |
| 93 | + */ |
| 94 | + for (final JsonPointer pointer: pointers) |
| 95 | + walk(report, tree.append(pointer)); |
| 96 | + } |
| 97 | + |
| 98 | + private static ProcessingMessage newMsg(final SchemaTree tree) |
| 99 | + { |
| 100 | + return new ProcessingMessage().put("schema", tree); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String toString() |
| 105 | + { |
| 106 | + return "schema waler"; |
| 107 | + } |
| 108 | +} |
0 commit comments