~ubuntu-branches/ubuntu/raring/ant-contrib/raring

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/antcontrib/property/PropertySelector.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-09-26 08:45:47 UTC
  • Revision ID: james.westby@ubuntu.com-20090926084547-ynj34y27mg9dr60c
Tags: upstream-1.0~b3+svn177
ImportĀ upstreamĀ versionĀ 1.0~b3+svn177

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.Enumeration;
 
19
import java.util.Hashtable;
 
20
import java.util.Vector;
 
21
 
 
22
import org.apache.tools.ant.BuildException;
 
23
import org.apache.tools.ant.types.RegularExpression;
 
24
import org.apache.tools.ant.util.regexp.Regexp;
 
25
 
 
26
 
 
27
/****************************************************************************
 
28
 * Place class description here.
 
29
 *
 
30
 * @author <a href='mailto:mattinger@yahoo.com'>Matthew Inger</a>
 
31
 * @author              <additional author>
 
32
 *
 
33
 * @since
 
34
 *
 
35
 ****************************************************************************/
 
36
 
 
37
 
 
38
public class PropertySelector
 
39
        extends AbstractPropertySetterTask
 
40
{
 
41
    private RegularExpression match;
 
42
    private String select = "\\0";
 
43
    private char delim = ',';
 
44
    private boolean caseSensitive = true;
 
45
    private boolean distinct = false;
 
46
 
 
47
 
 
48
    public PropertySelector()
 
49
    {
 
50
        super();
 
51
    }
 
52
 
 
53
 
 
54
    public void setMatch(String match)
 
55
    {
 
56
        this.match = new RegularExpression();
 
57
        this.match.setPattern(match);
 
58
    }
 
59
 
 
60
 
 
61
    public void setSelect(String select)
 
62
    {
 
63
        this.select = select;
 
64
    }
 
65
 
 
66
 
 
67
    public void setCaseSensitive(boolean caseSensitive)
 
68
    {
 
69
        this.caseSensitive = caseSensitive;
 
70
    }
 
71
 
 
72
 
 
73
    public void setDelimiter(char delim)
 
74
    {
 
75
        this.delim = delim;
 
76
    }
 
77
 
 
78
 
 
79
    public void setDistinct(boolean distinct)
 
80
    {
 
81
        this.distinct = distinct;
 
82
    }
 
83
 
 
84
 
 
85
    protected void validate()
 
86
    {
 
87
        super.validate();
 
88
        if (match == null)
 
89
            throw new BuildException("No match expression specified.");
 
90
    }
 
91
 
 
92
 
 
93
    public void execute()
 
94
            throws BuildException
 
95
    {
 
96
        validate();
 
97
 
 
98
        int options = 0;
 
99
        if (!caseSensitive)
 
100
            options |= Regexp.MATCH_CASE_INSENSITIVE;
 
101
 
 
102
        Regexp regex = match.getRegexp(project);
 
103
        Hashtable props = project.getProperties();
 
104
        Enumeration e = props.keys();
 
105
        StringBuffer buf = new StringBuffer();
 
106
        int cnt = 0;
 
107
 
 
108
        Vector used = new Vector();
 
109
 
 
110
        while (e.hasMoreElements())
 
111
        {
 
112
            String key = (String) (e.nextElement());
 
113
            if (regex.matches(key, options))
 
114
            {
 
115
                String output = select;
 
116
                Vector groups = regex.getGroups(key, options);
 
117
                int sz = groups.size();
 
118
                for (int i = 0; i < sz; i++)
 
119
                {
 
120
                    String s = (String) (groups.elementAt(i));
 
121
 
 
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);
 
127
                }
 
128
 
 
129
                if (!(distinct && used.contains(output)))
 
130
                {
 
131
                    used.addElement(output);
 
132
                    if (cnt != 0) buf.append(delim);
 
133
                    buf.append(output);
 
134
                    cnt++;
 
135
                }
 
136
            }
 
137
        }
 
138
 
 
139
        if (buf.length() > 0)
 
140
            setPropertyValue(buf.toString());
 
141
    }
 
142
}