<commit_before>/**
 * File: StyleSheetWriterImpl.java
 *
 * Author: <NAME> (<EMAIL>)
 * Creation Date: Apr 21, 2010
 * Creation Time: 12:23:54 PM
 *
 * Copyright 2010 Local Matters, Inc.
 * All Rights Reserved
 *
 * Last checkin:
 *  $Author$
 *  $Revision$
 *  $Date$
 */
package org.lesscss4j.output;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.lesscss4j.model.BodyElement;
import org.lesscss4j.model.Declaration;
import org.lesscss4j.model.DeclarationElement;
import org.lesscss4j.model.Media;
import org.lesscss4j.model.Page;
import org.lesscss4j.model.RuleSet;
import org.lesscss4j.model.Selector;
import org.lesscss4j.model.StyleSheet;

// todo: It might make sense to break this up into separate writers for each type of element in the stylesheet
public class StyleSheetWriterImpl implements StyleSheetWriter {
    private boolean _prettyPrintEnabled = false;
    private String _defaultEncoding = "UTF-8";
    private String _newline = "\n";
    private PrettyPrintOptions _prettyPrintOptions;

    public PrettyPrintOptions getPrettyPrintOptions() {
        if (_prettyPrintOptions == null) {
            _prettyPrintOptions = new PrettyPrintOptions();
        }
        return _prettyPrintOptions;
    }

    public void setPrettyPrintOptions(PrettyPrintOptions prettyPrintOptions) {
        _prettyPrintOptions = prettyPrintOptions;
    }

    public String getNewline() {
        return _newline;
    }

    public void setNewline(String newline) {
        _newline = newline;
    }

    public String getDefaultEncoding() {
        return _defaultEncoding;
    }

    public void setDefaultEncoding(String defaultEncoding) {
        _defaultEncoding = defaultEncoding;
    }

    public boolean isPrettyPrintEnabled() {
        return _prettyPrintEnabled;
    }

    public void setPrettyPrintEnabled(boolean prettyPrintEnabled) {
        _prettyPrintEnabled = prettyPrintEnabled;
    }

    public void write(OutputStream output, StyleSheet styleSheet) throws IOException {
        String encoding = styleSheet.getCharset();
        if (encoding == null || encoding.length() == 0) {
            encoding = getDefaultEncoding();
        }

        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(output, encoding));
            write(writer, styleSheet);
        }
        finally {
            IOUtils.closeQuietly(writer);
        }
    }

    protected void writeBreak(Writer writer, int indent) throws IOException {
        if (isPrettyPrintEnabled()) {
            writer.write(getNewline());
            if (indent > 0) {
                writeIndent(writer, indent);
            }
        }
    }

    protected void writeBreak(Writer writer) throws IOException {
        writeBreak(writer, 0);
    }

    protected void writeSpace(Writer writer) throws IOException {
        if (isPrettyPrintEnabled()) {
            writer.write(" ");
        }
    }

    protected void writeSemi(Writer writer, boolean withBreak) throws IOException {
        writer.write(";");
        if (withBreak) {
            writeBreak(writer);
        }
    }

    protected void writeSemi(Writer writer) throws IOException {
        writeSemi(writer, true);
    }

    private void writeSeparator(Writer writer, String separator) throws IOException {
        writer.write(separator);
        writeSpace(writer);
    }

    protected void writeIndent(Writer writer, int level) throws IOException {
        for (int idx = 0; idx < level; idx++) {
            for (int jdx = 0; jdx < getPrettyPrintOptions().getIndentSize(); jdx++) {
                writer.write(' ');
            }
        }
    }

    protected void write(Writer writer, StyleSheet styleSheet) throws IOException {
        writeCharset(writer, styleSheet);
        writeImports(writer, styleSheet);

        writeBodyElements(writer, styleSheet.getBodyElements(), 0);
    }

    private void writeImports(Writer writer, StyleSheet styleSheet) throws IOException {
        for (String importElement : styleSheet.getImports()) {
            writer.write("@import ");
            if ("'\"".indexOf(importElement.charAt(0)) < 0 && !importElement.startsWith("url")) {
                writer.write("'");
                writer.write(importElement);
                writer.write("'");
            }
            else {
                writer.write(importElement);
            }
            writeSemi(writer);
        }
    }

    private void writeCharset(Writer writer, StyleSheet styleSheet) throws IOException {
        if (styleSheet.getCharset() != null && styleSheet.getCharset().length() > 0) {<commit_msg>Don't write indents if pretty print is disabled<commit_after>/**
 * File: StyleSheetWriterImpl.java
 *
 * Author: <NAME> (<EMAIL>)
 * Creation Date: Apr 21, 2010
 * Creation Time: 12:23:54 PM
 *
 * Copyright 2010 Local Matters, Inc.
 * All Rights Reserved
 *
 * Last checkin:
 *  $Author$
 *  $Revision$
 *  $Date$
 */
package org.lesscss4j.output;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.lesscss4j.model.BodyElement;
import org.lesscss4j.model.Declaration;
import org.lesscss4j.model.DeclarationElement;
import org.lesscss4j.model.Media;
import org.lesscss4j.model.Page;
import org.lesscss4j.model.RuleSet;
import org.lesscss4j.model.Selector;
import org.lesscss4j.model.StyleSheet;

// todo: It might make sense to break this up into separate writers for each type of element in the stylesheet
public class StyleSheetWriterImpl implements StyleSheetWriter {
    private boolean _prettyPrintEnabled = false;
    private String _defaultEncoding = "UTF-8";
    private String _newline = "\n";
    private PrettyPrintOptions _prettyPrintOptions;

    public PrettyPrintOptions getPrettyPrintOptions() {
        if (_prettyPrintOptions == null) {
            _prettyPrintOptions = new PrettyPrintOptions();
        }
        return _prettyPrintOptions;
    }

    public void setPrettyPrintOptions(PrettyPrintOptions prettyPrintOptions) {
        _prettyPrintOptions = prettyPrintOptions;
    }

    public String getNewline() {
        return _newline;
    }

    public void setNewline(String newline) {
        _newline = newline;
    }

    public String getDefaultEncoding() {
        return _defaultEncoding;
    }

    public void setDefaultEncoding(String defaultEncoding) {
        _defaultEncoding = defaultEncoding;
    }

    public boolean isPrettyPrintEnabled() {
        return _prettyPrintEnabled;
    }

    public void setPrettyPrintEnabled(boolean prettyPrintEnabled) {
        _prettyPrintEnabled = prettyPrintEnabled;
    }

    public void write(OutputStream output, StyleSheet styleSheet) throws IOException {
        String encoding = styleSheet.getCharset();
        if (encoding == null || encoding.length() == 0) {
            encoding = getDefaultEncoding();
        }

        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(output, encoding));
            write(writer, styleSheet);
        }
        finally {
            IOUtils.closeQuietly(writer);
        }
    }

    protected void writeBreak(Writer writer, int indent) throws IOException {
        if (isPrettyPrintEnabled()) {
            writer.write(getNewline());
            if (indent > 0) {
                writeIndent(writer, indent);
            }
        }
    }

    protected void writeBreak(Writer writer) throws IOException {
        writeBreak(writer, 0);
    }

    protected void writeSpace(Writer writer) throws IOException {
        if (isPrettyPrintEnabled()) {
            writer.write(" ");
        }
    }

    protected void writeSemi(Writer writer, boolean withBreak) throws IOException {
        writer.write(";");
        if (withBreak) {
            writeBreak(writer);
        }
    }

    protected void writeSemi(Writer writer) throws IOException {
        writeSemi(writer, true);
    }

    private void writeSeparator(Writer writer, String separator) throws IOException {
        writer.write(separator);
        writeSpace(writer);
    }

    protected void writeIndent(Writer writer, int level) throws IOException {
        if (isPrettyPrintEnabled()) {
            for (int idx = 0; idx < level; idx++) {
                for (int jdx = 0; jdx < getPrettyPrintOptions().getIndentSize(); jdx++) {
                    writer.write(' ');
                }
            }
        }
    }

    protected void write(Writer writer, StyleSheet styleSheet) throws IOException {
        writeCharset(writer, styleSheet);
        writeImports(writer, styleSheet);

        writeBodyElements(writer, styleSheet.getBodyElements(), 0);
    }

    private void writeImports(Writer writer, StyleSheet styleSheet) throws IOException {
        for (String importElement : styleSheet.getImports()) {
            writer.write("@import ");
            if ("'\"".indexOf(importElement.charAt(0)) < 0 && !importElement.startsWith("url")) {
                writer.write("'");
                writer.write(importElement);
                writer.write("'");
            }
            else {
                writer.write(importElement);
            }
            writeSemi(writer);
        }
    }

    private void writeCharset(Writer writer, StyleSheet styleSheet) throws IOException {
        if (styleSheet.getCharset() != null && styleSheet.getCharset().length() > 0) {
{'fraction_non_alphanumeric': 0.09541984732824428, 'fraction_numerical': 0.007220961419434702, 'mean_word_length': 2.6256544502617802, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '6814548', 'n_tokens_mistral': 2667, 'n_tokens_neox': 2534, 'n_words': 645}