001 /* 002 * Copyright 2005 Stephen J. McConnell 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 013 * implied. 014 * 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package net.dpml.tools.tasks; 020 021 import net.dpml.library.ResourceNotFoundException; 022 import net.dpml.library.Resource; 023 024 import net.dpml.tools.Context; 025 026 import org.apache.tools.ant.BuildException; 027 028 /** 029 * Locate a named feature of the a project or resource. 030 * 031 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a> 032 * @version 1.2.0 033 */ 034 public abstract class ResourceTask extends GenericTask 035 { 036 private String m_key; 037 private String m_ref; 038 039 /** 040 * Set the key of the target project or resource description from which features will be 041 * resolved from. If not declared the key defaults to the current defintion. 042 * 043 * @param key the resource key 044 */ 045 public void setKey( final String key ) 046 { 047 m_key = key; 048 } 049 050 /** 051 * Set the ref of the target project or resource description from which features will be 052 * resolved from. 053 * 054 * @param ref the resource reference 055 */ 056 public void setRef( final String ref ) 057 { 058 m_ref = ref; 059 } 060 061 /** 062 * Get the project definition. 063 * @return the resource 064 */ 065 protected Resource getResource() 066 { 067 if( null != m_ref ) 068 { 069 return getResource( m_ref ); 070 } 071 else if( null != m_key ) 072 { 073 Context context = getContext(); 074 Resource resource = context.getResource(); 075 Resource parent = resource.getParent(); 076 String ref = parent.getResourcePath() + "/" + m_key; 077 return getResource( ref ); 078 } 079 else 080 { 081 return getContext().getResource(); 082 } 083 } 084 085 private Resource getResource( String ref ) 086 { 087 try 088 { 089 return getContext().getLibrary().getResource( ref ); 090 } 091 catch( ResourceNotFoundException e ) 092 { 093 final String error = 094 "Feature reference [" 095 + ref 096 + "] in the project [" 097 + getResource() 098 + "] is unknown."; 099 throw new BuildException( error, e ); 100 } 101 } 102 }