|
22 | 22 | import com.fasterxml.jackson.databind.JsonNode;
|
23 | 23 | import com.github.fge.jsonschema.core.exceptions.ProcessingException;
|
24 | 24 | import com.github.fge.jsonschema.core.processing.ProcessingResult;
|
25 |
| -import com.github.fge.jsonschema.core.processing.Processor; |
26 |
| -import com.github.fge.jsonschema.core.report.ListProcessingReport; |
27 |
| -import com.github.fge.jsonschema.core.report.MessageProvider; |
28 | 25 | import com.github.fge.jsonschema.core.report.ProcessingReport;
|
29 | 26 | import com.github.fge.jsonschema.core.report.ReportProvider;
|
30 | 27 | import com.github.fge.jsonschema.core.tree.SchemaTree;
|
31 | 28 | import com.github.fge.jsonschema.core.tree.SimpleJsonTree;
|
32 | 29 | import com.github.fge.jsonschema.processors.data.FullData;
|
33 | 30 | import com.github.fge.jsonschema.processors.validation.ValidationProcessor;
|
34 |
| - |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.Iterator; |
| 34 | +import java.util.List; |
35 | 35 | import javax.annotation.concurrent.Immutable;
|
36 | 36 |
|
37 | 37 | /**
|
@@ -147,4 +147,126 @@ public boolean validInstanceUnchecked(final JsonNode instance)
|
147 | 147 | {
|
148 | 148 | return doValidateUnchecked(instance, false).isSuccess();
|
149 | 149 | }
|
| 150 | + |
| 151 | + /** |
| 152 | + * {@inheritDoc} |
| 153 | + */ |
| 154 | + @Override |
| 155 | + public Iterator<String> getPropertyNames() { |
| 156 | + return getProperties().fieldNames(); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * {@inheritDoc} |
| 161 | + */ |
| 162 | + @Override |
| 163 | + public List<String> getPropertyEnum(final String name) { |
| 164 | + final JsonNode node = getProperty(name); |
| 165 | + if (node != null) { |
| 166 | + return getElementsAsText(node.get("enum")); |
| 167 | + } |
| 168 | + return Collections.emptyList(); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * {@inheritDoc} |
| 173 | + */ |
| 174 | + @Override |
| 175 | + public String getPropertyType(final String name) { |
| 176 | + return getPropertyElementAsText(name, "type"); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * {@inheritDoc} |
| 181 | + */ |
| 182 | + @Override |
| 183 | + public String getPropertyDescription(final String name) { |
| 184 | + return getPropertyElementAsText(name, "description"); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * {@inheritDoc} |
| 189 | + */ |
| 190 | + @Override |
| 191 | + public boolean isRequired(final String name) { |
| 192 | + final JsonNode requiredNode = schema.getNode().findValue("required"); |
| 193 | + if (requiredNode != null) { |
| 194 | + final Iterator<JsonNode> it = requiredNode.elements(); |
| 195 | + while (it.hasNext()) { |
| 196 | + if (name.equals(it.next().asText())) { |
| 197 | + return true; |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + /* |
| 205 | + /********************************************************** |
| 206 | + /* Internal methods |
| 207 | + /********************************************************** |
| 208 | + */ |
| 209 | + |
| 210 | + /** |
| 211 | + * Method to retrieve all JSON Schema attributes. |
| 212 | + * |
| 213 | + * @return Node of the attributes |
| 214 | + */ |
| 215 | + private JsonNode getProperties() { |
| 216 | + return schema.getNode().findValue("properties"); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Method to finding a JSON Schema attribute with specified name and returning the node. |
| 221 | + * If no matching attribute is found, returns null. |
| 222 | + * |
| 223 | + * @param name Name of attribute to look for |
| 224 | + * |
| 225 | + * @return Node of the attribute, if any; null if none |
| 226 | + */ |
| 227 | + private JsonNode getProperty(final String name) { |
| 228 | + return getProperties().get(name); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Method to retrieve a JSON Schema property element as text. |
| 233 | + * If no matching attribute is found, returns null. |
| 234 | + * |
| 235 | + * @param name Name of property to look for |
| 236 | + * @param element Name of the element of the property |
| 237 | + * |
| 238 | + * @return a JSON Schema property element as text; null if it is not exist |
| 239 | + */ |
| 240 | + |
| 241 | + private String getPropertyElementAsText(final String name, final String element) { |
| 242 | + final JsonNode node = getProperty(name); |
| 243 | + if (node == null) { |
| 244 | + return null; |
| 245 | + } |
| 246 | + final JsonNode nodeElement = node.get(element); |
| 247 | + if (nodeElement == null) { |
| 248 | + return null; |
| 249 | + } |
| 250 | + return nodeElement.asText(); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Method to retrieve a JsonNode elements as text. |
| 255 | + * |
| 256 | + * @param node Node to look for |
| 257 | + * |
| 258 | + * @return List of the elements of the node |
| 259 | + */ |
| 260 | + |
| 261 | + private List<String> getElementsAsText(final JsonNode node) { |
| 262 | + if (node == null) { |
| 263 | + return Collections.emptyList(); |
| 264 | + } |
| 265 | + final List<String> nodeNames = new ArrayList<String>(); |
| 266 | + final Iterator<JsonNode> it = node.elements(); |
| 267 | + while (it.hasNext()) { |
| 268 | + nodeNames.add(it.next().asText()); |
| 269 | + } |
| 270 | + return nodeNames; |
| 271 | + } |
150 | 272 | }
|
0 commit comments