<commit_before>/*
 * Copyright 2005-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.javacrumbs.smock.common.groovy;

import static net.javacrumbs.smock.common.XmlUtil.loadDocument;
import static net.javacrumbs.smock.common.XmlUtil.serialize;
import groovy.text.SimpleTemplateEngine;
import groovy.text.TemplateEngine;
import groovy.util.XmlSlurper;

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

import javax.xml.transform.Source;

import net.javacrumbs.smock.common.TemplateProcessor;

import org.springframework.xml.transform.StringSource;
import org.w3c.dom.Document;

public class GroovyTemplateProcessor implements TemplateProcessor {
	private final TemplateEngine templateEngine;
	
	public GroovyTemplateProcessor(TemplateEngine templateEngine) {
		this.templateEngine = templateEngine;
	}

	public GroovyTemplateProcessor() {
		this(new SimpleTemplateEngine());
	}

	@SuppressWarnings("unchecked")
	public Document processTemplate(Document template, Source input, Map<String, Object> parameters) {
		try {
			String templateText = serialize(template);
			HashMap binding = new HashMap(parameters);
			if (input!=null)
			{
				Document inputDocument = loadDocument(input);
				binding.put(inputDocument.getFirstChild().getNodeName(), new XmlSlurper().parse(new StringReader(serialize(inputDocument))));
			}
			return loadDocument(new StringSource(templateEngine.createTemplate(templateText).make(binding).toString()));
		} catch (Exception e) {
			throw new IllegalArgumentException("Can not process Groovy template.",e);
		}
	}


}<commit_msg>Groovy code changed<commit_after>/*
 * Copyright 2005-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.javacrumbs.smock.common.groovy;

import static net.javacrumbs.smock.common.XmlUtil.loadDocument;
import static net.javacrumbs.smock.common.XmlUtil.serialize;
import groovy.text.SimpleTemplateEngine;
import groovy.text.TemplateEngine;
import groovy.util.XmlSlurper;

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

import javax.xml.transform.Source;

import net.javacrumbs.smock.common.TemplateProcessor;

import org.springframework.xml.transform.StringSource;
import org.w3c.dom.Document;

/**
 * Groovy based template engine.
 * @author <NAME>
 */
public class GroovyTemplateProcessor implements TemplateProcessor {
	private final TemplateEngine templateEngine;
	
	public GroovyTemplateProcessor(TemplateEngine templateEngine) {
		this.templateEngine = templateEngine;
	}

	public GroovyTemplateProcessor() {
		this(new SimpleTemplateEngine());
	}

	public Document processTemplate(Document template, Source input, Map<String, Object> parameters) {
		try {
			String templateText = serialize(template);
			HashMap<String, Object>  binding = new HashMap<String, Object> (parameters);
			if (input!=null)
			{
				Document inputDocument = loadDocument(input);
				binding.put(inputDocument.getFirstChild().getNodeName(), new XmlSlurper().parse(new StringReader(serialize(inputDocument))));
			}
			return loadDocument(new StringSource(templateEngine.createTemplate(templateText).make(binding).toString()));
		} catch (Exception e) {
			throw new IllegalArgumentException("Can not process Groovy template.",e);
		}
	}


}
{'fraction_non_alphanumeric': 0.09301260022909508, 'fraction_numerical': 0.005956471935853379, 'mean_word_length': 5.106293706293706, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '5017022', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1173, 'n_words': 354}