<commit_before>
/*
 * Carrot2 project.
 *
 * Copyright (C) 2002-2006, <NAME>, <NAME>.
 * Portions (C) Contributors listed in "carrot2.CONTRIBUTORS" file.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */

package org.carrot2.webapp.serializers;

import java.io.*;
import java.util.*;

import org.carrot2.webapp.*;
import org.carrot2.webapp.SearchSettings.SearchRequest;
import org.dom4j.*;
import org.dom4j.io.XMLWriter;

import com.dawidweiss.carrot.util.net.URLEncoding;

/**
 * A serializer for main search page.
 * 
 * @author <NAME>
 */
public class XMLPageSerializer implements PageSerializer {
    private final DocumentFactory factory = DocumentFactory.getInstance();
    private final String skinBase;
    private final String contextPath;

    public XMLPageSerializer(String contextPath, String stylesheetsBase) {
        this.contextPath = contextPath;
        this.skinBase = stylesheetsBase;
    }

    public String getContentType() {
        return Constants.MIME_XML_CHARSET_UTF;
    }

    public void writePage(final OutputStream os, 
            final SearchSettings searchSettings,
            final SearchRequest searchRequest)
        throws IOException
    {
        final Writer writer = new OutputStreamWriter(os, Constants.ENCODING_UTF);

        // Assume a plain request type.
        final Document doc = factory.createDocument();
        final Element root = factory.createElement("page");

        // We add '@' to inform xslt processor that the stylesheet
        // is webapp-relative (not fs-root relative); this way we can avoid
        // loopback connections from the xslt parser to the webapp container
        doc.add(factory.createProcessingInstruction(
                "xml-stylesheet", "type=\"text/xsl\" href=\"@" + 
                skinBase + "/page.xsl\""));

        doc.add(factory.createProcessingInstruction("skin-uri", 
                contextPath + skinBase));

        doc.add(root);

        // Attach the meta information block.
        root.add(createMeta(factory, contextPath, searchSettings, searchRequest));

        // Output the result.
        final XMLWriter xmlwriter = new XMLWriter(writer);
        try {
            xmlwriter.write(doc);
        } finally {
            xmlwriter.flush();
        }
    }

    /**
     * Create the meta information block containing input search tabs,
     * algorithms and other info required to construct the final HTML page.   
     */
    private final Element createMeta(final DocumentFactory factory, 
            final String contextPath,
            final SearchSettings searchSettings,
            final SearchRequest searchRequest) throws UnsupportedEncodingException {
        final Element meta = factory.createElement("meta");

        // Emit action URLs
        final Element actionUrls = meta.addElement("action-urls");
        actionUrls.addElement("new-search").setText(contextPath + "search");
        final String uri = contextPath + "search?"
            + QueryProcessorServlet.PARAM_Q + "=" + URLEncoding.encode(searchRequest.query, "UTF-8")
            + "&" + QueryProcessorServlet.PARAM_INPUT + "=" + searchRequest.inputTabIndex
            + "&" + QueryProcessorServlet.PARAM_ALG + "=" + searchRequest.algorithmIndex
            + "&" + QueryProcessorServlet.PARAM_SIZE + "=" + searchRequest.inputSizeIndex;
        actionUrls.addElement("query-docs").setText(uri + "&type=d");
        actionUrls.addElement("query-clusters").setText(uri + "&type=c");

        // Emit interface strings, TODO: depending on the input locale?
        final Element strings = meta.addElement("strings");
        strings.addElement("search").setText("Search");

        // Emit input search tabs
        final Element tabs = meta.addElement("tabs");
        tabs.addAttribute("form-element", QueryProcessorServlet.PARAM_INPUT);
        final List inputTabs = searchSettings.getInputTabs();
        final int maxTab = inputTabs.size();
        for (int i = 0; i < maxTab; i++) {
            final TabSearchInput inputTab = (TabSearchInput) inputTabs.get(i);
            final Element tab = tabs.addElement("tab");
            tab.addAttribute("id", Integer.toString(i));
            if (searchRequest.inputTabIndex == i) {
                tab.addAttribute("selected", "selected");
            }
            tab.addElement("short").setText(inputTab.getShortName());
            tab.addElement("long").setText(inputTab.getLongDescription());
            
            for (Iterator j = inputTab.getOtherProperties().entrySet().iterator(); j.hasNext();) {<commit_msg>- application context path fixed<commit_after>
/*
 * Carrot2 project.
 *
 * Copyright (C) 2002-2006, <NAME>, <NAME>.
 * Portions (C) Contributors listed in "carrot2.CONTRIBUTORS" file.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */

package org.carrot2.webapp.serializers;

import java.io.*;
import java.util.*;

import org.carrot2.webapp.*;
import org.carrot2.webapp.SearchSettings.SearchRequest;
import org.dom4j.*;
import org.dom4j.io.XMLWriter;

import com.dawidweiss.carrot.util.net.URLEncoding;

/**
 * A serializer for main search page.
 * 
 * @author <NAME>
 */
public class XMLPageSerializer implements PageSerializer {
    private final DocumentFactory factory = DocumentFactory.getInstance();
    private final String skinBase;
    private final String contextPath;

    public XMLPageSerializer(String contextPath, String stylesheetsBase) {
        this.contextPath = contextPath;
        this.skinBase = stylesheetsBase;
    }

    public String getContentType() {
        return Constants.MIME_XML_CHARSET_UTF;
    }

    public void writePage(final OutputStream os, 
            final SearchSettings searchSettings,
            final SearchRequest searchRequest)
        throws IOException
    {
        final Writer writer = new OutputStreamWriter(os, Constants.ENCODING_UTF);

        // Assume a plain request type.
        final Document doc = factory.createDocument();
        final Element root = factory.createElement("page");

        // We add '@' to inform xslt processor that the stylesheet
        // is webapp-relative (not fs-root relative); this way we can avoid
        // loopback connections from the xslt parser to the webapp container
        doc.add(factory.createProcessingInstruction(
                "xml-stylesheet", "type=\"text/xsl\" href=\"@" + 
                skinBase + "/page.xsl\""));

        doc.add(factory.createProcessingInstruction("skin-uri", 
                contextPath + skinBase));

        doc.add(root);

        // Attach the meta information block.
        root.add(createMeta(factory, contextPath, searchSettings, searchRequest));

        // Output the result.
        final XMLWriter xmlwriter = new XMLWriter(writer);
        try {
            xmlwriter.write(doc);
        } finally {
            xmlwriter.flush();
        }
    }

    /**
     * Create the meta information block containing input search tabs,
     * algorithms and other info required to construct the final HTML page.   
     */
    private final Element createMeta(final DocumentFactory factory, 
            final String contextPath,
            final SearchSettings searchSettings,
            final SearchRequest searchRequest) throws UnsupportedEncodingException {
        final Element meta = factory.createElement("meta");

        // Emit action URLs
        final Element actionUrls = meta.addElement("action-urls");
        actionUrls.addElement("new-search").setText(contextPath + "/search");
        final String uri = contextPath + "/search?"
            + QueryProcessorServlet.PARAM_Q + "=" + URLEncoding.encode(searchRequest.query, "UTF-8")
            + "&" + QueryProcessorServlet.PARAM_INPUT + "=" + searchRequest.inputTabIndex
            + "&" + QueryProcessorServlet.PARAM_ALG + "=" + searchRequest.algorithmIndex
            + "&" + QueryProcessorServlet.PARAM_SIZE + "=" + searchRequest.inputSizeIndex;
        actionUrls.addElement("query-docs").setText(uri + "&type=d");
        actionUrls.addElement("query-clusters").setText(uri + "&type=c");

        // Emit interface strings, TODO: depending on the input locale?
        final Element strings = meta.addElement("strings");
        strings.addElement("search").setText("Search");

        // Emit input search tabs
        final Element tabs = meta.addElement("tabs");
        tabs.addAttribute("form-element", QueryProcessorServlet.PARAM_INPUT);
        final List inputTabs = searchSettings.getInputTabs();
        final int maxTab = inputTabs.size();
        for (int i = 0; i < maxTab; i++) {
            final TabSearchInput inputTab = (TabSearchInput) inputTabs.get(i);
            final Element tab = tabs.addElement("tab");
            tab.addAttribute("id", Integer.toString(i));
            if (searchRequest.inputTabIndex == i) {
                tab.addAttribute("selected", "selected");
            }
            tab.addElement("short").setText(inputTab.getShortName());
            tab.addElement("long").setText(inputTab.getLongDescription());
            
            for (Iterator j = inputTab.getOtherProperties().entrySet().iterator(); j.hasNext();) {
{'fraction_non_alphanumeric': 0.10879949049994693, 'fraction_numerical': 0.004245833775607685, 'mean_word_length': 2.753784860557769, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, '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': '7255151', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2431, 'n_words': 632}