1 package org.apache.maven.resolver.internal.ant.tasks;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.ComponentHelper;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.types.Reference;
27
28
29
30 public abstract class RefTask
31 extends Task
32 {
33
34 private Reference ref;
35
36 public boolean isReference()
37 {
38 return ref != null;
39 }
40
41 public void setRefid( final Reference ref )
42 {
43 this.ref = ref;
44 }
45
46 protected void checkAttributesAllowed()
47 {
48 if ( isReference() )
49 {
50 throw tooManyAttributes();
51 }
52 }
53
54 protected void checkChildrenAllowed()
55 {
56 if ( isReference() )
57 {
58 throw noChildrenAllowed();
59 }
60 }
61
62 protected BuildException tooManyAttributes()
63 {
64 return new BuildException( "You must not specify more than one " + "attribute when using refid" );
65 }
66
67 protected BuildException noChildrenAllowed()
68 {
69 return new BuildException( "You must not specify nested elements " + "when using refid" );
70 }
71
72 protected String getDataTypeName()
73 {
74 return ComponentHelper.getElementName( getProject(), this, true );
75 }
76
77 protected Object getCheckedRef()
78 {
79 return getCheckedRef( getClass(), getDataTypeName(), getProject() );
80 }
81
82 protected Object getCheckedRef( final Class<?> requiredClass, final String dataTypeName, final Project project )
83 {
84 if ( project == null )
85 {
86 throw new BuildException( "No Project specified" );
87 }
88 Object o = ref.getReferencedObject( project );
89 if ( !( requiredClass.isAssignableFrom( o.getClass() ) ) )
90 {
91 log( "Class " + o.getClass() + " is not a subclass of " + requiredClass, Project.MSG_VERBOSE );
92 String msg = ref.getRefId() + " doesn\'t denote a " + dataTypeName;
93 throw new BuildException( msg );
94 }
95 return o;
96 }
97
98 }