2
* Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package net.sf.antcontrib.property;
18
import java.util.Enumeration;
19
import java.util.Hashtable;
20
import java.util.Vector;
22
import org.apache.tools.ant.BuildException;
23
import org.apache.tools.ant.types.RegularExpression;
24
import org.apache.tools.ant.util.regexp.Regexp;
27
/****************************************************************************
28
* Place class description here.
30
* @author <a href='mailto:mattinger@yahoo.com'>Matthew Inger</a>
31
* @author <additional author>
35
****************************************************************************/
38
public class PropertySelector
39
extends AbstractPropertySetterTask
41
private RegularExpression match;
42
private String select = "\\0";
43
private char delim = ',';
44
private boolean caseSensitive = true;
45
private boolean distinct = false;
48
public PropertySelector()
54
public void setMatch(String match)
56
this.match = new RegularExpression();
57
this.match.setPattern(match);
61
public void setSelect(String select)
67
public void setCaseSensitive(boolean caseSensitive)
69
this.caseSensitive = caseSensitive;
73
public void setDelimiter(char delim)
79
public void setDistinct(boolean distinct)
81
this.distinct = distinct;
85
protected void validate()
89
throw new BuildException("No match expression specified.");
100
options |= Regexp.MATCH_CASE_INSENSITIVE;
102
Regexp regex = match.getRegexp(project);
103
Hashtable props = project.getProperties();
104
Enumeration e = props.keys();
105
StringBuffer buf = new StringBuffer();
108
Vector used = new Vector();
110
while (e.hasMoreElements())
112
String key = (String) (e.nextElement());
113
if (regex.matches(key, options))
115
String output = select;
116
Vector groups = regex.getGroups(key, options);
117
int sz = groups.size();
118
for (int i = 0; i < sz; i++)
120
String s = (String) (groups.elementAt(i));
122
RegularExpression result = null;
123
result = new RegularExpression();
124
result.setPattern("\\\\" + i);
125
Regexp sregex = result.getRegexp(project);
126
output = sregex.substitute(output, s, Regexp.MATCH_DEFAULT);
129
if (!(distinct && used.contains(output)))
131
used.addElement(output);
132
if (cnt != 0) buf.append(delim);
139
if (buf.length() > 0)
140
setPropertyValue(buf.toString());