Debian Codegraph @

src/main/java/net/sf/antcontrib/property/RegexTask.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.property;
17
18 import java.util.Vector;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.types.RegularExpression;
22 import org.apache.tools.ant.types.Substitution;
23 import org.apache.tools.ant.util.regexp.Regexp;
24
25 /****************************************************************************
26 * Place class description here.
27 *
28 * @author <a href='mailto:[email protected]'>Matthew Inger</a>
29 * @author <additional author>
30 *
31 * @since
32 *
33 ****************************************************************************/
34
35
36 public class RegexTask
37 extends AbstractPropertySetterTask
38 {
39 private String input;
40
41 private RegularExpression regexp;
42 private String select;
43 private Substitution replace;
44 private String defaultValue;
45
46 private boolean caseSensitive = true;
47 private boolean global = true;
48
49 public RegexTask()
50 {
51 super();
52 }
53
54 public void setInput(String input)
55 {
56 this.input = input;
57 }
58
59 public void setDefaultValue(String defaultValue)
60 {
61 this.defaultValue = defaultValue;
62 }
63
64 public void setRegexp(String regex)
65 {
66 if (this.regexp != null)
67 throw new BuildException("Cannot specify more than one regular expression");
68
69 this.regexp = new RegularExpression();
70 this.regexp.setPattern(regex);
71 }
72
73
74 public RegularExpression createRegexp()
75 {
76 if (this.regexp != null)
77 throw new BuildException("Cannot specify more than one regular expression");
78 regexp = new RegularExpression();
79 return regexp;
80 }
81
82 public void setReplace(String replace)
83 {
84 if (this.replace != null)
85 throw new BuildException("Cannot specify more than one replace expression");
86 if (select != null)
87 throw new BuildException("You cannot specify both a select and replace expression");
88 this.replace = new Substitution();
89 this.replace.setExpression(replace);
90 }
91
92 public Substitution createReplace()
93 {
94 if (replace != null)
95 throw new BuildException("Cannot specify more than one replace expression");
96 if (select != null)
97 throw new BuildException("You cannot specify both a select and replace expression");
98 replace = new Substitution();
99 return replace;
100 }
101
102 public void setSelect(String select)
103 {
104 if (replace != null)
105 throw new BuildException("You cannot specify both a select and replace expression");
106 this.select = select;
107 }
108
109 public void setCaseSensitive(boolean caseSensitive)
110 {
111 this.caseSensitive = caseSensitive;
112 }
113
114 public void setGlobal(boolean global)
115 {
116 this.global = global;
117 }
118
119 protected String doReplace()
120 throws BuildException
121 {
122 if (replace == null)
123 throw new BuildException("No replace expression specified.");
124
125 int options = 0;
126 if (! caseSensitive)
127 options |= Regexp.MATCH_CASE_INSENSITIVE;
128 if (global)
129 options |= Regexp.REPLACE_ALL;
130
131 Regexp sregex = regexp.getRegexp(project);
132
133 String output = null;
134
135 if (sregex.matches(input, options)) {
136 String expression = replace.getExpression(project);
137 output = sregex.substitute(input,
138 expression,
139 options);
140 }
141
142 if (output == null)
143 output = defaultValue;
144
145 return output;
146 }
147
148 protected String doSelect()
149 throws BuildException
150 {
151 int options = 0;
152 if (! caseSensitive)
153 options |= Regexp.MATCH_CASE_INSENSITIVE;
154
155 Regexp sregex = regexp.getRegexp(project);
156
157 String output = select;
158 Vector groups = sregex.getGroups(input, options);
159
160 if (groups != null && groups.size() > 0)
161 {
162 output = RegexUtil.select(select, groups);
163 }
164 else
165 {
166 output = null;
167 }
168
169 if (output == null)
170 output = defaultValue;
171
172 return output;
173 }
174
175
176 protected void validate()
177 {
178 super.validate();
179 if (regexp == null)
180 throw new BuildException("No match expression specified.");
181 if (replace == null && select == null)
182 throw new BuildException("You must specify either a replace or select expression");
183 }
184
185 public void execute()
186 throws BuildException
187 {
188 validate();
189
190 String output = input;
191 if (replace != null)
192 output = doReplace();
193 else
194 output = doSelect();
195
196 if (output != null)
197 setPropertyValue(output);
198 }
199 }