aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/property/ParseProperties.java
blob: f03f966e2da45a2a61e945b1bcd243b46cfed81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.property;

import java.text.ParsePosition;
import java.util.Collection;

import org.apache.tools.ant.Project;

/**
 * Parse properties using a collection of expanders.
 *
 * @since Ant 1.8.0
 */
public class ParseProperties implements ParseNextProperty {

    private final Project project;
    private final GetProperty getProperty;
    private final Collection<PropertyExpander> expanders;

    /**
     * Constructor with a getProperty.
     * @param project the current Ant project.
     * @param expanders a sequence of expanders
     * @param getProperty property resolver.
     */
    public ParseProperties(Project project, Collection<PropertyExpander> expanders, GetProperty getProperty) {
        this.project = project;
        this.expanders = expanders;
        this.getProperty = getProperty;
    }

    /**
     * Get the project.
     * @return the current Ant project.
     */
    public Project getProject() {
        return project;
    }

    /**
     * Decode properties from a String representation.
     *
     * <ul>
     *
     *  <li>This implementation starts parsing the <code>value</code>
     *  parameter (unsurprisingly) at the beginning and asks each
     *  {@link PropertyExpander PropertyExpander} whether there is a
     *  property reference at that point.  PropertyExpanders return
     *  the name of a property they may find and may advance the parse
     *  position.</li>
     *
     *  <li>If the PropertyExpander returns <code>null</code> the
     *  method continues with the next PropertyExpander, otherwise it
     *  tries to look up the property's value using the configured
     *  {@link GetProperty GetProperty} instance.</li>
     *
     *  <li>Once all PropertyExpanders have been consulted, the parse
     *  position is advanced by one character and the process repeated
     *  until <code>value</code> is exhausted.</li>
     *
     * </ul>
     *
     * <p>If the entire contents of <code>value</code> resolves to a
     * single property, the looked up property value is returned.
     * Otherwise a String is returned that concatenates the
     * non-property parts of <code>value</code> and the expanded
     * values of the properties that have been found.</p>
     *
     * @param value The string to be scanned for property references.
     *              May be <code>null</code>, in which case this
     *              method returns immediately with no effect.
     *
     * @return the original string with the properties replaced, or
     *         <code>null</code> if the original string is <code>null</code>.
     */
    public Object parseProperties(String value) {
        if (value == null || "".equals(value)) {
            return value;
        }
        final int len = value.length();
        ParsePosition pos = new ParsePosition(0);
        Object o = parseNextProperty(value, pos);
        if (o != null && pos.getIndex() >= len) {
            return o;
        }
        StringBuffer sb = new StringBuffer(len * 2);
        if (o == null) {
            sb.append(value.charAt(pos.getIndex()));
            pos.setIndex(pos.getIndex() + 1);
        } else {
            sb.append(o);
        }
        while (pos.getIndex() < len) {
            o = parseNextProperty(value, pos);
            if (o == null) {
                sb.append(value.charAt(pos.getIndex()));
                pos.setIndex(pos.getIndex() + 1);
            } else {
                sb.append(o);
            }
        }
        return sb.toString();
    }

    /**
     * Learn whether a String contains replaceable properties.
     *
     * <p>Uses the configured {@link PropertyExpander
     *  PropertyExpanders} and scans through the string.  Returns true
     *  as soon as any expander finds a property.</p>
     *
     * @param value the String to check.
     * @return <code>true</code> if <code>value</code> contains property notation.
     */
    public boolean containsProperties(String value) {
        if (value == null) {
            return false;
        }
        final int len = value.length();
        for (ParsePosition pos = new ParsePosition(0); pos.getIndex() < len;) {
            if (parsePropertyName(value, pos) != null) {
                return true;
            }
            pos.setIndex(pos.getIndex() + 1);
        }
        return false;
    }

    /**
     * Return any property that can be parsed from the specified position
     * in the specified String.
     *
     * <p>Uses the configured {@link PropertyExpander
     *  PropertyExpanders} and {@link GetProperty GetProperty}
     *  instance .</p>
     *
     * @param value String to parse
     * @param pos ParsePosition
     * @return Object or null if no property is at the current
     * location.  If a property reference has been found but the
     * property doesn't expand to a value, the property's name is
     * returned.
     */
    public Object parseNextProperty(String value, ParsePosition pos) {
        final int start = pos.getIndex();

        if (start > value.length()) {
            // early exit, can't find any property here, no need to
            // consult all the delegates.
            return null;
        }

        String propertyName = parsePropertyName(value, pos);
        if (propertyName != null) {
            Object result = getProperty(propertyName);
            if (result != null) {
                return result;
            }
            if (project != null) {
                project.log(
                    "Property \"" + propertyName
                    + "\" has not been set", Project.MSG_VERBOSE);
            }
            return value.substring(start, pos.getIndex());
        }
        return null;
    }

    private String parsePropertyName(String value, ParsePosition pos) {
        for (PropertyExpander propertyExpander : expanders) {
            String propertyName = propertyExpander.parsePropertyName(value, pos, this);
            if (propertyName == null) {
                continue;
            }
            return propertyName;
        }
        return null;
    }

    private Object getProperty(String propertyName) {
        return getProperty.getProperty(propertyName);
    }
}