View Javadoc
1   package org.apache.maven.resolver.internal.ant.tasks;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  }