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

org.summerb.stringtemplate.api.validation.StringTemplateValidationUtils Maven / Gradle / Ivy

There is a newer version: 6.4.0
Show newest version
package org.summerb.stringtemplate.api.validation;

import org.summerb.stringtemplate.api.StringTemplateCompiler;
import org.summerb.validation.ValidationContext;

import com.google.common.base.Preconditions;

/**
 * Simple utility class which provides validation function(s) for string
 * templates
 * 
 * @author skarpushin
 * 
 */
public class StringTemplateValidationUtils {

	/**
	 * Validate string template basically using compiler. If compile successful,
	 * that template validates fine
	 * 
	 * @param stringTemplateCompiler
	 *            compiler to validate against
	 * @param stringTemplate
	 *            template to validate
	 * @param ctx
	 *            validation context
	 * @param fieldToken
	 *            field token
	 */
	public static void validateStringTemplate(StringTemplateCompiler stringTemplateCompiler, String stringTemplate,
			ValidationContext ctx, String fieldToken) {

		Preconditions.checkArgument(stringTemplateCompiler != null);
		Preconditions.checkArgument(ctx != null);

		try {
			stringTemplateCompiler.compile(stringTemplate);
		} catch (Throwable t) {
			ctx.getErrors().add(new StringTemplateValidationError(fieldToken, t));
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy