<commit_before>/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2008-2012 <NAME> / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.render;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;

import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.facet.Facet;
import com.jaeksoft.searchlib.facet.FacetField;
import com.jaeksoft.searchlib.facet.FacetItem;
import com.jaeksoft.searchlib.facet.FacetList;
import com.jaeksoft.searchlib.function.expression.SyntaxError;
import com.jaeksoft.searchlib.query.ParseException;
import com.jaeksoft.searchlib.request.SearchRequest;
import com.jaeksoft.searchlib.result.AbstractResultSearch;
import com.jaeksoft.searchlib.result.ResultDocument;
import com.jaeksoft.searchlib.schema.Field;
import com.jaeksoft.searchlib.schema.FieldValueItem;
import com.jaeksoft.searchlib.snippet.SnippetField;

public class RenderSearchXml extends
		AbstractRenderXml<SearchRequest, AbstractResultSearch> {

	private PrintWriter writer;
	private AbstractResultSearch result;
	private SearchRequest searchRequest;

	public RenderSearchXml(AbstractResultSearch result) {
		super(result);
	}

	private void renderPrefix() throws ParseException, SyntaxError,
			SearchLibException, IOException {
		writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		writer.println("<response>");
		writer.println("<header>");
		writer.println("\t<status>0</status>");
		writer.print("\t<query>");
		writer.print(StringEscapeUtils.escapeXml(searchRequest.getQueryParsed()));
		writer.println("</query>");
		writer.println("</header>");
	}

	private void renderSuffix() {
		writer.println("</response>");
	}

	private void renderDocuments() throws IOException, ParseException,
			SyntaxError {
		SearchRequest searchRequest = result.getRequest();
		int start = searchRequest.getStart();
		int end = result.getDocumentCount() + searchRequest.getStart();
		writer.print("<result name=\"response\" numFound=\"");
		writer.print(result.getNumFound());
		writer.print("\" collapsedDocCount=\"");
		writer.print(result.getCollapseDocCount());
		writer.print("\" start=\"");
		writer.print(searchRequest.getStart());
		writer.print("\" rows=\"");
		writer.print(searchRequest.getRows());
		writer.print("\" maxScore=\"");
		writer.print(result.getMaxScore());
		writer.print("\" time=\"");
		writer.print(searchRequest.getFinalTime());
		writer.println("\">");
		for (int i = start; i < end; i++)
			this.renderDocument(i);
		writer.println("</result>");
	}

	private void renderDocument(int pos) throws IOException, ParseException,
			SyntaxError {
		writer.print("\t<doc score=\"");
		writer.print(result.getScore(pos));
		writer.print("\" pos=\"");
		writer.print(pos);
		writer.println("\">");
		ResultDocument doc = result.getDocument(pos);
		for (Field field : searchRequest.getReturnFieldList())
			renderField(doc, field);
		for (SnippetField field : searchRequest.getSnippetFieldList())
			renderSnippetValue(doc, field);

		int cc = result.getCollapseCount(pos);
		if (cc > 0) {
			writer.print("\t\t<collapseCount>");
			writer.print(cc);
			writer.println("</collapseCount>");
		}
		writer.println("\t</doc>");
	}

	private void renderField(ResultDocument doc, Field field)
			throws IOException {
		String fieldName = field.getName();
		List<FieldValueItem> values = doc.getValueList(field);
		if (values == null)
			return;
		for (FieldValueItem v : values) {
			writer.print("\t\t<field name=\"");
			writer.print(fieldName);
			writer.print('"');
			Float b = v.getBoost();
			if (b != null) {
				writer.print(" boost=\"");
				writer.print(b);
				writer.print('"');<commit_msg>SpellCheck request refactory<commit_after>/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2008-2012 <NAME> / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.render;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;

import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.facet.Facet;
import com.jaeksoft.searchlib.facet.FacetField;
import com.jaeksoft.searchlib.facet.FacetItem;
import com.jaeksoft.searchlib.facet.FacetList;
import com.jaeksoft.searchlib.function.expression.SyntaxError;
import com.jaeksoft.searchlib.query.ParseException;
import com.jaeksoft.searchlib.request.SearchRequest;
import com.jaeksoft.searchlib.result.AbstractResultSearch;
import com.jaeksoft.searchlib.result.ResultDocument;
import com.jaeksoft.searchlib.schema.Field;
import com.jaeksoft.searchlib.schema.FieldValueItem;
import com.jaeksoft.searchlib.snippet.SnippetField;

public class RenderSearchXml extends
		AbstractRenderXml<SearchRequest, AbstractResultSearch> {

	private PrintWriter writer;

	public RenderSearchXml(AbstractResultSearch result) {
		super(result);
	}

	private void renderPrefix() throws ParseException, SyntaxError,
			SearchLibException, IOException {
		writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		writer.println("<response>");
		writer.println("<header>");
		writer.println("\t<status>0</status>");
		writer.print("\t<query>");
		writer.print(StringEscapeUtils.escapeXml(request.getQueryParsed()));
		writer.println("</query>");
		writer.println("</header>");
	}

	private void renderSuffix() {
		writer.println("</response>");
	}

	private void renderDocuments() throws IOException, ParseException,
			SyntaxError {
		SearchRequest searchRequest = result.getRequest();
		int start = searchRequest.getStart();
		int end = result.getDocumentCount() + searchRequest.getStart();
		writer.print("<result name=\"response\" numFound=\"");
		writer.print(result.getNumFound());
		writer.print("\" collapsedDocCount=\"");
		writer.print(result.getCollapseDocCount());
		writer.print("\" start=\"");
		writer.print(searchRequest.getStart());
		writer.print("\" rows=\"");
		writer.print(searchRequest.getRows());
		writer.print("\" maxScore=\"");
		writer.print(result.getMaxScore());
		writer.print("\" time=\"");
		writer.print(searchRequest.getFinalTime());
		writer.println("\">");
		for (int i = start; i < end; i++)
			this.renderDocument(i);
		writer.println("</result>");
	}

	private void renderDocument(int pos) throws IOException, ParseException,
			SyntaxError {
		writer.print("\t<doc score=\"");
		writer.print(result.getScore(pos));
		writer.print("\" pos=\"");
		writer.print(pos);
		writer.println("\">");
		ResultDocument doc = result.getDocument(pos);
		for (Field field : request.getReturnFieldList())
			renderField(doc, field);
		for (SnippetField field : request.getSnippetFieldList())
			renderSnippetValue(doc, field);

		int cc = result.getCollapseCount(pos);
		if (cc > 0) {
			writer.print("\t\t<collapseCount>");
			writer.print(cc);
			writer.println("</collapseCount>");
		}
		writer.println("\t</doc>");
	}

	private void renderField(ResultDocument doc, Field field)
			throws IOException {
		String fieldName = field.getName();
		List<FieldValueItem> values = doc.getValueList(field);
		if (values == null)
			return;
		for (FieldValueItem v : values) {
			writer.print("\t\t<field name=\"");
			writer.print(fieldName);
			writer.print('"');
			Float b = v.getBoost();
			if (b != null) {
				writer.print(" boost=\"");
				writer.print(b);
				writer.print('"');
{'fraction_non_alphanumeric': 0.14219718309859156, 'fraction_numerical': 0.0031549295774647886, 'mean_word_length': 4.820327868852459, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 2, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, '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': '2401847', 'n_tokens_mistral': 2860, 'n_tokens_neox': 2648, 'n_words': 619}