aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/DataType.java
blob: fda4af62a13deeaee20e8964dcce2287ab63f69f (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *  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.types;

import java.util.Stack;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ComponentHelper;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.util.IdentityStack;

/**
 * Base class for those classes that can appear inside the build file
 * as stand alone data types.
 *
 * <p>This class handles the common description attribute and provides
 * a default implementation for reference handling and checking for
 * circular references that is appropriate for types that can not be
 * nested inside elements of the same type (i.e. &lt;patternset&gt;
 * but not &lt;path&gt;).</p>
 *
 */
public abstract class DataType extends ProjectComponent implements Cloneable {
    // CheckStyle:VisibilityModifier OFF

    /**
     * Value to the refid attribute.
     *
     * @deprecated since 1.7.
     *             The user should not be directly referencing
     *             variable. Please use {@link #getRefid} instead.
     */
    protected Reference ref;

    /**
     * Are we sure we don't hold circular references?
     *
     * <p>Subclasses are responsible for setting this value to false
     * if we'd need to investigate this condition (usually because a
     * child element has been added that is a subclass of
     * DataType).</p>
     *
     * @deprecated since 1.7.
     *             The user should not be directly referencing
     *             variable. Please use {@link #setChecked} or
     *             {@link #isChecked} instead.
     */
    protected boolean checked = true;
    // CheckStyle:VisibilityModifier ON

    /**
     * Has the refid attribute of this element been set?
     * @return true if the refid attribute has been set
     */
    public boolean isReference() {
        return ref != null;
    }

    /**
     * Set the value of the refid attribute.
     *
     * <p>Subclasses may need to check whether any other attributes
     * have been set as well or child elements have been created and
     * thus override this method. if they do the must call
     * <code>super.setRefid</code>.</p>
     * @param ref the reference to use
     */
    public void setRefid(final Reference ref) {
        this.ref = ref;
        checked = false;
    }

    /**
     * Gets as descriptive as possible a name used for this datatype instance.
     * @return <code>String</code> name.
     */
    protected String getDataTypeName() {
        return ComponentHelper.getElementName(getProject(), this, true);
    }

    /**
     * Convenience method.
     * @since Ant 1.7
     */
    protected void dieOnCircularReference() {
        dieOnCircularReference(getProject());
    }

    /**
     * Convenience method.
     * @param p the Ant Project instance against which to resolve references.
     * @since Ant 1.7
     */
    protected void dieOnCircularReference(Project p) {
        if (checked || !isReference()) {
            return;
        }
        dieOnCircularReference(new IdentityStack<Object>(this), p);
    }

    /**
     * Check to see whether any DataType we hold references to is
     * included in the Stack (which holds all DataType instances that
     * directly or indirectly reference this instance, including this
     * instance itself).
     *
     * <p>If one is included, throw a BuildException created by {@link
     * #circularReference circularReference}.</p>
     *
     * <p>This implementation is appropriate only for a DataType that
     * cannot hold other DataTypes as children.</p>
     *
     * <p>The general contract of this method is that it shouldn't do
     * anything if {@link #checked <code>checked</code>} is true and
     * set it to true on exit.</p>
     * @param stack the stack of references to check.
     * @param project the project to use to dereference the references.
     * @throws BuildException on error.
     */
    protected void dieOnCircularReference(final Stack<Object> stack,
                                          final Project project)
        throws BuildException {

        if (checked || !isReference()) {
            return;
        }
        Object o = ref.getReferencedObject(project);

        if (o instanceof DataType) {
            IdentityStack<Object> id = IdentityStack.getInstance(stack);

            if (id.contains(o)) {
                throw circularReference();
            } else {
                id.push(o);
                ((DataType) o).dieOnCircularReference(id, project);
                id.pop();
            }
        }
        checked = true;
    }

    /**
     * Allow DataTypes outside org.apache.tools.ant.types to indirectly call
     * dieOnCircularReference on nested DataTypes.
     * @param dt the DataType to check.
     * @param stk the stack of references to check.
     * @param p the project to use to dereference the references.
     * @throws BuildException on error.
     * @since Ant 1.7
     */
    public static void invokeCircularReferenceCheck(DataType dt, Stack<Object> stk,
                                                    Project p) {
        dt.dieOnCircularReference(stk, p);
    }

    /**
     * Allow DataTypes outside org.apache.tools.ant.types to indirectly call
     * dieOnCircularReference on nested DataTypes.
     *
     * <p>Pushes dt on the stack, runs dieOnCircularReference and pops
     * it again.</p>
     * @param dt the DataType to check.
     * @param stk the stack of references to check.
     * @param p the project to use to dereference the references.
     * @throws BuildException on error.
     * @since Ant 1.8.0
     */
    public static void pushAndInvokeCircularReferenceCheck(DataType dt,
                                                           Stack<Object> stk,
                                                           Project p) {
        stk.push(dt);
        dt.dieOnCircularReference(stk, p);
        stk.pop();
    }

    /**
     * Performs the check for circular references and returns the
     * referenced object.
     * @return the dereferenced object.
     * @throws BuildException if the reference is invalid (circular ref, wrong class, etc).
     * @since Ant 1.7
     */
    protected Object getCheckedRef() {
        return getCheckedRef(getProject());
    }

    /**
     * Performs the check for circular references and returns the
     * referenced object.
     * @param p the Ant Project instance against which to resolve references.
     * @return the dereferenced object.
     * @throws BuildException if the reference is invalid (circular ref, wrong class, etc).
     * @since Ant 1.7
     */
    protected Object getCheckedRef(Project p) {
        return getCheckedRef(getClass(), getDataTypeName(), p);
    }

    /**
     * Performs the check for circular references and returns the
     * referenced object.
     * @param requiredClass the class that this reference should be a subclass of.
     * @param dataTypeName  the name of the datatype that the reference should be
     *                      (error message use only).
     * @return the dereferenced object.
     * @throws BuildException if the reference is invalid (circular ref, wrong class, etc).
     */
    protected <T> T getCheckedRef(final Class<T> requiredClass,
                                   final String dataTypeName) {
        return getCheckedRef(requiredClass, dataTypeName, getProject());
    }

    /**
     * Performs the check for circular references and returns the
     * referenced object.  This version allows the fallback Project instance to be specified.
     * @param requiredClass the class that this reference should be a subclass of.
     * @param dataTypeName  the name of the datatype that the reference should be
     *                      (error message use only).
     * @param project       the fallback Project instance for dereferencing.
     * @return the dereferenced object.
     * @throws BuildException if the reference is invalid (circular ref, wrong class, etc),
     *                        or if <code>project</code> is <code>null</code>.
     * @since Ant 1.7
     */
    protected <T> T getCheckedRef(final Class<T> requiredClass,
                                  final String dataTypeName, final Project project) {
        if (project == null) {
            throw new BuildException("No Project specified");
        }
        dieOnCircularReference(project);
        Object o = ref.getReferencedObject(project);
        if (!(requiredClass.isAssignableFrom(o.getClass()))) {
            log("Class " + displayName(o.getClass())
                    + " is not a subclass of "
                    + displayName(requiredClass),
                    Project.MSG_VERBOSE);
            String msg = ref.getRefId() + " doesn\'t denote a " + dataTypeName;
            throw new BuildException(msg);
        }
        @SuppressWarnings("unchecked")
        final T result = (T) o;
        return result;
    }

    /**
     * Creates an exception that indicates that refid has to be the
     * only attribute if it is set.
     * @return the exception to throw
     */
    protected BuildException tooManyAttributes() {
        return new BuildException("You must not specify more than one "
            + "attribute when using refid");
    }

    /**
     * Creates an exception that indicates that this XML element must
     * not have child elements if the refid attribute is set.
     * @return the exception to throw
     */
    protected BuildException noChildrenAllowed() {
        return new BuildException("You must not specify nested elements "
            + "when using refid");
    }

    /**
     * Creates an exception that indicates the user has generated a
     * loop of data types referencing each other.
     * @return the exception to throw
     */
    protected BuildException circularReference() {
        return new BuildException("This data type contains a circular "
            + "reference.");
    }

    /**
     * The flag that is used to indicate that circular references have been checked.
     * @return true if circular references have been checked
     */
    protected boolean isChecked() {
        return checked;
    }

    /**
     * Set the flag that is used to indicate that circular references have been checked.
     * @param checked if true, if circular references have been checked
     */
    protected void setChecked(final boolean checked) {
        this.checked = checked;
    }

    /**
     * get the reference set on this object
     * @return the reference or null
     */
    public Reference getRefid() {
        return ref;
    }

    /**
     * check that it is ok to set attributes, i.e that no reference is defined
     * @since Ant 1.6
     * @throws BuildException if not allowed
     */
    protected void checkAttributesAllowed() {
        if (isReference()) {
            throw tooManyAttributes();
        }
    }

    /**
     * check that it is ok to add children, i.e that no reference is defined
     * @since Ant 1.6
     * @throws BuildException if not allowed
     */
    protected void checkChildrenAllowed() {
        if (isReference()) {
            throw noChildrenAllowed();
        }
    }

    /**
     * Basic DataType toString().
     * @return this DataType formatted as a String.
     */
    public String toString() {
        String d = getDescription();
        return d == null ? getDataTypeName() : getDataTypeName() + " " + d;
    }

    /**
     * @since Ant 1.7
     * @return a shallow copy of this DataType.
     * @throws CloneNotSupportedException if there is a problem.
     */
    public Object clone() throws CloneNotSupportedException {
        DataType dt = (DataType) super.clone();
        dt.setDescription(getDescription());
        if (getRefid() != null) {
            dt.setRefid(getRefid());
        }
        dt.setChecked(isChecked());
        return dt;
    }

    private String displayName(Class<?> clazz) {
        return clazz.getName() + " (loaded via " + clazz.getClassLoader() +")";
    }
}