View Javadoc

1   package org.kuali.common.jdbc;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.util.Arrays;
6   import java.util.List;
7   
8   import org.apache.commons.lang3.StringUtils;
9   import org.springframework.util.Assert;
10  
11  public class DefaultSqlReader implements SqlReader {
12  
13  	public static final String DEFAULT_DELIMITER = "/";
14  	public static final DelimiterMode DEFAULT_DELIMITER_MODE = DelimiterMode.OWN_LINE;
15  	public static final LineSeparator DEFAULT_LINE_SEPARATOR = LineSeparator.LF;
16  	public static final List<String> DEFAULT_COMMENT_TOKENS = Arrays.asList(new String[] { "#", "--" });
17  	public static final boolean DEFAULT_IS_TRIM = true;
18  	public static final boolean DEFAULT_IS_IGNORE_COMMENTS = true;
19  
20  	String delimiter = DEFAULT_DELIMITER;
21  	DelimiterMode delimiterMode = DEFAULT_DELIMITER_MODE;
22  	LineSeparator lineSeparator = DEFAULT_LINE_SEPARATOR;
23  	boolean trim = DEFAULT_IS_TRIM;
24  	boolean ignoreComments = DEFAULT_IS_IGNORE_COMMENTS;
25  	List<String> commentTokens = DEFAULT_COMMENT_TOKENS;
26  
27  	@Override
28  	public String getSqlStatement(BufferedReader reader) throws IOException {
29  		Assert.notNull(delimiter, "delimiter is null");
30  		String line = reader.readLine();
31  		String trimmedLine = StringUtils.trimToNull(line);
32  		StringBuilder sb = new StringBuilder();
33  		while (proceed(line, trimmedLine, delimiter, delimiterMode)) {
34  			if (!ignore(ignoreComments, sb, trimmedLine, commentTokens)) {
35  				sb.append(line + lineSeparator.getValue());
36  			}
37  			line = reader.readLine();
38  			trimmedLine = StringUtils.trimToNull(line);
39  		}
40  		return getReturnValue(sb.toString(), trim, lineSeparator);
41  	}
42  
43  	protected String getReturnValue(String sql, boolean trim, LineSeparator lineSeparator) {
44  		if (trim) {
45  			sql = StringUtils.trimToNull(sql);
46  		}
47  		if (sql == null) {
48  			return null;
49  		} else if (StringUtils.endsWith(sql, lineSeparator.getValue())) {
50  			int endIndex = sql.length() - lineSeparator.getValue().length();
51  			return StringUtils.substring(sql, 0, endIndex);
52  		} else {
53  			return sql;
54  		}
55  	}
56  
57  	protected boolean isEndOfSqlStatement(String trimmedLine, String delimiter, DelimiterMode delimiterMode) {
58  		switch (delimiterMode) {
59  		case END_OF_LINE:
60  			return StringUtils.endsWith(trimmedLine, delimiter);
61  		case OWN_LINE:
62  			return StringUtils.equals(trimmedLine, delimiter);
63  		default:
64  			throw new IllegalArgumentException("Delimiter mode '" + delimiterMode + "' is unknown");
65  		}
66  	}
67  
68  	protected boolean proceed(String line, String trimmedLine, String delimiter, DelimiterMode delimiterMode) {
69  		return line != null && !isEndOfSqlStatement(trimmedLine, delimiter, delimiterMode);
70  	}
71  
72  	protected boolean ignore(boolean ignoreComments, StringBuilder sql, String trimmedLine, List<String> commentTokens) {
73  		return ignoreComments && StringUtils.isBlank(sql.toString()) && isSqlComment(trimmedLine, commentTokens);
74  	}
75  
76  	protected boolean isSqlComment(String trimmedLine, List<String> commentTokens) {
77  		for (String commentToken : commentTokens) {
78  			if (StringUtils.startsWith(trimmedLine, commentToken)) {
79  				return true;
80  			}
81  		}
82  		return false;
83  	}
84  
85  	public String getDelimiter() {
86  		return delimiter;
87  	}
88  
89  	public void setDelimiter(String delimiter) {
90  		this.delimiter = delimiter;
91  	}
92  
93  	public boolean isTrim() {
94  		return trim;
95  	}
96  
97  	public void setTrim(boolean trim) {
98  		this.trim = trim;
99  	}
100 
101 	public boolean isIgnoreComments() {
102 		return ignoreComments;
103 	}
104 
105 	public void setIgnoreComments(boolean ignoreComments) {
106 		this.ignoreComments = ignoreComments;
107 	}
108 
109 	public LineSeparator getLineSeparator() {
110 		return lineSeparator;
111 	}
112 
113 	public void setLineSeparator(LineSeparator lineSeparator) {
114 		this.lineSeparator = lineSeparator;
115 	}
116 
117 	public DelimiterMode getDelimiterMode() {
118 		return delimiterMode;
119 	}
120 
121 	public void setDelimiterMode(DelimiterMode delimiterMode) {
122 		this.delimiterMode = delimiterMode;
123 	}
124 
125 	public List<String> getCommentTokens() {
126 		return commentTokens;
127 	}
128 
129 	public void setCommentTokens(List<String> commentTokens) {
130 		this.commentTokens = commentTokens;
131 	}
132 
133 }