/*
 * 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 net.revelc.code.formatter;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Objects;

import org.apache.maven.plugin.logging.Log;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.text.edits.MalformedTreeException;

/**
 * @author marvin.froeder
 */
public abstract class AbstractCacheableFormatter {

    protected Log log;

    protected Charset encoding;

    protected abstract void init(Map<String, String> options, ConfigurationSource cfg);

    protected void initCfg(ConfigurationSource cfg) {
        this.log = cfg.getLog();
        this.encoding = cfg.getEncoding();
    }

    public String formatFile(File file, String originalCode, LineEnding ending) {
        try {
            this.log.debug("Processing file: " + file + " with line ending: " + ending);
            LineEnding formatterLineEnding = ending;
            // if the line ending is set as KEEP we have to determine the current line ending of the file
            // and let the formatter use this one. Otherwise it would likely fall back to current system line separator
            if (formatterLineEnding == LineEnding.KEEP) {
                formatterLineEnding = LineEnding.determineLineEnding(originalCode);
                this.log.debug("Determined line ending: " + formatterLineEnding + " to keep for file: " + file);
            }
            String formattedCode = doFormat(originalCode, formatterLineEnding);

            if (formattedCode == null) {
                this.log.debug("Nothing formatted. Try to fix line endings.");
                formattedCode = fixLineEnding(originalCode, ending);
            }

            if (formattedCode == null) {
                this.log.debug("Equal code. Not writing result to file.");
                return originalCode;
            }

            this.log.debug("Line endings fixed");

            return formattedCode;
        } catch (IOException | MalformedTreeException | BadLocationException e) {
            this.log.warn(e);
            return null;
        }
    }

    private static String fixLineEnding(String code, LineEnding ending) {
        if (ending == LineEnding.KEEP) {
            return null;
        }

        LineEnding current = LineEnding.determineLineEnding(code);
        if (current == LineEnding.UNKNOWN) {
            return null;
        }
        if (current == ending) {
            return null;
        }
        if (ending == LineEnding.AUTO && Objects.equals(current.getChars(), ending.getChars())) {
            return null;
        }

        return code.replace(current.getChars(), ending.getChars());
    }

    protected abstract String doFormat(String code, LineEnding ending) throws IOException, BadLocationException;

}

@@ -699,34 +699,8 @@
 Map;
-%0Aimport java.util.Objects;
 %0A%0Aim
@@ -848,16 +848,172 @@
 ption;%0A%0A
+import net.revelc.code.formatter.css.CssFormatter;%0Aimport net.revelc.code.formatter.html.HTMLFormatter;%0Aimport net.revelc.code.formatter.xml.XMLFormatter;%0A%0A
 /**%0A * @
@@ -2381,32 +2381,335 @@
 );%0A            %7D
+ else if (this instanceof CssFormatter %7C%7C this instanceof HTMLFormatter %7C%7C this instanceof XMLFormatter) %7B%0A                this.log.debug(%22Formatted but line endings not supported by tooling. Try to fix line endings.%22);%0A                formattedCode = fixLineEnding(formattedCode, ending);%0A            %7D
 %0A%0A            if
@@ -3264,278 +3264,162 @@
     
-LineEnding current = LineEnding.determineLineEnding(code);%0A        if (current == LineEnding.UNKNOWN) %7B%0A            return null;%0A        %7D%0A        if (current == ending) %7B%0A            return null;%0A        %7D%0A        if (ending == LineEnding.AUTO && Objects.equals(current
+// Normalize all line endings%0A        code = code.replace(LineEnding.CRLF.getChars(), LineEnding.LF.getChars());%0A        code = code.replace(LineEnding.CR
 .get
@@ -3419,39 +3419,46 @@
 .CR.getChars(), 
-e
+LineE
 nding.
+LF.
 getChars())) %7B%0A 
@@ -3452,19 +3452,18 @@
 Chars())
-) %7B
+;%0A
 %0A       
@@ -3467,35 +3467,49 @@
     
-    return null;%0A        %7D%0A
+// Replace line endings with chosen style
 %0A   
@@ -3533,23 +3533,29 @@
 replace(
-current
+LineEnding.LF
 .getChar

Correct how line endings are addressed and allow better support for this on css, html, and xml