source: release-kits/lirk3/ant-scripts/tasks/antelope/src/ise/antelope/tasks/Find.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 5.2 KB
Line 
1package ise.antelope.tasks;
2
3import java.util.regex.*;
4
5import org.apache.tools.ant.BuildException;
6
7import org.apache.tools.ant.Task;
8
9/**
10 * Copyright 2003
11 *
12 * @version $Revision: 1.2 $
13 */
14public class Find extends Task {
15
16 private String findIn = null;
17 private String regex = null;
18 private String property = null;
19 private int group = 0;
20 private boolean dotall = false;
21 private boolean caseInsensitive = false;
22 private boolean multiLine = false;
23 private boolean unicodeCase = false;
24 private boolean canonEq = false;
25 private boolean comments = false;
26 private boolean unixLines = false;
27 private boolean allMatches = false;
28 private String separator = System.getProperty("line.separator");
29
30 /**
31 * Where to look.
32 *
33 * @param string The new in value
34 */
35 public void setIn(String string) {
36 findIn = string;
37 }
38
39 /**
40 * What to look for.
41 *
42 * @param regex The new regex value
43 */
44 public void setRegex(String regex) {
45 this.regex = regex;
46 }
47
48 /**
49 * Where to put the results of the search. If 'allmatches' is true, then
50 * a second property with this name plus "_count" will be created with the
51 * number of matches found.
52 *
53 * @param name The new property value
54 */
55 public void setProperty(String name) {
56 property = name;
57 }
58
59 /**
60 * Set a specific group from the regex.
61 *
62 * @param g The new group value
63 */
64 public void setGroup(int g) {
65 group = g;
66 }
67
68 /**
69 * Sets the dotall attribute for the regex.
70 *
71 * @param b The new dotall value
72 */
73 public void setDotall(boolean b) {
74 dotall = b;
75 }
76
77 /**
78 * Sets the caseinsensitive attribute for the regex.
79 *
80 * @param b The new caseinsensitive value
81 */
82 public void setCaseinsensitive(boolean b) {
83 caseInsensitive = b;
84 }
85
86 /**
87 * Sets the multiline attribute for the regex.
88 *
89 * @param b The new multiline value
90 */
91 public void setMultiline(boolean b) {
92 multiLine = b;
93 }
94
95 /**
96 * Sets the unicodecase attribute for the regex.
97 *
98 * @param b The new unicodecase value
99 */
100 public void setUnicodecase(boolean b) {
101 unicodeCase = b;
102 }
103
104 /**
105 * Sets the canoneq attribute for the regex.
106 *
107 * @param b The new canoneq value
108 */
109 public void setCanoneq(boolean b) {
110 canonEq = b;
111 }
112
113 /**
114 * Sets the comments attribute for the regex.
115 *
116 * @param b The new comments value
117 */
118 public void setComments(boolean b) {
119 comments = b;
120 }
121
122 public void setUnixlines(boolean b) {
123 unixLines = b;
124 }
125
126 /**
127 * If true, concatentates all matches into a single result, if false, only
128 * the first match is returned in the result.
129 *
130 * @param b default is false, only show the first match.
131 */
132 public void setAllmatches(boolean b) {
133 allMatches = b;
134 }
135
136 /**
137 * Uses in conjunction with <code>setAllmatches</code>, this string will be
138 * placed between each match in the final result.
139 *
140 * @param s the separator, default is "".
141 */
142 public void setSeparator(String s) {
143 separator = s;
144 }
145
146 /** Do the grep */
147 public void execute() {
148 // check attributes
149 if (findIn == null)
150 throw new BuildException("'in' is required");
151 if (regex == null)
152 throw new BuildException("'regex' is required");
153 if (property == null)
154 throw new BuildException("'property' is required");
155
156 // set flags for pattern
157 int flags = 0;
158 if (dotall)
159 flags += Pattern.DOTALL;
160 if (caseInsensitive)
161 flags += Pattern.CASE_INSENSITIVE;
162 if (multiLine)
163 flags += Pattern.MULTILINE;
164 if (unicodeCase)
165 flags += Pattern.UNICODE_CASE;
166 if (canonEq)
167 flags += Pattern.CANON_EQ;
168 if (comments)
169 flags += Pattern.COMMENTS;
170 if (unixLines)
171 flags += Pattern.UNIX_LINES;
172
173 try {
174 Pattern p = Pattern.compile(regex, flags);
175 Matcher m = p.matcher(findIn);
176 StringBuffer result = new StringBuffer();
177 int count = 0;
178 if (allMatches) {
179 while (m.find()) {
180 String match = m.group(group);
181 if (match != null) {
182 result.append(match).append(separator);
183 ++ count;
184 }
185 }
186 }
187 else if (m.find()) {
188 String match = m.group(group);
189 if (match != null)
190 result.append(match);
191 }
192
193 if (result.length() > 0) {
194 getProject().setUserProperty(property, result.toString());
195 if (allMatches)
196 getProject().setUserProperty(property + "_count", String.valueOf(count));
197 }
198 else
199 log("No match.");
200 }
201 catch (Exception e) {
202 throw new BuildException(e.getMessage());
203 }
204 }
205}
206
Note: See TracBrowser for help on using the repository browser.