<commit_before>package com.silentmatt.dss;

import com.silentmatt.dss.util.JoinedList;
import com.silentmatt.dss.util.JoinedSelectorList;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author <NAME>
 */
public class RuleSet extends Rule {
    private final List<Rule> rules = new ArrayList<Rule>();
    private final DeclarationList declarations = new DeclarationList();
    private JoinedSelectorList selectors = new JoinedSelectorList(new ArrayList<Selector>(), null, new ArrayList<Selector>());
    private final List<RuleSet> nestedRuleSets = new ArrayList<RuleSet>();

    public DeclarationList getDeclarations() {
        return declarations;
    }

    public void addDeclaration(Declaration declaration) {
        declarations.add(declaration);
    }

    public List<Selector> getSelectors() {
        return selectors;
    }

    public List<Rule> getRules() {
        return rules;
    }

    public void addRule(Rule directive) {
        rules.add(directive);
    }

    public Declaration getDeclaration(String name) {
        for (Declaration d : declarations) {
            if (d.getName().equalsIgnoreCase(name)) {
                return d;
            }
        }
        return null;
    }

    public Expression getValue(String name) {
        return declarations.get(name);
    }

    public List<RuleSet> getNestedRuleSets() {
        return nestedRuleSets;
    }

    public void addNestedRuleSet(RuleSet nested, Combinator cb) {
        nestedRuleSets.add(nested);

        nested.selectors.setParents(selectors);
        nested.selectors.setCombinator(cb);

        /*List<Selector> childSelectors = new ArrayList<Selector>(nested.getSelectors());
        nested.selectors.clear();

        for (Selector parent : selectors) {
             for (Selector child : childSelectors) {
                 Selector combined = new Selector(parent, cb, child);
                 nested.selectors.add(combined);
             }
         }*/
    }

    @Override
    public String toString() {
        return toString(0);
    }

    public String toString(int nesting) {
        String start = Rule.getIndent(nesting);

        StringBuilder txt = new StringBuilder();
        boolean first = true;
        for (Selector sel : selectors) {
            if (first) {
                first = false;
                txt.append(start);
            }
            else {
                txt.append(", ");
            }
            txt.append(sel.toString());
        }
        txt.append(" {");

        for (Rule dir : rules) {
            txt.append("\n\t" + start);
            txt.append(dir.toString(nesting + 1));
        }
        for (Declaration dec : declarations) {
            txt.append("\n\t" + start);
            txt.append(dec.toString());
            txt.append(";");
        }

        txt.append("\n" + start + "}");

        return txt.toString();
    }

    public String toCssString(int nesting) {
        Stack<String> nestedString = new Stack<String>();
        StringBuilder text = new StringBuilder(toCssString(nesting, nestedString));
        while (!nestedString.empty()) {
            text.append('\n');
            text.append(nestedString.pop());
        }
        return text.toString();
    }

    public String toCssString(int nesting, Stack<String> nestedString) {
        String start = Rule.getIndent(nesting);

        StringBuilder txt = new StringBuilder();
        boolean first = true;
        for (Selector sel : selectors) {
            if (first) {
                first = false;
                txt.append(start);
            }
            else {
                txt.append(", ");
            }
            txt.append(sel.toString());
        }
        txt.append(" {");

        for (Rule dir : rules) {
            String dirString = dir.toCssString(nesting + 1);
            if (dirString.length() > 0) {
                txt.append("\n\t" + start);<commit_msg>make RuleSet#toCssString(int, Stack) private.<commit_after>package com.silentmatt.dss;

import com.silentmatt.dss.util.JoinedList;
import com.silentmatt.dss.util.JoinedSelectorList;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author <NAME>
 */
public class RuleSet extends Rule {
    private final List<Rule> rules = new ArrayList<Rule>();
    private final DeclarationList declarations = new DeclarationList();
    private JoinedSelectorList selectors = new JoinedSelectorList(new ArrayList<Selector>(), null, new ArrayList<Selector>());
    private final List<RuleSet> nestedRuleSets = new ArrayList<RuleSet>();

    public DeclarationList getDeclarations() {
        return declarations;
    }

    public void addDeclaration(Declaration declaration) {
        declarations.add(declaration);
    }

    public List<Selector> getSelectors() {
        return selectors;
    }

    public List<Rule> getRules() {
        return rules;
    }

    public void addRule(Rule directive) {
        rules.add(directive);
    }

    public Declaration getDeclaration(String name) {
        for (Declaration d : declarations) {
            if (d.getName().equalsIgnoreCase(name)) {
                return d;
            }
        }
        return null;
    }

    public Expression getValue(String name) {
        return declarations.get(name);
    }

    public List<RuleSet> getNestedRuleSets() {
        return nestedRuleSets;
    }

    public void addNestedRuleSet(RuleSet nested, Combinator cb) {
        nestedRuleSets.add(nested);

        nested.selectors.setParents(selectors);
        nested.selectors.setCombinator(cb);

        /*List<Selector> childSelectors = new ArrayList<Selector>(nested.getSelectors());
        nested.selectors.clear();

        for (Selector parent : selectors) {
             for (Selector child : childSelectors) {
                 Selector combined = new Selector(parent, cb, child);
                 nested.selectors.add(combined);
             }
         }*/
    }

    @Override
    public String toString() {
        return toString(0);
    }

    public String toString(int nesting) {
        String start = Rule.getIndent(nesting);

        StringBuilder txt = new StringBuilder();
        boolean first = true;
        for (Selector sel : selectors) {
            if (first) {
                first = false;
                txt.append(start);
            }
            else {
                txt.append(", ");
            }
            txt.append(sel.toString());
        }
        txt.append(" {");

        for (Rule dir : rules) {
            txt.append("\n\t" + start);
            txt.append(dir.toString(nesting + 1));
        }
        for (Declaration dec : declarations) {
            txt.append("\n\t" + start);
            txt.append(dec.toString());
            txt.append(";");
        }

        txt.append("\n" + start + "}");

        return txt.toString();
    }

    public String toCssString(int nesting) {
        Stack<String> nestedString = new Stack<String>();
        StringBuilder text = new StringBuilder(toCssString(nesting, nestedString));
        while (!nestedString.empty()) {
            text.append('\n');
            text.append(nestedString.pop());
        }
        return text.toString();
    }

    private String toCssString(int nesting, Stack<String> nestedString) {
        String start = Rule.getIndent(nesting);

        StringBuilder txt = new StringBuilder();
        boolean first = true;
        for (Selector sel : selectors) {
            if (first) {
                first = false;
                txt.append(start);
            }
            else {
                txt.append(", ");
            }
            txt.append(sel.toString());
        }
        txt.append(" {");

        for (Rule dir : rules) {
            String dirString = dir.toCssString(nesting + 1);
            if (dirString.length() > 0) {
                txt.append("\n\t" + start);
{'fraction_non_alphanumeric': 0.11457946240554936, 'fraction_numerical': 0.0009909575126966432, 'mean_word_length': 2.0115628496829543, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, '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': '2870007', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2145, 'n_words': 488}