1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.sql;
17
18 import org.codehaus.plexus.util.StringUtils;
19
20
21
22
23 public class SqlSplitter {
24
25
26
27 public static final int NO_END = -1;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public static int containsSqlEnd(String line, String delimiter) {
43
44 boolean isComment = false;
45
46 boolean isAlphaDelimiter = StringUtils.isAlpha(delimiter);
47
48 if (line == null || line.length() == 0) {
49 return NO_END;
50 }
51
52 int pos = 0;
53
54 do {
55 if (isComment) {
56 if (line.startsWith("*/", pos)) {
57 isComment = false;
58 } else {
59 pos++;
60 continue;
61 }
62 }
63
64 if (line.startsWith("/*", pos)) {
65 isComment = true;
66 pos += 2;
67 continue;
68 }
69
70 if (line.startsWith("--", pos)) {
71 return NO_END;
72 }
73
74 if (line.startsWith("'", pos) || line.startsWith("\"", pos)) {
75 String quoteChar = "" + line.charAt(pos);
76 String quoteEscape = "\\" + quoteChar;
77 pos++;
78
79 if (line.length() <= pos) {
80 return NO_END;
81 }
82
83 do {
84 if (line.startsWith(quoteEscape, pos)) {
85 pos += 2;
86 }
87 } while (!line.startsWith(quoteChar, pos++));
88
89 continue;
90 }
91
92 if (line.startsWith(delimiter, pos)) {
93 if (isAlphaDelimiter) {
94
95
96 if ((pos == 0 || !isAlpha(line.charAt(pos - 1)))
97 && (line.length() == pos + delimiter.length() || !isAlpha(line
98 .charAt(pos + delimiter.length())))) {
99 return pos + delimiter.length();
100 }
101 } else {
102 return pos + delimiter.length();
103 }
104 }
105
106 pos++;
107
108 } while (line.length() >= pos);
109
110 return NO_END;
111 }
112
113
114
115
116
117
118
119 private static boolean isAlpha(char c) {
120 return Character.isUpperCase(c) || Character.isLowerCase(c);
121 }
122
123 }