1 55 56 package org.apache.bsf.util.cf; 57 58 import java.io.*; 59 import java.util.*; 60 import org.apache.bsf.util.*; 61 62 74 public class CodeFormatter 75 { 76 79 public static final int DEFAULT_MAX = 74; 80 83 public static final int DEFAULT_STEP = 2; 84 87 public static final String DEFAULT_DELIM = "(+"; 88 91 public static final String DEFAULT_S_DELIM = ","; 92 93 private int maxLineLength = DEFAULT_MAX; 95 private int indentationStep = DEFAULT_STEP; 96 private String delimiters = DEFAULT_DELIM; 97 private String stickyDelimiters = DEFAULT_S_DELIM; 98 99 private int indent; 101 private int hangingIndent; 102 private int origIndent; 103 private boolean inCPP_Comment; 104 105 private void addTok(StringBuffer targetBuf, StringBuffer tokBuf, 106 IndentWriter out) 107 { 108 int tokLength = tokBuf.length(), 109 targetLength = targetBuf.length(); 110 111 if (indent + targetLength + tokLength > maxLineLength) 112 { 113 if (targetLength == 0) 114 { 115 out.println(indent, tokBuf.toString()); 116 indent = hangingIndent; 117 targetBuf.setLength(0); 118 119 return; 120 } 121 else 122 { 123 out.println(indent, targetBuf.toString().trim()); 124 indent = hangingIndent; 125 targetBuf.setLength(0); 126 } 127 } 128 129 targetBuf.append(tokBuf.toString()); 130 131 return; 132 } 133 140 public void formatCode(Reader source, Writer target) 141 { 142 String line; 143 BufferedReader in = new BufferedReader(source); 144 IndentWriter out = new IndentWriter(new BufferedWriter(target), true); 145 146 try 147 { 148 origIndent = 0; 149 inCPP_Comment = false; 150 151 while ((line = in.readLine()) != null) 152 { 153 line = line.trim(); 154 155 if (line.length() > 0) 156 { 157 indent = origIndent; 158 hangingIndent = indent + indentationStep; 159 printLine(line, out); 160 } 161 else 162 out.println(); 163 } 164 } 165 catch (IOException e) 166 { 167 e.printStackTrace(); 168 } 169 } 170 176 public String getDelimiters() 177 { 178 return delimiters; 179 } 180 186 public int getIndentationStep() 187 { 188 return indentationStep; 189 } 190 196 public int getMaxLineLength() 197 { 198 return maxLineLength; 199 } 200 206 public String getStickyDelimiters() 207 { 208 return stickyDelimiters; 209 } 210 private void printLine(String line, IndentWriter out) 211 { 212 char[] source = line.toCharArray(); 213 char ch; 214 char quoteChar = ' '; 215 boolean inEscapeSequence = false; 216 boolean inString = false; 217 StringBuffer tokBuf = new StringBuffer (), 218 targetBuf = new StringBuffer (hangingIndent + line.length()); 219 220 for (int i = 0; i < source.length; i++) 221 { 222 ch = source[i]; 223 224 if (inEscapeSequence) 225 { 226 tokBuf.append(ch); 227 inEscapeSequence = false; 228 } 229 else 230 { 231 if (inString) 232 { 233 switch (ch) 234 { 235 case '\\' : 236 tokBuf.append('\\'); 237 inEscapeSequence = true; 238 break; 239 case '\'' : 240 case '\"' : 241 tokBuf.append(ch); 242 243 if (ch == quoteChar) 244 { 245 addTok(targetBuf, tokBuf, out); 246 tokBuf.setLength(0); 247 inString = false; 248 } 249 break; 250 case 9 : tokBuf.append(ch); 252 break; 253 default : 254 if (ch > 31) 255 tokBuf.append(ch); 256 break; 257 } 258 } 259 else { 261 if (inCPP_Comment) 262 { 263 tokBuf.append(ch); 264 265 if (ch == '/' && i > 0 && source[i - 1] == '*') 266 inCPP_Comment = false; 267 } 268 else 269 { 270 switch (ch) 271 { 272 case '/' : 273 tokBuf.append(ch); 274 275 if (i > 0 && source[i - 1] == '/') 276 { 277 String tokStr = tokBuf.append(source, 278 i + 1, 279 source.length - (i + 1)).toString(); 280 281 out.println(indent, targetBuf.append(tokStr).toString()); 282 283 return; 284 } 285 break; 286 case '*' : 287 tokBuf.append(ch); 288 289 if (i > 0 && source[i - 1] == '/') 290 inCPP_Comment = true; 291 break; 292 case '\'' : 293 case '\"' : 294 addTok(targetBuf, tokBuf, out); 295 tokBuf.setLength(0); 296 tokBuf.append(ch); 297 quoteChar = ch; 298 inString = true; 299 break; 300 case 9 : tokBuf.append(StringUtils.getChars(indentationStep, ' ')); 302 break; 303 case '{' : 304 tokBuf.append(ch); 305 origIndent += indentationStep; 306 break; 307 case '}' : 308 tokBuf.append(ch); 309 origIndent -= indentationStep; 310 311 if (i == 0) 312 indent = origIndent; 313 break; 314 default : 315 if (ch > 31) 316 { 317 if (delimiters.indexOf(ch) != -1) 318 { 319 addTok(targetBuf, tokBuf, out); 320 tokBuf.setLength(0); 321 tokBuf.append(ch); 322 } 323 else if (stickyDelimiters.indexOf(ch) != -1) 324 { 325 tokBuf.append(ch); 326 addTok(targetBuf, tokBuf, out); 327 tokBuf.setLength(0); 328 } 329 else 330 tokBuf.append(ch); 331 } 332 break; 333 } 334 } 335 } 336 } 337 } 338 339 if (tokBuf.length() > 0) 340 addTok(targetBuf, tokBuf, out); 341 342 String lastLine = targetBuf.toString().trim(); 343 344 if (lastLine.length() > 0) 345 out.println(indent, lastLine); 346 } 347 359 public void setDelimiters(String newDelimiters) 360 { 361 delimiters = newDelimiters; 362 } 363 371 public void setIndentationStep(int newIndentationStep) 372 { 373 indentationStep = (newIndentationStep < 0 ? 0 : newIndentationStep); 374 } 375 386 public void setMaxLineLength(int newMaxLineLength) 387 { 388 maxLineLength = (newMaxLineLength < 0 ? 0 : newMaxLineLength); 389 } 390 402 public void setStickyDelimiters(String newStickyDelimiters) 403 { 404 stickyDelimiters = newStickyDelimiters; 405 } 406 } 407 | Popular Tags |