All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.etlunit.json.validator.ClasspathSchemaResolver Maven / Gradle / Ivy

There is a newer version: 1.6.9
Show newest version
package org.etlunit.json.validator;

import org.codehaus.jackson.JsonNode;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class ClasspathSchemaResolver extends CachingSchemaResolver {
	private final ClassLoader loader;

	public ClasspathSchemaResolver(Object context) throws JsonSchemaValidationException {
		loader = context.getClass().getClassLoader();
	}

	public static String resolveClasspath(String url, ClassLoader loader) {
		InputStream in = loader != null ? loader.getResourceAsStream(url) : Thread.currentThread().getContextClassLoader().getResourceAsStream(url);

		if (in != null) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			int offset = 0;
			char[] buffer = new char[8192];

			StringWriter writer = new StringWriter();

			try {
				while ((offset = reader.read(buffer)) != -1) {
					writer.write(buffer, 0, offset);
				}
			} catch (IOException e) {
				throw new ResourceException("Error reading classpath stream", e);
			}

			return writer.toString();
		} else {
			throw new ResourceNotFoundException("Could not locate schema uri: " + url);
		}
	}

	public static JsonSchema resolveByUri(String uri, ClassLoader loader) throws JsonSchemaValidationException {
		String schema = resolveClasspath(uri, loader);

		JsonSchema jschema = new JsonSchema(schema);

		return jschema;
	}

	@Override
	public JsonSchema resolveByUriSub(String uri) throws JsonSchemaValidationException
	{
		return resolveByUri(uri, loader);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy