<commit_before>package se.l4.dust.core.internal.template.dom;

import java.io.IOException;
import java.util.HashMap;

import se.l4.dust.api.template.RenderingContext;
import se.l4.dust.api.template.dom.Comment;
import se.l4.dust.api.template.dom.Content;
import se.l4.dust.api.template.dom.DocType;
import se.l4.dust.api.template.dom.DynamicContent;
import se.l4.dust.api.template.dom.Element;
import se.l4.dust.api.template.dom.Element.Attribute;
import se.l4.dust.api.template.dom.ParsedTemplate;
import se.l4.dust.api.template.dom.Text;
import se.l4.dust.api.template.dom.WrappedElement;
import se.l4.dust.api.template.mixin.ElementEncounter;
import se.l4.dust.api.template.mixin.ElementWrapper;
import se.l4.dust.api.template.spi.TemplateOutputStream;
import se.l4.dust.core.internal.template.components.EmittableComponent;

/**
 * Emitter of templates. Takes a {@link ParsedTemplate} and processes it 
 * sending the results to {@link TemplateOutputStream}.
 * 
 * @author <NAME>
 *
 */
public class Emitter
	implements ElementEncounter
{
	private static final String[] attrsCacheEmpty = new String[10*2];
	
	private final ParsedTemplate template;
	private final RenderingContext ctx;

	private final String[] attrsCache;
	
	private Object current;
	private Integer currentId;
	private final HashMap<Integer, Object> dataMap;
	
	private Element currentComponent;
	private Integer currentComponentId;
	private final HashMap<Integer, Element> componentMap;
	private boolean skip;
	
	
	public Emitter(ParsedTemplate template, RenderingContext ctx, Object data)
	{
		this.template = template;
		this.ctx = ctx;
		
		dataMap = new HashMap<Integer, Object>(32);
		componentMap = new HashMap<Integer, Element>(32);
		
		current = data;
		currentId = template.getRawId();
		dataMap.put(currentId, current);
		
		componentMap.put(currentId, template.getRoot());
		attrsCache = new String[10*2];
	}
	
	public void process(TemplateOutputStream out)
		throws IOException
	{
		DocType docType = template.getDocType();
		if(docType != null)
		{
			// Emit the document type
			out.docType(docType.getName(), docType.getPublicId(), docType.getSystemId());
		}
		
		emit(out, template.getRoot());
	}

	private String[] createAttributes(Element element, Object data)
	{
		Attribute[] rawAttrs = element.getAttributes();
		String[] attrs;
		if(rawAttrs.length > 10)
		{
			attrs = new String[rawAttrs.length * 2];
		}
		else
		{
			attrs = attrsCache;
			attrs[rawAttrs.length * 2] = null;<commit_msg>Fixed Emitter for more than 10 attributes<commit_after>package se.l4.dust.core.internal.template.dom;

import java.io.IOException;
import java.util.HashMap;

import se.l4.dust.api.template.RenderingContext;
import se.l4.dust.api.template.dom.Comment;
import se.l4.dust.api.template.dom.Content;
import se.l4.dust.api.template.dom.DocType;
import se.l4.dust.api.template.dom.DynamicContent;
import se.l4.dust.api.template.dom.Element;
import se.l4.dust.api.template.dom.Element.Attribute;
import se.l4.dust.api.template.dom.ParsedTemplate;
import se.l4.dust.api.template.dom.Text;
import se.l4.dust.api.template.dom.WrappedElement;
import se.l4.dust.api.template.mixin.ElementEncounter;
import se.l4.dust.api.template.mixin.ElementWrapper;
import se.l4.dust.api.template.spi.TemplateOutputStream;
import se.l4.dust.core.internal.template.components.EmittableComponent;

/**
 * Emitter of templates. Takes a {@link ParsedTemplate} and processes it 
 * sending the results to {@link TemplateOutputStream}.
 * 
 * @author <NAME>
 *
 */
public class Emitter
	implements ElementEncounter
{
	private static final String[] attrsCacheEmpty = new String[10*2];
	
	private final ParsedTemplate template;
	private final RenderingContext ctx;

	private final String[] attrsCache;
	
	private Object current;
	private Integer currentId;
	private final HashMap<Integer, Object> dataMap;
	
	private Element currentComponent;
	private Integer currentComponentId;
	private final HashMap<Integer, Element> componentMap;
	private boolean skip;
	
	
	public Emitter(ParsedTemplate template, RenderingContext ctx, Object data)
	{
		this.template = template;
		this.ctx = ctx;
		
		dataMap = new HashMap<Integer, Object>(32);
		componentMap = new HashMap<Integer, Element>(32);
		
		current = data;
		currentId = template.getRawId();
		dataMap.put(currentId, current);
		
		componentMap.put(currentId, template.getRoot());
		attrsCache = new String[10*2];
	}
	
	public void process(TemplateOutputStream out)
		throws IOException
	{
		DocType docType = template.getDocType();
		if(docType != null)
		{
			// Emit the document type
			out.docType(docType.getName(), docType.getPublicId(), docType.getSystemId());
		}
		
		emit(out, template.getRoot());
	}

	private String[] createAttributes(Element element, Object data)
	{
		Attribute[] rawAttrs = element.getAttributes();
		String[] attrs;
		if(rawAttrs.length >= 10)
		{
			attrs = new String[rawAttrs.length * 2];
		}
		else
		{
			attrs = attrsCache;
			attrs[rawAttrs.length * 2] = null;
{'fraction_non_alphanumeric': 0.11568431568431568, 'fraction_numerical': 0.011988011988011988, 'mean_word_length': 4.618406285072952, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '831680', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1576, 'n_words': 353}