1 33 34 package net.percederberg.grammatica.code; 35 36 45 public class CodeStyle { 46 47 50 public static final CodeStyle JAVA = new CodeStyle(70, " "); 51 52 55 public static final CodeStyle CSHARP = new CodeStyle(70, " "); 56 57 60 public static final CodeStyle VISUAL_BASIC = new CodeStyle(70, " "); 61 62 65 private int margin; 66 67 70 private String indentString; 71 72 78 public CodeStyle(int margin, String indent) { 79 this.margin = margin; 80 this.indentString = indent; 81 } 82 83 88 public int getMargin() { 89 return margin; 90 } 91 92 99 public String getIndent(int level) { 100 StringBuffer buffer = new StringBuffer (); 101 102 for (int i = 0; i < level; i++) { 103 buffer.append(indentString); 104 } 105 return buffer.toString(); 106 } 107 108 119 public String getStringConstant(String str, char escape) { 120 StringBuffer res = new StringBuffer (); 121 122 res.append('"'); 123 res.append(addStringEscapes(str, escape)); 124 res.append('"'); 125 126 return res.toString(); 127 } 128 129 141 public String getUpperCase(String str) { 142 StringBuffer res = new StringBuffer (); 143 char last = 'A'; 144 145 for (int i = 0; i < str.length(); i++) { 146 if (Character.isLowerCase(last) 147 && Character.isUpperCase(str.charAt(i))) { 148 149 res.append("_"); 150 } 151 last = str.charAt(i); 152 if (('A' <= last && last <= 'Z') 153 || ('a' <= last && last <= 'z') 154 || ('0' <= last && last <= '9') 155 || last == '_') { 156 157 res.append(Character.toUpperCase(last)); 158 } 159 } 160 161 return res.toString(); 162 } 163 164 176 public String getLowerCase(String str) { 177 StringBuffer res = new StringBuffer (); 178 char last = 'A'; 179 180 for (int i = 0; i < str.length(); i++) { 181 if (Character.isLowerCase(last) 182 && Character.isUpperCase(str.charAt(i))) { 183 184 res.append("_"); 185 } 186 last = str.charAt(i); 187 if (('A' <= last && last <= 'Z') 188 || ('a' <= last && last <= 'z') 189 || ('0' <= last && last <= '9') 190 || last == '_') { 191 192 res.append(Character.toLowerCase(last)); 193 } 194 } 195 196 return res.toString(); 197 } 198 199 212 public String getMixedCase(String str, boolean initialUpper) { 213 StringBuffer res = new StringBuffer (); 214 char last = 'A'; 215 216 for (int i = 0; i < str.length(); i++) { 217 if (Character.isLowerCase(last) 218 && Character.isUpperCase(str.charAt(i))) { 219 220 initialUpper = true; 221 } else if (str.charAt(i) == '_') { 222 initialUpper = true; 223 } 224 last = str.charAt(i); 225 if (('A' <= last && last <= 'Z') 226 || ('a' <= last && last <= 'z') 227 || ('0' <= last && last <= '9')) { 228 229 if (initialUpper) { 230 res.append(Character.toUpperCase(last)); 231 initialUpper = false; 232 } else { 233 res.append(Character.toLowerCase(last)); 234 } 235 } 236 } 237 238 return res.toString(); 239 } 240 241 250 public String addStringEscapes(String str, char escape) { 251 StringBuffer res = new StringBuffer (); 252 253 for (int i = 0; i < str.length(); i++) { 254 if (str.charAt(i) == '"') { 255 res.append(escape); 256 res.append("\""); 257 } else if (str.charAt(i) == escape) { 258 res.append(escape); 259 res.append(escape); 260 } else { 261 res.append(str.charAt(i)); 262 } 263 } 264 265 return res.toString(); 266 } 267 } 268 | Popular Tags |