Skip to content

Commit cb26405

Browse files
committed
Refactor SchemaLoader
1 parent bd1ded0 commit cb26405

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+543
-515
lines changed

src/main/java/com/networknt/schema/SchemaRegistry.java

Lines changed: 111 additions & 104 deletions
Large diffs are not rendered by default.

src/main/java/com/networknt/schema/resource/AllowSchemaLoader.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/main/java/com/networknt/schema/resource/ClasspathSchemaLoader.java renamed to src/main/java/com/networknt/schema/resource/ClasspathResourceLoader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@
2424
/**
2525
* Loads from classpath.
2626
*/
27-
public class ClasspathSchemaLoader implements SchemaLoader {
27+
public class ClasspathResourceLoader implements ResourceLoader {
2828
private final Supplier<ClassLoader> classLoaderSource;
2929

3030
/**
3131
* Constructor.
3232
*/
33-
public ClasspathSchemaLoader() {
34-
this(ClasspathSchemaLoader::getClassLoader);
33+
public ClasspathResourceLoader() {
34+
this(ClasspathResourceLoader::getClassLoader);
3535
}
3636

3737
/**
3838
* Constructor.
3939
*
4040
* @param classLoaderSource the class loader source
4141
*/
42-
public ClasspathSchemaLoader(Supplier<ClassLoader> classLoaderSource) {
42+
public ClasspathResourceLoader(Supplier<ClassLoader> classLoaderSource) {
4343
this.classLoaderSource = classLoaderSource;
4444
}
4545

4646
@Override
47-
public InputStreamSource getSchema(AbsoluteIri absoluteIri) {
47+
public InputStreamSource getResource(AbsoluteIri absoluteIri) {
4848
String iri = absoluteIri != null ? absoluteIri.toString() : "";
4949
String name = null;
5050
if (iri.startsWith("classpath:")) {
@@ -75,7 +75,7 @@ public InputStreamSource getSchema(AbsoluteIri absoluteIri) {
7575
protected static ClassLoader getClassLoader() {
7676
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
7777
if (classLoader == null) {
78-
classLoader = SchemaLoader.class.getClassLoader();
78+
classLoader = ResourceLoader.class.getClassLoader();
7979
}
8080
return classLoader;
8181
}

src/main/java/com/networknt/schema/resource/DefaultSchemaLoader.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/main/java/com/networknt/schema/resource/DisallowSchemaLoader.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/main/java/com/networknt/schema/resource/UriSchemaLoader.java renamed to src/main/java/com/networknt/schema/resource/IriResourceLoader.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@
2727
import com.networknt.schema.utils.AbsoluteIris;
2828

2929
/**
30-
* Loads from uri.
30+
* Loads from iri.
3131
*/
32-
public class UriSchemaLoader implements SchemaLoader {
32+
public class IriResourceLoader implements ResourceLoader {
33+
private static class Holder {
34+
private static final IriResourceLoader INSTANCE = new IriResourceLoader();
35+
}
36+
37+
public static IriResourceLoader getInstance() {
38+
return Holder.INSTANCE;
39+
}
40+
3341
@Override
34-
public InputStreamSource getSchema(AbsoluteIri absoluteIri) {
42+
public InputStreamSource getResource(AbsoluteIri absoluteIri) {
3543
URI uri = toURI(absoluteIri);
3644
URL url = toURL(uri);
3745
return () -> {
@@ -97,7 +105,7 @@ protected InputStream openConnectionCheckRedirects(URLConnection c) throws IOExc
97105
// and should be limited to 5 redirections at most.
98106
if (target == null || !(target.getProtocol().equals("http") || target.getProtocol().equals("https"))
99107
|| redirects >= 5) {
100-
throw new SecurityException("illegal URL redirect");
108+
throw new SecurityException("Maximum number of redirects exceeded");
101109
}
102110
redir = true;
103111
c = target.openConnection();

src/main/java/com/networknt/schema/resource/MapSchemaLoader.java renamed to src/main/java/com/networknt/schema/resource/MapResourceLoader.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,42 @@
88
import com.networknt.schema.AbsoluteIri;
99

1010
/**
11-
* Map implementation of {@link SchemaLoader}.
11+
* Map implementation of {@link ResourceLoader}.
1212
*/
13-
public class MapSchemaLoader implements SchemaLoader {
13+
public class MapResourceLoader implements ResourceLoader {
1414
private final Function<String, String> mappings;
1515

1616
/**
17-
* Sets the schema data by absolute IRI.
17+
* Sets the resource data by absolute IRI.
1818
*
1919
* @param mappings the mappings
2020
*/
21-
public MapSchemaLoader(Map<String, String> mappings) {
21+
public MapResourceLoader(Map<String, String> mappings) {
2222
this(mappings::get);
2323
}
2424

2525
/**
26-
* Sets the schema data by absolute IRI function.
26+
* Sets the resource data by absolute IRI function.
2727
*
2828
* @param mappings the mappings
2929
*/
30-
public MapSchemaLoader(Function<String, String> mappings) {
30+
public MapResourceLoader(Function<String, String> mappings) {
3131
this.mappings = mappings;
3232
}
3333

3434
/**
35-
* Sets the schema data by using two mapping functions.
35+
* Sets the resource data by using two mapping functions.
3636
* <p>
3737
* Firstly to map the IRI to an object. If the object is null no mapping is
3838
* performed.
3939
* <p>
40-
* Next to map the object to the schema data.
40+
* Next to map the object to the resource data.
4141
*
4242
* @param <T> the type of the object
4343
* @param mapIriToObject the mapping of IRI to object
44-
* @param mapObjectToData the mappingof object to schema data
44+
* @param mapObjectToData the mappingof object to resource data
4545
*/
46-
public <T> MapSchemaLoader(Function<String, T> mapIriToObject, Function<T, String> mapObjectToData) {
46+
public <T> MapResourceLoader(Function<String, T> mapIriToObject, Function<T, String> mapObjectToData) {
4747
this.mappings = iri -> {
4848
T result = mapIriToObject.apply(iri);
4949
if (result != null) {
@@ -54,7 +54,7 @@ public <T> MapSchemaLoader(Function<String, T> mapIriToObject, Function<T, Strin
5454
}
5555

5656
@Override
57-
public InputStreamSource getSchema(AbsoluteIri absoluteIri) {
57+
public InputStreamSource getResource(AbsoluteIri absoluteIri) {
5858
try {
5959
String result = mappings.apply(absoluteIri.toString());
6060
if (result != null) {

src/main/java/com/networknt/schema/resource/MapSchemaMapper.java renamed to src/main/java/com/networknt/schema/resource/MapSchemaIdResolver.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
import com.networknt.schema.AbsoluteIri;
88

99
/**
10-
* Map implementation of {@link SchemaMapper}.
10+
* Map implementation of {@link SchemaIdResolver}.
1111
*/
12-
public class MapSchemaMapper implements SchemaMapper {
12+
public class MapSchemaIdResolver implements SchemaIdResolver {
1313
private final Function<String, String> mappings;
1414

15-
public MapSchemaMapper(Map<String, String> mappings) {
15+
public MapSchemaIdResolver(Map<String, String> mappings) {
1616
this(mappings::get);
1717
}
1818

19-
public MapSchemaMapper(Function<String, String> mappings) {
19+
public MapSchemaIdResolver(Function<String, String> mappings) {
2020
this.mappings = mappings;
2121
}
2222

@@ -26,7 +26,7 @@ public MapSchemaMapper(Function<String, String> mappings) {
2626
* @param test the predicate
2727
* @param mappings the mapping
2828
*/
29-
public MapSchemaMapper(Predicate<String> test, Function<String, String> mappings) {
29+
public MapSchemaIdResolver(Predicate<String> test, Function<String, String> mappings) {
3030
this.mappings = iri -> {
3131
if (test.test(iri)) {
3232
return mappings.apply(iri);
@@ -36,7 +36,7 @@ public MapSchemaMapper(Predicate<String> test, Function<String, String> mappings
3636
}
3737

3838
@Override
39-
public AbsoluteIri map(AbsoluteIri absoluteIRI) {
39+
public AbsoluteIri resolve(AbsoluteIri absoluteIRI) {
4040
String mapped = this.mappings.apply(absoluteIRI.toString());
4141
if (mapped != null) {
4242
return AbsoluteIri.of(mapped);

src/main/java/com/networknt/schema/resource/MetaSchemaMapper.java renamed to src/main/java/com/networknt/schema/resource/MetaSchemaIdResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
/**
2121
* Maps the JSON Schema meta schema to the class path location.
2222
*/
23-
public class MetaSchemaMapper implements SchemaMapper {
23+
public class MetaSchemaIdResolver implements SchemaIdResolver {
2424
private static final char ANCHOR = '#';
2525
private static final String CLASSPATH_PREFIX = "classpath:";
2626
private static final String HTTP_JSON_SCHEMA_ORG_PREFIX = "http://json-schema.org/";
2727
private static final String HTTPS_JSON_SCHEMA_ORG_PREFIX = "https://json-schema.org/";
2828

2929
@Override
30-
public AbsoluteIri map(AbsoluteIri absoluteIRI) {
30+
public AbsoluteIri resolve(AbsoluteIri absoluteIRI) {
3131
String absoluteIRIString = absoluteIRI != null ? absoluteIRI.toString() : null;
3232
if (absoluteIRIString != null) {
3333
if (absoluteIRIString.startsWith(HTTPS_JSON_SCHEMA_ORG_PREFIX)) {

src/main/java/com/networknt/schema/resource/PrefixSchemaMapper.java renamed to src/main/java/com/networknt/schema/resource/PrefixSchemaIdResolver.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
import com.networknt.schema.AbsoluteIri;
44

55
/**
6-
* Prefix implementation of {@link SchemaMapper}.
6+
* Prefix implementation of {@link SchemaIdResolver}.
77
*/
8-
public class PrefixSchemaMapper implements SchemaMapper {
8+
public class PrefixSchemaIdResolver implements SchemaIdResolver {
99
private final String source;
1010
private final String replacement;
1111

12-
public PrefixSchemaMapper(String source, String replacement) {
12+
public PrefixSchemaIdResolver(String source, String replacement) {
1313
this.source = source;
1414
this.replacement = replacement;
1515
}
1616

1717
@Override
18-
public AbsoluteIri map(AbsoluteIri absoluteIRI) {
18+
public AbsoluteIri resolve(AbsoluteIri absoluteIRI) {
1919
String absoluteIRIString = absoluteIRI != null ? absoluteIRI.toString() : null;
2020
if (absoluteIRIString != null && absoluteIRIString.startsWith(source)) {
2121
return AbsoluteIri.of(replacement + absoluteIRIString.substring(source.length()));

0 commit comments

Comments
 (0)