<commit_before>/*
 * Copyright 2012-2020 <NAME>
 *
 * 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 org.devacfr.maven.skins.reflow.snippet;

import java.util.List;
import java.util.Map;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;
import org.devacfr.maven.skins.reflow.snippet.ComponentToken.Tag;
import org.devacfr.maven.skins.reflow.snippet.ComponentToken.Type;
import org.devacfr.maven.skins.reflow.snippet.internal.ContainsOwnText;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Collector;
import org.jsoup.select.Elements;
import org.jsoup.select.Evaluator;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * Resolve the type and tag type of component.
 *
 * @author <NAME>
 * @version 2.4
 */
public class ComponentResolver {

    /** **/
    private static final Pattern RESOLVER_PATTERN = Pattern.compile(
        "\\{\\{(<|%) (\\/?)([a-zA-Z\\-_]*)(\\/?)(?:\\s+([^>%]*))(>|%)\\}\\}",
        Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);

    private static final Pattern ATTRIBUTE_PATTERN = Pattern.compile("\\s?(\\w*)=\"(\\w*)\"\\s?",
        Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);

    /**
     * Default constructor
     */
    public ComponentResolver() {
        super();

    }

    /**
     * Collects all (start,end,empty) Element corresponding to a snippet component.
     *
     * @param document
     *            the Jsoup document to use
     * @return Return a {@link Elements} representing all web components contained in Jsoup document.
     */
    public Elements collect(final Document document) {
        final Evaluator evaluator = new ContainsOwnText(RESOLVER_PATTERN, -1);
        return Collector.collect(evaluator, document);
    }

    public Document normalize(final Document document) {
        final Elements elements = collect(document);

        elements.forEach((element) -> {
            String text = StringEscapeUtils.unescapeHtml(element.html());
            final Matcher matcher = RESOLVER_PATTERN.matcher(text);

            final List<MatchResult> results = Lists.newArrayList();

            while (matcher.find()) {
                results.add(0, matcher.toMatchResult());
            }
            if (results.size() > 1) {
                MatchResult matchResult = null;
                for (int i = 0; i < results.size(); i++) {
                    matchResult = results.get(i);
                    final String snippet = text.substring(matchResult.start(), matchResult.end());
                    text = text.substring(0, matchResult.start()) + "<span>" + StringEscapeUtils.escapeHtml(snippet)
                            + "</span>" + text.substring(matchResult.end());
                }
                element.html(text);

            }
        });
        return document;
    }

    /**
     * Create a {@link ComponentToken} corresponding to the element.
     *
     * @param element
     *            the element to use.
     * @return Return a new instance of {@link ComponentToken} representing the element.<commit_msg>Snippet Improvement and documentation.<commit_after>/*
 * Copyright 2012-2020 <NAME>
 *
 * 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 org.devacfr.maven.skins.reflow.snippet;

import java.util.List;
import java.util.Map;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;
import org.devacfr.maven.skins.reflow.snippet.ComponentToken.Tag;
import org.devacfr.maven.skins.reflow.snippet.ComponentToken.Type;
import org.devacfr.maven.skins.reflow.snippet.internal.ContainsOwnText;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Collector;
import org.jsoup.select.Elements;
import org.jsoup.select.Evaluator;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * Resolve the type and tag type of component.
 *
 * @author <NAME>
 * @version 2.4
 */
public class ComponentResolver {

    /** **/
    private static final Pattern RESOLVER_PATTERN = Pattern.compile(
        "\\{\\{(<|%) (\\/?)([a-zA-Z\\-_]*)(\\/?)(?:\\s+([^>%]*))(>|%)\\}\\}",
        Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);

    private static final Pattern ATTRIBUTE_PATTERN = Pattern.compile("\\s?(\\w*)=\"(\\w*)\"\\s?",
        Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);

    /**
     * Default constructor
     */
    public ComponentResolver() {
        super();

    }

    /**
     * Collects all (start,end,empty) Element corresponding to a snippet component.
     *
     * @param document
     *            the Jsoup document to use
     * @return Return a {@link Elements} representing all web components contained in Jsoup document.
     */
    public Elements collect(final Document document) {
        final Evaluator evaluator = new ContainsOwnText(RESOLVER_PATTERN, -1);
        return Collector.collect(evaluator, document);
    }

    public Document normalize(final Document document) {
        final Elements elements = collect(document);

        elements.forEach((element) -> {
            String text = StringEscapeUtils.unescapeHtml(element.html());
            final Matcher matcher = RESOLVER_PATTERN.matcher(text);

            final List<MatchResult> results = Lists.newArrayList();

            while (matcher.find()) {
                results.add(0, matcher.toMatchResult());
            }
            if (results.size() > 1) {
                for (final MatchResult matchResult : results) {
                    final String snippet = text.substring(matchResult.start(), matchResult.end());
                    text = text.substring(0, matchResult.start()) + "<span>" + StringEscapeUtils.escapeHtml(snippet)
                            + "</span>" + text.substring(matchResult.end());
                }
                element.html(text);

            }
        });
        return document;
    }

    /**
     * Create a {@link ComponentToken} corresponding to the element.
     *
     * @param element
     *            the element to use.
     * @return Return a new instance of {@link ComponentToken} representing the element.
{'fraction_non_alphanumeric': 0.12060166532366372, 'fraction_numerical': 0.00496911093204405, 'mean_word_length': 2.747861097131354, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2282491', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2062, 'n_words': 551}