/* * Copyright (c) 2012, Francis Galiegue * * This program is free software: you can redistribute it and/or modify * it under the terms of the Lesser GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * Lesser GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package com.github.fge.jsonschema.examples; import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jsonschema.main.JsonSchema; import com.github.fge.jsonschema.main.JsonSchemaException; import com.github.fge.jsonschema.main.JsonSchemaFactory; import com.github.fge.jsonschema.ref.JsonRef; import com.github.fge.jsonschema.report.ValidationReport; import java.io.IOException; import static com.github.fge.jsonschema.main.JsonSchemaFactory.*; /** * Sixth example: URI redirection * *

link to source code

* *

In this example, the same schema file is used as in {@link Example1}. This * time, though, it is assumed that the base URI used for addressing this schema * is {@code http://my.site/schemas/fstab.json#}. But instead of trying to * fetch it from the web directly, we want to use the local copy, which is * located under URI {@code * resource:/org/eel/kitchen/jsonschema/examples/fstab.json#}.

* *

The solution is to use another method from {@link Builder}: {@link * Builder#addRedirection(String, String)}. This method can be called for as * many schemas as you wish to redirect.

* *

The effect is that if you required a schema via URI {@code * http://my.site/schemas/fstab.json#}, it will silently transform this URI into * {@code resource:/org/eel/kitchen/jsonschema/examples/fstab.json#} * internally.

* *

Note that URIs must be absolute JSON references (see {@link JsonRef}).

*/ public final class Example6 extends ExampleBase { private static final String FROM = "http://my.site/schemas/fstab.json#"; private static final String TO = "resource:/com/github/fge/jsonschema/examples/fstab.json#"; public static void main(final String... args) throws IOException, JsonSchemaException { final JsonNode good = loadResource("/fstab-good.json"); final JsonNode bad = loadResource("/fstab-bad.json"); final JsonNode bad2 = loadResource("/fstab-bad2.json"); final JsonSchemaFactory factory = JsonSchemaFactory.builder() .addRedirection(FROM, TO).build(); final JsonSchema schema = factory.fromURI(FROM); ValidationReport report; report = schema.validate(good); printReport(report); report = schema.validate(bad); printReport(report); report = schema.validate(bad2); printReport(report); } }