<commit_before>package se.l4.dust.jaxrs.internal.template;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import se.l4.dust.api.Context;
import se.l4.dust.api.template.Component;
import se.l4.dust.api.template.RenderingContext;
import se.l4.dust.api.template.Template;
import se.l4.dust.api.template.TemplateCache;
import se.l4.dust.api.template.TemplateOutputStream;
import se.l4.dust.api.template.TemplateRenderer;
import se.l4.dust.api.template.dom.ParsedTemplate;
import se.l4.dust.core.template.html.HtmlTemplateOutput;
import se.l4.dust.servlet.WebRenderingContext;

import com.google.inject.Inject;

/**
 * {@link MessageBodyWriter} that renders the templates.
 * 
 * @author <NAME>
 *
 */
@Provider
@Produces({ "text/html; charset=utf-8", MediaType.TEXT_XML })
public class TemplateWriter
	implements MessageBodyWriter<Object>
{
	private final TemplateRenderer renderer;
	private final TemplateCache cache;
	private final com.google.inject.Provider<Context> ctx;
	private final com.google.inject.Provider<HttpServletRequest> requests;

	@Inject
	public TemplateWriter(TemplateRenderer renderer, TemplateCache cache,
			com.google.inject.Provider<Context> ctx,
			com.google.inject.Provider<HttpServletRequest> requests)
	{
		this.renderer = renderer;
		this.cache = cache;
		this.ctx = ctx;
		this.requests = requests;
	}
	
	@Override
	public long getSize(Object t, Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType)
	{
		return -1;
	}

	@Override
	public boolean isWriteable(Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType)
	{
		for(Annotation a : annotations)
		{
			if(a instanceof Template)
			{
				return true;
			}
		}
		
		while(type != Object.class)
		{
			if(type.getAnnotation(Template.class) != null || type.getAnnotation(Component.class) != null)
			{
				return true;
			}
			
			type = type.getSuperclass();
		}
		
		return false;
	}

	@Override
	public void writeTo(
			Object t, 
			Class<?> type, 
			Type genericType,
			Annotation[] annotations, 
			MediaType mediaType,
			MultivaluedMap<String, Object> httpHeaders,
			OutputStream entityStream) 
		throws IOException, WebApplicationException<commit_msg>Avoid looping and checking for @Template<commit_after>package se.l4.dust.jaxrs.internal.template;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import se.l4.dust.api.Context;
import se.l4.dust.api.template.Component;
import se.l4.dust.api.template.RenderingContext;
import se.l4.dust.api.template.Template;
import se.l4.dust.api.template.TemplateCache;
import se.l4.dust.api.template.TemplateOutputStream;
import se.l4.dust.api.template.TemplateRenderer;
import se.l4.dust.api.template.dom.ParsedTemplate;
import se.l4.dust.core.template.html.HtmlTemplateOutput;
import se.l4.dust.servlet.WebRenderingContext;

import com.google.inject.Inject;

/**
 * {@link MessageBodyWriter} that renders the templates.
 * 
 * @author <NAME>
 *
 */
@Provider
@Produces({ MediaType.TEXT_HTML })
public class TemplateWriter
	implements MessageBodyWriter<Object>
{
	private final TemplateRenderer renderer;
	private final TemplateCache cache;
	private final com.google.inject.Provider<Context> ctx;
	private final com.google.inject.Provider<HttpServletRequest> requests;

	@Inject
	public TemplateWriter(TemplateRenderer renderer, TemplateCache cache,
			com.google.inject.Provider<Context> ctx,
			com.google.inject.Provider<HttpServletRequest> requests)
	{
		this.renderer = renderer;
		this.cache = cache;
		this.ctx = ctx;
		this.requests = requests;
	}
	
	@Override
	public long getSize(Object t, Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType)
	{
		return -1;
	}

	@Override
	public boolean isWriteable(Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType)
	{
		while(type != Object.class)
		{
			if(type.getAnnotation(Template.class) != null || type.getAnnotation(Component.class) != null)
			{
				return true;
			}
			
			type = type.getSuperclass();
		}
		
		return false;
	}

	@Override
	public void writeTo(
			Object t, 
			Class<?> type, 
			Type genericType,
			Annotation[] annotations, 
			MediaType mediaType,
			MultivaluedMap<String, Object> httpHeaders,
			OutputStream entityStream) 
		throws IOException, WebApplicationException
{'fraction_non_alphanumeric': 0.11068931068931069, 'fraction_numerical': 0.004995004995004995, 'mean_word_length': 4.695108077360637, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2656780', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1567, 'n_words': 326}