1 | package ise.antelope.tasks;
|
---|
2 |
|
---|
3 | import java.util.*;
|
---|
4 | import java.util.regex.*;
|
---|
5 |
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Borrowed from Antelope, modified to be a general purpose class instead of
|
---|
9 | * an Ant task.
|
---|
10 | *
|
---|
11 | * @version $Revision: 1.1 $
|
---|
12 | */
|
---|
13 | public class Grep {
|
---|
14 |
|
---|
15 | private String findIn = null;
|
---|
16 | private String regex = null;
|
---|
17 | private int group = 0;
|
---|
18 | private boolean dotall = false;
|
---|
19 | private boolean caseInsensitive = false;
|
---|
20 | private boolean multiLine = false;
|
---|
21 | private boolean unicodeCase = false;
|
---|
22 | private boolean canonEq = false;
|
---|
23 | private boolean comments = false;
|
---|
24 | private boolean unixLines = false;
|
---|
25 | private boolean allMatches = false;
|
---|
26 | private String separator = System.getProperty("line.separator");
|
---|
27 |
|
---|
28 | private String grep_match = null;
|
---|
29 | private int count = 0;
|
---|
30 |
|
---|
31 | private List matches = new ArrayList();
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Where to look.
|
---|
35 | *
|
---|
36 | * @param string The new in value
|
---|
37 | */
|
---|
38 | public void setIn(String string) {
|
---|
39 | findIn = string;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * What to look for.
|
---|
44 | *
|
---|
45 | * @param regex The new regex value
|
---|
46 | */
|
---|
47 | public void setRegex(String regex) {
|
---|
48 | this.regex = regex;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Set a specific group from the regex.
|
---|
53 | *
|
---|
54 | * @param g The new group value
|
---|
55 | */
|
---|
56 | public void setGroup(int g) {
|
---|
57 | group = g;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Sets the dotall attribute for the regex.
|
---|
62 | *
|
---|
63 | * @param b The new dotall value
|
---|
64 | */
|
---|
65 | public void setDotall(boolean b) {
|
---|
66 | dotall = b;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Sets the caseinsensitive attribute for the regex.
|
---|
71 | *
|
---|
72 | * @param b The new caseinsensitive value
|
---|
73 | */
|
---|
74 | public void setCaseinsensitive(boolean b) {
|
---|
75 | caseInsensitive = b;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Sets the multiline attribute for the regex.
|
---|
80 | *
|
---|
81 | * @param b The new multiline value
|
---|
82 | */
|
---|
83 | public void setMultiline(boolean b) {
|
---|
84 | multiLine = b;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Sets the unicodecase attribute for the regex.
|
---|
89 | *
|
---|
90 | * @param b The new unicodecase value
|
---|
91 | */
|
---|
92 | public void setUnicodecase(boolean b) {
|
---|
93 | unicodeCase = b;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Sets the canoneq attribute for the regex.
|
---|
98 | *
|
---|
99 | * @param b The new canoneq value
|
---|
100 | */
|
---|
101 | public void setCanoneq(boolean b) {
|
---|
102 | canonEq = b;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Sets the comments attribute for the regex.
|
---|
107 | *
|
---|
108 | * @param b The new comments value
|
---|
109 | */
|
---|
110 | public void setComments(boolean b) {
|
---|
111 | comments = b;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void setUnixlines(boolean b) {
|
---|
115 | unixLines = b;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * If true, concatentates all matches into a single result, if false, only
|
---|
120 | * the first match is returned in the result.
|
---|
121 | *
|
---|
122 | * @param b default is false, only show the first match.
|
---|
123 | */
|
---|
124 | public void setAllmatches(boolean b) {
|
---|
125 | allMatches = b;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * @return the count of the matches found by the regular expression in
|
---|
130 | * the string
|
---|
131 | */
|
---|
132 | public int getCount() {
|
---|
133 | return matches.size();
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * @return the match found by the regular expression in the string. If
|
---|
138 | * 'all matches' was set to true, then each match will be contained in
|
---|
139 | * this string, separated by the separator specified in <code>setSeparator</code>
|
---|
140 | */
|
---|
141 | public String getMatch() {
|
---|
142 | return grep_match;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * @return an Iterator over all matches found by the regular expression.
|
---|
147 | */
|
---|
148 | public Iterator getMatches() {
|
---|
149 | return matches.iterator();
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * @return if there are multiple matches, return the <code>i</code><sup>th</sup> match.
|
---|
154 | */
|
---|
155 | public String getMatch(int i) {
|
---|
156 | return (i < 0 || i >= matches.size()) ? null : (String)matches.get(i);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Used in conjunction with <code>setAllmatches</code>, this string will be
|
---|
161 | * placed between each match in the final result.
|
---|
162 | *
|
---|
163 | * @param s the separator, default is "".
|
---|
164 | */
|
---|
165 | public void setSeparator(String s) {
|
---|
166 | separator = s;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** Do the grep */
|
---|
170 | public String grep() {
|
---|
171 | // check attributes
|
---|
172 | if (findIn == null)
|
---|
173 | throw new IllegalArgumentException("'in' is required");
|
---|
174 | if (regex == null)
|
---|
175 | throw new IllegalArgumentException("'regex' is required");
|
---|
176 |
|
---|
177 | // set flags for pattern
|
---|
178 | int flags = 0;
|
---|
179 | if (dotall)
|
---|
180 | flags += Pattern.DOTALL;
|
---|
181 | if (caseInsensitive)
|
---|
182 | flags += Pattern.CASE_INSENSITIVE;
|
---|
183 | if (multiLine)
|
---|
184 | flags += Pattern.MULTILINE;
|
---|
185 | if (unicodeCase)
|
---|
186 | flags += Pattern.UNICODE_CASE;
|
---|
187 | if (canonEq)
|
---|
188 | flags += Pattern.CANON_EQ;
|
---|
189 | if (comments)
|
---|
190 | flags += Pattern.COMMENTS;
|
---|
191 | if (unixLines)
|
---|
192 | flags += Pattern.UNIX_LINES;
|
---|
193 |
|
---|
194 | try {
|
---|
195 | Pattern p = Pattern.compile(regex, flags);
|
---|
196 | Matcher m = p.matcher(findIn);
|
---|
197 | StringBuffer result = new StringBuffer();
|
---|
198 | matches = new ArrayList();
|
---|
199 | if (allMatches) {
|
---|
200 | while (m.find()) {
|
---|
201 | String match = m.group(group);
|
---|
202 | if (match != null) {
|
---|
203 | result.append(match).append(separator);
|
---|
204 | matches.add(match);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 | else if (m.find()) {
|
---|
209 | String match = m.group(group);
|
---|
210 | if (match != null) {
|
---|
211 | result.append(match);
|
---|
212 | matches.add(match);
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | grep_match = result.length() > 0 ? result.toString() : null;
|
---|
217 | return grep_match;
|
---|
218 | }
|
---|
219 | catch (Exception e) {
|
---|
220 | throw new RuntimeException(e.getMessage());
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|