0
\$\begingroup\$

I have a java code that is supposed to format the content of a .css file so that I could use the result to make a enum class.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class CreateCssEnum {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader("D:\\eclipse\\font-awesome.css"));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append("\n");
                line = br.readLine();
            }
            String croppedFile = sb.toString();
            croppedFile = croppedFile.substring(croppedFile.lastIndexOf("*/") + 3, croppedFile.lastIndexOf("}"));
            String[] sections = croppedFile.toString().split("}");
            String finalResult = "";
            for (String st : sections) {
                int NewLines = st.replaceAll("[^\n]", "").length();
                if (NewLines <= 3) {
                    String className = st.substring(st.indexOf("-") + 1, st.indexOf(":")).toUpperCase();
                    String hexa = st.substring(st.indexOf("\\") + 1, st.indexOf("\";"));
                    int decimal = Integer.parseInt(hexa, 16);
                    String Result = className + "(\"" + decimal + "\")" + ",\n";
                    finalResult += Result;
                }else if (NewLines == 11) {
                    String[] parts = st.split("\\s*\n\\s*");
                    String a = parts[1];
                    String b = parts[2];
                    String c = parts[3];
                    String d = parts[4];
                    String e = parts[5];
                    String f = parts[6];
                    String g = parts[7];
                    String h = parts[8];
                    String i = parts[9];
                    String j = parts[10];
                    a = a.substring(a.indexOf("-") + 1, a.indexOf(":")).toUpperCase();
                    b = b.substring(b.indexOf("-") + 1, b.indexOf(":")).toUpperCase();
                    c = c.substring(c.indexOf("-") + 1, c.indexOf(":")).toUpperCase();
                    d = d.substring(d.indexOf("-") + 1, d.indexOf(":")).toUpperCase();
                    e = e.substring(e.indexOf("-") + 1, e.indexOf(":")).toUpperCase();
                    f = f.substring(f.indexOf("-") + 1, f.indexOf(":")).toUpperCase();
                    g = g.substring(g.indexOf("-") + 1, g.indexOf(":")).toUpperCase();
                    h = h.substring(h.indexOf("-") + 1, h.indexOf(":")).toUpperCase();
                    i = i.substring(i.indexOf("-") + 1, i.indexOf(":")).toUpperCase();
                    j = h.substring(j.indexOf("\\") + 1,j.lastIndexOf("\""));
                    int decimal = Integer.parseInt(i, 16);
                    String Result = a + "(" + "\"" + decimal + "\"" + ")" + ",\n"
                                    + b + "(" + "\"" + decimal + ":1"+ "\"" + ")" + ",\n"
                                    + c + "(" + "\"" + decimal + ":2" + "\"" + ")" + ",\n" 
                                    + d + "(" + "\"" + decimal + ":3" + "\"" + ")" + ",\n"
                                    + e + "(" + "\"" + decimal + ":4" + "\"" + ")"+ ",\n"
                                    + f + "(" + "\"" + decimal + ":5" + "\"" + ")"+ ",\n"
                                    + g + "(" + "\"" + decimal + ":6" + "\"" + ")"+ ",\n"
                                    + h + "(" + "\"" + decimal + ":7" + "\"" + ")"+ ",\n"
                                    + j + "(" + "\"" + decimal + ":8" + "\"" + ")"+ ",\n";
                    finalResult += Result;
                }else if (NewLines == 10) {
                    String[] parts = st.split("\\s*\n\\s*");
                    String a = parts[1];
                    String b = parts[2];
                    String c = parts[3];
                    String d = parts[4];
                    String e = parts[5];
                    String f = parts[6];
                    String g = parts[7];
                    String h = parts[8];
                    String i = parts[9];
                    a = a.substring(a.indexOf("-") + 1, a.indexOf(":")).toUpperCase();
                    b = b.substring(b.indexOf("-") + 1, b.indexOf(":")).toUpperCase();
                    c = c.substring(c.indexOf("-") + 1, c.indexOf(":")).toUpperCase();
                    d = d.substring(d.indexOf("-") + 1, d.indexOf(":")).toUpperCase();
                    e = e.substring(e.indexOf("-") + 1, e.indexOf(":")).toUpperCase();
                    f = f.substring(f.indexOf("-") + 1, f.indexOf(":")).toUpperCase();
                    g = g.substring(g.indexOf("-") + 1, g.indexOf(":")).toUpperCase();
                    h = h.substring(h.indexOf("-") + 1, h.indexOf(":")).toUpperCase();
                    i = h.substring(i.indexOf("\\") + 1,i.lastIndexOf("\""));
                    int decimal = Integer.parseInt(i, 16);
                    String Result = a + "(" + "\"" + decimal + "\"" + ")" + ",\n"
                                    + b + "(" + "\"" + decimal + ":1"+ "\"" + ")" + ",\n"
                                    + c + "(" + "\"" + decimal + ":2" + "\"" + ")" + ",\n" 
                                    + d + "(" + "\"" + decimal + ":3" + "\"" + ")" + ",\n"
                                    + e + "(" + "\"" + decimal + ":4" + "\"" + ")"+ ",\n"
                                    + f + "(" + "\"" + decimal + ":5" + "\"" + ")"+ ",\n"
                                    + g + "(" + "\"" + decimal + ":6" + "\"" + ")"+ ",\n"
                                    + h + "(" + "\"" + decimal + ":7" + "\"" + ")"+ ",\n";
                    finalResult += Result;
                }else if (NewLines == 9) {
                    String[] parts = st.split("\\s*\n\\s*");
                    String a = parts[1];
                    String b = parts[2];
                    String c = parts[3];
                    String d = parts[4];
                    String e = parts[5];
                    String f = parts[6];
                    String g = parts[7];
                    String h = parts[8];
                    a = a.substring(a.indexOf("-") + 1, a.indexOf(":")).toUpperCase();
                    b = b.substring(b.indexOf("-") + 1, b.indexOf(":")).toUpperCase();
                    c = c.substring(c.indexOf("-") + 1, c.indexOf(":")).toUpperCase();
                    d = d.substring(d.indexOf("-") + 1, d.indexOf(":")).toUpperCase();
                    e = e.substring(e.indexOf("-") + 1, e.indexOf(":")).toUpperCase();
                    f = f.substring(f.indexOf("-") + 1, f.indexOf(":")).toUpperCase();
                    g = g.substring(g.indexOf("-") + 1, g.indexOf(":")).toUpperCase();
                    h = h.substring(h.indexOf("\\") + 1, h.lastIndexOf("\""));
                    int decimal = Integer.parseInt(h, 16);
                    String Result = a + "(" + "\"" + decimal + "\"" + ")" + ",\n"
                                    + b + "(" + "\"" + decimal + ":1"+ "\"" + ")" + ",\n"
                                    + c + "(" + "\"" + decimal + ":2" + "\"" + ")" + ",\n" 
                                    + d + "(" + "\"" + decimal + ":3" + "\"" + ")" + ",\n"
                                    + e + "(" + "\"" + decimal + ":4" + "\"" + ")"+ ",\n"
                                    + f + "(" + "\"" + decimal + ":5" + "\"" + ")"+ ",\n"
                                    + g + "(" + "\"" + decimal + ":6" + "\"" + ")"+ ",\n";
                    finalResult += Result;
                }else if (NewLines == 8) {
                    String[] parts = st.split("\\s*\n\\s*");
                    String a = parts[1];
                    String b = parts[2];
                    String c = parts[3];
                    String d = parts[4];
                    String e = parts[5];
                    String f = parts[6];
                    String g = parts[7];
                    a = a.substring(a.indexOf("-") + 1, a.indexOf(":")).toUpperCase();
                    b = b.substring(b.indexOf("-") + 1, b.indexOf(":")).toUpperCase();
                    c = c.substring(c.indexOf("-") + 1, c.indexOf(":")).toUpperCase();
                    d = d.substring(d.indexOf("-") + 1, d.indexOf(":")).toUpperCase();
                    e = e.substring(e.indexOf("-") + 1, e.indexOf(":")).toUpperCase();
                    f = f.substring(f.indexOf("-") + 1, f.indexOf(":")).toUpperCase();
                    g = g.substring(g.indexOf("\\") + 1, g.lastIndexOf("\""));
                    int decimal = Integer.parseInt(g, 16);
                    String Result = a + "(" + "\"" + decimal + "\"" + ")" + ",\n"
                                    + b + "(" + "\"" + decimal + ":1"+ "\"" + ")" + ",\n"
                                    + c + "(" + "\"" + decimal + ":2" + "\"" + ")" + ",\n" 
                                    + d + "(" + "\"" + decimal + ":3" + "\"" + ")" + ",\n"
                                    + e + "(" + "\"" + decimal + ":4" + "\"" + ")"+ ",\n"
                                    + f + "(" + "\"" + decimal + ":5" + "\"" + ")"+ ",\n";
                    finalResult += Result;
                }else if (NewLines == 7) {
                    String[] parts = st.split("\\s*\n\\s*");
                    String a = parts[1];
                    String b = parts[2];
                    String c = parts[3];
                    String d = parts[4];
                    String e = parts[5];
                    String f = parts[6];
                    a = a.substring(a.indexOf("-") + 1, a.indexOf(":")).toUpperCase();
                    b = b.substring(b.indexOf("-") + 1, b.indexOf(":")).toUpperCase();
                    c = c.substring(c.indexOf("-") + 1, c.indexOf(":")).toUpperCase();
                    d = d.substring(d.indexOf("-") + 1, d.indexOf(":")).toUpperCase();
                    e = e.substring(e.indexOf("-") + 1, e.indexOf(":")).toUpperCase();
                    f = f.substring(f.indexOf("\\") + 1, f.lastIndexOf("\""));
                    int decimal = Integer.parseInt(f, 16);
                    String Result = a + "(" + "\"" + decimal + "\"" + ")" + ",\n" + b + "(" + "\"" + decimal + ":1"
                            + "\"" + ")" + ",\n" + c + "(" + "\"" + decimal + ":2" + "\"" + ")" + ",\n" + d + "(" + "\""
                            + decimal + ":3" + "\"" + ")" + ",\n" + e + "(" + "\"" + decimal + ":4" + "\"" + ")"
                            + ",\n";
                    finalResult += Result;
                } else if (NewLines == 6) {
                    String[] parts = st.split("\\s*\n\\s*");
                    String a = parts[1];
                    String b = parts[2];
                    String c = parts[3];
                    String d = parts[4];
                    String e = parts[5];
                    a = a.substring(a.indexOf("-") + 1, a.indexOf(":")).toUpperCase();
                    b = b.substring(b.indexOf("-") + 1, b.indexOf(":")).toUpperCase();
                    c = c.substring(c.indexOf("-") + 1, c.indexOf(":")).toUpperCase();
                    d = d.substring(d.indexOf("-") + 1, d.indexOf(":")).toUpperCase();
                    e = e.substring(e.indexOf("\\") + 1, e.lastIndexOf("\""));

                    int decimal = Integer.parseInt(e, 16);
                    String Result = a + "(" + "\"" + decimal + "\"" + ")" + ",\n" + b + "(" + "\"" + decimal + ":1"
                            + "\"" + ")" + ",\n" + c + "(" + "\"" + decimal + ":2" + "\"" + ")" + ",\n" + d + "(" + "\""
                            + decimal + ":3" + "\"" + ")" + ",\n";
                    finalResult += Result;
                } else if (NewLines == 5) {
                    String[] parts = st.split("\\s*\n\\s*");
                    String a = parts[1];
                    String b = parts[2];
                    String c = parts[3];
                    String d = parts[4];
                    a = a.substring(a.indexOf("-") + 1, a.indexOf(":")).toUpperCase();
                    b = b.substring(b.indexOf("-") + 1, b.indexOf(":")).toUpperCase();
                    c = c.substring(c.indexOf("-") + 1, c.indexOf(":")).toUpperCase();
                    d = d.substring(d.indexOf("\\") + 1, d.lastIndexOf("\""));
                    int decimal = Integer.parseInt(d, 16);
                    String Result = a + "(" + "\"" + decimal + "\"" + ")" + ",\n" + b + "(" + "\"" + decimal + ":1"
                            + "\"" + ")" + ",\n" + c + "(" + "\"" + decimal + ":2" + "\"" + ")" + ",\n";
                    finalResult += Result;
                } else if (NewLines == 4) {
                    String[] parts = st.split("\\s*\n\\s*");
                    String a = parts[1];
                    String b = parts[2];
                    String c = parts[3];
                    a = a.substring(a.indexOf("-") + 1, a.indexOf(":")).toUpperCase();
                    b = b.substring(b.indexOf("-") + 1, b.indexOf(":")).toUpperCase();
                    c = c.substring(c.indexOf("\\") + 1, c.lastIndexOf("\""));
                    int decimal = Integer.parseInt(c, 16);
                    String Result = a + "(" + "\"" + decimal + "\"" + ")" + ",\n" + b + "(" + "\"" + decimal + ":1"
                            + "\"" + ")" + ",\n";
                    finalResult += Result;
                }
            }

            String newResult = finalResult.substring(0, finalResult.lastIndexOf(","));
            System.out.println(newResult);

        }

        finally {
            br.close();
        }

    }
}

My file text looks pretty much like this:

.fa-music:before {
 content: "\f001";
}
.fa-remove:before,
.fa-close:before,
.fa-times:before {
  content: "\f00d";
} 

And I want the output to look like this
MUSIC("61441"), REMOVE("61453"), CLOSE("61453:1"), TIMES("61453:2"), If it wasn´t for those damn classed that share the same value the code would have been much, much shorter. But I had to include those too and did not know another way other than the if statements. The code is fragile tough. If I get a file that has more than 11 Newlines the output will be wrong.

Anyone has any other ideas?

\$\endgroup\$
7
  • \$\begingroup\$ Wow. Have you ever heard of loops? \$\endgroup\$
    – user49930
    Oct 23 '15 at 10:02
  • \$\begingroup\$ Can you clarify how does having more than 11 newlines generate the wrong output? What exactly are you trying to do here? Also, what OS are you on? If you're on Linux/OS X, or happen to be running cygwin/mingw on Windows, and you are just looking for a simple text-processing tool, you don't have to use Java for it... \$\endgroup\$
    – h.j.k.
    Oct 23 '15 at 12:04
  • \$\begingroup\$ hi @h.j.k. I was reffering to my integer called NewLines. In order to get the wanted output for the class names with the same value I split the string by "\n". For this example : <code>.fa-remove:before, .fa-close:before, .fa-times:before { content: "\f00d"; } </code> I get the string splitted in 4 parts. I made an if statement to format those string parts until it detects up to 11 parts if needed, as I don´t know how many classes with same value I have. That is why the code is so big.I have windows \$\endgroup\$
    – Daria M
    Oct 23 '15 at 12:31
  • \$\begingroup\$ And what do the numbers, e.g. 61441 represent? \$\endgroup\$
    – h.j.k.
    Oct 23 '15 at 12:46
  • \$\begingroup\$ sorry, I forgot to mention but it´s in the code. The actual value in the file is a hexadecimal number and I convert it to decimal. \$\endgroup\$
    – Daria M
    Oct 23 '15 at 12:47
3
\$\begingroup\$

Some thoughts:

  1. Use methods! They will make it much easier to read the code and understand what's going on.
  2. Use StringBuilder instead of string concatenation when building your output.
  3. Use try-with-resources to ensure your readers get closed.
  4. Separate your IO from your business logic. This will let you call the business logic from inside your code, not just from the IO layer.

I would rework your code to look more like:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

class CreateCssEnum {

    private static final Pattern NEWLINE_PATTERN = Pattern.compile("\\s*\n\\s*");

    public static void main(final String[] args) throws IOException {
        final String cssFile = loadCssFile("D:\\eclipse\\font-awesome.css");
        final String[] sections = parseSections(cssFile);
        System.out.println(transform(sections));
    }

    private static String loadCssFile(final String filename) throws IOException {
        try (final BufferedReader bufferedReader = new BufferedReader(new FileReader(filename))) {
            final StringBuilder css = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                css.append(line);
                css.append("\n");
            }
            return css.toString();
        }
    }

    private static String[] parseSections(final String input) {
        final String croppedFile = input.substring(input.lastIndexOf("*/") + 3, input.lastIndexOf("}"));
        return croppedFile.split("}\n");
    }

    private static String transform(final String... sections) {
        final StringBuilder result = new StringBuilder();
        for (final String section : sections) {
            result.append(transformSection(section));
        }

        result.delete(result.lastIndexOf(","), result.length());
        return result.toString();
    }

    private static String transformSection(final String section) {
        final String[] parts = NEWLINE_PATTERN.split(section);
        final StringBuilder result = new StringBuilder();
        final int decimal = parseDecimal(parts[parts.length - 1]);

        for (int i = 0; i < parts.length - 1; i++) {
            result.append(parseClassName(parts[i]));
            result.append("(\"");
            result.append(decimal);
            if (i > 0) {
                result.append(":");
                result.append(i);
            }
            result.append("\"),\n");
        }
        return result.toString();
    }

    private static int parseDecimal(final String string) {
        final int slashIndex = string.indexOf("\\");
        final int quoteIndex = string.indexOf("\";");
        final String hexString = string.substring(slashIndex + 1, quoteIndex);
        return Integer.parseInt(hexString, 16);
    }

    private static String parseClassName(final String string) {
        final int hypenIndex = string.indexOf("-");
        final int colonIndex = string.indexOf(":");
        return string.substring(hypenIndex + 1, colonIndex).toUpperCase();
    }
}
\$\endgroup\$
1
\$\begingroup\$

You guys, I did it myself (well...not like in a genious way just popping infomation out of pure nowhere, but I documented myself better and refactored my code like this:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class CreateCssEnum {
public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new FileReader("D:\\eclipse\\font-awesome.css"));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append("\n");
            line = br.readLine();
        }
        String croppedFile = sb.toString();
        croppedFile = croppedFile.substring(croppedFile.lastIndexOf("*/") + 3, croppedFile.lastIndexOf("}"));
        String[] sections = croppedFile.toString().split("}");
        String finalResult = "";
        String hexa = null;
        for (String st : sections) {
            String trimmedString = st.trim();
            hexa = st.substring(st.indexOf(": \"") + 4, st.lastIndexOf("\""));
            int decimal = Integer.parseInt(hexa, 16);
            String classNames = trimmedString.substring(trimmedString.indexOf(".fa-"), trimmedString.indexOf("{"));
            String delimiter = "\n";
            String[] listObj = classNames.split(delimiter);

            for (int i = 0; i < listObj.length; i++) {
                String capitalClass = listObj[i];
                capitalClass = capitalClass.substring(capitalClass.indexOf("-") + 1, capitalClass.indexOf(":")).toUpperCase();
                String result = capitalClass + "(" + "\"" + decimal + ((i == 0) ? "" : ":" + i) + "\"" + ")" + "," + "\n";
                finalResult +=result;
            }
        }
        System.out.print(finalResult.substring(0,finalResult.lastIndexOf(",")));
    } finally {
        br.close();
    }

}
}

Oh...that rewarding feeling is undescribable.

\$\endgroup\$
1
  • \$\begingroup\$ You can consider posting this as a follow-up, i.e. new, question. :) \$\endgroup\$
    – h.j.k.
    Oct 23 '15 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.