View Javadoc

1   package sharin.sql.formatter;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.List;
6   
7   public class BasicSqlFormatter implements SqlFormatter {
8   
9       private final List<BasicFormatCommand> commandList = new ArrayList<BasicFormatCommand>();
10  
11      public BasicSqlFormatter(String template) {
12          this(template, "'");
13      }
14  
15      public BasicSqlFormatter(String template, String escapeChars) {
16          int p = 0;
17          int q = 0;
18          int len = template.length();
19  
20          while (p < len) {
21              q = template.indexOf('\n', p);
22  
23              if (q == -1) {
24                  q = len;
25              }
26  
27              String line = template.substring(p, q);
28              BasicFormatCommand command = new BasicFormatCommand(line,
29                      escapeChars);
30              commandList.add(command);
31              p = q + 1;
32          }
33      }
34  
35      public String format(Object context) {
36  
37          if (context == null) {
38              context = Collections.emptyMap();
39          }
40  
41          BasicResultBuffer resultBuffer = new BasicResultBuffer();
42  
43          for (BasicFormatCommand command : commandList) {
44              command.execute(context, resultBuffer);
45          }
46  
47          return resultBuffer.getResult();
48      }
49  }