package io.trygvis.container.compiler; import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; import static java.lang.Character.toLowerCase; import static java.lang.Character.toUpperCase; public class Utils { public static String toFieldName(String s) { if (s.length() < 1) { return s.toLowerCase(); } if (s.startsWith("set")) { s = s.substring(3, s.length()); } else if (s.startsWith("get")) { s = s.substring(3, s.length()); } char[] chars = s.toCharArray(); boolean toUpper = false; int j = 0; for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (c == '_') { toUpper = true; } else { if (j == 0) { chars[j++] = toLowerCase(c); } else { chars[j++] = toUpper ? toUpperCase(c) : c; } toUpper = false; } } return new String(chars, 0, j); } public static String toSetterName(String s) { s = toFieldName(s); return "set" + toUpperCase(s.charAt(0)) + s.substring(1, s.length()); } public static String toGetterName(String s) { s = toFieldName(s); return "get" + toUpperCase(s.charAt(0)) + s.substring(1, s.length()); } public static String toJavaString(String s) { try { BufferedReader reader = new BufferedReader(new StringReader(s)); String line = reader.readLine(); StringBuilder buffer = new StringBuilder(); while (line != null) { buffer.append('"'); buffer.append(line.replace("\"", "\\\"")); buffer.append('"'); line = reader.readLine(); if(line != null) { buffer.append(" +\n"); } } return buffer.toString(); } catch (IOException e) { throw new RuntimeException(e); } } }